Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / themes / theme_syncable_service.cc
blob5b8a2d8344e4d49c5b63a36e05bb9b8fa3f383bd
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.
5 #include "chrome/browser/themes/theme_syncable_service.h"
7 #include "base/strings/stringprintf.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/themes/theme_service.h"
11 #include "chrome/common/extensions/sync_helper.h"
12 #include "extensions/browser/extension_prefs.h"
13 #include "extensions/browser/extension_system.h"
14 #include "extensions/common/extension.h"
15 #include "extensions/common/manifest_url_handlers.h"
16 #include "sync/protocol/sync.pb.h"
17 #include "sync/protocol/theme_specifics.pb.h"
19 using std::string;
21 namespace {
23 bool IsTheme(const extensions::Extension* extension) {
24 return extension->is_theme();
27 } // namespace
29 const char ThemeSyncableService::kCurrentThemeClientTag[] = "current_theme";
30 const char ThemeSyncableService::kCurrentThemeNodeTitle[] = "Current Theme";
32 ThemeSyncableService::ThemeSyncableService(Profile* profile,
33 ThemeService* theme_service)
34 : profile_(profile),
35 theme_service_(theme_service),
36 use_system_theme_by_default_(false) {
37 DCHECK(profile_);
38 DCHECK(theme_service_);
41 ThemeSyncableService::~ThemeSyncableService() {
44 void ThemeSyncableService::OnThemeChange() {
45 if (sync_processor_.get()) {
46 sync_pb::ThemeSpecifics current_specifics;
47 if (!GetThemeSpecificsFromCurrentTheme(&current_specifics))
48 return; // Current theme is unsyncable.
49 ProcessNewTheme(syncer::SyncChange::ACTION_UPDATE, current_specifics);
50 use_system_theme_by_default_ =
51 current_specifics.use_system_theme_by_default();
55 syncer::SyncMergeResult ThemeSyncableService::MergeDataAndStartSyncing(
56 syncer::ModelType type,
57 const syncer::SyncDataList& initial_sync_data,
58 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
59 scoped_ptr<syncer::SyncErrorFactory> error_handler) {
60 DCHECK(thread_checker_.CalledOnValidThread());
61 DCHECK(!sync_processor_.get());
62 DCHECK(sync_processor.get());
63 DCHECK(error_handler.get());
65 syncer::SyncMergeResult merge_result(type);
66 sync_processor_ = sync_processor.Pass();
67 sync_error_handler_ = error_handler.Pass();
69 if (initial_sync_data.size() > 1) {
70 sync_error_handler_->CreateAndUploadError(
71 FROM_HERE,
72 base::StringPrintf("Received %d theme specifics.",
73 static_cast<int>(initial_sync_data.size())));
76 sync_pb::ThemeSpecifics current_specifics;
77 if (!GetThemeSpecificsFromCurrentTheme(&current_specifics)) {
78 // Current theme is unsyncable - don't overwrite from sync data, and don't
79 // save the unsyncable theme to sync data.
80 return merge_result;
83 // Find the last SyncData that has theme data and set the current theme from
84 // it.
85 for (syncer::SyncDataList::const_reverse_iterator sync_data =
86 initial_sync_data.rbegin(); sync_data != initial_sync_data.rend();
87 ++sync_data) {
88 if (sync_data->GetSpecifics().has_theme()) {
89 MaybeSetTheme(current_specifics, *sync_data);
90 return merge_result;
94 // No theme specifics are found. Create one according to current theme.
95 merge_result.set_error(ProcessNewTheme(
96 syncer::SyncChange::ACTION_ADD, current_specifics));
97 return merge_result;
100 void ThemeSyncableService::StopSyncing(syncer::ModelType type) {
101 DCHECK(thread_checker_.CalledOnValidThread());
102 DCHECK_EQ(type, syncer::THEMES);
104 sync_processor_.reset();
105 sync_error_handler_.reset();
108 syncer::SyncDataList ThemeSyncableService::GetAllSyncData(
109 syncer::ModelType type) const {
110 DCHECK(thread_checker_.CalledOnValidThread());
111 DCHECK_EQ(type, syncer::THEMES);
113 syncer::SyncDataList list;
114 sync_pb::EntitySpecifics entity_specifics;
115 if (GetThemeSpecificsFromCurrentTheme(entity_specifics.mutable_theme())) {
116 list.push_back(syncer::SyncData::CreateLocalData(kCurrentThemeClientTag,
117 kCurrentThemeNodeTitle,
118 entity_specifics));
120 return list;
123 syncer::SyncError ThemeSyncableService::ProcessSyncChanges(
124 const tracked_objects::Location& from_here,
125 const syncer::SyncChangeList& change_list) {
126 DCHECK(thread_checker_.CalledOnValidThread());
128 if (!sync_processor_.get()) {
129 return syncer::SyncError(FROM_HERE,
130 syncer::SyncError::DATATYPE_ERROR,
131 "Theme syncable service is not started.",
132 syncer::THEMES);
135 // TODO(akalin): Normally, we should only have a single change and
136 // it should be an update. However, the syncapi may occasionally
137 // generates multiple changes. When we fix syncapi to not do that,
138 // we can remove the extra logic below. See:
139 // http://code.google.com/p/chromium/issues/detail?id=41696 .
140 if (change_list.size() != 1) {
141 string err_msg = base::StringPrintf("Received %d theme changes: ",
142 static_cast<int>(change_list.size()));
143 for (size_t i = 0; i < change_list.size(); ++i) {
144 base::StringAppendF(&err_msg, "[%s] ", change_list[i].ToString().c_str());
146 sync_error_handler_->CreateAndUploadError(FROM_HERE, err_msg);
147 } else if (change_list.begin()->change_type() !=
148 syncer::SyncChange::ACTION_ADD
149 && change_list.begin()->change_type() !=
150 syncer::SyncChange::ACTION_UPDATE) {
151 sync_error_handler_->CreateAndUploadError(
152 FROM_HERE,
153 "Invalid theme change: " + change_list.begin()->ToString());
156 sync_pb::ThemeSpecifics current_specifics;
157 if (!GetThemeSpecificsFromCurrentTheme(&current_specifics)) {
158 // Current theme is unsyncable, so don't overwrite it.
159 return syncer::SyncError();
162 // Set current theme from the theme specifics of the last change of type
163 // |ACTION_ADD| or |ACTION_UPDATE|.
164 for (syncer::SyncChangeList::const_reverse_iterator theme_change =
165 change_list.rbegin(); theme_change != change_list.rend();
166 ++theme_change) {
167 if (theme_change->sync_data().GetSpecifics().has_theme() &&
168 (theme_change->change_type() == syncer::SyncChange::ACTION_ADD ||
169 theme_change->change_type() == syncer::SyncChange::ACTION_UPDATE)) {
170 MaybeSetTheme(current_specifics, theme_change->sync_data());
171 return syncer::SyncError();
175 return syncer::SyncError(FROM_HERE,
176 syncer::SyncError::DATATYPE_ERROR,
177 "Didn't find valid theme specifics",
178 syncer::THEMES);
181 void ThemeSyncableService::MaybeSetTheme(
182 const sync_pb::ThemeSpecifics& current_specs,
183 const syncer::SyncData& sync_data) {
184 const sync_pb::ThemeSpecifics& sync_theme = sync_data.GetSpecifics().theme();
185 use_system_theme_by_default_ = sync_theme.use_system_theme_by_default();
186 DVLOG(1) << "Set current theme from specifics: " << sync_data.ToString();
187 if (!AreThemeSpecificsEqual(
188 current_specs,
189 sync_theme,
190 theme_service_->IsSystemThemeDistinctFromDefaultTheme())) {
191 SetCurrentThemeFromThemeSpecifics(sync_theme);
192 } else {
193 DVLOG(1) << "Skip setting theme because specs are equal";
197 void ThemeSyncableService::SetCurrentThemeFromThemeSpecifics(
198 const sync_pb::ThemeSpecifics& theme_specifics) {
199 if (theme_specifics.use_custom_theme()) {
200 // TODO(akalin): Figure out what to do about third-party themes
201 // (i.e., those not on either Google gallery).
202 string id(theme_specifics.custom_theme_id());
203 GURL update_url(theme_specifics.custom_theme_update_url());
204 DVLOG(1) << "Applying theme " << id << " with update_url " << update_url;
205 ExtensionService* extensions_service =
206 extensions::ExtensionSystem::Get(profile_)->extension_service();
207 CHECK(extensions_service);
208 const extensions::Extension* extension =
209 extensions_service->GetExtensionById(id, true);
210 if (extension) {
211 if (!extension->is_theme()) {
212 DVLOG(1) << "Extension " << id << " is not a theme; aborting";
213 return;
215 int disabled_reasons =
216 extensions::ExtensionPrefs::Get(profile_)->GetDisableReasons(id);
217 if (!extensions_service->IsExtensionEnabled(id) &&
218 disabled_reasons != extensions::Extension::DISABLE_USER_ACTION) {
219 DVLOG(1) << "Theme " << id << " is disabled with reason "
220 << disabled_reasons << "; aborting";
221 return;
223 // An enabled theme extension with the given id was found, so
224 // just set the current theme to it.
225 theme_service_->SetTheme(extension);
226 } else {
227 // No extension with this id exists -- we must install it; we do
228 // so by adding it as a pending extension and then triggering an
229 // auto-update cycle.
230 const bool kRemoteInstall = false;
231 const bool kInstalledByCustodian = false;
232 if (!extensions_service->pending_extension_manager()->AddFromSync(
234 update_url,
235 &IsTheme,
236 kRemoteInstall,
237 kInstalledByCustodian)) {
238 LOG(WARNING) << "Could not add pending extension for " << id;
239 return;
241 extensions_service->CheckForUpdatesSoon();
243 } else if (theme_specifics.use_system_theme_by_default()) {
244 DVLOG(1) << "Switch to use system theme";
245 theme_service_->UseSystemTheme();
246 } else {
247 DVLOG(1) << "Switch to use default theme";
248 theme_service_->UseDefaultTheme();
252 bool ThemeSyncableService::GetThemeSpecificsFromCurrentTheme(
253 sync_pb::ThemeSpecifics* theme_specifics) const {
254 const extensions::Extension* current_theme =
255 theme_service_->UsingDefaultTheme() ?
256 NULL :
257 extensions::ExtensionSystem::Get(profile_)->extension_service()->
258 GetExtensionById(theme_service_->GetThemeID(), false);
259 if (current_theme && !extensions::sync_helper::IsSyncable(current_theme)) {
260 DVLOG(1) << "Ignoring extension from external source: " <<
261 current_theme->location();
262 return false;
264 bool use_custom_theme = (current_theme != NULL);
265 theme_specifics->set_use_custom_theme(use_custom_theme);
266 if (theme_service_->IsSystemThemeDistinctFromDefaultTheme()) {
267 // On platform where system theme is different from default theme, set
268 // use_system_theme_by_default to true if system theme is used, false
269 // if default system theme is used. Otherwise restore it to value in sync.
270 if (theme_service_->UsingSystemTheme()) {
271 theme_specifics->set_use_system_theme_by_default(true);
272 } else if (theme_service_->UsingDefaultTheme()) {
273 theme_specifics->set_use_system_theme_by_default(false);
274 } else {
275 theme_specifics->set_use_system_theme_by_default(
276 use_system_theme_by_default_);
278 } else {
279 // Restore use_system_theme_by_default when platform doesn't distinguish
280 // between default theme and system theme.
281 theme_specifics->set_use_system_theme_by_default(
282 use_system_theme_by_default_);
285 if (use_custom_theme) {
286 DCHECK(current_theme);
287 DCHECK(current_theme->is_theme());
288 theme_specifics->set_custom_theme_name(current_theme->name());
289 theme_specifics->set_custom_theme_id(current_theme->id());
290 theme_specifics->set_custom_theme_update_url(
291 extensions::ManifestURL::GetUpdateURL(current_theme).spec());
292 } else {
293 DCHECK(!current_theme);
294 theme_specifics->clear_custom_theme_name();
295 theme_specifics->clear_custom_theme_id();
296 theme_specifics->clear_custom_theme_update_url();
298 return true;
301 /* static */
302 bool ThemeSyncableService::AreThemeSpecificsEqual(
303 const sync_pb::ThemeSpecifics& a,
304 const sync_pb::ThemeSpecifics& b,
305 bool is_system_theme_distinct_from_default_theme) {
306 if (a.use_custom_theme() != b.use_custom_theme()) {
307 return false;
310 if (a.use_custom_theme()) {
311 // We're using a custom theme, so simply compare IDs since those
312 // are guaranteed unique.
313 return a.custom_theme_id() == b.custom_theme_id();
314 } else if (is_system_theme_distinct_from_default_theme) {
315 // We're not using a custom theme, but we care about system
316 // vs. default.
317 return a.use_system_theme_by_default() == b.use_system_theme_by_default();
318 } else {
319 // We're not using a custom theme, and we don't care about system
320 // vs. default.
321 return true;
325 syncer::SyncError ThemeSyncableService::ProcessNewTheme(
326 syncer::SyncChange::SyncChangeType change_type,
327 const sync_pb::ThemeSpecifics& theme_specifics) {
328 syncer::SyncChangeList changes;
329 sync_pb::EntitySpecifics entity_specifics;
330 entity_specifics.mutable_theme()->CopyFrom(theme_specifics);
332 changes.push_back(
333 syncer::SyncChange(FROM_HERE, change_type,
334 syncer::SyncData::CreateLocalData(
335 kCurrentThemeClientTag, kCurrentThemeNodeTitle,
336 entity_specifics)));
338 DVLOG(1) << "Update theme specifics from current theme: "
339 << changes.back().ToString();
341 return sync_processor_->ProcessSyncChanges(FROM_HERE, changes);