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/declarative/rules_registry_with_cache.h" | 5 #include "chrome/browser/extensions/api/declarative/rules_registry_with_cache.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 } | 35 } |
36 | 36 |
37 std::vector<linked_ptr<extensions::RulesRegistry::Rule> > RulesFromValue( | 37 std::vector<linked_ptr<extensions::RulesRegistry::Rule> > RulesFromValue( |
38 const base::Value* value) { | 38 const base::Value* value) { |
39 std::vector<linked_ptr<extensions::RulesRegistry::Rule> > rules; | 39 std::vector<linked_ptr<extensions::RulesRegistry::Rule> > rules; |
40 | 40 |
41 const base::ListValue* list = NULL; | 41 const base::ListValue* list = NULL; |
42 if (!value || !value->GetAsList(&list)) | 42 if (!value || !value->GetAsList(&list)) |
43 return rules; | 43 return rules; |
44 | 44 |
| 45 rules.reserve(list->GetSize()); |
45 for (size_t i = 0; i < list->GetSize(); ++i) { | 46 for (size_t i = 0; i < list->GetSize(); ++i) { |
46 const base::DictionaryValue* dict = NULL; | 47 const base::DictionaryValue* dict = NULL; |
47 if (!list->GetDictionary(i, &dict)) | 48 if (!list->GetDictionary(i, &dict)) |
48 continue; | 49 continue; |
49 linked_ptr<extensions::RulesRegistry::Rule> rule( | 50 linked_ptr<extensions::RulesRegistry::Rule> rule( |
50 new extensions::RulesRegistry::Rule()); | 51 new extensions::RulesRegistry::Rule()); |
51 if (extensions::RulesRegistry::Rule::Populate(*dict, rule.get())) | 52 if (extensions::RulesRegistry::Rule::Populate(*dict, rule.get())) |
52 rules.push_back(rule); | 53 rules.push_back(rule); |
53 } | 54 } |
54 | 55 |
(...skipping 16 matching lines...) Expand all Loading... |
71 | 72 |
72 // RulesRegistryWithCache | 73 // RulesRegistryWithCache |
73 | 74 |
74 RulesRegistryWithCache::RulesRegistryWithCache( | 75 RulesRegistryWithCache::RulesRegistryWithCache( |
75 Profile* profile, | 76 Profile* profile, |
76 const char* event_name, | 77 const char* event_name, |
77 content::BrowserThread::ID owner_thread, | 78 content::BrowserThread::ID owner_thread, |
78 bool log_storage_init_delay, | 79 bool log_storage_init_delay, |
79 scoped_ptr<RuleStorageOnUI>* ui_part) | 80 scoped_ptr<RuleStorageOnUI>* ui_part) |
80 : RulesRegistry(owner_thread, event_name), | 81 : RulesRegistry(owner_thread, event_name), |
81 weak_ptr_factory_((profile) ? this : NULL), | 82 weak_ptr_factory_(this), |
82 storage_on_ui_((profile | 83 storage_on_ui_((profile |
83 ? (new RuleStorageOnUI(profile, | 84 ? (new RuleStorageOnUI(profile, |
84 GetDeclarativeRuleStorageKey( | 85 GetDeclarativeRuleStorageKey( |
85 event_name, | 86 event_name, |
86 profile->IsOffTheRecord()), | 87 profile->IsOffTheRecord()), |
87 owner_thread, | 88 owner_thread, |
88 weak_ptr_factory_.GetWeakPtr(), | 89 weak_ptr_factory_.GetWeakPtr(), |
89 log_storage_init_delay)) | 90 log_storage_init_delay)) |
90 ->GetWeakPtr() | 91 ->GetWeakPtr() |
91 : base::WeakPtr<RuleStorageOnUI>())) { | 92 : base::WeakPtr<RuleStorageOnUI>())), |
| 93 process_changed_rules_requested_(false) { |
92 if (!profile) { | 94 if (!profile) { |
93 CHECK(!ui_part); | 95 CHECK(!ui_part); |
94 return; | 96 return; |
95 } | 97 } |
96 | 98 |
97 ui_part->reset(storage_on_ui_.get()); | 99 ui_part->reset(storage_on_ui_.get()); |
98 | 100 |
99 storage_on_ui_->Init(); | 101 storage_on_ui_->Init(); |
100 } | 102 } |
101 | 103 |
(...skipping 17 matching lines...) Expand all Loading... |
119 return error; | 121 return error; |
120 | 122 |
121 // Commit all rules into |rules_| on success. | 123 // Commit all rules into |rules_| on success. |
122 for (std::vector<linked_ptr<Rule> >::const_iterator i = | 124 for (std::vector<linked_ptr<Rule> >::const_iterator i = |
123 rules.begin(); i != rules.end(); ++i) { | 125 rules.begin(); i != rules.end(); ++i) { |
124 const RuleId& rule_id = *((*i)->id); | 126 const RuleId& rule_id = *((*i)->id); |
125 RulesDictionaryKey key(extension_id, rule_id); | 127 RulesDictionaryKey key(extension_id, rule_id); |
126 rules_[key] = *i; | 128 rules_[key] = *i; |
127 } | 129 } |
128 | 130 |
129 ProcessChangedRules(extension_id); | 131 if (!process_changed_rules_requested_) { |
| 132 process_changed_rules_requested_ = true; |
| 133 ready_.Post(FROM_HERE, |
| 134 base::Bind(&RulesRegistryWithCache::ProcessChangedRules, |
| 135 weak_ptr_factory_.GetWeakPtr(), |
| 136 extension_id)); |
| 137 } |
130 return kSuccess; | 138 return kSuccess; |
131 } | 139 } |
132 | 140 |
133 std::string RulesRegistryWithCache::RemoveRules( | 141 std::string RulesRegistryWithCache::RemoveRules( |
134 const std::string& extension_id, | 142 const std::string& extension_id, |
135 const std::vector<std::string>& rule_identifiers) { | 143 const std::vector<std::string>& rule_identifiers) { |
136 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 144 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
137 | 145 |
138 std::string error = RemoveRulesImpl(extension_id, rule_identifiers); | 146 std::string error = RemoveRulesImpl(extension_id, rule_identifiers); |
139 | 147 |
140 if (!error.empty()) | 148 if (!error.empty()) |
141 return error; | 149 return error; |
142 | 150 |
143 // Commit removal of rules from |rules_| on success. | 151 // Commit removal of rules from |rules_| on success. |
144 for (std::vector<std::string>::const_iterator i = | 152 for (std::vector<std::string>::const_iterator i = |
145 rule_identifiers.begin(); i != rule_identifiers.end(); ++i) { | 153 rule_identifiers.begin(); i != rule_identifiers.end(); ++i) { |
146 RulesDictionaryKey lookup_key(extension_id, *i); | 154 RulesDictionaryKey lookup_key(extension_id, *i); |
147 rules_.erase(lookup_key); | 155 rules_.erase(lookup_key); |
148 } | 156 } |
149 | 157 |
150 ProcessChangedRules(extension_id); | 158 if (!process_changed_rules_requested_) { |
| 159 process_changed_rules_requested_ = true; |
| 160 ready_.Post(FROM_HERE, |
| 161 base::Bind(&RulesRegistryWithCache::ProcessChangedRules, |
| 162 weak_ptr_factory_.GetWeakPtr(), |
| 163 extension_id)); |
| 164 } |
151 return kSuccess; | 165 return kSuccess; |
152 } | 166 } |
153 | 167 |
154 std::string RulesRegistryWithCache::RemoveAllRules( | 168 std::string RulesRegistryWithCache::RemoveAllRules( |
155 const std::string& extension_id) { | 169 const std::string& extension_id) { |
156 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 170 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
157 | 171 |
158 std::string error = RemoveAllRulesImpl(extension_id); | 172 std::string error = RemoveAllRulesImpl(extension_id); |
159 | 173 |
160 if (!error.empty()) | 174 if (!error.empty()) |
161 return error; | 175 return error; |
162 | 176 |
163 // Commit removal of rules from |rules_| on success. | 177 // Commit removal of rules from |rules_| on success. |
164 for (RulesDictionary::const_iterator i = rules_.begin(); | 178 for (RulesDictionary::const_iterator i = rules_.begin(); |
165 i != rules_.end();) { | 179 i != rules_.end();) { |
166 const RulesDictionaryKey& key = i->first; | 180 const RulesDictionaryKey& key = i->first; |
167 ++i; | 181 ++i; |
168 if (key.first == extension_id) | 182 if (key.first == extension_id) |
169 rules_.erase(key); | 183 rules_.erase(key); |
170 } | 184 } |
171 | 185 |
172 ProcessChangedRules(extension_id); | 186 if (!process_changed_rules_requested_) { |
| 187 process_changed_rules_requested_ = true; |
| 188 ready_.Post(FROM_HERE, |
| 189 base::Bind(&RulesRegistryWithCache::ProcessChangedRules, |
| 190 weak_ptr_factory_.GetWeakPtr(), |
| 191 extension_id)); |
| 192 } |
173 return kSuccess; | 193 return kSuccess; |
174 } | 194 } |
175 | 195 |
176 std::string RulesRegistryWithCache::GetRules( | 196 std::string RulesRegistryWithCache::GetRules( |
177 const std::string& extension_id, | 197 const std::string& extension_id, |
178 const std::vector<std::string>& rule_identifiers, | 198 const std::vector<std::string>& rule_identifiers, |
179 std::vector<linked_ptr<RulesRegistry::Rule> >* out) { | 199 std::vector<linked_ptr<RulesRegistry::Rule> >* out) { |
180 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 200 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
181 | 201 |
182 for (std::vector<std::string>::const_iterator i = rule_identifiers.begin(); | 202 for (std::vector<std::string>::const_iterator i = rule_identifiers.begin(); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 scoped_ptr<base::Value> rules) { | 250 scoped_ptr<base::Value> rules) { |
231 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 251 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
232 | 252 |
233 AddRules(extension_id, RulesFromValue(rules.get())); | 253 AddRules(extension_id, RulesFromValue(rules.get())); |
234 } | 254 } |
235 | 255 |
236 void RulesRegistryWithCache::ProcessChangedRules( | 256 void RulesRegistryWithCache::ProcessChangedRules( |
237 const std::string& extension_id) { | 257 const std::string& extension_id) { |
238 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 258 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
239 | 259 |
| 260 process_changed_rules_requested_ = false; |
| 261 |
240 std::vector<linked_ptr<RulesRegistry::Rule> > new_rules; | 262 std::vector<linked_ptr<RulesRegistry::Rule> > new_rules; |
241 std::string error = GetAllRules(extension_id, &new_rules); | 263 std::string error = GetAllRules(extension_id, &new_rules); |
242 DCHECK_EQ(std::string(), error); | 264 DCHECK_EQ(std::string(), error); |
243 content::BrowserThread::PostTask( | 265 content::BrowserThread::PostTask( |
244 content::BrowserThread::UI, | 266 content::BrowserThread::UI, |
245 FROM_HERE, | 267 FROM_HERE, |
246 base::Bind(&RuleStorageOnUI::WriteToStorage, | 268 base::Bind(&RuleStorageOnUI::WriteToStorage, |
247 storage_on_ui_, | 269 storage_on_ui_, |
248 extension_id, | 270 extension_id, |
249 base::Passed(RulesToValue(new_rules)))); | 271 base::Passed(RulesToValue(new_rules)))); |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 base::Passed(&value))); | 407 base::Passed(&value))); |
386 | 408 |
387 waiting_for_extensions_.erase(extension_id); | 409 waiting_for_extensions_.erase(extension_id); |
388 | 410 |
389 if (waiting_for_extensions_.empty()) | 411 if (waiting_for_extensions_.empty()) |
390 ExtensionSystem::Get(profile_)->ready().Post( | 412 ExtensionSystem::Get(profile_)->ready().Post( |
391 FROM_HERE, base::Bind(&RuleStorageOnUI::CheckIfReady, GetWeakPtr())); | 413 FROM_HERE, base::Bind(&RuleStorageOnUI::CheckIfReady, GetWeakPtr())); |
392 } | 414 } |
393 | 415 |
394 } // namespace extensions | 416 } // namespace extensions |
OLD | NEW |