| 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/extensions/settings/settings_backend.h" | 5 #include "chrome/browser/extensions/settings/settings_backend.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 | 26 |
| 27 SettingsBackend::SettingsBackend( | 27 SettingsBackend::SettingsBackend( |
| 28 const scoped_refptr<SettingsStorageFactory>& storage_factory, | 28 const scoped_refptr<SettingsStorageFactory>& storage_factory, |
| 29 const FilePath& base_path, | 29 const FilePath& base_path, |
| 30 const SettingsStorageQuotaEnforcer::Limits& quota, | 30 const SettingsStorageQuotaEnforcer::Limits& quota, |
| 31 const scoped_refptr<SettingsObserverList>& observers) | 31 const scoped_refptr<SettingsObserverList>& observers) |
| 32 : storage_factory_(storage_factory), | 32 : storage_factory_(storage_factory), |
| 33 base_path_(base_path), | 33 base_path_(base_path), |
| 34 quota_(quota), | 34 quota_(quota), |
| 35 observers_(observers), | 35 observers_(observers), |
| 36 sync_type_(syncable::UNSPECIFIED) { | 36 sync_type_(syncer::UNSPECIFIED) { |
| 37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 38 } | 38 } |
| 39 | 39 |
| 40 SettingsBackend::~SettingsBackend() { | 40 SettingsBackend::~SettingsBackend() { |
| 41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 42 } | 42 } |
| 43 | 43 |
| 44 ValueStore* SettingsBackend::GetStorage( | 44 ValueStore* SettingsBackend::GetStorage( |
| 45 const std::string& extension_id) const { | 45 const std::string& extension_id) const { |
| 46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 result.insert(maybe_as_ascii); | 128 result.insert(maybe_as_ascii); |
| 129 } | 129 } |
| 130 } | 130 } |
| 131 | 131 |
| 132 return result; | 132 return result; |
| 133 } | 133 } |
| 134 | 134 |
| 135 static void AddAllSyncData( | 135 static void AddAllSyncData( |
| 136 const std::string& extension_id, | 136 const std::string& extension_id, |
| 137 const DictionaryValue& src, | 137 const DictionaryValue& src, |
| 138 syncable::ModelType type, | 138 syncer::ModelType type, |
| 139 syncer::SyncDataList* dst) { | 139 syncer::SyncDataList* dst) { |
| 140 for (DictionaryValue::Iterator it(src); it.HasNext(); it.Advance()) { | 140 for (DictionaryValue::Iterator it(src); it.HasNext(); it.Advance()) { |
| 141 dst->push_back(settings_sync_util::CreateData( | 141 dst->push_back(settings_sync_util::CreateData( |
| 142 extension_id, it.key(), it.value(), type)); | 142 extension_id, it.key(), it.value(), type)); |
| 143 } | 143 } |
| 144 } | 144 } |
| 145 | 145 |
| 146 syncer::SyncDataList SettingsBackend::GetAllSyncData( | 146 syncer::SyncDataList SettingsBackend::GetAllSyncData( |
| 147 syncable::ModelType type) const { | 147 syncer::ModelType type) const { |
| 148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 149 // Ignore the type, it's just for sanity checking; assume that whatever base | 149 // Ignore the type, it's just for sanity checking; assume that whatever base |
| 150 // path we're constructed with is correct for the sync type. | 150 // path we're constructed with is correct for the sync type. |
| 151 DCHECK(type == syncable::EXTENSION_SETTINGS || | 151 DCHECK(type == syncer::EXTENSION_SETTINGS || |
| 152 type == syncable::APP_SETTINGS); | 152 type == syncer::APP_SETTINGS); |
| 153 | 153 |
| 154 // For all extensions, get all their settings. This has the effect | 154 // For all extensions, get all their settings. This has the effect |
| 155 // of bringing in the entire state of extension settings in memory; sad. | 155 // of bringing in the entire state of extension settings in memory; sad. |
| 156 syncer::SyncDataList all_sync_data; | 156 syncer::SyncDataList all_sync_data; |
| 157 std::set<std::string> known_extension_ids(GetKnownExtensionIDs()); | 157 std::set<std::string> known_extension_ids(GetKnownExtensionIDs()); |
| 158 | 158 |
| 159 for (std::set<std::string>::const_iterator it = known_extension_ids.begin(); | 159 for (std::set<std::string>::const_iterator it = known_extension_ids.begin(); |
| 160 it != known_extension_ids.end(); ++it) { | 160 it != known_extension_ids.end(); ++it) { |
| 161 ValueStore::ReadResult maybe_settings = GetStorage(*it)->Get(); | 161 ValueStore::ReadResult maybe_settings = GetStorage(*it)->Get(); |
| 162 if (maybe_settings->HasError()) { | 162 if (maybe_settings->HasError()) { |
| 163 LOG(WARNING) << "Failed to get settings for " << *it << ": " << | 163 LOG(WARNING) << "Failed to get settings for " << *it << ": " << |
| 164 maybe_settings->error(); | 164 maybe_settings->error(); |
| 165 continue; | 165 continue; |
| 166 } | 166 } |
| 167 AddAllSyncData(*it, *maybe_settings->settings().get(), | 167 AddAllSyncData(*it, *maybe_settings->settings().get(), |
| 168 type, &all_sync_data); | 168 type, &all_sync_data); |
| 169 } | 169 } |
| 170 | 170 |
| 171 return all_sync_data; | 171 return all_sync_data; |
| 172 } | 172 } |
| 173 | 173 |
| 174 syncer::SyncError SettingsBackend::MergeDataAndStartSyncing( | 174 syncer::SyncError SettingsBackend::MergeDataAndStartSyncing( |
| 175 syncable::ModelType type, | 175 syncer::ModelType type, |
| 176 const syncer::SyncDataList& initial_sync_data, | 176 const syncer::SyncDataList& initial_sync_data, |
| 177 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, | 177 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, |
| 178 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) { | 178 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) { |
| 179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 180 DCHECK(type == syncable::EXTENSION_SETTINGS || | 180 DCHECK(type == syncer::EXTENSION_SETTINGS || |
| 181 type == syncable::APP_SETTINGS); | 181 type == syncer::APP_SETTINGS); |
| 182 DCHECK_EQ(sync_type_, syncable::UNSPECIFIED); | 182 DCHECK_EQ(sync_type_, syncer::UNSPECIFIED); |
| 183 DCHECK(!sync_processor_.get()); | 183 DCHECK(!sync_processor_.get()); |
| 184 DCHECK(sync_processor.get()); | 184 DCHECK(sync_processor.get()); |
| 185 DCHECK(sync_error_factory.get()); | 185 DCHECK(sync_error_factory.get()); |
| 186 | 186 |
| 187 sync_type_ = type; | 187 sync_type_ = type; |
| 188 sync_processor_ = sync_processor.Pass(); | 188 sync_processor_ = sync_processor.Pass(); |
| 189 sync_error_factory_ = sync_error_factory.Pass(); | 189 sync_error_factory_ = sync_error_factory.Pass(); |
| 190 | 190 |
| 191 // Group the initial sync data by extension id. | 191 // Group the initial sync data by extension id. |
| 192 std::map<std::string, linked_ptr<DictionaryValue> > grouped_sync_data; | 192 std::map<std::string, linked_ptr<DictionaryValue> > grouped_sync_data; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 SyncableSettingsStorage* storage = | 258 SyncableSettingsStorage* storage = |
| 259 GetOrCreateStorageWithSyncData(it->first, empty); | 259 GetOrCreateStorageWithSyncData(it->first, empty); |
| 260 syncer::SyncError error = storage->ProcessSyncChanges(it->second); | 260 syncer::SyncError error = storage->ProcessSyncChanges(it->second); |
| 261 if (error.IsSet()) | 261 if (error.IsSet()) |
| 262 storage->StopSyncing(); | 262 storage->StopSyncing(); |
| 263 } | 263 } |
| 264 | 264 |
| 265 return syncer::SyncError(); | 265 return syncer::SyncError(); |
| 266 } | 266 } |
| 267 | 267 |
| 268 void SettingsBackend::StopSyncing(syncable::ModelType type) { | 268 void SettingsBackend::StopSyncing(syncer::ModelType type) { |
| 269 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 269 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 270 DCHECK(type == syncable::EXTENSION_SETTINGS || | 270 DCHECK(type == syncer::EXTENSION_SETTINGS || |
| 271 type == syncable::APP_SETTINGS); | 271 type == syncer::APP_SETTINGS); |
| 272 DCHECK(sync_type_ == type || sync_type_ == syncable::UNSPECIFIED); | 272 DCHECK(sync_type_ == type || sync_type_ == syncer::UNSPECIFIED); |
| 273 | 273 |
| 274 for (StorageObjMap::iterator it = storage_objs_.begin(); | 274 for (StorageObjMap::iterator it = storage_objs_.begin(); |
| 275 it != storage_objs_.end(); ++it) { | 275 it != storage_objs_.end(); ++it) { |
| 276 // Some storage areas may have already stopped syncing if they had areas | 276 // Some storage areas may have already stopped syncing if they had areas |
| 277 // and syncing was disabled, but StopSyncing is safe to call multiple times. | 277 // and syncing was disabled, but StopSyncing is safe to call multiple times. |
| 278 it->second->StopSyncing(); | 278 it->second->StopSyncing(); |
| 279 } | 279 } |
| 280 | 280 |
| 281 sync_type_ = syncable::UNSPECIFIED; | 281 sync_type_ = syncer::UNSPECIFIED; |
| 282 sync_processor_.reset(); | 282 sync_processor_.reset(); |
| 283 sync_error_factory_.reset(); | 283 sync_error_factory_.reset(); |
| 284 } | 284 } |
| 285 | 285 |
| 286 scoped_ptr<SettingsSyncProcessor> SettingsBackend::CreateSettingsSyncProcessor( | 286 scoped_ptr<SettingsSyncProcessor> SettingsBackend::CreateSettingsSyncProcessor( |
| 287 const std::string& extension_id) const { | 287 const std::string& extension_id) const { |
| 288 CHECK(sync_processor_.get()); | 288 CHECK(sync_processor_.get()); |
| 289 return scoped_ptr<SettingsSyncProcessor>( | 289 return scoped_ptr<SettingsSyncProcessor>( |
| 290 new SettingsSyncProcessor(extension_id, | 290 new SettingsSyncProcessor(extension_id, |
| 291 sync_type_, | 291 sync_type_, |
| 292 sync_processor_.get())); | 292 sync_processor_.get())); |
| 293 } | 293 } |
| 294 | 294 |
| 295 } // namespace extensions | 295 } // namespace extensions |
| OLD | NEW |