| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_cache_delegate.h" | 5 #include "chrome/browser/extensions/api/declarative/rules_cache_delegate.h" |
| 6 | 6 |
| 7 #include "chrome/browser/chrome_notification_types.h" | 7 #include "chrome/browser/chrome_notification_types.h" |
| 8 #include "chrome/browser/extensions/api/declarative/rules_registry.h" | 8 #include "chrome/browser/extensions/api/declarative/rules_registry.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/extensions/extension_util.h" | 10 #include "chrome/browser/extensions/extension_util.h" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 result += incognito ? ".incognito." : "."; | 53 result += incognito ? ".incognito." : "."; |
| 54 return result + event_name; | 54 return result + event_name; |
| 55 } | 55 } |
| 56 | 56 |
| 57 // This is called from the constructor of RulesRegistry, so it is | 57 // This is called from the constructor of RulesRegistry, so it is |
| 58 // important that it both | 58 // important that it both |
| 59 // 1. calls no (in particular virtual) methods of the rules registry, and | 59 // 1. calls no (in particular virtual) methods of the rules registry, and |
| 60 // 2. does not create scoped_refptr holding the registry. (A short-lived | 60 // 2. does not create scoped_refptr holding the registry. (A short-lived |
| 61 // scoped_refptr might delete the rules registry before it is constructed.) | 61 // scoped_refptr might delete the rules registry before it is constructed.) |
| 62 void RulesCacheDelegate::Init(RulesRegistry* registry) { | 62 void RulesCacheDelegate::Init(RulesRegistry* registry) { |
| 63 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 63 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 64 | 64 |
| 65 // WARNING: The first use of |registry_| will bind it to the calling thread | 65 // WARNING: The first use of |registry_| will bind it to the calling thread |
| 66 // so don't use this here. | 66 // so don't use this here. |
| 67 registry_ = registry->GetWeakPtr(); | 67 registry_ = registry->GetWeakPtr(); |
| 68 | 68 |
| 69 profile_ = registry->profile(); | 69 profile_ = registry->profile(); |
| 70 storage_key_ = | 70 storage_key_ = |
| 71 GetDeclarativeRuleStorageKey(registry->event_name(), | 71 GetDeclarativeRuleStorageKey(registry->event_name(), |
| 72 profile_->IsOffTheRecord()); | 72 profile_->IsOffTheRecord()); |
| 73 rules_stored_key_ = GetRulesStoredKey(registry->event_name(), | 73 rules_stored_key_ = GetRulesStoredKey(registry->event_name(), |
| (...skipping 12 matching lines...) Expand all Loading... |
| 86 FROM_HERE, | 86 FROM_HERE, |
| 87 base::Bind(&RulesCacheDelegate::ReadRulesForInstalledExtensions, | 87 base::Bind(&RulesCacheDelegate::ReadRulesForInstalledExtensions, |
| 88 weak_ptr_factory_.GetWeakPtr())); | 88 weak_ptr_factory_.GetWeakPtr())); |
| 89 system.ready().Post(FROM_HERE, | 89 system.ready().Post(FROM_HERE, |
| 90 base::Bind(&RulesCacheDelegate::CheckIfReady, | 90 base::Bind(&RulesCacheDelegate::CheckIfReady, |
| 91 weak_ptr_factory_.GetWeakPtr())); | 91 weak_ptr_factory_.GetWeakPtr())); |
| 92 } | 92 } |
| 93 | 93 |
| 94 void RulesCacheDelegate::WriteToStorage(const std::string& extension_id, | 94 void RulesCacheDelegate::WriteToStorage(const std::string& extension_id, |
| 95 scoped_ptr<base::Value> value) { | 95 scoped_ptr<base::Value> value) { |
| 96 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 96 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 97 if (!profile_) | 97 if (!profile_) |
| 98 return; | 98 return; |
| 99 | 99 |
| 100 const base::ListValue* rules = NULL; | 100 const base::ListValue* rules = NULL; |
| 101 CHECK(value->GetAsList(&rules)); | 101 CHECK(value->GetAsList(&rules)); |
| 102 bool rules_stored_previously = GetDeclarativeRulesStored(extension_id); | 102 bool rules_stored_previously = GetDeclarativeRulesStored(extension_id); |
| 103 bool store_rules = !rules->empty(); | 103 bool store_rules = !rules->empty(); |
| 104 SetDeclarativeRulesStored(extension_id, store_rules); | 104 SetDeclarativeRulesStored(extension_id, store_rules); |
| 105 if (!rules_stored_previously && !store_rules) | 105 if (!rules_stored_previously && !store_rules) |
| 106 return; | 106 return; |
| 107 | 107 |
| 108 StateStore* store = ExtensionSystem::Get(profile_)->rules_store(); | 108 StateStore* store = ExtensionSystem::Get(profile_)->rules_store(); |
| 109 if (store) | 109 if (store) |
| 110 store->SetExtensionValue(extension_id, storage_key_, value.Pass()); | 110 store->SetExtensionValue(extension_id, storage_key_, value.Pass()); |
| 111 } | 111 } |
| 112 | 112 |
| 113 void RulesCacheDelegate::CheckIfReady() { | 113 void RulesCacheDelegate::CheckIfReady() { |
| 114 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 114 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 115 if (notified_registry_ || !waiting_for_extensions_.empty()) | 115 if (notified_registry_ || !waiting_for_extensions_.empty()) |
| 116 return; | 116 return; |
| 117 | 117 |
| 118 content::BrowserThread::PostTask( | 118 content::BrowserThread::PostTask( |
| 119 rules_registry_thread_, | 119 rules_registry_thread_, |
| 120 FROM_HERE, | 120 FROM_HERE, |
| 121 base::Bind( | 121 base::Bind( |
| 122 &RulesRegistry::MarkReady, registry_, storage_init_time_)); | 122 &RulesRegistry::MarkReady, registry_, storage_init_time_)); |
| 123 notified_registry_ = true; | 123 notified_registry_ = true; |
| 124 } | 124 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 141 bool respects_off_the_record = | 141 bool respects_off_the_record = |
| 142 !(profile_->IsOffTheRecord()) || | 142 !(profile_->IsOffTheRecord()) || |
| 143 util::IsIncognitoEnabled((*i)->id(), profile_); | 143 util::IsIncognitoEnabled((*i)->id(), profile_); |
| 144 if (needs_apis_storing_rules && respects_off_the_record) | 144 if (needs_apis_storing_rules && respects_off_the_record) |
| 145 ReadFromStorage((*i)->id()); | 145 ReadFromStorage((*i)->id()); |
| 146 } | 146 } |
| 147 } | 147 } |
| 148 } | 148 } |
| 149 | 149 |
| 150 void RulesCacheDelegate::ReadFromStorage(const std::string& extension_id) { | 150 void RulesCacheDelegate::ReadFromStorage(const std::string& extension_id) { |
| 151 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 151 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 152 if (!profile_) | 152 if (!profile_) |
| 153 return; | 153 return; |
| 154 | 154 |
| 155 if (log_storage_init_delay_ && storage_init_time_.is_null()) | 155 if (log_storage_init_delay_ && storage_init_time_.is_null()) |
| 156 storage_init_time_ = base::Time::Now(); | 156 storage_init_time_ = base::Time::Now(); |
| 157 | 157 |
| 158 if (!GetDeclarativeRulesStored(extension_id)) { | 158 if (!GetDeclarativeRulesStored(extension_id)) { |
| 159 ExtensionSystem::Get(profile_)->ready().Post( | 159 ExtensionSystem::Get(profile_)->ready().Post( |
| 160 FROM_HERE, base::Bind(&RulesCacheDelegate::CheckIfReady, | 160 FROM_HERE, base::Bind(&RulesCacheDelegate::CheckIfReady, |
| 161 weak_ptr_factory_.GetWeakPtr())); | 161 weak_ptr_factory_.GetWeakPtr())); |
| 162 return; | 162 return; |
| 163 } | 163 } |
| 164 | 164 |
| 165 extensions::StateStore* store = ExtensionSystem::Get(profile_)->rules_store(); | 165 extensions::StateStore* store = ExtensionSystem::Get(profile_)->rules_store(); |
| 166 if (!store) | 166 if (!store) |
| 167 return; | 167 return; |
| 168 waiting_for_extensions_.insert(extension_id); | 168 waiting_for_extensions_.insert(extension_id); |
| 169 store->GetExtensionValue( | 169 store->GetExtensionValue( |
| 170 extension_id, | 170 extension_id, |
| 171 storage_key_, | 171 storage_key_, |
| 172 base::Bind(&RulesCacheDelegate::ReadFromStorageCallback, | 172 base::Bind(&RulesCacheDelegate::ReadFromStorageCallback, |
| 173 weak_ptr_factory_.GetWeakPtr(), | 173 weak_ptr_factory_.GetWeakPtr(), |
| 174 extension_id)); | 174 extension_id)); |
| 175 } | 175 } |
| 176 | 176 |
| 177 void RulesCacheDelegate::ReadFromStorageCallback( | 177 void RulesCacheDelegate::ReadFromStorageCallback( |
| 178 const std::string& extension_id, | 178 const std::string& extension_id, |
| 179 scoped_ptr<base::Value> value) { | 179 scoped_ptr<base::Value> value) { |
| 180 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 180 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 181 content::BrowserThread::PostTask( | 181 content::BrowserThread::PostTask( |
| 182 rules_registry_thread_, | 182 rules_registry_thread_, |
| 183 FROM_HERE, | 183 FROM_HERE, |
| 184 base::Bind(&RulesRegistry::DeserializeAndAddRules, | 184 base::Bind(&RulesRegistry::DeserializeAndAddRules, |
| 185 registry_, | 185 registry_, |
| 186 extension_id, | 186 extension_id, |
| 187 base::Passed(&value))); | 187 base::Passed(&value))); |
| 188 | 188 |
| 189 waiting_for_extensions_.erase(extension_id); | 189 waiting_for_extensions_.erase(extension_id); |
| 190 | 190 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 218 DCHECK(extension_service); | 218 DCHECK(extension_service); |
| 219 DCHECK(extension_service->GetInstalledExtension(extension_id)); | 219 DCHECK(extension_service->GetInstalledExtension(extension_id)); |
| 220 ExtensionScopedPrefs* extension_prefs = ExtensionPrefs::Get(profile_); | 220 ExtensionScopedPrefs* extension_prefs = ExtensionPrefs::Get(profile_); |
| 221 extension_prefs->UpdateExtensionPref( | 221 extension_prefs->UpdateExtensionPref( |
| 222 extension_id, | 222 extension_id, |
| 223 rules_stored_key_, | 223 rules_stored_key_, |
| 224 new base::FundamentalValue(rules_stored)); | 224 new base::FundamentalValue(rules_stored)); |
| 225 } | 225 } |
| 226 | 226 |
| 227 } // namespace extensions | 227 } // namespace extensions |
| OLD | NEW |