Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(624)

Side by Side Diff: chrome/browser/extensions/api/declarative/rules_registry.cc

Issue 108193008: Don't post tasks from RulesRegistry c-tor (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix weak pointer dereference on a wrong thread Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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.h" 5 #include "chrome/browser/extensions/api/declarative/rules_registry.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 content::BrowserThread::ID owner_thread, 79 content::BrowserThread::ID owner_thread,
80 RulesCacheDelegate* cache_delegate, 80 RulesCacheDelegate* cache_delegate,
81 const WebViewKey& webview_key) 81 const WebViewKey& webview_key)
82 : profile_(profile), 82 : profile_(profile),
83 owner_thread_(owner_thread), 83 owner_thread_(owner_thread),
84 event_name_(event_name), 84 event_name_(event_name),
85 webview_key_(webview_key), 85 webview_key_(webview_key),
86 weak_ptr_factory_(profile ? this : NULL), 86 weak_ptr_factory_(profile ? this : NULL),
87 last_generated_rule_identifier_id_(0) { 87 last_generated_rule_identifier_id_(0) {
88 if (cache_delegate) { 88 if (cache_delegate) {
89 ready_.reset(new OneShotEvent);
89 cache_delegate_ = cache_delegate->GetWeakPtr(); 90 cache_delegate_ = cache_delegate->GetWeakPtr();
90 cache_delegate->Init(this); 91 cache_delegate->Init(this);
91 } else {
92 content::BrowserThread::PostTask(
93 owner_thread,
94 FROM_HERE,
95 base::Bind(&RulesRegistry::MarkReady, this, base::Time::Now()));
Jeffrey Yasskin 2013/12/17 22:55:05 Would this CL be simpler if you just called ready_
vabr (Chromium) 2013/12/18 12:01:51 I'm afraid calling ready_.Signal() here would lead
96 } 92 }
93 // Note that if |cache_delegate| is NULL, the Rulesregistry never signals its
Jeffrey Yasskin 2013/12/17 22:55:05 capitalization: RulesRegistry
vabr (Chromium) 2013/12/18 12:01:51 Done.
94 // |ready_| event. That's OK, because that event is only ever needed by the
95 // cache delegate.
97 } 96 }
98 97
99 std::string RulesRegistry::AddRulesNoFill( 98 std::string RulesRegistry::AddRulesNoFill(
100 const std::string& extension_id, 99 const std::string& extension_id,
101 const std::vector<linked_ptr<Rule> >& rules) { 100 const std::vector<linked_ptr<Rule> >& rules) {
102 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); 101 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread()));
103 102
104 // Verify that all rule IDs are new. 103 // Verify that all rule IDs are new.
105 for (std::vector<linked_ptr<Rule> >::const_iterator i = 104 for (std::vector<linked_ptr<Rule> >::const_iterator i =
106 rules.begin(); i != rules.end(); ++i) { 105 rules.begin(); i != rules.end(); ++i) {
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 } 264 }
266 265
267 void RulesRegistry::MarkReady(base::Time storage_init_time) { 266 void RulesRegistry::MarkReady(base::Time storage_init_time) {
268 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); 267 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread()));
269 268
270 if (!storage_init_time.is_null()) { 269 if (!storage_init_time.is_null()) {
271 UMA_HISTOGRAM_TIMES("Extensions.DeclarativeRulesStorageInitialization", 270 UMA_HISTOGRAM_TIMES("Extensions.DeclarativeRulesStorageInitialization",
272 base::Time::Now() - storage_init_time); 271 base::Time::Now() - storage_init_time);
273 } 272 }
274 273
275 ready_.Signal(); 274 ready_->Signal();
276 } 275 }
277 276
278 void RulesRegistry::ProcessChangedRules(const std::string& extension_id) { 277 void RulesRegistry::ProcessChangedRules(const std::string& extension_id) {
279 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); 278 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread()));
280 279
281 DCHECK(ContainsKey(process_changed_rules_requested_, extension_id)); 280 DCHECK(ContainsKey(process_changed_rules_requested_, extension_id));
282 process_changed_rules_requested_[extension_id] = NOT_SCHEDULED_FOR_PROCESSING; 281 process_changed_rules_requested_[extension_id] = NOT_SCHEDULED_FOR_PROCESSING;
283 282
284 std::vector<linked_ptr<Rule> > new_rules; 283 std::vector<linked_ptr<Rule> > new_rules;
285 GetAllRules(extension_id, &new_rules); 284 GetAllRules(extension_id, &new_rules);
286 content::BrowserThread::PostTask( 285 content::BrowserThread::PostTask(
287 content::BrowserThread::UI, 286 content::BrowserThread::UI,
288 FROM_HERE, 287 FROM_HERE,
289 base::Bind(&RulesCacheDelegate::WriteToStorage, 288 base::Bind(&RulesCacheDelegate::WriteToStorage,
290 cache_delegate_, 289 cache_delegate_,
291 extension_id, 290 extension_id,
292 base::Passed(RulesToValue(new_rules)))); 291 base::Passed(RulesToValue(new_rules))));
293 } 292 }
294 293
295 void RulesRegistry::MaybeProcessChangedRules(const std::string& extension_id) { 294 void RulesRegistry::MaybeProcessChangedRules(const std::string& extension_id) {
296 // Read and initialize |process_changed_rules_requested_[extension_id]| if 295 // Read and initialize |process_changed_rules_requested_[extension_id]| if
297 // necessary. (Note that the insertion below will not overwrite 296 // necessary. (Note that the insertion below will not overwrite
298 // |process_changed_rules_requested_[extension_id]| if that already exists. 297 // |process_changed_rules_requested_[extension_id]| if that already exists.
298 bool processing_needed = profile_ && ready_;
299 std::pair<ProcessStateMap::iterator, bool> insertion = 299 std::pair<ProcessStateMap::iterator, bool> insertion =
300 process_changed_rules_requested_.insert(std::make_pair( 300 process_changed_rules_requested_.insert(std::make_pair(
301 extension_id, 301 extension_id,
302 profile_ ? NOT_SCHEDULED_FOR_PROCESSING : NEVER_PROCESS)); 302 processing_needed ? NOT_SCHEDULED_FOR_PROCESSING : NEVER_PROCESS));
303 if (insertion.first->second != NOT_SCHEDULED_FOR_PROCESSING) 303 if (insertion.first->second != NOT_SCHEDULED_FOR_PROCESSING)
304 return; 304 return;
305 305
306 process_changed_rules_requested_[extension_id] = SCHEDULED_FOR_PROCESSING; 306 process_changed_rules_requested_[extension_id] = SCHEDULED_FOR_PROCESSING;
307 ready_.Post(FROM_HERE, 307 ready_->Post(FROM_HERE,
308 base::Bind(&RulesRegistry::ProcessChangedRules, 308 base::Bind(&RulesRegistry::ProcessChangedRules,
309 weak_ptr_factory_.GetWeakPtr(), 309 weak_ptr_factory_.GetWeakPtr(),
310 extension_id)); 310 extension_id));
311 } 311 }
312 312
313 bool RulesRegistry::IsUniqueId(const std::string& extension_id, 313 bool RulesRegistry::IsUniqueId(const std::string& extension_id,
314 const std::string& rule_id) const { 314 const std::string& rule_id) const {
315 RuleIdentifiersMap::const_iterator identifiers = 315 RuleIdentifiersMap::const_iterator identifiers =
316 used_rule_identifiers_.find(extension_id); 316 used_rule_identifiers_.find(extension_id);
317 if (identifiers == used_rule_identifiers_.end()) 317 if (identifiers == used_rule_identifiers_.end())
318 return true; 318 return true;
319 return identifiers->second.find(rule_id) == identifiers->second.end(); 319 return identifiers->second.find(rule_id) == identifiers->second.end();
320 } 320 }
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 for (i = identifiers.begin(); i != identifiers.end(); ++i) 376 for (i = identifiers.begin(); i != identifiers.end(); ++i)
377 used_rule_identifiers_[extension_id].erase(*i); 377 used_rule_identifiers_[extension_id].erase(*i);
378 } 378 }
379 379
380 void RulesRegistry::RemoveAllUsedRuleIdentifiers( 380 void RulesRegistry::RemoveAllUsedRuleIdentifiers(
381 const std::string& extension_id) { 381 const std::string& extension_id) {
382 used_rule_identifiers_.erase(extension_id); 382 used_rule_identifiers_.erase(extension_id);
383 } 383 }
384 384
385 } // namespace extensions 385 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698