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_namespace.h" | 5 #include "chrome/browser/extensions/settings/settings_namespace.h" |
6 #include "chrome/browser/extensions/settings/settings_sync_processor.h" | 6 #include "chrome/browser/extensions/settings/settings_sync_processor.h" |
7 #include "chrome/browser/extensions/settings/settings_sync_util.h" | 7 #include "chrome/browser/extensions/settings/settings_sync_util.h" |
8 #include "content/public/browser/browser_thread.h" | 8 #include "content/public/browser/browser_thread.h" |
9 #include "sync/api/sync_change_processor.h" | 9 #include "sync/api/sync_change_processor.h" |
10 #include "sync/api/sync_data.h" | 10 #include "sync/api/sync_data.h" |
11 #include "sync/protocol/extension_setting_specifics.pb.h" | 11 #include "sync/protocol/extension_setting_specifics.pb.h" |
12 | 12 |
13 using content::BrowserThread; | 13 using content::BrowserThread; |
14 | 14 |
15 namespace extensions { | 15 namespace extensions { |
16 | 16 |
17 SettingsSyncProcessor::SettingsSyncProcessor( | 17 SettingsSyncProcessor::SettingsSyncProcessor( |
18 const std::string& extension_id, | 18 const std::string& extension_id, |
19 syncable::ModelType type, | 19 syncable::ModelType type, |
20 SyncChangeProcessor* sync_processor) | 20 csync::SyncChangeProcessor* sync_processor) |
21 : extension_id_(extension_id), | 21 : extension_id_(extension_id), |
22 type_(type), | 22 type_(type), |
23 sync_processor_(sync_processor), | 23 sync_processor_(sync_processor), |
24 initialized_(false) { | 24 initialized_(false) { |
25 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 25 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
26 CHECK(type == syncable::EXTENSION_SETTINGS || | 26 CHECK(type == syncable::EXTENSION_SETTINGS || |
27 type == syncable::APP_SETTINGS); | 27 type == syncable::APP_SETTINGS); |
28 CHECK(sync_processor); | 28 CHECK(sync_processor); |
29 } | 29 } |
30 | 30 |
31 SettingsSyncProcessor::~SettingsSyncProcessor() { | 31 SettingsSyncProcessor::~SettingsSyncProcessor() { |
32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
33 } | 33 } |
34 | 34 |
35 void SettingsSyncProcessor::Init(const DictionaryValue& initial_state) { | 35 void SettingsSyncProcessor::Init(const DictionaryValue& initial_state) { |
36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
37 CHECK(!initialized_) << "Init called multiple times"; | 37 CHECK(!initialized_) << "Init called multiple times"; |
38 | 38 |
39 for (DictionaryValue::Iterator i(initial_state); i.HasNext(); i.Advance()) | 39 for (DictionaryValue::Iterator i(initial_state); i.HasNext(); i.Advance()) |
40 synced_keys_.insert(i.key()); | 40 synced_keys_.insert(i.key()); |
41 | 41 |
42 initialized_ = true; | 42 initialized_ = true; |
43 } | 43 } |
44 | 44 |
45 SyncError SettingsSyncProcessor::SendChanges( | 45 csync::SyncError SettingsSyncProcessor::SendChanges( |
46 const ValueStoreChangeList& changes) { | 46 const ValueStoreChangeList& changes) { |
47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
48 CHECK(initialized_) << "Init not called"; | 48 CHECK(initialized_) << "Init not called"; |
49 | 49 |
50 SyncChangeList sync_changes; | 50 csync::SyncChangeList sync_changes; |
51 std::set<std::string> added_keys; | 51 std::set<std::string> added_keys; |
52 std::set<std::string> deleted_keys; | 52 std::set<std::string> deleted_keys; |
53 | 53 |
54 for (ValueStoreChangeList::const_iterator i = changes.begin(); | 54 for (ValueStoreChangeList::const_iterator i = changes.begin(); |
55 i != changes.end(); ++i) { | 55 i != changes.end(); ++i) { |
56 const std::string& key = i->key(); | 56 const std::string& key = i->key(); |
57 const Value* value = i->new_value(); | 57 const Value* value = i->new_value(); |
58 if (value) { | 58 if (value) { |
59 if (synced_keys_.count(key)) { | 59 if (synced_keys_.count(key)) { |
60 // New value, key is synced; send ACTION_UPDATE. | 60 // New value, key is synced; send ACTION_UPDATE. |
(...skipping 11 matching lines...) Expand all Loading... |
72 sync_changes.push_back(settings_sync_util::CreateDelete( | 72 sync_changes.push_back(settings_sync_util::CreateDelete( |
73 extension_id_, key, type_)); | 73 extension_id_, key, type_)); |
74 deleted_keys.insert(key); | 74 deleted_keys.insert(key); |
75 } else { | 75 } else { |
76 LOG(WARNING) << "Deleted " << key << " but not in synced_keys_"; | 76 LOG(WARNING) << "Deleted " << key << " but not in synced_keys_"; |
77 } | 77 } |
78 } | 78 } |
79 } | 79 } |
80 | 80 |
81 if (sync_changes.empty()) | 81 if (sync_changes.empty()) |
82 return SyncError(); | 82 return csync::SyncError(); |
83 | 83 |
84 SyncError error = | 84 csync::SyncError error = |
85 sync_processor_->ProcessSyncChanges(FROM_HERE, sync_changes); | 85 sync_processor_->ProcessSyncChanges(FROM_HERE, sync_changes); |
86 if (error.IsSet()) | 86 if (error.IsSet()) |
87 return error; | 87 return error; |
88 | 88 |
89 synced_keys_.insert(added_keys.begin(), added_keys.end()); | 89 synced_keys_.insert(added_keys.begin(), added_keys.end()); |
90 for (std::set<std::string>::iterator i = deleted_keys.begin(); | 90 for (std::set<std::string>::iterator i = deleted_keys.begin(); |
91 i != deleted_keys.end(); ++i) { | 91 i != deleted_keys.end(); ++i) { |
92 synced_keys_.erase(*i); | 92 synced_keys_.erase(*i); |
93 } | 93 } |
94 | 94 |
95 return SyncError(); | 95 return csync::SyncError(); |
96 } | 96 } |
97 | 97 |
98 void SettingsSyncProcessor::NotifyChanges(const ValueStoreChangeList& changes) { | 98 void SettingsSyncProcessor::NotifyChanges(const ValueStoreChangeList& changes) { |
99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
100 CHECK(initialized_) << "Init not called"; | 100 CHECK(initialized_) << "Init not called"; |
101 | 101 |
102 for (ValueStoreChangeList::const_iterator i = changes.begin(); | 102 for (ValueStoreChangeList::const_iterator i = changes.begin(); |
103 i != changes.end(); ++i) { | 103 i != changes.end(); ++i) { |
104 if (i->new_value()) | 104 if (i->new_value()) |
105 synced_keys_.insert(i->key()); | 105 synced_keys_.insert(i->key()); |
106 else | 106 else |
107 synced_keys_.erase(i->key()); | 107 synced_keys_.erase(i->key()); |
108 } | 108 } |
109 } | 109 } |
110 | 110 |
111 } // namespace extensions | 111 } // namespace extensions |
OLD | NEW |