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 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 54 return cache.get() ? cache->GetSyncableService(type)->AsWeakPtr() |
| 55 : WeakPtr<SyncableService>(); |
| 56 } |
| 57 |
47 } // namespace | 58 } // namespace |
48 | 59 |
49 syncer::SyncData CreateData( | 60 syncer::SyncData CreateData(const std::string& extension_id, |
50 const std::string& extension_id, | 61 const std::string& key, |
51 const std::string& key, | 62 const base::Value& value, |
52 const base::Value& value, | 63 ModelType type) { |
53 syncer::ModelType type) { | |
54 sync_pb::EntitySpecifics specifics; | 64 sync_pb::EntitySpecifics specifics; |
55 switch (type) { | 65 switch (type) { |
56 case syncer::EXTENSION_SETTINGS: | 66 case syncer::EXTENSION_SETTINGS: |
57 PopulateExtensionSettingSpecifics( | 67 PopulateExtensionSettingSpecifics( |
58 extension_id, | 68 extension_id, |
59 key, | 69 key, |
60 value, | 70 value, |
61 specifics.mutable_extension_setting()); | 71 specifics.mutable_extension_setting()); |
62 break; | 72 break; |
63 | 73 |
64 case syncer::APP_SETTINGS: | 74 case syncer::APP_SETTINGS: |
65 PopulateAppSettingSpecifics( | 75 PopulateAppSettingSpecifics( |
66 extension_id, | 76 extension_id, |
67 key, | 77 key, |
68 value, | 78 value, |
69 specifics.mutable_app_setting()); | 79 specifics.mutable_app_setting()); |
70 break; | 80 break; |
71 | 81 |
72 default: | 82 default: |
73 NOTREACHED(); | 83 NOTREACHED(); |
74 } | 84 } |
75 | 85 |
76 return syncer::SyncData::CreateLocalData( | 86 return syncer::SyncData::CreateLocalData( |
77 extension_id + "/" + key, key, specifics); | 87 extension_id + "/" + key, key, specifics); |
78 } | 88 } |
79 | 89 |
80 syncer::SyncChange CreateAdd( | 90 syncer::SyncChange CreateAdd(const std::string& extension_id, |
81 const std::string& extension_id, | 91 const std::string& key, |
82 const std::string& key, | 92 const base::Value& value, |
83 const base::Value& value, | 93 ModelType type) { |
84 syncer::ModelType type) { | |
85 return syncer::SyncChange( | 94 return syncer::SyncChange( |
86 FROM_HERE, | 95 FROM_HERE, |
87 syncer::SyncChange::ACTION_ADD, | 96 syncer::SyncChange::ACTION_ADD, |
88 CreateData(extension_id, key, value, type)); | 97 CreateData(extension_id, key, value, type)); |
89 } | 98 } |
90 | 99 |
91 syncer::SyncChange CreateUpdate( | 100 syncer::SyncChange CreateUpdate(const std::string& extension_id, |
92 const std::string& extension_id, | 101 const std::string& key, |
93 const std::string& key, | 102 const base::Value& value, |
94 const base::Value& value, | 103 ModelType type) { |
95 syncer::ModelType type) { | |
96 return syncer::SyncChange( | 104 return syncer::SyncChange( |
97 FROM_HERE, | 105 FROM_HERE, |
98 syncer::SyncChange::ACTION_UPDATE, | 106 syncer::SyncChange::ACTION_UPDATE, |
99 CreateData(extension_id, key, value, type)); | 107 CreateData(extension_id, key, value, type)); |
100 } | 108 } |
101 | 109 |
102 syncer::SyncChange CreateDelete( | 110 syncer::SyncChange CreateDelete(const std::string& extension_id, |
103 const std::string& extension_id, | 111 const std::string& key, |
104 const std::string& key, | 112 ModelType type) { |
105 syncer::ModelType type) { | |
106 base::DictionaryValue no_value; | 113 base::DictionaryValue no_value; |
107 return syncer::SyncChange( | 114 return syncer::SyncChange( |
108 FROM_HERE, | 115 FROM_HERE, |
109 syncer::SyncChange::ACTION_DELETE, | 116 syncer::SyncChange::ACTION_DELETE, |
110 CreateData(extension_id, key, no_value, type)); | 117 CreateData(extension_id, key, no_value, type)); |
111 } | 118 } |
112 | 119 |
113 syncer::SyncableService* GetSyncableService(content::BrowserContext* context, | 120 syncer::SyncClient::ServiceProvider GetSyncableServiceProvider( |
114 syncer::ModelType type) { | 121 content::BrowserContext* context, |
115 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 122 ModelType type) { |
| 123 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
116 DCHECK(type == syncer::APP_SETTINGS || type == syncer::EXTENSION_SETTINGS); | 124 DCHECK(type == syncer::APP_SETTINGS || type == syncer::EXTENSION_SETTINGS); |
117 StorageFrontend* frontend = StorageFrontend::Get(context); | 125 StorageFrontend* frontend = StorageFrontend::Get(context); |
118 SyncValueStoreCache* sync_cache = static_cast<SyncValueStoreCache*>( | 126 SyncValueStoreCache* sync_cache = static_cast<SyncValueStoreCache*>( |
119 frontend->GetValueStoreCache(settings_namespace::SYNC)); | 127 frontend->GetValueStoreCache(settings_namespace::SYNC)); |
120 return sync_cache->GetSyncableService(type); | 128 // We must rely on our caller to guarantee that sync_cache->AsWeakPtr() is a |
| 129 // valid call right now, and that shutdown has not begun. |
| 130 return base::Bind(&GetSyncableService, sync_cache->AsWeakPtr(), type); |
121 } | 131 } |
122 | 132 |
123 } // namespace settings_sync_util | 133 } // namespace settings_sync_util |
124 | 134 |
125 } // namespace extensions | 135 } // namespace extensions |
OLD | NEW |