1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/web_resource/eula_accepted_notifier.h"
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/bind.h"
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/logging.h"
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/prefs/pref_service.h"
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/browser_process.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/common/pref_names.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)EulaAcceptedNotifier::EulaAcceptedNotifier(PrefService* local_state)
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    : local_state_(local_state), observer_(NULL) {
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)EulaAcceptedNotifier::~EulaAcceptedNotifier() {
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void EulaAcceptedNotifier::Init(Observer* observer) {
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DCHECK(!observer_ && observer);
22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  observer_ = observer;
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool EulaAcceptedNotifier::IsEulaAccepted() {
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (local_state_->GetBoolean(prefs::kEulaAccepted))
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return true;
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Register for the notification, if this is the first time.
30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (registrar_.IsEmpty()) {
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    registrar_.Init(local_state_);
32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    registrar_.Add(prefs::kEulaAccepted,
33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                   base::Bind(&EulaAcceptedNotifier::OnPrefChanged,
34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                              base::Unretained(this)));
35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return false;
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// static
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)EulaAcceptedNotifier* EulaAcceptedNotifier::Create() {
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // First run EULA only exists on ChromeOS, Android and iOS. On ChromeOS, it is
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // only shown in official builds.
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#if (defined(OS_CHROMEOS) && defined(GOOGLE_CHROME_BUILD)) || \
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    defined(OS_ANDROID) || defined(OS_IOS)
45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  PrefService* local_state = g_browser_process->local_state();
46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Tests that use higher-level classes that use EulaAcceptNotifier may not
47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // register this pref. In this case, return NULL which is equivalent to not
48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // needing to check the EULA.
49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (local_state->FindPreference(prefs::kEulaAccepted) == NULL)
50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return new EulaAcceptedNotifier(local_state);
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#else
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return NULL;
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#endif
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void EulaAcceptedNotifier::NotifyObserver() {
58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  observer_->OnEulaAccepted();
59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void EulaAcceptedNotifier::OnPrefChanged() {
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DCHECK(!registrar_.IsEmpty());
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  registrar_.RemoveAll();
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DCHECK(local_state_->GetBoolean(prefs::kEulaAccepted));
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  observer_->OnEulaAccepted();
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
69