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

Unified Diff: chrome/browser/extensions/api/declarative_content/content_rules_registry.h

Issue 11547033: Implement declarativeContent API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Sync past refactoring and fix build+tests Created 7 years, 11 months 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/declarative_content/content_rules_registry.h
diff --git a/chrome/browser/extensions/api/declarative_content/content_rules_registry.h b/chrome/browser/extensions/api/declarative_content/content_rules_registry.h
new file mode 100644
index 0000000000000000000000000000000000000000..5ff874b5ffcb57035457ede7455b7711ec66043a
--- /dev/null
+++ b/chrome/browser/extensions/api/declarative_content/content_rules_registry.h
@@ -0,0 +1,165 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_RULES_REGISTRY_H_
+#define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_RULES_REGISTRY_H_
+
+#include <map>
+#include <set>
+#include <vector>
+
+#include "base/memory/linked_ptr.h"
+#include "base/memory/ref_counted.h"
+#include "base/time.h"
+#include "chrome/browser/extensions/api/declarative/declarative_rule.h"
+#include "chrome/browser/extensions/api/declarative/rules_registry_with_cache.h"
+#include "chrome/browser/extensions/api/declarative_content/content_action.h"
+#include "chrome/browser/extensions/api/declarative_content/content_condition.h"
+#include "chrome/browser/extensions/extension_info_map.h"
+#include "chrome/common/extensions/matcher/url_matcher.h"
+#include "content/public/browser/notification_observer.h"
+#include "content/public/browser/notification_registrar.h"
+
+class Profile;
+class ContentPermissions;
+
+namespace content {
+class RenderProcessHost;
+class WebContents;
+struct FrameNavigateParams;
+struct LoadCommittedDetails;
+}
+
+namespace extension_web_request_api_helpers {
+struct EventResponseDelta;
+}
+
+namespace net {
+class URLRequest;
+}
+
+namespace extensions {
+
+class RulesRegistryService;
+
+typedef DeclarativeRule<ContentCondition, ContentAction> ContentRule;
+
+// The ContentRulesRegistry is responsible for managing
+// the internal representation of rules for the Declarative Content API.
+//
+// Here is the high level overview of this functionality:
+//
+// RulesRegistry::Rule consists of Conditions and Actions, these are
+// represented as a ContentRule with ContentConditions and
+// ContentRuleActions.
+//
+// ContentConditions represent JSON dictionaries as the following:
+// {
+// 'instanceType': 'URLMatcher',
+// 'host_suffix': 'example.com',
+// 'path_prefix': '/query',
+// 'scheme': 'http'
+// }
+//
+// The evaluation of URL related condition attributes (host_suffix, path_prefix)
+// is delegated to a URLMatcher, because this is capable of evaluating many
+// of such URL related condition attributes in parallel.
+//
+// For this, the URLRequestCondition has a URLMatcherConditionSet, which
+// represents the {'host_suffix': 'example.com', 'path_prefix': '/query'} part.
+// We will then ask the URLMatcher, whether a given URL
+// "http://www.example.com/query/" has any matches, and the URLMatcher
+// will respond with the URLMatcherConditionSet::ID. We can map this
+// to the ContentRule and check whether also the other conditions (in this
+// example 'scheme': 'http') are fulfilled.
+class ContentRulesRegistry : public RulesRegistryWithCache,
+ public content::NotificationObserver {
+ public:
+ ContentRulesRegistry(Profile* profile, Delegate* delegate);
+
+ // Tells a renderer what page attributes to watch for using an
+ // ExtensionMsg_WatchPages.
+ void InstructRenderProcess(content::RenderProcessHost* process);
+
+ // Applies all content rules given an update from the renderer.
+ void Apply(content::WebContents* contents,
+ const std::vector<std::string>& matching_css_selectors);
+
+ // Applies all content rules given that a tab was just navigated.
+ void DidNavigateMainFrame(content::WebContents* tab,
+ const content::LoadCommittedDetails& details,
+ const content::FrameNavigateParams& params);
+
+ // Keep track of what rules are currently active, for each tab_id.
+ // When a message comes in for a tab, find the rules that match
+ // it. Diff the sets. Call Action.Apply for the new actions;
+ // Action.Revert for the old ones. Update the set. When a tab is
+ // closed, clear the tab_id. Should we call Action.Revert in that
+ // case? For PageActions, doesn't matter.
+
+ // Implementation of RulesRegistryWithCache:
+ virtual std::string AddRulesImpl(
+ const std::string& extension_id,
+ const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) OVERRIDE;
+ virtual std::string RemoveRulesImpl(
+ const std::string& extension_id,
+ const std::vector<std::string>& rule_identifiers) OVERRIDE;
+ virtual std::string RemoveAllRulesImpl(
+ const std::string& extension_id) OVERRIDE;
+ virtual content::BrowserThread::ID GetOwnerThread() const OVERRIDE;
+
+ // content::NotificationObserver implementation.
+ virtual void Observe(int type,
+ const content::NotificationSource& source,
+ const content::NotificationDetails& details) OVERRIDE;
+
+ // Returns true if this object retains no allocated data. Only for debugging.
+ bool IsEmpty() const;
+
+ protected:
+ virtual ~ContentRulesRegistry();
+
+ // Virtual for testing:
+ virtual base::Time GetExtensionInstallationTime(
+ const std::string& extension_id) const;
+
+ private:
+ std::set<ContentRule*>
+ GetMatches(const RendererContentMatchData& renderer_data);
+
+ // Scans the rules for the set of conditions they're watching. If the set has
+ // changed, calls InstructRenderProcess() for each RenderProcessHost in the
+ // current profile.
+ void UpdateConditionCache();
+
+ typedef std::map<URLMatcherConditionSet::ID, ContentRule*> URLMatcherIdToRule;
+ typedef std::map<ContentRule::GlobalRuleId, linked_ptr<ContentRule> >
+ RulesMap;
+
+ Profile* const profile_;
+
+ // Map that tells us which ContentRules may match under the condition that
+ // the URLMatcherConditionSet::ID was returned by the |url_matcher_|.
+ URLMatcherIdToRule match_id_to_rule_;
+
+ RulesMap content_rules_;
+
+ // Maps tab_id to the set of rules that match on that tab.
+ std::map<int, std::set<ContentRule*> > active_rules_;
+
+ // Matches URLs for the page_url condition.
+ URLMatcher url_matcher_;
+
+ // All CSS selectors any rule's conditions watch for.
+ std::vector<std::string> watched_css_selectors_;
+
+ // Manages our notification registrations.
+ content::NotificationRegistrar registrar_;
+
+ scoped_refptr<ExtensionInfoMap> extension_info_map_;
+};
+
+} // namespace extensions
+
+#endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_RULES_REGISTRY_H_

Powered by Google App Engine
This is Rietveld 408576698