10f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
20f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
30f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// found in the LICENSE file.
40f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/api/declarative/rules_cache_delegate.h"
60f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
71320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "content/public/browser/browser_context.h"
80f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "content/public/browser/notification_details.h"
90f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "content/public/browser/notification_source.h"
101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/api/declarative/rules_registry.h"
11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "extensions/browser/extension_prefs.h"
12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "extensions/browser/extension_registry.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/browser/extension_system.h"
14f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/browser/info_map.h"
15116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "extensions/browser/state_store.h"
1646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "extensions/common/permissions/permissions_data.h"
170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)namespace {
190f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Returns the key to use for storing declarative rules in the state store.
210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)std::string GetDeclarativeRuleStorageKey(const std::string& event_name,
220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                                         bool incognito) {
230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (incognito)
240f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return "declarative_rules.incognito." + event_name;
250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  else
260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return "declarative_rules." + event_name;
270f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
280f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}  // namespace
310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
320f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)namespace extensions {
330f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
340f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// RulesCacheDelegate
350f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
360f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)const char RulesCacheDelegate::kRulesStoredKey[] =
370f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    "has_declarative_rules";
380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)RulesCacheDelegate::RulesCacheDelegate(bool log_storage_init_delay)
401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    : browser_context_(NULL),
410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      log_storage_init_delay_(log_storage_init_delay),
420f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      notified_registry_(false),
430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      weak_ptr_factory_(this) {
440f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)RulesCacheDelegate::~RulesCacheDelegate() {}
470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Returns the key to use for storing whether the rules have been stored.
490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// static
500f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)std::string RulesCacheDelegate::GetRulesStoredKey(const std::string& event_name,
510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                                                  bool incognito) {
520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  std::string result(kRulesStoredKey);
530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  result += incognito ? ".incognito." : ".";
540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  return result + event_name;
550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// This is called from the constructor of RulesRegistry, so it is
580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// important that it both
590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// 1. calls no (in particular virtual) methods of the rules registry, and
600f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// 2. does not create scoped_refptr holding the registry. (A short-lived
610f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// scoped_refptr might delete the rules registry before it is constructed.)
620f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void RulesCacheDelegate::Init(RulesRegistry* registry) {
63effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
650f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // WARNING: The first use of |registry_| will bind it to the calling thread
660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // so don't use this here.
670f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  registry_ = registry->GetWeakPtr();
680f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  browser_context_ = registry->browser_context();
700f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  storage_key_ =
710f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      GetDeclarativeRuleStorageKey(registry->event_name(),
721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                   browser_context_->IsOffTheRecord());
730f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  rules_stored_key_ = GetRulesStoredKey(registry->event_name(),
741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                        browser_context_->IsOffTheRecord());
750f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  rules_registry_thread_ = registry->owner_thread();
760f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ExtensionSystem& system = *ExtensionSystem::Get(browser_context_);
78cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  StateStore* store = system.rules_store();
790f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (store)
800f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    store->RegisterKey(storage_key_);
810f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (browser_context_->IsOffTheRecord())
830f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    log_storage_init_delay_ = false;
840f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
850f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  system.ready().Post(
860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      FROM_HERE,
870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      base::Bind(&RulesCacheDelegate::ReadRulesForInstalledExtensions,
880f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                 weak_ptr_factory_.GetWeakPtr()));
890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  system.ready().Post(FROM_HERE,
900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                      base::Bind(&RulesCacheDelegate::CheckIfReady,
910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                                 weak_ptr_factory_.GetWeakPtr()));
920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
940f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void RulesCacheDelegate::WriteToStorage(const std::string& extension_id,
950f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                                     scoped_ptr<base::Value> value) {
96effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!browser_context_)
980f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return;
990f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1000f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  const base::ListValue* rules = NULL;
1010f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  CHECK(value->GetAsList(&rules));
1020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  bool rules_stored_previously = GetDeclarativeRulesStored(extension_id);
1030f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  bool store_rules = !rules->empty();
1040f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  SetDeclarativeRulesStored(extension_id, store_rules);
1050f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (!rules_stored_previously && !store_rules)
1060f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return;
1070f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  StateStore* store = ExtensionSystem::Get(browser_context_)->rules_store();
1090f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (store)
1100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    store->SetExtensionValue(extension_id, storage_key_, value.Pass());
1110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
1120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void RulesCacheDelegate::CheckIfReady() {
114effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
1150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (notified_registry_ || !waiting_for_extensions_.empty())
1160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return;
1170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  content::BrowserThread::PostTask(
1190f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      rules_registry_thread_,
1200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      FROM_HERE,
1210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      base::Bind(
1220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)          &RulesRegistry::MarkReady, registry_, storage_init_time_));
1230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  notified_registry_ = true;
1240f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
1250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void RulesCacheDelegate::ReadRulesForInstalledExtensions() {
1271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  bool is_ready = ExtensionSystem::Get(browser_context_)->ready().is_signaled();
1281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // In an OTR context, we start on top of a normal context already, so the
1290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // extension service should be ready.
1301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DCHECK(!browser_context_->IsOffTheRecord() || is_ready);
1311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (is_ready) {
1321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const ExtensionSet& extensions =
1331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        ExtensionRegistry::Get(browser_context_)->enabled_extensions();
1341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const ExtensionPrefs* extension_prefs =
1351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        ExtensionPrefs::Get(browser_context_);
1361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    for (ExtensionSet::const_iterator i = extensions.begin();
1371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci         i != extensions.end();
1380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)         ++i) {
1390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      bool needs_apis_storing_rules =
14046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)          (*i)->permissions_data()->HasAPIPermission(
14146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)              APIPermission::kDeclarativeContent) ||
14246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)          (*i)->permissions_data()->HasAPIPermission(
14346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)              APIPermission::kDeclarativeWebRequest);
1440f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      bool respects_off_the_record =
1451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          !(browser_context_->IsOffTheRecord()) ||
1461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          extension_prefs->IsIncognitoEnabled((*i)->id());
1470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      if (needs_apis_storing_rules && respects_off_the_record)
1480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        ReadFromStorage((*i)->id());
1490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    }
1500f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
1510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
1520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void RulesCacheDelegate::ReadFromStorage(const std::string& extension_id) {
154effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
1551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!browser_context_)
1560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return;
1570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (log_storage_init_delay_ && storage_init_time_.is_null())
1590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    storage_init_time_ = base::Time::Now();
1600f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1610f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (!GetDeclarativeRulesStored(extension_id)) {
1621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ExtensionSystem::Get(browser_context_)->ready().Post(
1630f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        FROM_HERE, base::Bind(&RulesCacheDelegate::CheckIfReady,
1640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                              weak_ptr_factory_.GetWeakPtr()));
1650f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return;
1660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
1670f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  StateStore* store = ExtensionSystem::Get(browser_context_)->rules_store();
1690f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (!store)
1700f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return;
1710f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  waiting_for_extensions_.insert(extension_id);
1720f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  store->GetExtensionValue(
1730f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      extension_id,
1740f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      storage_key_,
1750f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      base::Bind(&RulesCacheDelegate::ReadFromStorageCallback,
1760f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                 weak_ptr_factory_.GetWeakPtr(),
1770f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                 extension_id));
1780f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
1790f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1800f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void RulesCacheDelegate::ReadFromStorageCallback(
1810f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    const std::string& extension_id,
1820f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    scoped_ptr<base::Value> value) {
183effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
1840f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  content::BrowserThread::PostTask(
1850f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      rules_registry_thread_,
1860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      FROM_HERE,
1870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      base::Bind(&RulesRegistry::DeserializeAndAddRules,
1880f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                 registry_,
1890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                 extension_id,
1900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                 base::Passed(&value)));
1910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  waiting_for_extensions_.erase(extension_id);
1930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1940f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (waiting_for_extensions_.empty())
1951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ExtensionSystem::Get(browser_context_)->ready().Post(
1960f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        FROM_HERE, base::Bind(&RulesCacheDelegate::CheckIfReady,
1970f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                              weak_ptr_factory_.GetWeakPtr()));
1980f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
1990f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2000f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)bool RulesCacheDelegate::GetDeclarativeRulesStored(
2010f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    const std::string& extension_id) const {
2021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  CHECK(browser_context_);
2031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  const ExtensionScopedPrefs* extension_prefs =
2041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      ExtensionPrefs::Get(browser_context_);
2050f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2060f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  bool rules_stored = true;
2070f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (extension_prefs->ReadPrefAsBoolean(
2080f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)          extension_id, rules_stored_key_, &rules_stored))
2090f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return rules_stored;
2100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Safe default -- if we don't know that the rules are not stored, we force
2120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // a read by returning true.
2130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  return true;
2140f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
2150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void RulesCacheDelegate::SetDeclarativeRulesStored(
2170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    const std::string& extension_id,
2180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    bool rules_stored) {
2191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  CHECK(browser_context_);
2201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DCHECK(ExtensionRegistry::Get(browser_context_)
221cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)             ->GetExtensionById(extension_id, ExtensionRegistry::EVERYTHING));
222cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
2231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ExtensionScopedPrefs* extension_prefs = ExtensionPrefs::Get(browser_context_);
2240f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  extension_prefs->UpdateExtensionPref(
2250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      extension_id,
2260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      rules_stored_key_,
2270f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      new base::FundamentalValue(rules_stored));
2280f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
2290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}  // namespace extensions
231