Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(423)

Side by Side Diff: chrome/browser/protector/protected_prefs_watcher.cc

Issue 9620010: Added Protector backup for Preferences. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Whitespace. Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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/protector/protected_prefs_watcher.h"
6
7 #include <sstream>
8
9 #include "base/base64.h"
10 #include "base/values.h"
11 #include "chrome/browser/extensions/extension_prefs.h"
12 #include "chrome/browser/prefs/pref_service.h"
13 #include "chrome/browser/prefs/pref_set_observer.h"
14 #include "chrome/browser/prefs/scoped_user_pref_update.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/protector/protector_service.h"
17 #include "chrome/common/chrome_notification_types.h"
18 #include "chrome/common/pref_names.h"
19 #include "content/public/browser/notification_service.h"
20
21 namespace protector {
22
23 namespace {
24
25 // Backup pref names.
26 const char kBackupHomePage[] = "backup.homepage";
27 const char kBackupHomePageIsNewTabPage[] = "backup.homepage_is_newtabpage";
28 const char kBackupShowHomeButton[] = "backup.browser.show_home_button";
29 const char kBackupRestoreOnStartup[] = "backup.session.restore_on_startup";
30 const char kBackupURLsToRestoreOnStartup[] =
31 "backup.session.urls_to_restore_on_startup";
32 const char kBackupExtensionsIDs[] = "backup.extensions.ids";
33 const char kBackupSignature[] = "backup._signature";
whywhat 2012/03/07 18:53:30 why this pref name has underscore?
Ivan Korotkov 2012/03/11 12:59:44 To emphasize that it's special among other "backup
34
35 } // namespace
36
37 ProtectedPrefsWatcher::ProtectedPrefsWatcher(Profile* profile)
38 : did_startup_settings_change_(false),
39 profile_(profile) {
40 pref_observer_.reset(PrefSetObserver::CreateProtectedPrefSetObserver(
41 profile->GetPrefs(), this));
42 UpdateCachedPrefs();
43 if (!HasBackup())
44 UpdateBackupSignature();
45 VLOG(1) << "Initialized pref watcher";
46 }
47
48 ProtectedPrefsWatcher::~ProtectedPrefsWatcher() {
49 }
50
51 // static
52 void ProtectedPrefsWatcher::RegisterUserPrefs(PrefService* prefs) {
53 prefs->RegisterStringPref(kBackupHomePage, "",
54 PrefService::UNSYNCABLE_PREF);
55 prefs->RegisterBooleanPref(kBackupHomePageIsNewTabPage, false,
56 PrefService::UNSYNCABLE_PREF);
57 prefs->RegisterBooleanPref(kBackupShowHomeButton, false,
58 PrefService::UNSYNCABLE_PREF);
59 prefs->RegisterIntegerPref(kBackupRestoreOnStartup, 0,
60 PrefService::UNSYNCABLE_PREF);
61 prefs->RegisterListPref(kBackupURLsToRestoreOnStartup,
62 PrefService::UNSYNCABLE_PREF);
63 prefs->RegisterListPref(kBackupExtensionsIDs,
64 PrefService::UNSYNCABLE_PREF);
65 prefs->RegisterStringPref(kBackupSignature, "",
66 PrefService::UNSYNCABLE_PREF);
67 }
68
69 void ProtectedPrefsWatcher::CheckForPrefChanges() {
70 // TODO(ivankr): this is a stub that always updates the backup to match the
71 // settings. Replace it with an actual check.
72 UpdateBackupSignature();
73 }
74
75 void ProtectedPrefsWatcher::Observe(
76 int type,
77 const content::NotificationSource& source,
78 const content::NotificationDetails& details) {
79 DCHECK(type == chrome::NOTIFICATION_PREF_CHANGED);
80 const std::string* pref_name = content::Details<std::string>(details).ptr();
81 DCHECK(pref_name && pref_observer_->IsObserved(*pref_name));
82 // For changes in extension dictionary, do nothing if the IDs list remained
83 // the same.
84 if (*pref_name == ExtensionPrefs::kExtensionsPref && !UpdateCachedPrefs())
85 return;
86 UpdateBackupSignature();
87 }
88
89 bool ProtectedPrefsWatcher::UpdateCachedPrefs() {
90 PrefService* prefs = profile_->GetPrefs();
91 const base::DictionaryValue* extensions =
92 prefs->GetDictionary(ExtensionPrefs::kExtensionsPref);
93 std::set<std::string> extension_ids(extensions->begin_keys(),
94 extensions->end_keys());
95 if (extension_ids == cached_extension_ids_)
96 return false;
97 cached_extension_ids_.swap(extension_ids);
98 return true;
99 }
100
101 void ProtectedPrefsWatcher::UpdateBackupSignature() {
102 PrefService* prefs = profile_->GetPrefs();
103 // Copy actual settings into backup.
104 prefs->SetString(kBackupHomePage, prefs->GetString(prefs::kHomePage));
105 prefs->SetBoolean(kBackupHomePageIsNewTabPage,
106 prefs->GetBoolean(prefs::kHomePageIsNewTabPage));
107 prefs->SetBoolean(kBackupShowHomeButton,
108 prefs->GetBoolean(prefs::kShowHomeButton));
109 prefs->SetInteger(kBackupRestoreOnStartup,
110 prefs->GetInteger(prefs::kRestoreOnStartup));
111 prefs->Set(kBackupURLsToRestoreOnStartup,
112 *prefs->GetList(prefs::kURLsToRestoreOnStartup));
113 {
114 ListPrefUpdate extension_ids_update(prefs, kBackupExtensionsIDs);
115 base::ListValue* extension_ids = extension_ids_update.Get();
116 extension_ids->Clear();
117 for (std::set<std::string>::const_iterator it =
118 cached_extension_ids_.begin();
119 it != cached_extension_ids_.end(); ++it) {
120 extension_ids->Append(base::Value::CreateStringValue(*it));
121 }
122 }
123 // Update the signature.
124 std::string signed_data = GetSignatureData(prefs);
125 DCHECK(!signed_data.empty());
126 std::string signature = SignSetting(signed_data);
127 DCHECK(!signature.empty());
128 std::string signature_base64;
129 if (!base::Base64Encode(signature, &signature_base64))
130 NOTREACHED();
131 prefs->SetString(kBackupSignature, signature_base64);
132 // Schedule disk write on FILE thread as soon as possible.
133 prefs->CommitPendingWrite();
134 VLOG(1) << "Updated backup signature";
135 }
136
137 bool ProtectedPrefsWatcher::IsSignatureValid() {
138 DCHECK(HasBackup());
139 PrefService* prefs = profile_->GetPrefs();
140 std::string signed_data = GetSignatureData(prefs);
141 DCHECK(!signed_data.empty());
142 std::string signature;
143 if (!base::Base64Decode(prefs->GetString(kBackupSignature), &signature))
144 return false;
145 return IsSettingValid(signed_data, signature);
146 }
147
148 bool ProtectedPrefsWatcher::HasBackup() {
149 // TODO(ivankr): as soon as some irreversible change to Preferences happens,
150 // add a condition that this change has occured as well (otherwise it's
151 // possible to simply clear the "backup" dictionary to make settings
152 // unprotected).
153 return profile_->GetPrefs()->HasPrefPath(kBackupSignature);
154 }
155
156 std::string ProtectedPrefsWatcher::GetSignatureData(PrefService* prefs) {
157 std::stringstream data;
whywhat 2012/03/07 18:53:30 Don't we have StringPrintf in chrome code base?
Ivan Korotkov 2012/03/11 12:59:44 We do. I thought stream IO is a bit more expressiv
158 data << prefs->GetString(kBackupHomePage) << " "
159 << prefs->GetBoolean(kBackupHomePageIsNewTabPage) << " "
160 << prefs->GetBoolean(kBackupShowHomeButton) << " "
161 << prefs->GetInteger(kBackupRestoreOnStartup) << " ";
162 const base::ListValue* startup_urls =
163 prefs->GetList(kBackupURLsToRestoreOnStartup);
164 for (base::ListValue::const_iterator it = startup_urls->begin();
165 it != startup_urls->end(); ++it) {
166 std::string url;
167 if (!(*it)->GetAsString(&url))
168 NOTREACHED();
169 data << url << " ";
170 }
171 // These are safe to use becase they are always up-to-date and returned in
172 // a consistent (sorted) order.
173 for (std::set<std::string>::const_iterator it = cached_extension_ids_.begin();
174 it != cached_extension_ids_.end(); ++it) {
175 data << *it << " ";
176 }
177 return data.str();
178 }
179
180 } // namespace protector
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698