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