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