Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / web_resource / eula_accepted_notifier.cc
blob8faa23496f8a6b7d03bfb05678a9ff23bbc76c99
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.
5 #include "components/web_resource/eula_accepted_notifier.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/prefs/pref_service.h"
10 #include "components/web_resource/web_resource_pref_names.h"
12 namespace web_resource {
14 EulaAcceptedNotifier::EulaAcceptedNotifier(PrefService* local_state)
15 : local_state_(local_state), observer_(nullptr) {
18 EulaAcceptedNotifier::~EulaAcceptedNotifier() {
21 void EulaAcceptedNotifier::Init(Observer* observer) {
22 DCHECK(!observer_ && observer);
23 observer_ = observer;
26 bool EulaAcceptedNotifier::IsEulaAccepted() {
27 if (local_state_->GetBoolean(prefs::kEulaAccepted))
28 return true;
30 // Register for the notification, if this is the first time.
31 if (registrar_.IsEmpty()) {
32 registrar_.Init(local_state_);
33 registrar_.Add(prefs::kEulaAccepted,
34 base::Bind(&EulaAcceptedNotifier::OnPrefChanged,
35 base::Unretained(this)));
37 return false;
40 // static
41 EulaAcceptedNotifier* EulaAcceptedNotifier::Create(PrefService* local_state) {
42 // First run EULA only exists on ChromeOS, Android and iOS. On ChromeOS, it is
43 // only shown in official builds.
44 #if (defined(OS_CHROMEOS) && defined(GOOGLE_CHROME_BUILD)) || \
45 defined(OS_ANDROID) || defined(OS_IOS)
46 // Tests that use higher-level classes that use EulaAcceptNotifier may not
47 // have local state or may not register this pref. Return null to indicate not
48 // needing to check the EULA.
49 if (!local_state || !local_state->FindPreference(prefs::kEulaAccepted))
50 return nullptr;
51 return new EulaAcceptedNotifier(local_state);
52 #else
53 return nullptr;
54 #endif
57 void EulaAcceptedNotifier::NotifyObserver() {
58 observer_->OnEulaAccepted();
61 void EulaAcceptedNotifier::OnPrefChanged() {
62 DCHECK(!registrar_.IsEmpty());
63 registrar_.RemoveAll();
65 DCHECK(local_state_->GetBoolean(prefs::kEulaAccepted));
66 observer_->OnEulaAccepted();
69 } // namespace web_resource