1 // Copyright 2015 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_DECLARATIVE_CONTENT_CSS_CONDITION_TRACKER_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_DECLARATIVE_CONTENT_CSS_CONDITION_TRACKER_H_
13 #include "base/callback.h"
14 #include "base/containers/hash_tables.h"
15 #include "base/memory/linked_ptr.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "content/public/browser/web_contents_observer.h"
27 struct FrameNavigateParams
;
28 struct LoadCommittedDetails
;
29 class RenderProcessHost
;
33 namespace extensions
{
37 // Tests whether all the specified CSS selectors match on the page.
38 class DeclarativeContentCssPredicate
{
40 ~DeclarativeContentCssPredicate();
42 const std::vector
<std::string
>& css_selectors() const {
43 return css_selectors_
;
46 static scoped_ptr
<DeclarativeContentCssPredicate
> Create(
47 const base::Value
& value
,
51 explicit DeclarativeContentCssPredicate(
52 const std::vector
<std::string
>& css_selectors
);
53 std::vector
<std::string
> css_selectors_
;
55 DISALLOW_COPY_AND_ASSIGN(DeclarativeContentCssPredicate
);
58 class DeclarativeContentConditionTrackerDelegate
;
60 // Supports watching of CSS selectors to across tab contents in a browser
61 // context, and querying for the matching CSS selectors for a context.
62 class DeclarativeContentCssConditionTracker
63 : public content::NotificationObserver
{
65 DeclarativeContentCssConditionTracker(
66 content::BrowserContext
* context
,
67 DeclarativeContentConditionTrackerDelegate
* delegate
);
68 ~DeclarativeContentCssConditionTracker() override
;
70 // Creates a new DeclarativeContentCssPredicate from |value|. Sets
71 // *|error| and returns null if creation failed for any reason.
72 scoped_ptr
<DeclarativeContentCssPredicate
> CreatePredicate(
73 const Extension
* extension
,
74 const base::Value
& value
,
77 // Sets the set of CSS selectors to watch for CSS condition evaluation.
78 void SetWatchedCssSelectors(
79 const std::set
<std::string
>& watched_css_selectors
);
81 // Requests that CSS conditions be tracked for |contents|.
82 void TrackForWebContents(content::WebContents
* contents
);
84 // Handles navigation of |contents|. We depend on the caller to notify us of
85 // this event rather than listening for it ourselves, so that the caller can
86 // coordinate evaluation with all the trackers that respond to it. If we
87 // listened ourselves and requested rule evaluation before another tracker
88 // received the notification, our conditions would be evaluated based on the
89 // new URL while the other tracker's conditions would still be evaluated based
90 // on the previous URL.
91 void OnWebContentsNavigation(content::WebContents
* contents
,
92 const content::LoadCommittedDetails
& details
,
93 const content::FrameNavigateParams
& params
);
95 // Returns the result of evaluating |predicate| on the per-tab state
96 // associated with |contents|.
97 bool EvaluatePredicate(const DeclarativeContentCssPredicate
* predicate
,
98 content::WebContents
* contents
) const;
100 // TODO(wittman): Remove once DeclarativeChromeContentRulesRegistry no longer
101 // depends on concrete condition implementations. At that point
102 // DeclarativeChromeContentRulesRegistryTest.ActiveRulesDoesntGrow will be
103 // able to use a test condition object and not need to depend on force setting
104 // matching CSS seleectors.
105 void UpdateMatchingCssSelectorsForTesting(
106 content::WebContents
* contents
,
107 const std::vector
<std::string
>& matching_css_selectors
);
110 // Monitors CSS selector matching state on one WebContents.
111 class PerWebContentsTracker
: public content::WebContentsObserver
{
113 using RequestEvaluationCallback
=
114 base::Callback
<void(content::WebContents
*)>;
115 using WebContentsDestroyedCallback
=
116 base::Callback
<void(content::WebContents
*)>;
118 PerWebContentsTracker(
119 content::WebContents
* contents
,
120 const RequestEvaluationCallback
& request_evaluation
,
121 const WebContentsDestroyedCallback
& web_contents_destroyed
);
122 ~PerWebContentsTracker() override
;
124 void OnWebContentsNavigation(const content::LoadCommittedDetails
& details
,
125 const content::FrameNavigateParams
& params
);
127 // See comment on similar function above.
128 void UpdateMatchingCssSelectorsForTesting(
129 const std::vector
<std::string
>& matching_css_selectors
);
131 const base::hash_set
<std::string
>& matching_css_selectors() const {
132 return matching_css_selectors_
;
136 // content::WebContentsObserver overrides.
137 bool OnMessageReceived(const IPC::Message
& message
) override
;
138 void WebContentsDestroyed() override
;
140 void OnWatchedPageChange(const std::vector
<std::string
>& css_selectors
);
142 const RequestEvaluationCallback request_evaluation_
;
143 const WebContentsDestroyedCallback web_contents_destroyed_
;
145 // We use a hash_set for maximally efficient lookup.
146 base::hash_set
<std::string
> matching_css_selectors_
;
148 DISALLOW_COPY_AND_ASSIGN(PerWebContentsTracker
);
151 // content::NotificationObserver implementation.
152 void Observe(int type
,
153 const content::NotificationSource
& source
,
154 const content::NotificationDetails
& details
) override
;
156 // If the renderer process is associated with our browser context, tells it
157 // what page attributes to watch for using an ExtensionMsg_WatchPages.
158 void InstructRenderProcessIfManagingBrowserContext(
159 content::RenderProcessHost
* process
);
161 // Called by PerWebContentsTracker on web contents destruction.
162 void DeletePerWebContentsTracker(content::WebContents
* contents
);
164 // The context whose state we're monitoring for evaluation.
165 content::BrowserContext
* context_
;
167 // All CSS selectors that are watched for by any rule's conditions. This
168 // vector is sorted by construction.
169 std::vector
<std::string
> watched_css_selectors_
;
171 // Maps WebContents to the tracker for that WebContents state.
172 std::map
<content::WebContents
*, linked_ptr
<PerWebContentsTracker
>>
173 per_web_contents_tracker_
;
176 DeclarativeContentConditionTrackerDelegate
* delegate_
;
178 // Manages our notification registrations.
179 content::NotificationRegistrar registrar_
;
181 DISALLOW_COPY_AND_ASSIGN(DeclarativeContentCssConditionTracker
);
184 } // namespace extensions
186 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_DECLARATIVE_CONTENT_CSS_CONDITION_TRACKER_H_