| 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/api/storage/settings_sync_util.h" | 5 #include "chrome/browser/extensions/api/storage/settings_sync_util.h" |
| 6 | 6 |
| 7 #include "base/bind.h" |
| 7 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 8 #include "base/values.h" | 9 #include "base/values.h" |
| 9 #include "chrome/browser/extensions/api/storage/sync_value_store_cache.h" | 10 #include "chrome/browser/extensions/api/storage/sync_value_store_cache.h" |
| 10 #include "components/sync/protocol/app_setting_specifics.pb.h" | 11 #include "components/sync/protocol/app_setting_specifics.pb.h" |
| 11 #include "components/sync/protocol/extension_setting_specifics.pb.h" | 12 #include "components/sync/protocol/extension_setting_specifics.pb.h" |
| 12 #include "components/sync/protocol/sync.pb.h" | 13 #include "components/sync/protocol/sync.pb.h" |
| 13 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 14 #include "extensions/browser/api/storage/storage_frontend.h" | 15 #include "extensions/browser/api/storage/storage_frontend.h" |
| 15 | 16 |
| 17 using base::WeakPtr; |
| 16 using content::BrowserThread; | 18 using content::BrowserThread; |
| 19 using syncer::ModelType; |
| 20 using syncer::SyncableService; |
| 17 | 21 |
| 18 namespace extensions { | 22 namespace extensions { |
| 19 | 23 |
| 20 namespace settings_sync_util { | 24 namespace settings_sync_util { |
| 21 | 25 |
| 22 namespace { | 26 namespace { |
| 23 | 27 |
| 24 void PopulateExtensionSettingSpecifics( | 28 void PopulateExtensionSettingSpecifics( |
| 25 const std::string& extension_id, | 29 const std::string& extension_id, |
| 26 const std::string& key, | 30 const std::string& key, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 37 | 41 |
| 38 void PopulateAppSettingSpecifics( | 42 void PopulateAppSettingSpecifics( |
| 39 const std::string& extension_id, | 43 const std::string& extension_id, |
| 40 const std::string& key, | 44 const std::string& key, |
| 41 const base::Value& value, | 45 const base::Value& value, |
| 42 sync_pb::AppSettingSpecifics* specifics) { | 46 sync_pb::AppSettingSpecifics* specifics) { |
| 43 PopulateExtensionSettingSpecifics( | 47 PopulateExtensionSettingSpecifics( |
| 44 extension_id, key, value, specifics->mutable_extension_setting()); | 48 extension_id, key, value, specifics->mutable_extension_setting()); |
| 45 } | 49 } |
| 46 | 50 |
| 51 WeakPtr<SyncableService> GetSyncableService(WeakPtr<SyncValueStoreCache> cache, |
| 52 ModelType type) { |
| 53 return cache.get() ? cache->GetSyncableService(type)->AsWeakPtr() |
| 54 : WeakPtr<SyncableService>(); |
| 55 } |
| 56 |
| 47 } // namespace | 57 } // namespace |
| 48 | 58 |
| 49 syncer::SyncData CreateData( | 59 syncer::SyncData CreateData(const std::string& extension_id, |
| 50 const std::string& extension_id, | 60 const std::string& key, |
| 51 const std::string& key, | 61 const base::Value& value, |
| 52 const base::Value& value, | 62 ModelType type) { |
| 53 syncer::ModelType type) { | |
| 54 sync_pb::EntitySpecifics specifics; | 63 sync_pb::EntitySpecifics specifics; |
| 55 switch (type) { | 64 switch (type) { |
| 56 case syncer::EXTENSION_SETTINGS: | 65 case syncer::EXTENSION_SETTINGS: |
| 57 PopulateExtensionSettingSpecifics( | 66 PopulateExtensionSettingSpecifics( |
| 58 extension_id, | 67 extension_id, |
| 59 key, | 68 key, |
| 60 value, | 69 value, |
| 61 specifics.mutable_extension_setting()); | 70 specifics.mutable_extension_setting()); |
| 62 break; | 71 break; |
| 63 | 72 |
| 64 case syncer::APP_SETTINGS: | 73 case syncer::APP_SETTINGS: |
| 65 PopulateAppSettingSpecifics( | 74 PopulateAppSettingSpecifics( |
| 66 extension_id, | 75 extension_id, |
| 67 key, | 76 key, |
| 68 value, | 77 value, |
| 69 specifics.mutable_app_setting()); | 78 specifics.mutable_app_setting()); |
| 70 break; | 79 break; |
| 71 | 80 |
| 72 default: | 81 default: |
| 73 NOTREACHED(); | 82 NOTREACHED(); |
| 74 } | 83 } |
| 75 | 84 |
| 76 return syncer::SyncData::CreateLocalData( | 85 return syncer::SyncData::CreateLocalData( |
| 77 extension_id + "/" + key, key, specifics); | 86 extension_id + "/" + key, key, specifics); |
| 78 } | 87 } |
| 79 | 88 |
| 80 syncer::SyncChange CreateAdd( | 89 syncer::SyncChange CreateAdd(const std::string& extension_id, |
| 81 const std::string& extension_id, | 90 const std::string& key, |
| 82 const std::string& key, | 91 const base::Value& value, |
| 83 const base::Value& value, | 92 ModelType type) { |
| 84 syncer::ModelType type) { | |
| 85 return syncer::SyncChange( | 93 return syncer::SyncChange( |
| 86 FROM_HERE, | 94 FROM_HERE, |
| 87 syncer::SyncChange::ACTION_ADD, | 95 syncer::SyncChange::ACTION_ADD, |
| 88 CreateData(extension_id, key, value, type)); | 96 CreateData(extension_id, key, value, type)); |
| 89 } | 97 } |
| 90 | 98 |
| 91 syncer::SyncChange CreateUpdate( | 99 syncer::SyncChange CreateUpdate(const std::string& extension_id, |
| 92 const std::string& extension_id, | 100 const std::string& key, |
| 93 const std::string& key, | 101 const base::Value& value, |
| 94 const base::Value& value, | 102 ModelType type) { |
| 95 syncer::ModelType type) { | |
| 96 return syncer::SyncChange( | 103 return syncer::SyncChange( |
| 97 FROM_HERE, | 104 FROM_HERE, |
| 98 syncer::SyncChange::ACTION_UPDATE, | 105 syncer::SyncChange::ACTION_UPDATE, |
| 99 CreateData(extension_id, key, value, type)); | 106 CreateData(extension_id, key, value, type)); |
| 100 } | 107 } |
| 101 | 108 |
| 102 syncer::SyncChange CreateDelete( | 109 syncer::SyncChange CreateDelete(const std::string& extension_id, |
| 103 const std::string& extension_id, | 110 const std::string& key, |
| 104 const std::string& key, | 111 ModelType type) { |
| 105 syncer::ModelType type) { | |
| 106 base::DictionaryValue no_value; | 112 base::DictionaryValue no_value; |
| 107 return syncer::SyncChange( | 113 return syncer::SyncChange( |
| 108 FROM_HERE, | 114 FROM_HERE, |
| 109 syncer::SyncChange::ACTION_DELETE, | 115 syncer::SyncChange::ACTION_DELETE, |
| 110 CreateData(extension_id, key, no_value, type)); | 116 CreateData(extension_id, key, no_value, type)); |
| 111 } | 117 } |
| 112 | 118 |
| 113 syncer::SyncableService* GetSyncableService(content::BrowserContext* context, | 119 syncer::SyncClient::ServiceProvider GetSyncableServiceProvider( |
| 114 syncer::ModelType type) { | 120 content::BrowserContext* context, |
| 115 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 121 ModelType type) { |
| 122 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 116 DCHECK(type == syncer::APP_SETTINGS || type == syncer::EXTENSION_SETTINGS); | 123 DCHECK(type == syncer::APP_SETTINGS || type == syncer::EXTENSION_SETTINGS); |
| 117 StorageFrontend* frontend = StorageFrontend::Get(context); | 124 StorageFrontend* frontend = StorageFrontend::Get(context); |
| 118 SyncValueStoreCache* sync_cache = static_cast<SyncValueStoreCache*>( | 125 SyncValueStoreCache* sync_cache = static_cast<SyncValueStoreCache*>( |
| 119 frontend->GetValueStoreCache(settings_namespace::SYNC)); | 126 frontend->GetValueStoreCache(settings_namespace::SYNC)); |
| 120 return sync_cache->GetSyncableService(type); | 127 return base::Bind(&GetSyncableService, sync_cache->AsWeakPtr(), type); |
| 121 } | 128 } |
| 122 | 129 |
| 123 } // namespace settings_sync_util | 130 } // namespace settings_sync_util |
| 124 | 131 |
| 125 } // namespace extensions | 132 } // namespace extensions |
| OLD | NEW |