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 "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "content/public/browser/web_contents_observer.h"
23 struct FrameNavigateParams
;
24 struct LoadCommittedDetails
;
25 class RenderProcessHost
;
29 namespace extensions
{
31 class DeclarativeContentConditionTrackerDelegate
;
33 // Supports watching of CSS selectors to across tab contents in a browser
34 // context, and querying for the matching CSS selectors for a context.
35 class DeclarativeContentCssConditionTracker
36 : public content::NotificationObserver
{
38 DeclarativeContentCssConditionTracker(
39 content::BrowserContext
* context
,
40 DeclarativeContentConditionTrackerDelegate
* delegate
);
41 ~DeclarativeContentCssConditionTracker() override
;
43 // Sets the set of CSS selectors to watch for CSS condition evaluation.
44 void SetWatchedCssSelectors(
45 const std::set
<std::string
>& watched_css_selectors
);
47 // Requests that CSS conditions be tracked for |contents|.
48 void TrackForWebContents(content::WebContents
* contents
);
50 // Handles navigation of |contents|. We depend on the caller to notify us of
51 // this event rather than listening for it ourselves, so that the caller can
52 // coordinate evaluation with all the trackers that respond to it. If we
53 // listened ourselves and requested rule evaluation before another tracker
54 // received the notification, our conditions would be evaluated based on the
55 // new URL while the other tracker's conditions would still be evaluated based
56 // on the previous URL.
57 void OnWebContentsNavigation(content::WebContents
* contents
,
58 const content::LoadCommittedDetails
& details
,
59 const content::FrameNavigateParams
& params
);
61 // Inserts the currently-matching CSS selectors into |css_selectors|. We use
62 // hash_set for maximally efficient lookup.
63 void GetMatchingCssSelectors(content::WebContents
* contents
,
64 base::hash_set
<std::string
>* css_selectors
);
66 // TODO(wittman): Remove once DeclarativeChromeContentRulesRegistry no longer
67 // depends on concrete condition implementations. At that point
68 // DeclarativeChromeContentRulesRegistryTest.ActiveRulesDoesntGrow will be
69 // able to use a test condition object and not need to depend on force setting
70 // matching CSS seleectors.
71 void UpdateMatchingCssSelectorsForTesting(
72 content::WebContents
* contents
,
73 const std::vector
<std::string
>& matching_css_selectors
);
76 // Monitors CSS selector matching state on one WebContents.
77 class PerWebContentsTracker
: public content::WebContentsObserver
{
79 using RequestEvaluationCallback
=
80 base::Callback
<void(content::WebContents
*)>;
81 using WebContentsDestroyedCallback
=
82 base::Callback
<void(content::WebContents
*)>;
84 PerWebContentsTracker(
85 content::WebContents
* contents
,
86 const RequestEvaluationCallback
& request_evaluation
,
87 const WebContentsDestroyedCallback
& web_contents_destroyed
);
88 ~PerWebContentsTracker() override
;
90 void OnWebContentsNavigation(const content::LoadCommittedDetails
& details
,
91 const content::FrameNavigateParams
& params
);
93 // See comment on similar function above.
94 void UpdateMatchingCssSelectorsForTesting(
95 const std::vector
<std::string
>& matching_css_selectors
);
97 const std::vector
<std::string
>& matching_css_selectors() const {
98 return matching_css_selectors_
;
102 // content::WebContentsObserver overrides.
103 bool OnMessageReceived(const IPC::Message
& message
) override
;
104 void WebContentsDestroyed() override
;
106 void OnWatchedPageChange(const std::vector
<std::string
>& css_selectors
);
108 const RequestEvaluationCallback request_evaluation_
;
109 const WebContentsDestroyedCallback web_contents_destroyed_
;
111 std::vector
<std::string
> matching_css_selectors_
;
113 DISALLOW_COPY_AND_ASSIGN(PerWebContentsTracker
);
116 // content::NotificationObserver implementation.
117 void Observe(int type
,
118 const content::NotificationSource
& source
,
119 const content::NotificationDetails
& details
) override
;
121 // If the renderer process is associated with our browser context, tells it
122 // what page attributes to watch for using an ExtensionMsg_WatchPages.
123 void InstructRenderProcessIfManagingBrowserContext(
124 content::RenderProcessHost
* process
);
126 // Called by PerWebContentsTracker on web contents destruction.
127 void DeletePerWebContentsTracker(content::WebContents
* contents
);
129 // The context whose state we're monitoring for evaluation.
130 content::BrowserContext
* context_
;
132 // All CSS selectors that are watched for by any rule's conditions. This
133 // vector is sorted by construction.
134 std::vector
<std::string
> watched_css_selectors_
;
136 // Maps WebContents to the tracker for that WebContents state.
137 std::map
<content::WebContents
*, linked_ptr
<PerWebContentsTracker
>>
138 per_web_contents_tracker_
;
141 DeclarativeContentConditionTrackerDelegate
* delegate_
;
143 // Manages our notification registrations.
144 content::NotificationRegistrar registrar_
;
146 DISALLOW_COPY_AND_ASSIGN(DeclarativeContentCssConditionTracker
);
149 } // namespace extensions
151 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_DECLARATIVE_CONTENT_CSS_CONDITION_TRACKER_H_