15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/api/declarative/rules_registry_service.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/lazy_instance.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_thread.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_details.h"
13f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "content/public/browser/notification_service.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_source.h"
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "content/public/browser/notification_types.h"
16f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "content/public/browser/render_process_host.h"
171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/api/declarative/rules_cache_delegate.h"
181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/api/declarative_content/content_rules_registry.h"
191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/api/declarative_webrequest/webrequest_constants.h"
201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/api/declarative_webrequest/webrequest_rules_registry.h"
211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/api/extensions_api_client.h"
221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/api/web_request/web_request_api.h"
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "extensions/browser/extension_registry.h"
24f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/common/extension.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace extensions {
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Registers |web_request_rules_registry| on the IO thread.
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void RegisterToExtensionWebRequestEventRouterOnIO(
321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    content::BrowserContext* browser_context,
33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const RulesRegistryService::WebViewKey& webview_key,
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry) {
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ExtensionWebRequestEventRouter::GetInstance()->RegisterRulesRegistry(
361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      browser_context, webview_key, web_request_rules_registry);
37f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
38f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
39f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)bool IsWebView(const RulesRegistryService::WebViewKey& webview_key) {
40f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return webview_key.embedder_process_id && webview_key.webview_instance_id;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)RulesRegistryService::RulesRegistryService(content::BrowserContext* context)
46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    : content_rules_registry_(NULL),
47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      extension_registry_observer_(this),
481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      browser_context_(context) {
491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (browser_context_) {
501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_));
51f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    registrar_.Add(
52f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
53f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        content::NotificationService::AllBrowserContextsAndSources());
54f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    EnsureDefaultRulesRegistriesRegistered(WebViewKey(0, 0));
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)RulesRegistryService::~RulesRegistryService() {}
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
60f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void RulesRegistryService::EnsureDefaultRulesRegistriesRegistered(
61f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const WebViewKey& webview_key) {
621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!browser_context_)
63f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
64f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
65f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  RulesRegistryKey key(declarative_webrequest_constants::kOnRequest,
66f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                       webview_key);
67f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // If we can find the key in the |rule_registries_| then we have already
68f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // installed the default registries.
69f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (ContainsKey(rule_registries_, key))
70f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
71f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
72f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
73f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  RulesCacheDelegate* web_request_cache_delegate = NULL;
74f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!IsWebView(webview_key)) {
75f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    web_request_cache_delegate =
76f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        new RulesCacheDelegate(true /*log_storage_init_delay*/);
77f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    cache_delegates_.push_back(web_request_cache_delegate);
78f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry(
801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      new WebRequestRulesRegistry(browser_context_,
81f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                  web_request_cache_delegate,
82f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                  webview_key));
83c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
84c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  RegisterRulesRegistry(web_request_rules_registry);
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::BrowserThread::PostTask(
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::BrowserThread::IO, FROM_HERE,
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO,
881320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          browser_context_, webview_key, web_request_rules_registry));
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
90f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Only create a ContentRulesRegistry for regular pages and not webviews.
91f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!IsWebView(webview_key)) {
92f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    RulesCacheDelegate* content_rules_cache_delegate =
93f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        new RulesCacheDelegate(false /*log_storage_init_delay*/);
94f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    cache_delegates_.push_back(content_rules_cache_delegate);
951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    scoped_refptr<ContentRulesRegistry> content_rules_registry =
961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        ExtensionsAPIClient::Get()->CreateContentRulesRegistry(
971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci            browser_context_, content_rules_cache_delegate);
98f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    RegisterRulesRegistry(content_rules_registry);
99f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    content_rules_registry_ = content_rules_registry.get();
100f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void RulesRegistryService::Shutdown() {
104868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Release the references to all registries. This would happen soon during
105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // destruction of |*this|, but we need the ExtensionWebRequestEventRouter to
106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // be the last to reference the WebRequestRulesRegistry objects, so that
107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // the posted task below causes their destruction on the IO thread, not on UI
108868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // where the destruction of |*this| takes place.
109868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // TODO(vabr): Remove once http://crbug.com/218451#c6 gets addressed.
110868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  rule_registries_.clear();
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::BrowserThread::PostTask(
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::BrowserThread::IO, FROM_HERE,
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO,
1141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          browser_context_, WebViewKey(0, 0),
115f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          scoped_refptr<WebRequestRulesRegistry>(NULL)));
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
118a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)static base::LazyInstance<BrowserContextKeyedAPIFactory<RulesRegistryService> >
119a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    g_factory = LAZY_INSTANCE_INITIALIZER;
120868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
121868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// static
122a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)BrowserContextKeyedAPIFactory<RulesRegistryService>*
123868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)RulesRegistryService::GetFactoryInstance() {
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return g_factory.Pointer();
125868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
126868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
127868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// static
128a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)RulesRegistryService* RulesRegistryService::Get(
129a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    content::BrowserContext* context) {
130a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return BrowserContextKeyedAPIFactory<RulesRegistryService>::Get(context);
131868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
132868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void RulesRegistryService::RegisterRulesRegistry(
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_refptr<RulesRegistry> rule_registry) {
135c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const std::string event_name(rule_registry->event_name());
136f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  RulesRegistryKey key(event_name, rule_registry->webview_key());
137f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DCHECK(rule_registries_.find(key) == rule_registries_.end());
138f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  rule_registries_[key] = rule_registry;
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)scoped_refptr<RulesRegistry> RulesRegistryService::GetRulesRegistry(
142f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const WebViewKey& webview_key,
143f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const std::string& event_name) {
144f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EnsureDefaultRulesRegistriesRegistered(webview_key);
145f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
146f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  RulesRegistryKey key(event_name, webview_key);
147f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  RulesRegistryMap::const_iterator i = rule_registries_.find(key);
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (i == rule_registries_.end())
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return scoped_refptr<RulesRegistry>();
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return i->second;
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
153f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void RulesRegistryService::RemoveWebViewRulesRegistries(int process_id) {
154f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DCHECK_NE(0, process_id);
155f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
156f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  std::set<RulesRegistryKey> registries_to_delete;
157f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  for (RulesRegistryMap::iterator it = rule_registries_.begin();
158f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)       it != rule_registries_.end(); ++it) {
159f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const RulesRegistryKey& key = it->first;
160f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const WebViewKey& webview_key = key.webview_key;
161f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    int embedder_process_id = webview_key.embedder_process_id;
162f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // |process_id| will always be non-zero.
163f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // |embedder_process_id| will only be non-zero if the key corresponds to a
164f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // webview registry.
165f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Thus, |embedder_process_id| == |process_id| ==> the process ID is a
166f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // webview embedder.
167f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (embedder_process_id != process_id)
168f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      continue;
169f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
170f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Modifying the container while iterating is bad so we'll save the keys we
171f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // wish to delete in another container, and delete them in another loop.
172f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    registries_to_delete.insert(key);
173f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
174f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  for (std::set<RulesRegistryKey>::iterator it = registries_to_delete.begin();
175f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)       it != registries_to_delete.end(); ++it) {
176f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    rule_registries_.erase(*it);
177f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
178f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
179f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
180f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void RulesRegistryService::SimulateExtensionUninstalled(
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& extension_id) {
182f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  NotifyRegistriesHelper(&RulesRegistry::OnExtensionUninstalled, extension_id);
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void RulesRegistryService::NotifyRegistriesHelper(
186f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    void (RulesRegistry::*notification_callback)(const std::string&),
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& extension_id) {
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RulesRegistryMap::iterator i;
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (i = rule_registries_.begin(); i != rule_registries_.end(); ++i) {
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_refptr<RulesRegistry> registry = i->second;
191c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (content::BrowserThread::CurrentlyOn(registry->owner_thread())) {
1921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      (registry.get()->*notification_callback)(extension_id);
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::BrowserThread::PostTask(
195c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          registry->owner_thread(),
196c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          FROM_HERE,
197f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          base::Bind(notification_callback, registry, extension_id));
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
202cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void RulesRegistryService::OnExtensionLoaded(
203cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    content::BrowserContext* browser_context,
204cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const Extension* extension) {
205cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  NotifyRegistriesHelper(&RulesRegistry::OnExtensionLoaded, extension->id());
206cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
207cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
208cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void RulesRegistryService::OnExtensionUnloaded(
209cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    content::BrowserContext* browser_context,
210cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const Extension* extension,
211cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    UnloadedExtensionInfo::Reason reason) {
212cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  NotifyRegistriesHelper(&RulesRegistry::OnExtensionUnloaded, extension->id());
213cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
214cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
215cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void RulesRegistryService::OnExtensionUninstalled(
216cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    content::BrowserContext* browser_context,
2175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    const Extension* extension,
2185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    extensions::UninstallReason reason) {
219cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  NotifyRegistriesHelper(&RulesRegistry::OnExtensionUninstalled,
220cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                         extension->id());
221cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
222cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void RulesRegistryService::Observe(
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int type,
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const content::NotificationSource& source,
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const content::NotificationDetails& details) {
227cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DCHECK_EQ(content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, type);
228cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
229cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  content::RenderProcessHost* process =
230cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      content::Source<content::RenderProcessHost>(source).ptr();
231cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  RemoveWebViewRulesRegistries(process->GetID());
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace extensions
235