Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: chrome/browser/extensions/api/declarative_content/declarative_content_css_condition_tracker.cc

Issue 1166393002: Reland "Encapsulate CSS selector declarative content condition tracking" (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkcr
Patch Set: address comments Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 DeclarativeContentCssConditionTracker::PerWebContentsTracker::
26 PerWebContentsTracker(
27 content::WebContents* contents,
28 const RequestEvaluationCallback& request_evaluation,
29 const WebContentsDestroyedCallback& web_contents_destroyed)
30 : WebContentsObserver(contents),
31 request_evaluation_(request_evaluation),
32 web_contents_destroyed_(web_contents_destroyed) {
33 }
34
35 DeclarativeContentCssConditionTracker::PerWebContentsTracker::
36 ~PerWebContentsTracker() {
37 }
38
39 void DeclarativeContentCssConditionTracker::PerWebContentsTracker::
40 OnWebContentsNavigation(const content::LoadCommittedDetails& details,
41 const content::FrameNavigateParams& params) {
42 if (details.is_in_page) {
43 // Within-page navigations don't change the set of elements that
44 // exist, and we only support filtering on the top-level URL, so
45 // this can't change which rules match.
46 return;
47 }
48
49 // Top-level navigation produces a new document. Initially, the
50 // document's empty, so no CSS rules match. The renderer will send
51 // an ExtensionHostMsg_OnWatchedPageChange later if any CSS rules
52 // match.
53 matching_css_selectors_.clear();
54 request_evaluation_.Run(web_contents());
55 }
56
57 void DeclarativeContentCssConditionTracker::PerWebContentsTracker::
58 UpdateMatchingCssSelectorsForTesting(
59 const std::vector<std::string>& matching_css_selectors) {
60 matching_css_selectors_ = matching_css_selectors;
61 request_evaluation_.Run(web_contents());
62 }
63
64 bool
65 DeclarativeContentCssConditionTracker::PerWebContentsTracker::
66 OnMessageReceived(
67 const IPC::Message& message) {
68 bool handled = true;
69 IPC_BEGIN_MESSAGE_MAP(PerWebContentsTracker, message)
70 IPC_MESSAGE_HANDLER(ExtensionHostMsg_OnWatchedPageChange,
71 OnWatchedPageChange)
72 IPC_MESSAGE_UNHANDLED(handled = false)
73 IPC_END_MESSAGE_MAP()
74 return handled;
75 }
76
77 void DeclarativeContentCssConditionTracker::PerWebContentsTracker::
78 WebContentsDestroyed() {
79 web_contents_destroyed_.Run(web_contents());
80 }
81
82 void
83 DeclarativeContentCssConditionTracker::PerWebContentsTracker::
84 OnWatchedPageChange(
85 const std::vector<std::string>& css_selectors) {
86 matching_css_selectors_ = css_selectors;
87 request_evaluation_.Run(web_contents());
88 }
89
90 //
91 // DeclarativeContentCssConditionTrackerDelegate
92 //
93
94 DeclarativeContentCssConditionTrackerDelegate::
95 DeclarativeContentCssConditionTrackerDelegate() {}
96
97 DeclarativeContentCssConditionTrackerDelegate::
98 ~DeclarativeContentCssConditionTrackerDelegate() {}
99
100 //
101 // DeclarativeContentCssConditionTracker
102 //
103
104 DeclarativeContentCssConditionTracker::DeclarativeContentCssConditionTracker(
105 content::BrowserContext* context,
106 DeclarativeContentCssConditionTrackerDelegate* delegate)
107 : context_(context),
108 delegate_(delegate) {
109 registrar_.Add(this,
110 content::NOTIFICATION_RENDERER_PROCESS_CREATED,
111 content::NotificationService::AllBrowserContextsAndSources());
112 }
113
114 DeclarativeContentCssConditionTracker::
115 ~DeclarativeContentCssConditionTracker() {}
116
117 // We use the sorted propery of the set for equality checks with
118 // watched_css_selectors_, which is guaranteed to be sorted because it's set
119 // from the set contents.
120 void DeclarativeContentCssConditionTracker::SetWatchedCssSelectors(
121 const std::set<std::string>& new_watched_css_selectors) {
122 if (new_watched_css_selectors.size() != watched_css_selectors_.size() ||
123 !std::equal(new_watched_css_selectors.begin(),
124 new_watched_css_selectors.end(),
125 watched_css_selectors_.begin())) {
126 watched_css_selectors_.assign(new_watched_css_selectors.begin(),
127 new_watched_css_selectors.end());
128
129 for (content::RenderProcessHost::iterator it(
130 content::RenderProcessHost::AllHostsIterator());
131 !it.IsAtEnd();
132 it.Advance()) {
133 InstructRenderProcessIfManagingBrowserContext(it.GetCurrentValue());
134 }
135 }
136 }
137
138 void DeclarativeContentCssConditionTracker::TrackForWebContents(
139 content::WebContents* contents) {
140 CreatePerWebContentsTracker(contents);
141 }
142
143 void DeclarativeContentCssConditionTracker::OnWebContentsNavigation(
144 content::WebContents* contents,
145 const content::LoadCommittedDetails& details,
146 const content::FrameNavigateParams& params) {
147 per_web_contents_tracker_[contents]->OnWebContentsNavigation(details, params);
148 }
149
150 void DeclarativeContentCssConditionTracker::GetMatchingCssSelectors(
151 content::WebContents* contents,
152 base::hash_set<std::string>* css_selectors) {
153 const std::vector<std::string>& matching_css_selectors =
154 per_web_contents_tracker_[contents]->matching_css_selectors();
155 css_selectors->insert(matching_css_selectors.begin(),
156 matching_css_selectors.end());
157 }
158
159 void DeclarativeContentCssConditionTracker::
160 UpdateMatchingCssSelectorsForTesting(
161 content::WebContents* contents,
162 const std::vector<std::string>& matching_css_selectors) {
163 per_web_contents_tracker_[contents]->
164 UpdateMatchingCssSelectorsForTesting(matching_css_selectors);
165 }
166
167 void DeclarativeContentCssConditionTracker::CreatePerWebContentsTracker(
168 content::WebContents* contents) {
169 per_web_contents_tracker_[contents] =
170 make_linked_ptr(new PerWebContentsTracker(
171 contents,
172 base::Bind(&DeclarativeContentCssConditionTrackerDelegate::
173 RequestEvaluation,
174 base::Unretained(delegate_)),
175 base::Bind(&DeclarativeContentCssConditionTracker::
176 DeletePerWebContentsTracker,
177 base::Unretained(this))));
178 }
179
180 void DeclarativeContentCssConditionTracker::Observe(
181 int type,
182 const content::NotificationSource& source,
183 const content::NotificationDetails& details) {
184 switch (type) {
185 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
186 content::RenderProcessHost* process =
187 content::Source<content::RenderProcessHost>(source).ptr();
188 InstructRenderProcessIfManagingBrowserContext(process);
189 break;
190 }
191 }
192 }
193
194 void DeclarativeContentCssConditionTracker::
195 InstructRenderProcessIfManagingBrowserContext(
196 content::RenderProcessHost* process) {
197 if (delegate_->ShouldManageConditionsForBrowserContext(
198 process->GetBrowserContext())) {
199 process->Send(new ExtensionMsg_WatchPages(watched_css_selectors_));
200 }
201 }
202
203 void DeclarativeContentCssConditionTracker::DeletePerWebContentsTracker(
204 content::WebContents* contents) {
205 per_web_contents_tracker_.erase(contents);
206 }
207
208 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698