summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/declarative
diff options
context:
space:
mode:
authorfsamuel@chromium.org <fsamuel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-11-13 03:28:23 +0000
committerfsamuel@chromium.org <fsamuel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-11-13 03:28:23 +0000
commitcfc7821418ec8759df693d7abd2fcb1be1aa24c6 (patch)
tree9992f1ac6ccd141dd63f96b3c2d15ca93737ee41 /chrome/browser/extensions/api/declarative
parent9fac4e42bec7716471a67ade46e6420cc51be989 (diff)
downloadchromium_src-cfc7821418ec8759df693d7abd2fcb1be1aa24c6.zip
chromium_src-cfc7821418ec8759df693d7abd2fcb1be1aa24c6.tar.gz
chromium_src-cfc7821418ec8759df693d7abd2fcb1be1aa24c6.tar.bz2
<webview>: Implement declarativeWebRequest API
This CL exposes the declarative WebRequest API to <webview>s. This CL isolates rules registries on a per-<profile, embedder_process_id, webview_instance_id> tuple. For extensions, the pair <embedder_process_id, webview_instance_id> == <0, 0>. Rules registries are now created on-demand rather than on creation of the RulesRegistryService. This is so that we only create rules registries for webviews if the webview adds rules. This also allows rules to be installed prior to initial navigation of a webview. Sample code: var webview = document.querySelector('webview'); var rule = { conditions: [ new chrome.webViewRequest.RequestMatcher({ url: { hostSuffix: 'slashdot.org' } }) ], actions: [ new chrome.webViewRequest.CancelRequest() ] }; webview.request.onRequest.addRules([rule]) BUG=273855 Review URL: https://codereview.chromium.org/28273006 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@234724 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions/api/declarative')
-rw-r--r--chrome/browser/extensions/api/declarative/declarative_api.cc48
-rw-r--r--chrome/browser/extensions/api/declarative/declarative_apitest.cc1
-rw-r--r--chrome/browser/extensions/api/declarative/initializing_rules_registry_unittest.cc6
-rw-r--r--chrome/browser/extensions/api/declarative/rules_registry.cc4
-rw-r--r--chrome/browser/extensions/api/declarative/rules_registry.h29
-rw-r--r--chrome/browser/extensions/api/declarative/rules_registry_service.cc111
-rw-r--r--chrome/browser/extensions/api/declarative/rules_registry_service.h35
-rw-r--r--chrome/browser/extensions/api/declarative/rules_registry_service_unittest.cc75
-rw-r--r--chrome/browser/extensions/api/declarative/rules_registry_with_cache_unittest.cc30
-rw-r--r--chrome/browser/extensions/api/declarative/test_rules_registry.cc12
-rw-r--r--chrome/browser/extensions/api/declarative/test_rules_registry.h6
11 files changed, 299 insertions, 58 deletions
diff --git a/chrome/browser/extensions/api/declarative/declarative_api.cc b/chrome/browser/extensions/api/declarative/declarative_api.cc
index f0bd305..6f3ec12 100644
--- a/chrome/browser/extensions/api/declarative/declarative_api.cc
+++ b/chrome/browser/extensions/api/declarative/declarative_api.cc
@@ -10,9 +10,12 @@
#include "base/values.h"
#include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
#include "chrome/browser/extensions/extension_system_factory.h"
+#include "chrome/browser/guestview/webview/webview_guest.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/api/events.h"
#include "content/public/browser/browser_thread.h"
+#include "content/public/browser/render_process_host.h"
+#include "content/public/browser/render_view_host.h"
#include "extensions/common/extension_api.h"
using extensions::api::events::Rule;
@@ -21,15 +24,43 @@ namespace AddRules = extensions::api::events::Event::AddRules;
namespace GetRules = extensions::api::events::Event::GetRules;
namespace RemoveRules = extensions::api::events::Event::RemoveRules;
+
namespace extensions {
-RulesFunction::RulesFunction() : rules_registry_(NULL) {}
+namespace {
+
+const char kWebRequest[] = "declarativeWebRequest.";
+const char kWebView[] = "webview.";
+const char kWebViewExpectedError[] = "Webview event with Webview ID expected.";
+
+bool IsWebViewEvent(const std::string& event_name) {
+ // Sample event names:
+ // webview.onRequest.
+ // webview.OnMessage.
+ return event_name.compare(0, strlen(kWebView), kWebView) == 0;
+}
+
+std::string GetWebRequestEventName(const std::string& event_name) {
+ std::string web_request_event_name(event_name);
+ if (IsWebViewEvent(web_request_event_name))
+ web_request_event_name.replace(0, strlen(kWebView), kWebRequest);
+ return web_request_event_name;
+}
+
+} // namespace
+
+RulesFunction::RulesFunction()
+ : rules_registry_(NULL) {
+}
RulesFunction::~RulesFunction() {}
bool RulesFunction::HasPermission() {
std::string event_name;
EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
+ if (IsWebViewEvent(event_name) &&
+ extension_->HasAPIPermission(extensions::APIPermission::kWebView))
+ return true;
Feature::Availability availability =
ExtensionAPI::GetSharedInstance()->IsAvailable(
event_name, extension_, Feature::BLESSED_EXTENSION_CONTEXT,
@@ -41,9 +72,22 @@ bool RulesFunction::RunImpl() {
std::string event_name;
EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
+ int webview_instance_id = 0;
+ EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &webview_instance_id));
+ int embedder_process_id = render_view_host()->GetProcess()->GetID();
+
+ bool has_webview = webview_instance_id != 0;
+ if (has_webview != IsWebViewEvent(event_name))
+ EXTENSION_FUNCTION_ERROR(kWebViewExpectedError);
+ event_name = GetWebRequestEventName(event_name);
+
+ // If we are not operating on a particular <webview>, then the key is (0, 0).
+ RulesRegistryService::WebViewKey key(
+ webview_instance_id ? embedder_process_id : 0, webview_instance_id);
+
RulesRegistryService* rules_registry_service =
RulesRegistryService::Get(GetProfile());
- rules_registry_ = rules_registry_service->GetRulesRegistry(event_name);
+ rules_registry_ = rules_registry_service->GetRulesRegistry(key, event_name);
// Raw access to this function is not available to extensions, therefore
// there should never be a request for a nonexisting rules registry.
EXTENSION_FUNCTION_VALIDATE(rules_registry_.get());
diff --git a/chrome/browser/extensions/api/declarative/declarative_apitest.cc b/chrome/browser/extensions/api/declarative/declarative_apitest.cc
index 8482ec7..056f153 100644
--- a/chrome/browser/extensions/api/declarative/declarative_apitest.cc
+++ b/chrome/browser/extensions/api/declarative/declarative_apitest.cc
@@ -57,6 +57,7 @@ IN_PROC_BROWSER_TEST_F(DeclarativeApiTest, DeclarativeApi) {
extensions::RulesRegistryService::Get(browser()->profile());
scoped_refptr<RulesRegistry> rules_registry =
rules_registry_service->GetRulesRegistry(
+ RulesRegistry::WebViewKey(0, 0),
extensions::declarative_webrequest_constants::kOnRequest);
std::vector<linked_ptr<RulesRegistry::Rule> > known_rules;
diff --git a/chrome/browser/extensions/api/declarative/initializing_rules_registry_unittest.cc b/chrome/browser/extensions/api/declarative/initializing_rules_registry_unittest.cc
index 12d707d..67ad2a2 100644
--- a/chrome/browser/extensions/api/declarative/initializing_rules_registry_unittest.cc
+++ b/chrome/browser/extensions/api/declarative/initializing_rules_registry_unittest.cc
@@ -22,9 +22,10 @@ TEST(InitializingRulesRegistryTest, FillOptionalIdentifiers) {
base::MessageLoopForUI message_loop;
content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop);
+ const RulesRegistry::WebViewKey key(0, 0);
std::string error;
scoped_refptr<RulesRegistry> registry =
- new TestRulesRegistry(content::BrowserThread::UI, "" /*event_name*/);
+ new TestRulesRegistry(content::BrowserThread::UI, "" /*event_name*/, key);
// Add rules and check that their identifiers are filled and unique.
@@ -137,9 +138,10 @@ TEST(InitializingRulesRegistryTest, FillOptionalPriority) {
base::MessageLoopForUI message_loop;
content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop);
+ const RulesRegistry::WebViewKey key(0, 0);
std::string error;
scoped_refptr<RulesRegistry> registry =
- new TestRulesRegistry(content::BrowserThread::UI, "" /*event_name*/);
+ new TestRulesRegistry(content::BrowserThread::UI, "" /*event_name*/, key);
// Add rules and check that their priorities are filled if they are empty.
diff --git a/chrome/browser/extensions/api/declarative/rules_registry.cc b/chrome/browser/extensions/api/declarative/rules_registry.cc
index 2f6c2fb..0fb789e 100644
--- a/chrome/browser/extensions/api/declarative/rules_registry.cc
+++ b/chrome/browser/extensions/api/declarative/rules_registry.cc
@@ -74,10 +74,12 @@ RulesRegistry::RulesRegistry(
Profile* profile,
const std::string& event_name,
content::BrowserThread::ID owner_thread,
- RulesCacheDelegate* cache_delegate)
+ RulesCacheDelegate* cache_delegate,
+ const WebViewKey& webview_key)
: profile_(profile),
owner_thread_(owner_thread),
event_name_(event_name),
+ webview_key_(webview_key),
weak_ptr_factory_(profile ? this : NULL),
process_changed_rules_requested_(profile ? NOT_SCHEDULED_FOR_PROCESSING
: NEVER_PROCESS),
diff --git a/chrome/browser/extensions/api/declarative/rules_registry.h b/chrome/browser/extensions/api/declarative/rules_registry.h
index f877a65..ae16269 100644
--- a/chrome/browser/extensions/api/declarative/rules_registry.h
+++ b/chrome/browser/extensions/api/declarative/rules_registry.h
@@ -40,6 +40,18 @@ class RulesCacheDelegate;
class RulesRegistry : public base::RefCountedThreadSafe<RulesRegistry> {
public:
typedef extensions::api::events::Rule Rule;
+ struct WebViewKey {
+ int embedder_process_id;
+ int webview_instance_id;
+ WebViewKey(int embedder_process_id, int webview_instance_id)
+ : embedder_process_id(embedder_process_id),
+ webview_instance_id(webview_instance_id) {}
+ bool operator<(const WebViewKey& other) const {
+ return embedder_process_id < other.embedder_process_id ||
+ ((embedder_process_id == other.embedder_process_id) &&
+ (webview_instance_id < other.webview_instance_id));
+ }
+ };
enum Defaults { DEFAULT_PRIORITY = 100 };
// After the RulesCacheDelegate object (the part of the registry which runs on
@@ -50,7 +62,8 @@ class RulesRegistry : public base::RefCountedThreadSafe<RulesRegistry> {
RulesRegistry(Profile* profile,
const std::string& event_name,
content::BrowserThread::ID owner_thread,
- RulesCacheDelegate* cache_delegate);
+ RulesCacheDelegate* cache_delegate,
+ const WebViewKey& webview_key);
const OneShotEvent& ready() const {
return ready_;
@@ -120,6 +133,11 @@ class RulesRegistry : public base::RefCountedThreadSafe<RulesRegistry> {
// Every ExtensionId counts as one entry, even if it contains no rules.
size_t GetNumberOfUsedRuleIdentifiersForTesting() const;
+ // Returns the RulesCacheDelegate. This is used for testing.
+ RulesCacheDelegate* rules_cache_delegate_for_testing() const {
+ return cache_delegate_.get();
+ }
+
// Returns the profile where the rules registry lives.
Profile* profile() const { return profile_; }
@@ -130,6 +148,12 @@ class RulesRegistry : public base::RefCountedThreadSafe<RulesRegistry> {
// The name of the event with which rules are registered.
const std::string& event_name() const { return event_name_; }
+ // The key that identifies the webview (or tabs) in which these rules apply.
+ // If the rules apply to the main browser, then this returns the tuple (0, 0).
+ const WebViewKey& webview_key() const {
+ return webview_key_;
+ }
+
protected:
virtual ~RulesRegistry();
@@ -192,6 +216,9 @@ class RulesRegistry : public base::RefCountedThreadSafe<RulesRegistry> {
// The name of the event with which rules are registered.
const std::string event_name_;
+ // The key that identifies the context in which these rules apply.
+ WebViewKey webview_key_;
+
RulesDictionary rules_;
// Signaled when we have finished reading from storage for all extensions that
diff --git a/chrome/browser/extensions/api/declarative/rules_registry_service.cc b/chrome/browser/extensions/api/declarative/rules_registry_service.cc
index a0685d0..abe3b24 100644
--- a/chrome/browser/extensions/api/declarative/rules_registry_service.cc
+++ b/chrome/browser/extensions/api/declarative/rules_registry_service.cc
@@ -11,12 +11,15 @@
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/api/declarative/rules_cache_delegate.h"
#include "chrome/browser/extensions/api/declarative_content/content_rules_registry.h"
+#include "chrome/browser/extensions/api/declarative_webrequest/webrequest_constants.h"
#include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_registry.h"
#include "chrome/browser/extensions/api/web_request/web_request_api.h"
#include "chrome/common/extensions/extension.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_details.h"
+#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
+#include "content/public/browser/render_process_host.h"
namespace extensions {
@@ -25,9 +28,14 @@ namespace {
// Registers |web_request_rules_registry| on the IO thread.
void RegisterToExtensionWebRequestEventRouterOnIO(
void* profile,
+ const RulesRegistryService::WebViewKey& webview_key,
scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry) {
ExtensionWebRequestEventRouter::GetInstance()->RegisterRulesRegistry(
- profile, web_request_rules_registry);
+ profile, webview_key, web_request_rules_registry);
+}
+
+bool IsWebView(const RulesRegistryService::WebViewKey& webview_key) {
+ return webview_key.embedder_process_id && webview_key.webview_instance_id;
}
} // namespace
@@ -38,34 +46,56 @@ RulesRegistryService::RulesRegistryService(Profile* profile)
if (profile) {
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
content::Source<Profile>(profile->GetOriginalProfile()));
- RegisterDefaultRulesRegistries();
+ registrar_.Add(
+ this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
+ content::NotificationService::AllBrowserContextsAndSources());
+ EnsureDefaultRulesRegistriesRegistered(WebViewKey(0, 0));
}
}
RulesRegistryService::~RulesRegistryService() {}
-void RulesRegistryService::RegisterDefaultRulesRegistries() {
- scoped_ptr<RulesCacheDelegate> web_request_cache_delegate(
- new RulesCacheDelegate(true /*log_storage_init_delay*/));
+void RulesRegistryService::EnsureDefaultRulesRegistriesRegistered(
+ const WebViewKey& webview_key) {
+ if (!profile_)
+ return;
+
+ RulesRegistryKey key(declarative_webrequest_constants::kOnRequest,
+ webview_key);
+ // If we can find the key in the |rule_registries_| then we have already
+ // installed the default registries.
+ if (ContainsKey(rule_registries_, key))
+ return;
+
+
+ RulesCacheDelegate* web_request_cache_delegate = NULL;
+ if (!IsWebView(webview_key)) {
+ web_request_cache_delegate =
+ new RulesCacheDelegate(true /*log_storage_init_delay*/);
+ cache_delegates_.push_back(web_request_cache_delegate);
+ }
scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry(
- new WebRequestRulesRegistry(profile_, web_request_cache_delegate.get()));
- cache_delegates_.push_back(web_request_cache_delegate.release());
+ new WebRequestRulesRegistry(profile_,
+ web_request_cache_delegate,
+ webview_key));
RegisterRulesRegistry(web_request_rules_registry);
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO,
- profile_, web_request_rules_registry));
+ profile_, webview_key, web_request_rules_registry));
#if defined(ENABLE_EXTENSIONS)
- scoped_ptr<RulesCacheDelegate> content_rules_cache_delegate(
- new RulesCacheDelegate(false /*log_storage_init_delay*/));
- scoped_refptr<ContentRulesRegistry> content_rules_registry(
- new ContentRulesRegistry(profile_, content_rules_cache_delegate.get()));
- cache_delegates_.push_back(content_rules_cache_delegate.release());
-
- RegisterRulesRegistry(content_rules_registry);
- content_rules_registry_ = content_rules_registry.get();
+ // Only create a ContentRulesRegistry for regular pages and not webviews.
+ if (!IsWebView(webview_key)) {
+ RulesCacheDelegate* content_rules_cache_delegate =
+ new RulesCacheDelegate(false /*log_storage_init_delay*/);
+ cache_delegates_.push_back(content_rules_cache_delegate);
+ scoped_refptr<ContentRulesRegistry> content_rules_registry(
+ new ContentRulesRegistry(profile_, content_rules_cache_delegate));
+ RegisterRulesRegistry(content_rules_registry);
+ content_rules_registry_ = content_rules_registry.get();
+ }
#endif // defined(ENABLE_EXTENSIONS)
}
@@ -80,7 +110,8 @@ void RulesRegistryService::Shutdown() {
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO,
- profile_, scoped_refptr<WebRequestRulesRegistry>(NULL)));
+ profile_, WebViewKey(0, 0),
+ scoped_refptr<WebRequestRulesRegistry>(NULL)));
}
static base::LazyInstance<ProfileKeyedAPIFactory<RulesRegistryService> >
@@ -100,18 +131,50 @@ RulesRegistryService* RulesRegistryService::Get(Profile* profile) {
void RulesRegistryService::RegisterRulesRegistry(
scoped_refptr<RulesRegistry> rule_registry) {
const std::string event_name(rule_registry->event_name());
- DCHECK(rule_registries_.find(event_name) == rule_registries_.end());
- rule_registries_[event_name] = rule_registry;
+ RulesRegistryKey key(event_name, rule_registry->webview_key());
+ DCHECK(rule_registries_.find(key) == rule_registries_.end());
+ rule_registries_[key] = rule_registry;
}
scoped_refptr<RulesRegistry> RulesRegistryService::GetRulesRegistry(
- const std::string& event_name) const {
- RulesRegistryMap::const_iterator i = rule_registries_.find(event_name);
+ const WebViewKey& webview_key,
+ const std::string& event_name) {
+ EnsureDefaultRulesRegistriesRegistered(webview_key);
+
+ RulesRegistryKey key(event_name, webview_key);
+ RulesRegistryMap::const_iterator i = rule_registries_.find(key);
if (i == rule_registries_.end())
return scoped_refptr<RulesRegistry>();
return i->second;
}
+void RulesRegistryService::RemoveWebViewRulesRegistries(int process_id) {
+ DCHECK_NE(0, process_id);
+
+ std::set<RulesRegistryKey> registries_to_delete;
+ for (RulesRegistryMap::iterator it = rule_registries_.begin();
+ it != rule_registries_.end(); ++it) {
+ const RulesRegistryKey& key = it->first;
+ const WebViewKey& webview_key = key.webview_key;
+ int embedder_process_id = webview_key.embedder_process_id;
+ // |process_id| will always be non-zero.
+ // |embedder_process_id| will only be non-zero if the key corresponds to a
+ // webview registry.
+ // Thus, |embedder_process_id| == |process_id| ==> the process ID is a
+ // webview embedder.
+ if (embedder_process_id != process_id)
+ continue;
+
+ // Modifying the container while iterating is bad so we'll save the keys we
+ // wish to delete in another container, and delete them in another loop.
+ registries_to_delete.insert(key);
+ }
+ for (std::set<RulesRegistryKey>::iterator it = registries_to_delete.begin();
+ it != registries_to_delete.end(); ++it) {
+ rule_registries_.erase(*it);
+ }
+}
+
void RulesRegistryService::SimulateExtensionUnloaded(
const std::string& extension_id) {
OnExtensionUnloaded(extension_id);
@@ -145,6 +208,12 @@ void RulesRegistryService::Observe(
OnExtensionUnloaded(extension->id());
break;
}
+ case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
+ content::RenderProcessHost* process =
+ content::Source<content::RenderProcessHost>(source).ptr();
+ RemoveWebViewRulesRegistries(process->GetID());
+ break;
+ }
default:
NOTREACHED();
break;
diff --git a/chrome/browser/extensions/api/declarative/rules_registry_service.h b/chrome/browser/extensions/api/declarative/rules_registry_service.h
index aa51607..461377d 100644
--- a/chrome/browser/extensions/api/declarative/rules_registry_service.h
+++ b/chrome/browser/extensions/api/declarative/rules_registry_service.h
@@ -38,6 +38,21 @@ namespace extensions {
class RulesRegistryService : public ProfileKeyedAPI,
public content::NotificationObserver {
public:
+ typedef RulesRegistry::WebViewKey WebViewKey;
+ struct RulesRegistryKey {
+ std::string event_name;
+ WebViewKey webview_key;
+ RulesRegistryKey(const std::string event_name,
+ const WebViewKey& webview_key)
+ : event_name(event_name),
+ webview_key(webview_key) {}
+ bool operator<(const RulesRegistryKey& other) const {
+ return (event_name < other.event_name) ||
+ ((event_name == other.event_name) &&
+ (webview_key < other.webview_key));
+ }
+ };
+
explicit RulesRegistryService(Profile* profile);
virtual ~RulesRegistryService();
@@ -52,28 +67,36 @@ class RulesRegistryService : public ProfileKeyedAPI,
static RulesRegistryService* Get(Profile* profile);
// Registers the default RulesRegistries used in Chromium.
- void RegisterDefaultRulesRegistries();
+ void EnsureDefaultRulesRegistriesRegistered(
+ const WebViewKey& webview_key);
// Registers a RulesRegistry and wraps it in an InitializingRulesRegistry.
void RegisterRulesRegistry(scoped_refptr<RulesRegistry> rule_registry);
- // Returns the RulesRegistry for |event_name| or NULL if no such registry
- // has been registered.
+ // Returns the RulesRegistry for |event_name| and |webview_key| or NULL if no
+ // such registry has been registered. Default rules registries (such as the
+ // WebRequest rules registry) will be created on first access.
scoped_refptr<RulesRegistry> GetRulesRegistry(
- const std::string& event_name) const;
+ const WebViewKey& webview_key,
+ const std::string& event_name);
// Accessors for each type of rules registry.
ContentRulesRegistry* content_rules_registry() const {
+ CHECK(content_rules_registry_);
return content_rules_registry_;
}
+ // Removes all rules registries of a given webview embedder process ID.
+ void RemoveWebViewRulesRegistries(int process_id);
+
// For testing.
void SimulateExtensionUnloaded(const std::string& extension_id);
private:
friend class ProfileKeyedAPIFactory<RulesRegistryService>;
- // Maps event names to RuleRegistries that handle these events.
- typedef std::map<std::string, scoped_refptr<RulesRegistry> >
+ // Maps <event name, webview key> to RuleRegistries that handle these
+ // events.
+ typedef std::map<RulesRegistryKey, scoped_refptr<RulesRegistry> >
RulesRegistryMap;
// Notifies all RulesRegistries that |extension_id| was unloaded.
diff --git a/chrome/browser/extensions/api/declarative/rules_registry_service_unittest.cc b/chrome/browser/extensions/api/declarative/rules_registry_service_unittest.cc
index 34f87ff..44617c9 100644
--- a/chrome/browser/extensions/api/declarative/rules_registry_service_unittest.cc
+++ b/chrome/browser/extensions/api/declarative/rules_registry_service_unittest.cc
@@ -7,6 +7,8 @@
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "chrome/browser/extensions/api/declarative/test_rules_registry.h"
+#include "chrome/browser/extensions/api/declarative_webrequest/webrequest_constants.h"
+#include "chrome/test/base/testing_profile.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -53,11 +55,12 @@ class RulesRegistryServiceTest : public testing::Test {
};
TEST_F(RulesRegistryServiceTest, TestConstructionAndMultiThreading) {
+ const RulesRegistry::WebViewKey key(0, 0);
TestRulesRegistry* ui_registry =
- new TestRulesRegistry(content::BrowserThread::UI, "ui");
+ new TestRulesRegistry(content::BrowserThread::UI, "ui", key);
TestRulesRegistry* io_registry =
- new TestRulesRegistry(content::BrowserThread::IO, "io");
+ new TestRulesRegistry(content::BrowserThread::IO, "io", key);
// Test registration.
@@ -65,29 +68,29 @@ TEST_F(RulesRegistryServiceTest, TestConstructionAndMultiThreading) {
registry_service.RegisterRulesRegistry(make_scoped_refptr(ui_registry));
registry_service.RegisterRulesRegistry(make_scoped_refptr(io_registry));
- EXPECT_TRUE(registry_service.GetRulesRegistry("ui").get());
- EXPECT_TRUE(registry_service.GetRulesRegistry("io").get());
- EXPECT_FALSE(registry_service.GetRulesRegistry("foo").get());
+ EXPECT_TRUE(registry_service.GetRulesRegistry(key, "ui").get());
+ EXPECT_TRUE(registry_service.GetRulesRegistry(key, "io").get());
+ EXPECT_FALSE(registry_service.GetRulesRegistry(key, "foo").get());
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
- base::Bind(&InsertRule, registry_service.GetRulesRegistry("ui"),
+ base::Bind(&InsertRule, registry_service.GetRulesRegistry(key, "ui"),
"ui_task"));
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
- base::Bind(&InsertRule, registry_service.GetRulesRegistry("io"),
+ base::Bind(&InsertRule, registry_service.GetRulesRegistry(key, "io"),
"io_task"));
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&VerifyNumberOfRules,
- registry_service.GetRulesRegistry("ui"), 1));
+ registry_service.GetRulesRegistry(key, "ui"), 1));
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::Bind(&VerifyNumberOfRules,
- registry_service.GetRulesRegistry("io"), 1));
+ registry_service.GetRulesRegistry(key, "io"), 1));
message_loop_.RunUntilIdle();
@@ -98,14 +101,64 @@ TEST_F(RulesRegistryServiceTest, TestConstructionAndMultiThreading) {
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&VerifyNumberOfRules,
- registry_service.GetRulesRegistry("ui"), 0));
+ registry_service.GetRulesRegistry(key, "ui"), 0));
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::Bind(&VerifyNumberOfRules,
- registry_service.GetRulesRegistry("io"), 0));
+ registry_service.GetRulesRegistry(key, "io"), 0));
message_loop_.RunUntilIdle();
}
+// This test verifies that removing rules registries by process ID works as
+// intended. This test ensures that removing registries associated with one
+// Webview embedder process does not remove registries associated with the
+// other.
+TEST_F(RulesRegistryServiceTest, TestWebViewKey) {
+ const int kEmbedderProcessID1 = 1;
+ const int kEmbedderProcessID2 = 2;
+ const int kWebViewInstanceID = 1;
+
+ const RulesRegistry::WebViewKey key1(kEmbedderProcessID1, kWebViewInstanceID);
+ const RulesRegistry::WebViewKey key2(kEmbedderProcessID2, kWebViewInstanceID);
+
+ TestRulesRegistry* ui_registry_key1 =
+ new TestRulesRegistry(content::BrowserThread::UI, "ui", key1);
+ TestRulesRegistry* ui_registry_key2 =
+ new TestRulesRegistry(content::BrowserThread::UI, "ui", key2);
+
+ RulesRegistryService registry_service(NULL);
+ registry_service.RegisterRulesRegistry(make_scoped_refptr(ui_registry_key1));
+ registry_service.RegisterRulesRegistry(make_scoped_refptr(ui_registry_key2));
+
+ content::BrowserThread::PostTask(
+ content::BrowserThread::UI, FROM_HERE,
+ base::Bind(&InsertRule, registry_service.GetRulesRegistry(key1, "ui"),
+ "ui_task"));
+ content::BrowserThread::PostTask(
+ content::BrowserThread::UI, FROM_HERE,
+ base::Bind(&InsertRule, registry_service.GetRulesRegistry(key2, "ui"),
+ "ui_task"));
+ message_loop_.RunUntilIdle();
+
+ registry_service.RemoveWebViewRulesRegistries(kEmbedderProcessID1);
+ EXPECT_FALSE(registry_service.GetRulesRegistry(key1, "ui").get());
+ EXPECT_TRUE(registry_service.GetRulesRegistry(key2, "ui").get());
+}
+
+TEST_F(RulesRegistryServiceTest, TestWebViewWebRequestRegistryHasNoCache) {
+ const int kEmbedderProcessID = 1;
+ const int kWebViewInstanceID = 1;
+ const RulesRegistry::WebViewKey key(kEmbedderProcessID, kWebViewInstanceID);
+ TestingProfile profile;
+ RulesRegistryService registry_service(&profile);
+ RulesRegistry* registry =
+ registry_service.GetRulesRegistry(
+ key,
+ declarative_webrequest_constants::kOnRequest).get();
+ EXPECT_TRUE(registry);
+ EXPECT_FALSE(registry->rules_cache_delegate_for_testing());
+}
+
} // namespace extensions
diff --git a/chrome/browser/extensions/api/declarative/rules_registry_with_cache_unittest.cc b/chrome/browser/extensions/api/declarative/rules_registry_with_cache_unittest.cc
index 141a0ac..96f7efe 100644
--- a/chrome/browser/extensions/api/declarative/rules_registry_with_cache_unittest.cc
+++ b/chrome/browser/extensions/api/declarative/rules_registry_with_cache_unittest.cc
@@ -46,7 +46,8 @@ class RulesRegistryWithCacheTest : public testing::Test {
: ui_thread_(content::BrowserThread::UI, &message_loop_),
file_thread_(content::BrowserThread::FILE, &message_loop_),
registry_(new TestRulesRegistry(content::BrowserThread::UI,
- "" /*event_name*/)) {}
+ "" /*event_name*/,
+ RulesRegistry::WebViewKey(0, 0))) {}
virtual ~RulesRegistryWithCacheTest() {}
@@ -229,12 +230,15 @@ TEST_F(RulesRegistryWithCacheTest, DeclarativeRulesStored) {
TestingValueStore* store = system->value_store();
const std::string event_name("testEvent");
+ const RulesRegistry::WebViewKey key(0, 0);
const std::string rules_stored_key(
RulesCacheDelegate::GetRulesStoredKey(
event_name, profile.IsOffTheRecord()));
scoped_ptr<RulesCacheDelegate> cache_delegate(new RulesCacheDelegate(false));
scoped_refptr<RulesRegistry> registry(new TestRulesRegistry(
- &profile, event_name, content::BrowserThread::UI, cache_delegate.get()));
+ &profile, event_name, content::BrowserThread::UI,
+ cache_delegate.get(),
+ RulesRegistry::WebViewKey(0, 0)));
// 1. Test the handling of preferences.
// Default value is always true.
@@ -301,6 +305,7 @@ TEST_F(RulesRegistryWithCacheTest, RulesStoredFlagMultipleRegistries) {
const std::string event_name1("testEvent1");
const std::string event_name2("testEvent2");
+ const RulesRegistry::WebViewKey key(0, 0);
const std::string rules_stored_key1(
RulesCacheDelegate::GetRulesStoredKey(
event_name1, profile.IsOffTheRecord()));
@@ -310,12 +315,14 @@ TEST_F(RulesRegistryWithCacheTest, RulesStoredFlagMultipleRegistries) {
scoped_ptr<RulesCacheDelegate> cache_delegate1(new RulesCacheDelegate(false));
scoped_refptr<RulesRegistry> registry1(new TestRulesRegistry(
&profile, event_name1, content::BrowserThread::UI,
- cache_delegate1.get()));
+ cache_delegate1.get(),
+ RulesRegistry::WebViewKey(0, 0)));
scoped_ptr<RulesCacheDelegate> cache_delegate2(new RulesCacheDelegate(false));
scoped_refptr<RulesRegistry> registry2(new TestRulesRegistry(
&profile, event_name2, content::BrowserThread::UI,
- cache_delegate2.get()));
+ cache_delegate2.get(),
+ RulesRegistry::WebViewKey(0, 0)));
// Checkt the correct default values.
EXPECT_TRUE(cache_delegate1->GetDeclarativeRulesStored(kExtensionId));
@@ -355,17 +362,24 @@ TEST_F(RulesRegistryWithCacheTest, RulesPreservedAcrossRestart) {
// 2. First run, adding a rule for the extension.
scoped_ptr<RulesCacheDelegate> cache_delegate(new RulesCacheDelegate(false));
scoped_refptr<TestRulesRegistry> registry(new TestRulesRegistry(
- &profile, "testEvent", content::BrowserThread::UI, cache_delegate.get()));
+ &profile,
+ "testEvent",
+ content::BrowserThread::UI,
+ cache_delegate.get(),
+ RulesRegistry::WebViewKey(0, 0)));
AddRule(kExtensionId, kRuleId, registry.get());
message_loop_.RunUntilIdle(); // Posted tasks store the added rule.
EXPECT_EQ(1, GetNumberOfRules(kExtensionId, registry.get()));
// 3. Restart the TestRulesRegistry and see the rule still there.
- cache_delegate.reset(
- new RulesCacheDelegate(false));
+ cache_delegate.reset(new RulesCacheDelegate(false));
registry = new TestRulesRegistry(
- &profile, "testEvent", content::BrowserThread::UI, cache_delegate.get());
+ &profile,
+ "testEvent",
+ content::BrowserThread::UI,
+ cache_delegate.get(),
+ RulesRegistry::WebViewKey(0, 0));
message_loop_.RunUntilIdle(); // Posted tasks retrieve the stored rule.
EXPECT_EQ(1, GetNumberOfRules(kExtensionId, registry.get()));
diff --git a/chrome/browser/extensions/api/declarative/test_rules_registry.cc b/chrome/browser/extensions/api/declarative/test_rules_registry.cc
index d22ce15..b1d9a7a 100644
--- a/chrome/browser/extensions/api/declarative/test_rules_registry.cc
+++ b/chrome/browser/extensions/api/declarative/test_rules_registry.cc
@@ -9,21 +9,25 @@
namespace extensions {
TestRulesRegistry::TestRulesRegistry(content::BrowserThread::ID owner_thread,
- const std::string& event_name)
+ const std::string& event_name,
+ const WebViewKey& webview_key)
: RulesRegistry(NULL /*profile*/,
event_name,
owner_thread,
- NULL) {}
+ NULL,
+ webview_key) {}
TestRulesRegistry::TestRulesRegistry(
Profile* profile,
const std::string& event_name,
content::BrowserThread::ID owner_thread,
- RulesCacheDelegate* cache_delegate)
+ RulesCacheDelegate* cache_delegate,
+ const WebViewKey& webview_key)
: RulesRegistry(profile,
event_name,
owner_thread,
- cache_delegate) {}
+ cache_delegate,
+ webview_key) {}
std::string TestRulesRegistry::AddRulesImpl(
const std::string& extension_id,
diff --git a/chrome/browser/extensions/api/declarative/test_rules_registry.h b/chrome/browser/extensions/api/declarative/test_rules_registry.h
index eaffc17..6d8536b 100644
--- a/chrome/browser/extensions/api/declarative/test_rules_registry.h
+++ b/chrome/browser/extensions/api/declarative/test_rules_registry.h
@@ -13,12 +13,14 @@ namespace extensions {
class TestRulesRegistry : public RulesRegistry {
public:
TestRulesRegistry(content::BrowserThread::ID owner_thread,
- const std::string& event_name);
+ const std::string& event_name,
+ const WebViewKey& webview_key);
TestRulesRegistry(
Profile* profile,
const std::string& event_name,
content::BrowserThread::ID owner_thread,
- RulesCacheDelegate* cache_delegate);
+ RulesCacheDelegate* cache_delegate,
+ const WebViewKey& webview_key);
// RulesRegistry implementation:
virtual std::string AddRulesImpl(