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 #include "chrome/browser/extensions/api/declarative_content/declarative_content_
css_condition_tracker.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "chrome/browser/chrome_notification_types.h" | |
10 #include "chrome/browser/profiles/profile.h" | |
11 #include "content/public/browser/navigation_details.h" | |
12 #include "content/public/browser/notification_service.h" | |
13 #include "content/public/browser/notification_source.h" | |
14 #include "content/public/browser/render_process_host.h" | |
15 #include "extensions/common/extension_messages.h" | |
16 #include "ipc/ipc_message.h" | |
17 #include "ipc/ipc_message_macros.h" | |
18 | |
19 namespace extensions { | |
20 | |
21 // | |
22 // PerWebContentsTracker | |
23 // | |
24 | |
25 // Monitors CSS selector matching state on one WebContents. | |
26 class DeclarativeContentCssConditionTracker:: | |
27 PerWebContentsTracker : public content::WebContentsObserver { | |
28 public: | |
29 using RequestEvaluationCallback = | |
30 DeclarativeContentCssConditionTrackerDelegate::WebContentsCallback; | |
31 using DestroyingCallback = | |
32 base::Callback<void(PerWebContentsTracker*)>; | |
33 | |
34 PerWebContentsTracker( | |
35 content::WebContents* contents, | |
36 const RequestEvaluationCallback& request_evaluation, | |
37 const DestroyingCallback& destroying); | |
38 ~PerWebContentsTracker() override; | |
39 | |
40 void OnWebContentsNavigation(const content::LoadCommittedDetails& details, | |
41 const content::FrameNavigateParams& params); | |
42 | |
43 // See comment on similar function above. | |
44 void UpdateMatchingCssSelectorsForTesting( | |
45 const std::vector<std::string>& matching_css_selectors); | |
46 | |
47 const std::vector<std::string>& matching_css_selectors() const { | |
48 return matching_css_selectors_; | |
49 } | |
50 | |
51 private: | |
52 // content::WebContentsObserver overrides. | |
53 bool OnMessageReceived(const IPC::Message& message) override; | |
54 void WebContentsDestroyed() override; | |
55 | |
56 void OnWatchedPageChange(const std::vector<std::string>& css_selectors); | |
57 | |
58 const RequestEvaluationCallback request_evaluation_; | |
59 const DestroyingCallback destroying_; | |
60 | |
61 std::vector<std::string> matching_css_selectors_; | |
62 | |
63 DISALLOW_COPY_AND_ASSIGN(PerWebContentsTracker); | |
64 }; | |
65 | |
66 DeclarativeContentCssConditionTracker::PerWebContentsTracker:: | |
67 PerWebContentsTracker( | |
68 content::WebContents* contents, | |
69 const RequestEvaluationCallback& request_evaluation, | |
70 const DestroyingCallback& destroying) | |
71 : WebContentsObserver(contents), | |
72 request_evaluation_(request_evaluation), | |
73 destroying_(destroying) { | |
74 } | |
75 | |
76 DeclarativeContentCssConditionTracker::PerWebContentsTracker:: | |
77 ~PerWebContentsTracker() { | |
78 } | |
79 | |
80 void DeclarativeContentCssConditionTracker::PerWebContentsTracker:: | |
81 OnWebContentsNavigation(const content::LoadCommittedDetails& details, | |
82 const content::FrameNavigateParams& params) { | |
83 if (details.is_in_page) { | |
84 // Within-page navigations don't change the set of elements that | |
85 // exist, and we only support filtering on the top-level URL, so | |
86 // this can't change which rules match. | |
87 return; | |
88 } | |
89 | |
90 // Top-level navigation produces a new document. Initially, the | |
91 // document's empty, so no CSS rules match. The renderer will send | |
92 // an ExtensionHostMsg_OnWatchedPageChange later if any CSS rules | |
93 // match. | |
94 matching_css_selectors_.clear(); | |
95 request_evaluation_.Run(web_contents()); | |
96 } | |
97 | |
98 void DeclarativeContentCssConditionTracker::PerWebContentsTracker:: | |
99 UpdateMatchingCssSelectorsForTesting( | |
100 const std::vector<std::string>& matching_css_selectors) { | |
101 matching_css_selectors_ = matching_css_selectors; | |
102 request_evaluation_.Run(web_contents()); | |
103 } | |
104 | |
105 bool | |
106 DeclarativeContentCssConditionTracker::PerWebContentsTracker:: | |
107 OnMessageReceived( | |
108 const IPC::Message& message) { | |
109 bool handled = true; | |
110 IPC_BEGIN_MESSAGE_MAP(PerWebContentsTracker, message) | |
111 IPC_MESSAGE_HANDLER(ExtensionHostMsg_OnWatchedPageChange, | |
112 OnWatchedPageChange) | |
113 IPC_MESSAGE_UNHANDLED(handled = false) | |
114 IPC_END_MESSAGE_MAP() | |
115 return handled; | |
116 } | |
117 | |
118 void DeclarativeContentCssConditionTracker::PerWebContentsTracker:: | |
119 WebContentsDestroyed() { | |
120 destroying_.Run(this); | |
121 delete this; | |
122 } | |
123 | |
124 void | |
125 DeclarativeContentCssConditionTracker::PerWebContentsTracker:: | |
126 OnWatchedPageChange( | |
127 const std::vector<std::string>& css_selectors) { | |
128 matching_css_selectors_ = css_selectors; | |
129 request_evaluation_.Run(web_contents()); | |
130 } | |
131 | |
132 // | |
133 // DeclarativeContentCssConditionTrackerDelegate | |
134 // | |
135 | |
136 DeclarativeContentCssConditionTrackerDelegate:: | |
137 DeclarativeContentCssConditionTrackerDelegate() {} | |
138 | |
139 DeclarativeContentCssConditionTrackerDelegate:: | |
140 ~DeclarativeContentCssConditionTrackerDelegate() {} | |
141 | |
142 // | |
143 // DeclarativeContentCssConditionTracker | |
144 // | |
145 | |
146 DeclarativeContentCssConditionTracker::DeclarativeContentCssConditionTracker( | |
147 content::BrowserContext* context, | |
148 DeclarativeContentCssConditionTrackerDelegate* delegate) | |
149 : context_(context), | |
150 delegate_(delegate) { | |
151 registrar_.Add(this, | |
152 content::NOTIFICATION_RENDERER_PROCESS_CREATED, | |
153 content::NotificationService::AllBrowserContextsAndSources()); | |
154 } | |
155 | |
156 DeclarativeContentCssConditionTracker:: | |
157 ~DeclarativeContentCssConditionTracker() { | |
158 // All monitored WebContents are expected to be destroyed before this object. | |
159 CHECK(per_web_contents_tracker_.empty()); | |
160 } | |
161 | |
162 // We use the sorted propery of the set for equality checks with | |
163 // watched_css_selectors_, which is guaranteed to be sorted because it's set | |
164 // from the set contents. | |
165 void DeclarativeContentCssConditionTracker::SetWatchedCssSelectors( | |
166 const std::set<std::string>& new_watched_css_selectors) { | |
167 if (new_watched_css_selectors.size() != watched_css_selectors_.size() || | |
168 !std::equal(new_watched_css_selectors.begin(), | |
169 new_watched_css_selectors.end(), | |
170 watched_css_selectors_.begin())) { | |
171 watched_css_selectors_.assign(new_watched_css_selectors.begin(), | |
172 new_watched_css_selectors.end()); | |
173 | |
174 for (content::RenderProcessHost::iterator it( | |
175 content::RenderProcessHost::AllHostsIterator()); | |
176 !it.IsAtEnd(); | |
177 it.Advance()) { | |
178 InstructRenderProcessIfManagingBrowserContext(it.GetCurrentValue()); | |
179 } | |
180 } | |
181 } | |
182 | |
183 void DeclarativeContentCssConditionTracker::TrackForWebContents( | |
184 content::WebContents* contents) { | |
185 CreatePerWebContentsTracker(contents); | |
186 } | |
187 | |
188 void DeclarativeContentCssConditionTracker::OnWebContentsNavigation( | |
189 content::WebContents* contents, | |
190 const content::LoadCommittedDetails& details, | |
191 const content::FrameNavigateParams& params) { | |
192 per_web_contents_tracker_[contents]->OnWebContentsNavigation(details, params); | |
193 } | |
194 | |
195 void DeclarativeContentCssConditionTracker::GetMatchingCssSelectors( | |
196 content::WebContents* contents, | |
197 base::hash_set<std::string>* css_selectors) { | |
198 const std::vector<std::string>& matching_css_selectors = | |
199 per_web_contents_tracker_[contents]->matching_css_selectors(); | |
200 css_selectors->insert(matching_css_selectors.begin(), | |
201 matching_css_selectors.end()); | |
202 } | |
203 | |
204 void DeclarativeContentCssConditionTracker:: | |
205 UpdateMatchingCssSelectorsForTesting( | |
206 content::WebContents* contents, | |
207 const std::vector<std::string>& matching_css_selectors) { | |
208 per_web_contents_tracker_[contents]-> | |
209 UpdateMatchingCssSelectorsForTesting(matching_css_selectors); | |
210 } | |
211 | |
212 void DeclarativeContentCssConditionTracker::CreatePerWebContentsTracker( | |
213 content::WebContents* contents) { | |
214 // This is a weak pointer; PerWebContentsTracker owns itself. | |
215 per_web_contents_tracker_[contents] = | |
216 new PerWebContentsTracker( | |
217 contents, | |
218 base::Bind(&DeclarativeContentCssConditionTrackerDelegate:: | |
219 RequestEvaluation, | |
220 base::Unretained(delegate_)), | |
221 base::Bind(&DeclarativeContentCssConditionTracker:: | |
222 RemovePerWebContentsTracker, | |
223 base::Unretained(this))); | |
224 } | |
225 | |
226 void DeclarativeContentCssConditionTracker::Observe( | |
227 int type, | |
228 const content::NotificationSource& source, | |
229 const content::NotificationDetails& details) { | |
230 switch (type) { | |
231 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: { | |
232 content::RenderProcessHost* process = | |
233 content::Source<content::RenderProcessHost>(source).ptr(); | |
234 InstructRenderProcessIfManagingBrowserContext(process); | |
235 break; | |
236 } | |
237 } | |
238 } | |
239 | |
240 void DeclarativeContentCssConditionTracker:: | |
241 InstructRenderProcessIfManagingBrowserContext( | |
242 content::RenderProcessHost* process) { | |
243 if (delegate_->ShouldManageConditionsForBrowserContext( | |
244 process->GetBrowserContext())) { | |
245 process->Send(new ExtensionMsg_WatchPages(watched_css_selectors_)); | |
246 } | |
247 } | |
248 | |
249 void DeclarativeContentCssConditionTracker::RemovePerWebContentsTracker( | |
250 PerWebContentsTracker* tracker) { | |
251 per_web_contents_tracker_.erase(tracker->web_contents()); | |
252 } | |
253 | |
254 } // namespace extensions | |
OLD | NEW |