OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_RULES_REGISTRY
_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_RULES_REGISTRY
_H_ | |
7 | |
8 #include <map> | |
9 #include <set> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
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_content/content_action.h" | |
18 #include "chrome/browser/extensions/api/declarative_content/content_condition.h" | |
19 #include "components/url_matcher/url_matcher.h" | |
20 #include "content/public/browser/notification_observer.h" | |
21 #include "content/public/browser/notification_registrar.h" | |
22 #include "extensions/browser/api/declarative/declarative_rule.h" | |
23 #include "extensions/browser/api/declarative/rules_registry.h" | |
24 #include "extensions/browser/info_map.h" | |
25 | |
26 class ContentPermissions; | |
27 | |
28 namespace content { | |
29 class BrowserContext; | |
30 class RenderProcessHost; | |
31 class WebContents; | |
32 struct FrameNavigateParams; | |
33 struct LoadCommittedDetails; | |
34 } | |
35 | |
36 namespace extension_web_request_api_helpers { | |
37 struct EventResponseDelta; | |
38 } | |
39 | |
40 namespace net { | |
41 class URLRequest; | |
42 } | |
43 | |
44 namespace extensions { | |
45 | |
46 class RulesRegistryService; | |
47 | |
48 typedef DeclarativeRule<ContentCondition, ContentAction> ContentRule; | |
49 | |
50 // The ContentRulesRegistry is responsible for managing | |
51 // the internal representation of rules for the Declarative Content API. | |
52 // | |
53 // Here is the high level overview of this functionality: | |
54 // | |
55 // RulesRegistry::Rule consists of Conditions and Actions, these are | |
56 // represented as a ContentRule with ContentConditions and | |
57 // ContentRuleActions. | |
58 // | |
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(content::BrowserContext* browser_context, | |
68 RulesCacheDelegate* cache_delegate); | |
69 | |
70 // Applies all content rules given an update (CSS match change or | |
71 // page navigation, for now) from the renderer. | |
72 void Apply(content::WebContents* contents, | |
73 const std::vector<std::string>& matching_css_selectors); | |
74 | |
75 // Applies all content rules given that a tab was just navigated. | |
76 void DidNavigateMainFrame(content::WebContents* tab, | |
77 const content::LoadCommittedDetails& details, | |
78 const content::FrameNavigateParams& params); | |
79 | |
80 // Implementation of RulesRegistry: | |
81 virtual std::string AddRulesImpl( | |
82 const std::string& extension_id, | |
83 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) OVERRIDE; | |
84 virtual std::string RemoveRulesImpl( | |
85 const std::string& extension_id, | |
86 const std::vector<std::string>& rule_identifiers) OVERRIDE; | |
87 virtual std::string RemoveAllRulesImpl( | |
88 const std::string& extension_id) OVERRIDE; | |
89 | |
90 // content::NotificationObserver implementation. | |
91 virtual void Observe(int type, | |
92 const content::NotificationSource& source, | |
93 const content::NotificationDetails& details) OVERRIDE; | |
94 | |
95 // Returns true if this object retains no allocated data. Only for debugging. | |
96 bool IsEmpty() const; | |
97 | |
98 protected: | |
99 virtual ~ContentRulesRegistry(); | |
100 | |
101 // Virtual for testing: | |
102 virtual base::Time GetExtensionInstallationTime( | |
103 const std::string& extension_id) const; | |
104 | |
105 private: | |
106 friend class DeclarativeContentRulesRegistryTest; | |
107 | |
108 std::set<ContentRule*> | |
109 GetMatches(const RendererContentMatchData& renderer_data) const; | |
110 | |
111 // Scans the rules for the set of conditions they're watching. If the set has | |
112 // changed, calls InstructRenderProcess() for each RenderProcessHost in the | |
113 // current browser_context. | |
114 void UpdateConditionCache(); | |
115 | |
116 // Tells a renderer what page attributes to watch for using an | |
117 // ExtensionMsg_WatchPages. | |
118 void InstructRenderProcess(content::RenderProcessHost* process); | |
119 | |
120 typedef std::map<url_matcher::URLMatcherConditionSet::ID, ContentRule*> | |
121 URLMatcherIdToRule; | |
122 typedef std::map<ContentRule::GlobalRuleId, linked_ptr<ContentRule> > | |
123 RulesMap; | |
124 | |
125 // Map that tells us which ContentRules may match under the condition that | |
126 // the URLMatcherConditionSet::ID was returned by the |url_matcher_|. | |
127 URLMatcherIdToRule match_id_to_rule_; | |
128 | |
129 RulesMap content_rules_; | |
130 | |
131 // Maps tab_id to the set of rules that match on that tab. This | |
132 // lets us call Revert as appropriate. | |
133 std::map<int, std::set<ContentRule*> > active_rules_; | |
134 | |
135 // Matches URLs for the page_url condition. | |
136 url_matcher::URLMatcher url_matcher_; | |
137 | |
138 // All CSS selectors any rule's conditions watch for. | |
139 std::vector<std::string> watched_css_selectors_; | |
140 | |
141 // Manages our notification registrations. | |
142 content::NotificationRegistrar registrar_; | |
143 | |
144 scoped_refptr<InfoMap> extension_info_map_; | |
145 | |
146 DISALLOW_COPY_AND_ASSIGN(ContentRulesRegistry); | |
147 }; | |
148 | |
149 } // namespace extensions | |
150 | |
151 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_RULES_REGIS
TRY_H_ | |
OLD | NEW |