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 "chrome/browser/extensions/api/declarative_content/content_predicate_evaluator.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
20 #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
: public ContentPredicate
{
40 ~DeclarativeContentCssPredicate() override
;
42 const std::vector
<std::string
>& css_selectors() const {
43 return css_selectors_
;
46 static scoped_ptr
<DeclarativeContentCssPredicate
> Create(
47 ContentPredicateEvaluator
* evaluator
,
48 const base::Value
& value
,
52 ContentPredicateEvaluator
* GetEvaluator() const override
;
55 DeclarativeContentCssPredicate(ContentPredicateEvaluator
* evaluator
,
56 const std::vector
<std::string
>& css_selectors
);
59 ContentPredicateEvaluator
* const evaluator_
;
60 std::vector
<std::string
> css_selectors_
;
62 DISALLOW_COPY_AND_ASSIGN(DeclarativeContentCssPredicate
);
65 // Supports watching of CSS selectors to across tab contents in a browser
66 // context, and querying for the matching CSS selectors for a context.
67 class DeclarativeContentCssConditionTracker
68 : public ContentPredicateEvaluator
,
69 public content::NotificationObserver
{
71 explicit DeclarativeContentCssConditionTracker(Delegate
* delegate
);
72 ~DeclarativeContentCssConditionTracker() override
;
74 // ContentPredicateEvaluator:
75 std::string
GetPredicateApiAttributeName() const override
;
76 scoped_ptr
<const ContentPredicate
> CreatePredicate(
77 const Extension
* extension
,
78 const base::Value
& value
,
79 std::string
* error
) override
;
81 const std::map
<const void*, std::vector
<const ContentPredicate
*>>&
83 void StopTrackingPredicates(
84 const std::vector
<const void*>& predicate_groups
) override
;
85 void TrackForWebContents(content::WebContents
* contents
) override
;
86 void OnWebContentsNavigation(
87 content::WebContents
* contents
,
88 const content::LoadCommittedDetails
& details
,
89 const content::FrameNavigateParams
& params
) override
;
90 bool EvaluatePredicate(const ContentPredicate
* predicate
,
91 content::WebContents
* tab
) const override
;
94 // Monitors CSS selector matching state on one WebContents.
95 class PerWebContentsTracker
: public content::WebContentsObserver
{
97 using RequestEvaluationCallback
=
98 base::Callback
<void(content::WebContents
*)>;
99 using WebContentsDestroyedCallback
=
100 base::Callback
<void(content::WebContents
*)>;
102 PerWebContentsTracker(
103 content::WebContents
* contents
,
104 const RequestEvaluationCallback
& request_evaluation
,
105 const WebContentsDestroyedCallback
& web_contents_destroyed
);
106 ~PerWebContentsTracker() override
;
108 void OnWebContentsNavigation(const content::LoadCommittedDetails
& details
,
109 const content::FrameNavigateParams
& params
);
111 const base::hash_set
<std::string
>& matching_css_selectors() const {
112 return matching_css_selectors_
;
116 // content::WebContentsObserver overrides.
117 bool OnMessageReceived(const IPC::Message
& message
) override
;
118 void WebContentsDestroyed() override
;
120 void OnWatchedPageChange(const std::vector
<std::string
>& css_selectors
);
122 const RequestEvaluationCallback request_evaluation_
;
123 const WebContentsDestroyedCallback web_contents_destroyed_
;
125 // We use a hash_set for maximally efficient lookup.
126 base::hash_set
<std::string
> matching_css_selectors_
;
128 DISALLOW_COPY_AND_ASSIGN(PerWebContentsTracker
);
131 // content::NotificationObserver implementation.
132 void Observe(int type
,
133 const content::NotificationSource
& source
,
134 const content::NotificationDetails
& details
) override
;
136 // Informs renderer processes of a new set of watched CSS selectors.
137 void UpdateRenderersWatchedCssSelectors(
138 const std::vector
<std::string
>& watched_css_selectors
);
140 // Returns the current list of watched CSS selectors.
141 std::vector
<std::string
> GetWatchedCssSelectors() const;
143 // If the renderer process is associated with our browser context, tells it
144 // what page attributes to watch for using an ExtensionMsg_WatchPages.
145 void InstructRenderProcessIfManagingBrowserContext(
146 content::RenderProcessHost
* process
,
147 std::vector
<std::string
> watched_css_selectors
);
149 // Called by PerWebContentsTracker on web contents destruction.
150 void DeletePerWebContentsTracker(content::WebContents
* contents
);
155 // Maps the CSS selectors to watch to the number of predicates specifying
157 std::map
<std::string
, int> watched_css_selector_predicate_count_
;
159 // Grouped predicates tracked by this object.
160 std::map
<const void*, std::vector
<const DeclarativeContentCssPredicate
*>>
163 // Maps WebContents to the tracker for that WebContents state.
164 std::map
<content::WebContents
*, linked_ptr
<PerWebContentsTracker
>>
165 per_web_contents_tracker_
;
167 // Manages our notification registrations.
168 content::NotificationRegistrar registrar_
;
170 DISALLOW_COPY_AND_ASSIGN(DeclarativeContentCssConditionTracker
);
173 } // namespace extensions
175 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_DECLARATIVE_CONTENT_CSS_CONDITION_TRACKER_H_