1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/web_resource/eula_accepted_notifier.h"
6
7#include "base/bind.h"
8#include "base/logging.h"
9#include "base/prefs/pref_service.h"
10#include "chrome/browser/browser_process.h"
11#include "chrome/common/pref_names.h"
12
13EulaAcceptedNotifier::EulaAcceptedNotifier(PrefService* local_state)
14    : local_state_(local_state), observer_(NULL) {
15}
16
17EulaAcceptedNotifier::~EulaAcceptedNotifier() {
18}
19
20void EulaAcceptedNotifier::Init(Observer* observer) {
21  DCHECK(!observer_ && observer);
22  observer_ = observer;
23}
24
25bool EulaAcceptedNotifier::IsEulaAccepted() {
26  if (local_state_->GetBoolean(prefs::kEulaAccepted))
27    return true;
28
29  // Register for the notification, if this is the first time.
30  if (registrar_.IsEmpty()) {
31    registrar_.Init(local_state_);
32    registrar_.Add(prefs::kEulaAccepted,
33                   base::Bind(&EulaAcceptedNotifier::OnPrefChanged,
34                              base::Unretained(this)));
35  }
36  return false;
37}
38
39// static
40EulaAcceptedNotifier* EulaAcceptedNotifier::Create() {
41  // First run EULA only exists on ChromeOS, Android and iOS. On ChromeOS, it is
42  // only shown in official builds.
43#if (defined(OS_CHROMEOS) && defined(GOOGLE_CHROME_BUILD)) || \
44    defined(OS_ANDROID) || defined(OS_IOS)
45  PrefService* local_state = g_browser_process->local_state();
46  // Tests that use higher-level classes that use EulaAcceptNotifier may not
47  // register this pref. In this case, return NULL which is equivalent to not
48  // needing to check the EULA.
49  if (local_state->FindPreference(prefs::kEulaAccepted) == NULL)
50    return NULL;
51  return new EulaAcceptedNotifier(local_state);
52#else
53  return NULL;
54#endif
55}
56
57void EulaAcceptedNotifier::NotifyObserver() {
58  observer_->OnEulaAccepted();
59}
60
61void EulaAcceptedNotifier::OnPrefChanged() {
62  DCHECK(!registrar_.IsEmpty());
63  registrar_.RemoveAll();
64
65  DCHECK(local_state_->GetBoolean(prefs::kEulaAccepted));
66  observer_->OnEulaAccepted();
67}
68
69