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 "chrome/browser/ui/browser.h" | |
12 #include "chrome/browser/ui/browser_iterator.h" | |
13 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
14 #include "content/public/browser/navigation_details.h" | |
15 #include "content/public/browser/notification_service.h" | |
16 #include "content/public/browser/notification_source.h" | |
17 #include "content/public/browser/render_process_host.h" | |
18 #include "extensions/common/extension_messages.h" | |
19 #include "ipc/ipc_message.h" | |
20 #include "ipc/ipc_message_macros.h" | |
21 | |
22 namespace extensions { | |
23 | |
24 DeclarativeContentCssConditionTrackerDelegate:: | |
25 DeclarativeContentCssConditionTrackerDelegate() {} | |
26 | |
27 DeclarativeContentCssConditionTrackerDelegate:: | |
28 ~DeclarativeContentCssConditionTrackerDelegate() {} | |
29 | |
30 DeclarativeContentCssConditionTracker::DeclarativeContentCssConditionTracker( | |
31 content::BrowserContext* context, | |
32 DeclarativeContentCssConditionTrackerDelegate* delegate) | |
33 : context_(context), | |
34 delegate_(delegate) { | |
35 // Observe all the existing WebContents for matched CSS selector changes. | |
36 for (chrome::BrowserIterator it; !it.done(); it.Next()) { | |
37 Browser* browser = *it; | |
38 if (!delegate_->ShouldManageConditionsForBrowserContext(browser->profile())) | |
39 continue; | |
40 | |
41 for (int i = 0, tab_count = browser->tab_strip_model()->count(); | |
42 i < tab_count; ++i) { | |
43 content::WebContents* contents = | |
44 browser->tab_strip_model()->GetWebContentsAt(i); | |
45 matched_selector_change_observers_[contents] = | |
46 make_linked_ptr(new MatchedSelectorChangeObserver( | |
47 contents, | |
48 base::Bind(&DeclarativeContentCssConditionTracker:: | |
49 UpdateMatchingCssSelectors, | |
50 base::Unretained(this)))); | |
51 } | |
52 } | |
53 | |
54 registrar_.Add(this, | |
55 content::NOTIFICATION_RENDERER_PROCESS_CREATED, | |
56 content::NotificationService::AllBrowserContextsAndSources()); | |
57 registrar_.Add(this, | |
58 chrome::NOTIFICATION_TAB_ADDED, | |
59 content::NotificationService::AllSources()); | |
60 registrar_.Add(this, | |
61 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | |
62 content::NotificationService::AllBrowserContextsAndSources()); | |
63 } | |
64 | |
65 DeclarativeContentCssConditionTracker:: | |
66 ~DeclarativeContentCssConditionTracker() {} | |
67 | |
68 // We use the sorted propery of the set for equality checks with | |
69 // watched_css_selectors_, which is guaranteed to be sorted because it's set | |
70 // from the set contents. | |
71 void DeclarativeContentCssConditionTracker::SetWatchedCssSelectors( | |
72 const std::set<std::string>& new_watched_css_selectors) { | |
73 if (new_watched_css_selectors.size() != watched_css_selectors_.size() || | |
74 !std::equal(new_watched_css_selectors.begin(), | |
75 new_watched_css_selectors.end(), | |
76 watched_css_selectors_.begin())) { | |
77 watched_css_selectors_.assign(new_watched_css_selectors.begin(), | |
78 new_watched_css_selectors.end()); | |
79 | |
80 for (content::RenderProcessHost::iterator it( | |
81 content::RenderProcessHost::AllHostsIterator()); | |
82 !it.IsAtEnd(); | |
83 it.Advance()) { | |
84 InstructRenderProcessIfManagingBrowserContext(it.GetCurrentValue()); | |
85 } | |
86 } | |
87 } | |
88 | |
89 void DeclarativeContentCssConditionTracker::OnWebContentsNavigation( | |
not at google - send to devlin
2015/06/01 22:51:11
Can this be part of your own WebContentsObserver i
Mike Wittman
2015/06/05 01:17:44
It could as it stands now, but once I encapsulate
not at google - send to devlin
2015/06/05 17:16:30
Ah right. No, what you have sounds right them. I d
Mike Wittman
2015/06/05 21:00:46
I think it should work if all the tracker state is
not at google - send to devlin
2015/06/05 21:15:21
not sure, but I don't particularly trust that sort
| |
90 content::WebContents* contents, | |
91 const content::LoadCommittedDetails& details, | |
92 const content::FrameNavigateParams& params) { | |
93 if (details.is_in_page) { | |
94 // Within-page navigations don't change the set of elements that | |
95 // exist, and we only support filtering on the top-level URL, so | |
96 // this can't change which rules match. | |
97 return; | |
98 } | |
99 | |
100 // Top-level navigation produces a new document. Initially, the | |
101 // document's empty, so no CSS rules match. The renderer will send | |
102 // an ExtensionHostMsg_OnWatchedPageChange later if any CSS rules | |
103 // match. | |
104 matching_css_selectors_[contents].clear(); | |
105 delegate_->RequestEvaluation(contents); | |
106 } | |
107 | |
108 void DeclarativeContentCssConditionTracker::GetMatchingCssSelectors( | |
109 content::WebContents* contents, | |
110 base::hash_set<std::string>* css_selectors) { | |
111 css_selectors->insert(matching_css_selectors_[contents].begin(), | |
112 matching_css_selectors_[contents].end()); | |
113 } | |
114 | |
115 void DeclarativeContentCssConditionTracker:: | |
116 UpdateMatchingCssSelectorsForTesting( | |
117 content::WebContents* contents, | |
118 const std::vector<std::string>& matching_css_selectors) { | |
119 UpdateMatchingCssSelectors(contents, matching_css_selectors); | |
120 } | |
121 | |
122 DeclarativeContentCssConditionTracker::MatchedSelectorChangeObserver:: | |
123 MatchedSelectorChangeObserver( | |
124 content::WebContents* contents, | |
125 const NotificationCallback& notification_callback) | |
126 : WebContentsObserver(contents), | |
127 notification_callback_(notification_callback) { | |
128 } | |
129 | |
130 DeclarativeContentCssConditionTracker::MatchedSelectorChangeObserver:: | |
131 ~MatchedSelectorChangeObserver() { | |
132 } | |
133 | |
134 bool | |
135 DeclarativeContentCssConditionTracker::MatchedSelectorChangeObserver:: | |
136 OnMessageReceived( | |
137 const IPC::Message& message) { | |
138 bool handled = true; | |
139 IPC_BEGIN_MESSAGE_MAP(MatchedSelectorChangeObserver, message) | |
140 IPC_MESSAGE_HANDLER(ExtensionHostMsg_OnWatchedPageChange, | |
141 OnWatchedPageChange) | |
142 IPC_MESSAGE_UNHANDLED(handled = false) | |
143 IPC_END_MESSAGE_MAP() | |
144 return handled; | |
145 } | |
146 | |
147 void | |
148 DeclarativeContentCssConditionTracker::MatchedSelectorChangeObserver:: | |
149 OnWatchedPageChange( | |
150 const std::vector<std::string>& css_selectors) { | |
151 notification_callback_.Run(web_contents(), css_selectors); | |
152 } | |
153 | |
154 void DeclarativeContentCssConditionTracker::Observe( | |
155 int type, | |
156 const content::NotificationSource& source, | |
157 const content::NotificationDetails& details) { | |
158 switch (type) { | |
159 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: { | |
160 content::RenderProcessHost* process = | |
161 content::Source<content::RenderProcessHost>(source).ptr(); | |
162 InstructRenderProcessIfManagingBrowserContext(process); | |
163 break; | |
164 } | |
165 case chrome::NOTIFICATION_TAB_ADDED: { | |
166 content::WebContents* contents = | |
167 content::Details<content::WebContents>(details).ptr(); | |
168 matched_selector_change_observers_[contents] = | |
169 make_linked_ptr(new MatchedSelectorChangeObserver( | |
170 contents, | |
171 base::Bind(&DeclarativeContentCssConditionTracker:: | |
172 UpdateMatchingCssSelectors, | |
173 base::Unretained(this)))); | |
174 break; | |
175 } | |
176 case content::NOTIFICATION_WEB_CONTENTS_DESTROYED: { | |
not at google - send to devlin
2015/06/01 22:51:11
You should be able to include this as part of a We
Mike Wittman
2015/06/05 01:17:44
Good point. I think it makes sense to go further a
| |
177 content::WebContents* contents = | |
178 content::Source<content::WebContents>(source).ptr(); | |
179 matched_selector_change_observers_.erase(contents); | |
180 matching_css_selectors_.erase(contents); | |
181 break; | |
182 } | |
183 } | |
184 } | |
185 | |
186 void DeclarativeContentCssConditionTracker:: | |
187 InstructRenderProcessIfManagingBrowserContext( | |
188 content::RenderProcessHost* process) { | |
189 if (delegate_->ShouldManageConditionsForBrowserContext( | |
190 process->GetBrowserContext())) { | |
191 process->Send(new ExtensionMsg_WatchPages(watched_css_selectors_)); | |
192 } | |
193 } | |
194 | |
195 void DeclarativeContentCssConditionTracker::UpdateMatchingCssSelectors( | |
196 content::WebContents* contents, | |
197 const std::vector<std::string>& matching_css_selectors) { | |
198 matching_css_selectors_[contents] = matching_css_selectors; | |
199 | |
200 delegate_->RequestEvaluation(contents); | |
201 } | |
202 | |
203 } // namespace extensions | |
OLD | NEW |