Revert 285173 "Removed InProcessBrowserTest::CleanUpOnMainThread()"
[chromium-blink-merge.git] / chrome / browser / extensions / api / declarative_content / content_rules_registry.h
blob9224374a7ee358dcc6e100be0aac06b2723ea685
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_RULES_REGISTRY_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_RULES_REGISTRY_H_
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
13 #include "base/memory/linked_ptr.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/time/time.h"
17 #include "chrome/browser/extensions/api/declarative/declarative_rule.h"
18 #include "chrome/browser/extensions/api/declarative/rules_registry.h"
19 #include "chrome/browser/extensions/api/declarative_content/content_action.h"
20 #include "chrome/browser/extensions/api/declarative_content/content_condition.h"
21 #include "components/url_matcher/url_matcher.h"
22 #include "content/public/browser/notification_observer.h"
23 #include "content/public/browser/notification_registrar.h"
24 #include "extensions/browser/info_map.h"
26 class Profile;
27 class ContentPermissions;
29 namespace content {
30 class RenderProcessHost;
31 class WebContents;
32 struct FrameNavigateParams;
33 struct LoadCommittedDetails;
36 namespace extension_web_request_api_helpers {
37 struct EventResponseDelta;
40 namespace net {
41 class URLRequest;
44 namespace extensions {
46 class RulesRegistryService;
48 typedef DeclarativeRule<ContentCondition, ContentAction> ContentRule;
50 // The ContentRulesRegistry is responsible for managing
51 // the internal representation of rules for the Declarative Content API.
53 // Here is the high level overview of this functionality:
55 // RulesRegistry::Rule consists of Conditions and Actions, these are
56 // represented as a ContentRule with ContentConditions and
57 // ContentRuleActions.
59 // The evaluation of URL related condition attributes (host_suffix, path_prefix)
60 // is delegated to a URLMatcher, because this is capable of evaluating many
61 // of such URL related condition attributes in parallel.
62 class ContentRulesRegistry : public RulesRegistry,
63 public content::NotificationObserver {
64 public:
65 // For testing, |ui_part| can be NULL. In that case it constructs the
66 // registry with storage functionality suspended.
67 ContentRulesRegistry(Profile* profile, RulesCacheDelegate* cache_delegate);
69 // Applies all content rules given an update (CSS match change or
70 // page navigation, for now) from the renderer.
71 void Apply(content::WebContents* contents,
72 const std::vector<std::string>& matching_css_selectors);
74 // Applies all content rules given that a tab was just navigated.
75 void DidNavigateMainFrame(content::WebContents* tab,
76 const content::LoadCommittedDetails& details,
77 const content::FrameNavigateParams& params);
79 // Implementation of RulesRegistry:
80 virtual std::string AddRulesImpl(
81 const std::string& extension_id,
82 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) OVERRIDE;
83 virtual std::string RemoveRulesImpl(
84 const std::string& extension_id,
85 const std::vector<std::string>& rule_identifiers) OVERRIDE;
86 virtual std::string RemoveAllRulesImpl(
87 const std::string& extension_id) OVERRIDE;
89 // content::NotificationObserver implementation.
90 virtual void Observe(int type,
91 const content::NotificationSource& source,
92 const content::NotificationDetails& details) OVERRIDE;
94 // Returns true if this object retains no allocated data. Only for debugging.
95 bool IsEmpty() const;
97 protected:
98 virtual ~ContentRulesRegistry();
100 // Virtual for testing:
101 virtual base::Time GetExtensionInstallationTime(
102 const std::string& extension_id) const;
104 private:
105 friend class DeclarativeContentRulesRegistryTest;
107 std::set<ContentRule*>
108 GetMatches(const RendererContentMatchData& renderer_data) const;
110 // Scans the rules for the set of conditions they're watching. If the set has
111 // changed, calls InstructRenderProcess() for each RenderProcessHost in the
112 // current profile.
113 void UpdateConditionCache();
115 // Tells a renderer what page attributes to watch for using an
116 // ExtensionMsg_WatchPages.
117 void InstructRenderProcess(content::RenderProcessHost* process);
119 typedef std::map<url_matcher::URLMatcherConditionSet::ID, ContentRule*>
120 URLMatcherIdToRule;
121 typedef std::map<ContentRule::GlobalRuleId, linked_ptr<ContentRule> >
122 RulesMap;
124 // Map that tells us which ContentRules may match under the condition that
125 // the URLMatcherConditionSet::ID was returned by the |url_matcher_|.
126 URLMatcherIdToRule match_id_to_rule_;
128 RulesMap content_rules_;
130 // Maps tab_id to the set of rules that match on that tab. This
131 // lets us call Revert as appropriate.
132 std::map<int, std::set<ContentRule*> > active_rules_;
134 // Matches URLs for the page_url condition.
135 url_matcher::URLMatcher url_matcher_;
137 // All CSS selectors any rule's conditions watch for.
138 std::vector<std::string> watched_css_selectors_;
140 // Manages our notification registrations.
141 content::NotificationRegistrar registrar_;
143 scoped_refptr<InfoMap> extension_info_map_;
145 DISALLOW_COPY_AND_ASSIGN(ContentRulesRegistry);
148 } // namespace extensions
150 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_RULES_REGISTRY_H_