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_CHROME_CONTENT_RULES_REGISTRY_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CHROME_CONTENT_RULES_REGISTRY_H_
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_content/content_rules_registry.h"
24 #include "extensions/browser/info_map.h"
26 class ContentPermissions
;
30 class RenderProcessHost
;
32 struct FrameNavigateParams
;
33 struct LoadCommittedDetails
;
36 namespace extension_web_request_api_helpers
{
37 struct EventResponseDelta
;
44 namespace extensions
{
46 class RulesRegistryService
;
48 typedef DeclarativeRule
<ContentCondition
, ContentAction
> ContentRule
;
50 // The ChromeContentRulesRegistry 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 ChromeContentRulesRegistry
: public ContentRulesRegistry
,
63 public content::NotificationObserver
{
65 // For testing, |ui_part| can be NULL. In that case it constructs the
66 // registry with storage functionality suspended.
67 ChromeContentRulesRegistry(content::BrowserContext
* browser_context
,
68 RulesCacheDelegate
* cache_delegate
);
70 // ChromeContentRulesRegistry implementation:
71 // Applies all content rules given an update (CSS match change or
72 // page navigation, for now) from the renderer.
73 void Apply(content::WebContents
* contents
,
74 const std::vector
<std::string
>& matching_css_selectors
) override
;
76 // Applies all content rules given that a tab was just navigated.
77 void DidNavigateMainFrame(
78 content::WebContents
* tab
,
79 const content::LoadCommittedDetails
& details
,
80 const content::FrameNavigateParams
& params
) override
;
82 // Implementation of RulesRegistry:
83 std::string
AddRulesImpl(
84 const std::string
& extension_id
,
85 const std::vector
<linked_ptr
<RulesRegistry::Rule
>>& rules
) override
;
86 std::string
RemoveRulesImpl(
87 const std::string
& extension_id
,
88 const std::vector
<std::string
>& rule_identifiers
) override
;
89 std::string
RemoveAllRulesImpl(const std::string
& extension_id
) override
;
91 // content::NotificationObserver implementation.
92 void Observe(int type
,
93 const content::NotificationSource
& source
,
94 const content::NotificationDetails
& details
) override
;
96 // Returns true if this object retains no allocated data. Only for debugging.
100 ~ChromeContentRulesRegistry() override
;
102 // Virtual for testing:
103 virtual base::Time
GetExtensionInstallationTime(
104 const std::string
& extension_id
) const;
107 friend class DeclarativeChromeContentRulesRegistryTest
;
109 std::set
<ContentRule
*> GetMatches(
110 const RendererContentMatchData
& renderer_data
) const;
112 // Scans the rules for the set of conditions they're watching. If the set has
113 // changed, calls InstructRenderProcess() for each RenderProcessHost in the
114 // current browser_context.
115 void UpdateConditionCache();
117 // Tells a renderer what page attributes to watch for using an
118 // ExtensionMsg_WatchPages.
119 void InstructRenderProcess(content::RenderProcessHost
* process
);
121 typedef std::map
<url_matcher::URLMatcherConditionSet::ID
, ContentRule
*>
123 typedef std::map
<ContentRule::GlobalRuleId
, linked_ptr
<ContentRule
> >
126 // Map that tells us which ContentRules may match under the condition that
127 // the URLMatcherConditionSet::ID was returned by the |url_matcher_|.
128 URLMatcherIdToRule match_id_to_rule_
;
130 RulesMap content_rules_
;
132 // Maps tab_id to the set of rules that match on that tab. This
133 // lets us call Revert as appropriate.
134 std::map
<int, std::set
<ContentRule
*> > active_rules_
;
136 // Matches URLs for the page_url condition.
137 url_matcher::URLMatcher url_matcher_
;
139 // All CSS selectors any rule's conditions watch for.
140 std::vector
<std::string
> watched_css_selectors_
;
142 // Manages our notification registrations.
143 content::NotificationRegistrar registrar_
;
145 scoped_refptr
<InfoMap
> extension_info_map_
;
147 DISALLOW_COPY_AND_ASSIGN(ChromeContentRulesRegistry
);
150 } // namespace extensions
152 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CHROME_CONTENT_RULES_REGISTRY_H_