| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/sync/glue/theme_change_processor.h" | 5 #include "chrome/browser/sync/glue/theme_change_processor.h" |
| 6 | 6 |
| 7 #include "base/location.h" | 7 #include "base/location.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 ThemeChangeProcessor::~ThemeChangeProcessor() {} | 34 ThemeChangeProcessor::~ThemeChangeProcessor() {} |
| 35 | 35 |
| 36 void ThemeChangeProcessor::Observe( | 36 void ThemeChangeProcessor::Observe( |
| 37 int type, | 37 int type, |
| 38 const content::NotificationSource& source, | 38 const content::NotificationSource& source, |
| 39 const content::NotificationDetails& details) { | 39 const content::NotificationDetails& details) { |
| 40 DCHECK(running()); | 40 DCHECK(running()); |
| 41 DCHECK(profile_); | 41 DCHECK(profile_); |
| 42 DCHECK(type == chrome::NOTIFICATION_BROWSER_THEME_CHANGED); | 42 DCHECK(type == chrome::NOTIFICATION_BROWSER_THEME_CHANGED); |
| 43 | 43 |
| 44 csync::WriteTransaction trans(FROM_HERE, share_handle()); | 44 syncer::WriteTransaction trans(FROM_HERE, share_handle()); |
| 45 csync::WriteNode node(&trans); | 45 syncer::WriteNode node(&trans); |
| 46 if (node.InitByClientTagLookup(syncable::THEMES, | 46 if (node.InitByClientTagLookup(syncable::THEMES, |
| 47 kCurrentThemeClientTag) != | 47 kCurrentThemeClientTag) != |
| 48 csync::BaseNode::INIT_OK) { | 48 syncer::BaseNode::INIT_OK) { |
| 49 std::string err = "Could not create node with client tag: "; | 49 std::string err = "Could not create node with client tag: "; |
| 50 error_handler()->OnSingleDatatypeUnrecoverableError(FROM_HERE, | 50 error_handler()->OnSingleDatatypeUnrecoverableError(FROM_HERE, |
| 51 err + kCurrentThemeClientTag); | 51 err + kCurrentThemeClientTag); |
| 52 return; | 52 return; |
| 53 } | 53 } |
| 54 | 54 |
| 55 sync_pb::ThemeSpecifics old_theme_specifics = node.GetThemeSpecifics(); | 55 sync_pb::ThemeSpecifics old_theme_specifics = node.GetThemeSpecifics(); |
| 56 // Make sure to base new_theme_specifics on old_theme_specifics so | 56 // Make sure to base new_theme_specifics on old_theme_specifics so |
| 57 // we preserve the state of use_system_theme_by_default. | 57 // we preserve the state of use_system_theme_by_default. |
| 58 sync_pb::ThemeSpecifics new_theme_specifics = old_theme_specifics; | 58 sync_pb::ThemeSpecifics new_theme_specifics = old_theme_specifics; |
| 59 GetThemeSpecificsFromCurrentTheme(profile_, &new_theme_specifics); | 59 GetThemeSpecificsFromCurrentTheme(profile_, &new_theme_specifics); |
| 60 // Do a write only if something actually changed so as to guard | 60 // Do a write only if something actually changed so as to guard |
| 61 // against cycles. | 61 // against cycles. |
| 62 if (!AreThemeSpecificsEqual(old_theme_specifics, new_theme_specifics)) { | 62 if (!AreThemeSpecificsEqual(old_theme_specifics, new_theme_specifics)) { |
| 63 node.SetThemeSpecifics(new_theme_specifics); | 63 node.SetThemeSpecifics(new_theme_specifics); |
| 64 } | 64 } |
| 65 return; | 65 return; |
| 66 } | 66 } |
| 67 | 67 |
| 68 void ThemeChangeProcessor::ApplyChangesFromSyncModel( | 68 void ThemeChangeProcessor::ApplyChangesFromSyncModel( |
| 69 const csync::BaseTransaction* trans, | 69 const syncer::BaseTransaction* trans, |
| 70 const csync::ImmutableChangeRecordList& changes) { | 70 const syncer::ImmutableChangeRecordList& changes) { |
| 71 if (!running()) { | 71 if (!running()) { |
| 72 return; | 72 return; |
| 73 } | 73 } |
| 74 // TODO(akalin): Normally, we should only have a single change and | 74 // TODO(akalin): Normally, we should only have a single change and |
| 75 // it should be an update. However, the syncapi may occasionally | 75 // it should be an update. However, the syncapi may occasionally |
| 76 // generates multiple changes. When we fix syncapi to not do that, | 76 // generates multiple changes. When we fix syncapi to not do that, |
| 77 // we can remove the extra logic below. See: | 77 // we can remove the extra logic below. See: |
| 78 // http://code.google.com/p/chromium/issues/detail?id=41696 . | 78 // http://code.google.com/p/chromium/issues/detail?id=41696 . |
| 79 if (changes.Get().empty()) { | 79 if (changes.Get().empty()) { |
| 80 error_handler()->OnSingleDatatypeUnrecoverableError(FROM_HERE, | 80 error_handler()->OnSingleDatatypeUnrecoverableError(FROM_HERE, |
| 81 "Change list unexpectedly empty"); | 81 "Change list unexpectedly empty"); |
| 82 return; | 82 return; |
| 83 } | 83 } |
| 84 const size_t change_count = changes.Get().size(); | 84 const size_t change_count = changes.Get().size(); |
| 85 if (change_count > 1u) { | 85 if (change_count > 1u) { |
| 86 LOG(WARNING) << change_count << " theme changes detected; " | 86 LOG(WARNING) << change_count << " theme changes detected; " |
| 87 << "only applying the last one"; | 87 << "only applying the last one"; |
| 88 } | 88 } |
| 89 const csync::ChangeRecord& change = | 89 const syncer::ChangeRecord& change = |
| 90 changes.Get()[change_count - 1]; | 90 changes.Get()[change_count - 1]; |
| 91 if (change.action != csync::ChangeRecord::ACTION_UPDATE && | 91 if (change.action != syncer::ChangeRecord::ACTION_UPDATE && |
| 92 change.action != csync::ChangeRecord::ACTION_DELETE) { | 92 change.action != syncer::ChangeRecord::ACTION_DELETE) { |
| 93 std::string err = "strange theme change.action " + | 93 std::string err = "strange theme change.action " + |
| 94 base::IntToString(change.action); | 94 base::IntToString(change.action); |
| 95 error_handler()->OnSingleDatatypeUnrecoverableError(FROM_HERE, err); | 95 error_handler()->OnSingleDatatypeUnrecoverableError(FROM_HERE, err); |
| 96 return; | 96 return; |
| 97 } | 97 } |
| 98 sync_pb::ThemeSpecifics theme_specifics; | 98 sync_pb::ThemeSpecifics theme_specifics; |
| 99 // If the action is a delete, simply use the default values for | 99 // If the action is a delete, simply use the default values for |
| 100 // ThemeSpecifics, which would cause the default theme to be set. | 100 // ThemeSpecifics, which would cause the default theme to be set. |
| 101 if (change.action != csync::ChangeRecord::ACTION_DELETE) { | 101 if (change.action != syncer::ChangeRecord::ACTION_DELETE) { |
| 102 csync::ReadNode node(trans); | 102 syncer::ReadNode node(trans); |
| 103 if (node.InitByIdLookup(change.id) != csync::BaseNode::INIT_OK) { | 103 if (node.InitByIdLookup(change.id) != syncer::BaseNode::INIT_OK) { |
| 104 error_handler()->OnSingleDatatypeUnrecoverableError(FROM_HERE, | 104 error_handler()->OnSingleDatatypeUnrecoverableError(FROM_HERE, |
| 105 "Theme node lookup failed."); | 105 "Theme node lookup failed."); |
| 106 return; | 106 return; |
| 107 } | 107 } |
| 108 DCHECK_EQ(node.GetModelType(), syncable::THEMES); | 108 DCHECK_EQ(node.GetModelType(), syncable::THEMES); |
| 109 DCHECK(profile_); | 109 DCHECK(profile_); |
| 110 theme_specifics = node.GetThemeSpecifics(); | 110 theme_specifics = node.GetThemeSpecifics(); |
| 111 } | 111 } |
| 112 ScopedStopObserving<ThemeChangeProcessor> stop_observing(this); | 112 ScopedStopObserving<ThemeChangeProcessor> stop_observing(this); |
| 113 SetCurrentThemeFromThemeSpecificsIfNecessary(theme_specifics, profile_); | 113 SetCurrentThemeFromThemeSpecificsIfNecessary(theme_specifics, profile_); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 133 ThemeServiceFactory::GetForProfile(profile_))); | 133 ThemeServiceFactory::GetForProfile(profile_))); |
| 134 } | 134 } |
| 135 | 135 |
| 136 void ThemeChangeProcessor::StopObserving() { | 136 void ThemeChangeProcessor::StopObserving() { |
| 137 DCHECK(profile_); | 137 DCHECK(profile_); |
| 138 DVLOG(1) << "Unobserving all notifications"; | 138 DVLOG(1) << "Unobserving all notifications"; |
| 139 notification_registrar_.RemoveAll(); | 139 notification_registrar_.RemoveAll(); |
| 140 } | 140 } |
| 141 | 141 |
| 142 } // namespace browser_sync | 142 } // namespace browser_sync |
| OLD | NEW |