OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_DECLARATIVE_CONTENT_CS
S_CONDITION_TRACKER_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_DECLARATIVE_CONTENT_CS
S_CONDITION_TRACKER_H_ | |
7 | |
8 #include <map> | |
9 #include <set> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include "base/callback.h" | |
14 #include "base/containers/hash_tables.h" | |
15 #include "content/public/browser/notification_observer.h" | |
16 #include "content/public/browser/notification_registrar.h" | |
17 #include "content/public/browser/web_contents_observer.h" | |
18 | |
19 | |
20 namespace content { | |
21 class BrowserContext; | |
22 struct FrameNavigateParams; | |
23 struct LoadCommittedDetails; | |
24 class RenderProcessHost; | |
25 class WebContents; | |
26 } | |
27 | |
28 namespace extensions { | |
29 | |
30 // Interface that allows the DeclarativeContentCssConditionTracker to request | |
31 // condition evaluation, and shields it from having to know about Browsers. | |
32 class DeclarativeContentCssConditionTrackerDelegate { | |
33 public: | |
34 // Callback for per-tab invocations. | |
35 using WebContentsCallback = | |
36 base::Callback<void(content::WebContents* content)>; | |
37 | |
38 // Requests re-evaluation of conditions for |contents|. | |
39 virtual void RequestEvaluation(content::WebContents* contents) = 0; | |
40 | |
41 // Returns true if the evaluator should manage condition state for |context|. | |
42 virtual bool ShouldManageConditionsForBrowserContext( | |
43 content::BrowserContext* context) = 0; | |
44 | |
45 protected: | |
46 DeclarativeContentCssConditionTrackerDelegate(); | |
47 virtual ~DeclarativeContentCssConditionTrackerDelegate(); | |
48 | |
49 private: | |
50 DISALLOW_COPY_AND_ASSIGN(DeclarativeContentCssConditionTrackerDelegate); | |
51 }; | |
52 | |
53 // Supports watching of CSS selectors to across tab contents in a browser | |
54 // context, and querying for the matching CSS selectors for a context. | |
55 class DeclarativeContentCssConditionTracker | |
56 : public content::NotificationObserver { | |
57 public: | |
58 DeclarativeContentCssConditionTracker( | |
59 content::BrowserContext* context, | |
60 DeclarativeContentCssConditionTrackerDelegate* delegate); | |
61 ~DeclarativeContentCssConditionTracker() override; | |
62 | |
63 // Sets the set of CSS selectors to watch for CSS condition evaluation. | |
64 void SetWatchedCssSelectors( | |
65 const std::set<std::string>& watched_css_selectors); | |
66 | |
67 // Requests that CSS conditions be tracked for |contents|. | |
68 void TrackForWebContents(content::WebContents* contents); | |
69 | |
70 // Handles navigation of |contents|. We depend on the caller to notify us of | |
71 // this event rather than listening for it ourselves, so that the caller can | |
72 // coordinate evaluation with all the trackers that respond to it. If we | |
73 // listened ourselves and requested rule evaluation before another tracker | |
74 // received the notification, our conditions would be evaluated based on the | |
75 // new URL while the other tracker's conditions would still be evaluated based | |
76 // on the previous URL. | |
77 void OnWebContentsNavigation(content::WebContents* contents, | |
78 const content::LoadCommittedDetails& details, | |
79 const content::FrameNavigateParams& params); | |
80 | |
81 // Inserts the currently-matching CSS selectors into |css_selectors|. We use | |
82 // hash_set for maximally efficient lookup. | |
83 void GetMatchingCssSelectors(content::WebContents* contents, | |
84 base::hash_set<std::string>* css_selectors); | |
85 | |
86 // TODO(wittman): Remove once DeclarativeChromeContentRulesRegistry no longer | |
87 // depends on concrete condition implementations. At that point | |
88 // DeclarativeChromeContentRulesRegistryTest.ActiveRulesDoesntGrow will be | |
89 // able to use a test condition object and not need to depend on force setting | |
90 // matching CSS seleectors. | |
91 void UpdateMatchingCssSelectorsForTesting( | |
92 content::WebContents* contents, | |
93 const std::vector<std::string>& matching_css_selectors); | |
94 | |
95 private: | |
96 class PerWebContentsTracker; | |
97 | |
98 // Instantiate a PerWebContentsTracker watching |contents|. | |
99 void CreatePerWebContentsTracker(content::WebContents* contents); | |
100 | |
101 // content::NotificationObserver implementation. | |
102 void Observe(int type, | |
103 const content::NotificationSource& source, | |
104 const content::NotificationDetails& details) override; | |
105 | |
106 // If the renderer process is associated with our browser context, tells it | |
107 // what page attributes to watch for using an ExtensionMsg_WatchPages. | |
108 void InstructRenderProcessIfManagingBrowserContext( | |
109 content::RenderProcessHost* process); | |
110 | |
111 // Called by PerWebContentsTracker on destruction. | |
112 void RemovePerWebContentsTracker(PerWebContentsTracker* tracker); | |
113 | |
114 // The context whose state we're monitoring for evaluation. | |
115 content::BrowserContext* context_; | |
116 | |
117 // All CSS selectors that are watched for by any rule's conditions. This | |
118 // vector is sorted by construction. | |
119 std::vector<std::string> watched_css_selectors_; | |
120 | |
121 // Maps WebContents to the tracker for that WebContents | |
122 // state. PerWebContentsTracker owns itself, so this pointer is weak. | |
123 std::map<content::WebContents*, PerWebContentsTracker*> | |
124 per_web_contents_tracker_; | |
125 | |
126 // Weak. | |
127 DeclarativeContentCssConditionTrackerDelegate* delegate_; | |
128 | |
129 // Manages our notification registrations. | |
130 content::NotificationRegistrar registrar_; | |
131 | |
132 DISALLOW_COPY_AND_ASSIGN(DeclarativeContentCssConditionTracker); | |
133 }; | |
134 | |
135 } // namespace extensions | |
136 | |
137 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_DECLARATIVE_CONTENT
_CSS_CONDITION_TRACKER_H_ | |
OLD | NEW |