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

Side by Side Diff: chrome/browser/extensions/api/web_navigation/web_navigation_api_helpers.cc

Issue 2898383002: [Extensions] Make Event::restrict_to_browser_context const. (Closed)
Patch Set: Created 3 years, 7 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Implements the Chrome Extensions WebNavigation API. 5 // Implements the Chrome Extensions WebNavigation API.
6 6
7 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api_helper s.h" 7 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api_helper s.h"
8 8
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 // Dispatches events to the extension message service. 48 // Dispatches events to the extension message service.
49 void DispatchEvent(content::BrowserContext* browser_context, 49 void DispatchEvent(content::BrowserContext* browser_context,
50 std::unique_ptr<Event> event, 50 std::unique_ptr<Event> event,
51 const GURL& url) { 51 const GURL& url) {
52 EventFilteringInfo info; 52 EventFilteringInfo info;
53 info.SetURL(url); 53 info.SetURL(url);
54 54
55 Profile* profile = Profile::FromBrowserContext(browser_context); 55 Profile* profile = Profile::FromBrowserContext(browser_context);
56 EventRouter* event_router = EventRouter::Get(profile); 56 EventRouter* event_router = EventRouter::Get(profile);
57 if (profile && event_router) { 57 if (profile && event_router) {
58 event->restrict_to_browser_context = profile;
Devlin 2017/05/24 16:08:18 maybe substitute this with DCHECK_EQ(profile, even
lazyboy 2017/05/24 19:19:33 Done.
59 event->filter_info = info; 58 event->filter_info = info;
60 event_router->BroadcastEvent(std::move(event)); 59 event_router->BroadcastEvent(std::move(event));
61 } 60 }
62 } 61 }
63 62
64 } // namespace 63 } // namespace
65 64
66 // Constructs an onBeforeNavigate event. 65 // Constructs an onBeforeNavigate event.
67 std::unique_ptr<Event> CreateOnBeforeNavigateEvent( 66 std::unique_ptr<Event> CreateOnBeforeNavigateEvent(
68 content::NavigationHandle* navigation_handle) { 67 content::NavigationHandle* navigation_handle) {
69 GURL url(navigation_handle->GetURL()); 68 GURL url(navigation_handle->GetURL());
70 69
71 web_navigation::OnBeforeNavigate::Details details; 70 web_navigation::OnBeforeNavigate::Details details;
72 details.tab_id = 71 details.tab_id =
73 ExtensionTabUtil::GetTabId(navigation_handle->GetWebContents()); 72 ExtensionTabUtil::GetTabId(navigation_handle->GetWebContents());
74 details.url = url.spec(); 73 details.url = url.spec();
75 details.process_id = -1; 74 details.process_id = -1;
76 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(navigation_handle); 75 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(navigation_handle);
77 details.parent_frame_id = 76 details.parent_frame_id =
78 ExtensionApiFrameIdMap::GetParentFrameId(navigation_handle); 77 ExtensionApiFrameIdMap::GetParentFrameId(navigation_handle);
79 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); 78 details.time_stamp = MilliSecondsFromTime(base::Time::Now());
80 79
81 std::unique_ptr<Event> event( 80 auto event = base::MakeUnique<Event>(
82 new Event(events::WEB_NAVIGATION_ON_BEFORE_NAVIGATE, 81 events::WEB_NAVIGATION_ON_BEFORE_NAVIGATE,
83 web_navigation::OnBeforeNavigate::kEventName, 82 web_navigation::OnBeforeNavigate::kEventName,
84 web_navigation::OnBeforeNavigate::Create(details))); 83 web_navigation::OnBeforeNavigate::Create(details),
84 navigation_handle->GetWebContents()->GetBrowserContext());
85 85
86 EventFilteringInfo info; 86 EventFilteringInfo info;
87 info.SetURL(navigation_handle->GetURL()); 87 info.SetURL(navigation_handle->GetURL());
88
89 event->restrict_to_browser_context =
90 navigation_handle->GetWebContents()->GetBrowserContext();
91 event->filter_info = info; 88 event->filter_info = info;
92 89
93 return event; 90 return event;
94 } 91 }
95 92
96 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated 93 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated
97 // event. 94 // event.
98 void DispatchOnCommitted(events::HistogramValue histogram_value, 95 void DispatchOnCommitted(events::HistogramValue histogram_value,
99 const std::string& event_name, 96 const std::string& event_name,
100 content::NavigationHandle* navigation_handle) { 97 content::NavigationHandle* navigation_handle) {
(...skipping 30 matching lines...) Expand all
131 if (transition_type & ui::PAGE_TRANSITION_SERVER_REDIRECT) 128 if (transition_type & ui::PAGE_TRANSITION_SERVER_REDIRECT)
132 qualifiers->AppendString("server_redirect"); 129 qualifiers->AppendString("server_redirect");
133 if (transition_type & ui::PAGE_TRANSITION_FORWARD_BACK) 130 if (transition_type & ui::PAGE_TRANSITION_FORWARD_BACK)
134 qualifiers->AppendString("forward_back"); 131 qualifiers->AppendString("forward_back");
135 if (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR) 132 if (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)
136 qualifiers->AppendString("from_address_bar"); 133 qualifiers->AppendString("from_address_bar");
137 dict->Set(keys::kTransitionQualifiersKey, std::move(qualifiers)); 134 dict->Set(keys::kTransitionQualifiersKey, std::move(qualifiers));
138 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); 135 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
139 args->Append(std::move(dict)); 136 args->Append(std::move(dict));
140 137
141 std::unique_ptr<Event> event( 138 content::BrowserContext* context =
142 new Event(histogram_value, event_name, std::move(args))); 139 navigation_handle->GetWebContents()->GetBrowserContext();
143 DispatchEvent(navigation_handle->GetWebContents()->GetBrowserContext(), 140 auto event = base::MakeUnique<Event>(histogram_value, event_name,
144 std::move(event), url); 141 std::move(args), context);
142 DispatchEvent(context, std::move(event), url);
145 } 143 }
146 144
147 // Constructs and dispatches an onDOMContentLoaded event. 145 // Constructs and dispatches an onDOMContentLoaded event.
148 void DispatchOnDOMContentLoaded(content::WebContents* web_contents, 146 void DispatchOnDOMContentLoaded(content::WebContents* web_contents,
149 content::RenderFrameHost* frame_host, 147 content::RenderFrameHost* frame_host,
150 const GURL& url) { 148 const GURL& url) {
151 web_navigation::OnDOMContentLoaded::Details details; 149 web_navigation::OnDOMContentLoaded::Details details;
152 details.tab_id = ExtensionTabUtil::GetTabId(web_contents); 150 details.tab_id = ExtensionTabUtil::GetTabId(web_contents);
153 details.url = url.spec(); 151 details.url = url.spec();
154 details.process_id = frame_host->GetProcess()->GetID(); 152 details.process_id = frame_host->GetProcess()->GetID();
155 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(frame_host); 153 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(frame_host);
156 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); 154 details.time_stamp = MilliSecondsFromTime(base::Time::Now());
157 155
158 std::unique_ptr<Event> event( 156 std::unique_ptr<Event> event(
159 new Event(events::WEB_NAVIGATION_ON_DOM_CONTENT_LOADED, 157 new Event(events::WEB_NAVIGATION_ON_DOM_CONTENT_LOADED,
160 web_navigation::OnDOMContentLoaded::kEventName, 158 web_navigation::OnDOMContentLoaded::kEventName,
161 web_navigation::OnDOMContentLoaded::Create(details))); 159 web_navigation::OnDOMContentLoaded::Create(details)));
Devlin 2017/05/24 16:08:18 We need to add restrict_to_browser_context here if
lazyboy 2017/05/24 19:19:33 Thanks for catching this! Done.
162 DispatchEvent(web_contents->GetBrowserContext(), std::move(event), url); 160 DispatchEvent(web_contents->GetBrowserContext(), std::move(event), url);
163 } 161 }
164 162
165 // Constructs and dispatches an onCompleted event. 163 // Constructs and dispatches an onCompleted event.
166 void DispatchOnCompleted(content::WebContents* web_contents, 164 void DispatchOnCompleted(content::WebContents* web_contents,
167 content::RenderFrameHost* frame_host, 165 content::RenderFrameHost* frame_host,
168 const GURL& url) { 166 const GURL& url) {
169 web_navigation::OnCompleted::Details details; 167 web_navigation::OnCompleted::Details details;
170 details.tab_id = ExtensionTabUtil::GetTabId(web_contents); 168 details.tab_id = ExtensionTabUtil::GetTabId(web_contents);
171 details.url = url.spec(); 169 details.url = url.spec();
172 details.process_id = frame_host->GetProcess()->GetID(); 170 details.process_id = frame_host->GetProcess()->GetID();
173 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(frame_host); 171 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(frame_host);
174 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); 172 details.time_stamp = MilliSecondsFromTime(base::Time::Now());
175 173
176 std::unique_ptr<Event> event( 174 std::unique_ptr<Event> event(
177 new Event(events::WEB_NAVIGATION_ON_COMPLETED, 175 new Event(events::WEB_NAVIGATION_ON_COMPLETED,
178 web_navigation::OnCompleted::kEventName, 176 web_navigation::OnCompleted::kEventName,
179 web_navigation::OnCompleted::Create(details))); 177 web_navigation::OnCompleted::Create(details)));
Devlin 2017/05/24 16:08:18 ditto
lazyboy 2017/05/24 19:19:32 Done.
180 DispatchEvent(web_contents->GetBrowserContext(), std::move(event), url); 178 DispatchEvent(web_contents->GetBrowserContext(), std::move(event), url);
181 } 179 }
182 180
183 // Constructs and dispatches an onCreatedNavigationTarget event. 181 // Constructs and dispatches an onCreatedNavigationTarget event.
184 void DispatchOnCreatedNavigationTarget( 182 void DispatchOnCreatedNavigationTarget(
185 content::WebContents* web_contents, 183 content::WebContents* web_contents,
186 content::BrowserContext* browser_context, 184 content::BrowserContext* browser_context,
187 content::RenderFrameHost* source_frame_host, 185 content::RenderFrameHost* source_frame_host,
188 content::WebContents* target_web_contents, 186 content::WebContents* target_web_contents,
189 const GURL& target_url) { 187 const GURL& target_url) {
190 // Check that the tab is already inserted into a tab strip model. This code 188 // Check that the tab is already inserted into a tab strip model. This code
191 // path is exercised by ExtensionApiTest.WebNavigationRequestOpenTab. 189 // path is exercised by ExtensionApiTest.WebNavigationRequestOpenTab.
192 DCHECK(ExtensionTabUtil::GetTabById( 190 DCHECK(ExtensionTabUtil::GetTabById(
193 ExtensionTabUtil::GetTabId(target_web_contents), 191 ExtensionTabUtil::GetTabId(target_web_contents),
194 Profile::FromBrowserContext(target_web_contents->GetBrowserContext()), 192 Profile::FromBrowserContext(target_web_contents->GetBrowserContext()),
195 false, NULL, NULL, NULL, NULL)); 193 false, NULL, NULL, NULL, NULL));
196 194
197 web_navigation::OnCreatedNavigationTarget::Details details; 195 web_navigation::OnCreatedNavigationTarget::Details details;
198 details.source_tab_id = ExtensionTabUtil::GetTabId(web_contents); 196 details.source_tab_id = ExtensionTabUtil::GetTabId(web_contents);
199 details.source_process_id = source_frame_host->GetProcess()->GetID(); 197 details.source_process_id = source_frame_host->GetProcess()->GetID();
200 details.source_frame_id = 198 details.source_frame_id =
201 ExtensionApiFrameIdMap::GetFrameId(source_frame_host); 199 ExtensionApiFrameIdMap::GetFrameId(source_frame_host);
202 details.url = target_url.possibly_invalid_spec(); 200 details.url = target_url.possibly_invalid_spec();
203 details.tab_id = ExtensionTabUtil::GetTabId(target_web_contents); 201 details.tab_id = ExtensionTabUtil::GetTabId(target_web_contents);
204 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); 202 details.time_stamp = MilliSecondsFromTime(base::Time::Now());
205 203
206 std::unique_ptr<Event> event( 204 auto event = base::MakeUnique<Event>(
207 new Event(events::WEB_NAVIGATION_ON_CREATED_NAVIGATION_TARGET, 205 events::WEB_NAVIGATION_ON_CREATED_NAVIGATION_TARGET,
208 web_navigation::OnCreatedNavigationTarget::kEventName, 206 web_navigation::OnCreatedNavigationTarget::kEventName,
209 web_navigation::OnCreatedNavigationTarget::Create(details))); 207 web_navigation::OnCreatedNavigationTarget::Create(details),
208 browser_context);
210 DispatchEvent(browser_context, std::move(event), target_url); 209 DispatchEvent(browser_context, std::move(event), target_url);
211 210
212 // If the target WebContents already received the onBeforeNavigate event, 211 // If the target WebContents already received the onBeforeNavigate event,
213 // send it immediately after the onCreatedNavigationTarget above. 212 // send it immediately after the onCreatedNavigationTarget above.
214 WebNavigationTabObserver* target_observer = 213 WebNavigationTabObserver* target_observer =
215 WebNavigationTabObserver::Get(target_web_contents); 214 WebNavigationTabObserver::Get(target_web_contents);
216 target_observer->DispatchCachedOnBeforeNavigate(); 215 target_observer->DispatchCachedOnBeforeNavigate();
217 } 216 }
218 217
219 // Constructs and dispatches an onErrorOccurred event. 218 // Constructs and dispatches an onErrorOccurred event.
220 void DispatchOnErrorOccurred(content::WebContents* web_contents, 219 void DispatchOnErrorOccurred(content::WebContents* web_contents,
221 content::RenderFrameHost* frame_host, 220 content::RenderFrameHost* frame_host,
222 const GURL& url, 221 const GURL& url,
223 int error_code) { 222 int error_code) {
224 web_navigation::OnErrorOccurred::Details details; 223 web_navigation::OnErrorOccurred::Details details;
225 details.tab_id = ExtensionTabUtil::GetTabId(web_contents); 224 details.tab_id = ExtensionTabUtil::GetTabId(web_contents);
226 details.url = url.spec(); 225 details.url = url.spec();
227 details.process_id = frame_host->GetProcess()->GetID(); 226 details.process_id = frame_host->GetProcess()->GetID();
228 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(frame_host); 227 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(frame_host);
229 details.error = net::ErrorToString(error_code); 228 details.error = net::ErrorToString(error_code);
230 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); 229 details.time_stamp = MilliSecondsFromTime(base::Time::Now());
231 230
232 std::unique_ptr<Event> event( 231 auto event =
233 new Event(events::WEB_NAVIGATION_ON_ERROR_OCCURRED, 232 base::MakeUnique<Event>(events::WEB_NAVIGATION_ON_ERROR_OCCURRED,
234 web_navigation::OnErrorOccurred::kEventName, 233 web_navigation::OnErrorOccurred::kEventName,
235 web_navigation::OnErrorOccurred::Create(details))); 234 web_navigation::OnErrorOccurred::Create(details),
235 web_contents->GetBrowserContext());
236 DispatchEvent(web_contents->GetBrowserContext(), std::move(event), url); 236 DispatchEvent(web_contents->GetBrowserContext(), std::move(event), url);
Devlin 2017/05/24 16:08:19 nit: cache context
lazyboy 2017/05/24 19:19:32 Done.
237 } 237 }
238 238
239 void DispatchOnErrorOccurred(content::NavigationHandle* navigation_handle) { 239 void DispatchOnErrorOccurred(content::NavigationHandle* navigation_handle) {
240 web_navigation::OnErrorOccurred::Details details; 240 web_navigation::OnErrorOccurred::Details details;
241 details.tab_id = 241 details.tab_id =
242 ExtensionTabUtil::GetTabId(navigation_handle->GetWebContents()); 242 ExtensionTabUtil::GetTabId(navigation_handle->GetWebContents());
243 details.url = navigation_handle->GetURL().spec(); 243 details.url = navigation_handle->GetURL().spec();
244 details.process_id = -1; 244 details.process_id = -1;
245 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(navigation_handle); 245 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(navigation_handle);
246 details.error = (navigation_handle->GetNetErrorCode() != net::OK) 246 details.error = (navigation_handle->GetNetErrorCode() != net::OK)
247 ? net::ErrorToString(navigation_handle->GetNetErrorCode()) 247 ? net::ErrorToString(navigation_handle->GetNetErrorCode())
248 : net::ErrorToString(net::ERR_ABORTED); 248 : net::ErrorToString(net::ERR_ABORTED);
249 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); 249 details.time_stamp = MilliSecondsFromTime(base::Time::Now());
250 250
251 std::unique_ptr<Event> event( 251 content::BrowserContext* context =
252 new Event(events::WEB_NAVIGATION_ON_ERROR_OCCURRED, 252 navigation_handle->GetWebContents()->GetBrowserContext();
253 web_navigation::OnErrorOccurred::kEventName, 253 auto event = base::MakeUnique<Event>(
254 web_navigation::OnErrorOccurred::Create(details))); 254 events::WEB_NAVIGATION_ON_ERROR_OCCURRED,
255 DispatchEvent(navigation_handle->GetWebContents()->GetBrowserContext(), 255 web_navigation::OnErrorOccurred::kEventName,
256 std::move(event), navigation_handle->GetURL()); 256 web_navigation::OnErrorOccurred::Create(details), context);
257 DispatchEvent(context, std::move(event), navigation_handle->GetURL());
257 } 258 }
258 259
259 // Constructs and dispatches an onTabReplaced event. 260 // Constructs and dispatches an onTabReplaced event.
260 void DispatchOnTabReplaced( 261 void DispatchOnTabReplaced(
261 content::WebContents* old_web_contents, 262 content::WebContents* old_web_contents,
262 content::BrowserContext* browser_context, 263 content::BrowserContext* browser_context,
263 content::WebContents* new_web_contents) { 264 content::WebContents* new_web_contents) {
264 web_navigation::OnTabReplaced::Details details; 265 web_navigation::OnTabReplaced::Details details;
265 details.replaced_tab_id = ExtensionTabUtil::GetTabId(old_web_contents); 266 details.replaced_tab_id = ExtensionTabUtil::GetTabId(old_web_contents);
266 details.tab_id = ExtensionTabUtil::GetTabId(new_web_contents); 267 details.tab_id = ExtensionTabUtil::GetTabId(new_web_contents);
267 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); 268 details.time_stamp = MilliSecondsFromTime(base::Time::Now());
268 269
269 std::unique_ptr<Event> event( 270 auto event = base::MakeUnique<Event>(
270 new Event(events::WEB_NAVIGATION_ON_TAB_REPLACED, 271 events::WEB_NAVIGATION_ON_TAB_REPLACED,
271 web_navigation::OnTabReplaced::kEventName, 272 web_navigation::OnTabReplaced::kEventName,
272 web_navigation::OnTabReplaced::Create(details))); 273 web_navigation::OnTabReplaced::Create(details), browser_context);
273 DispatchEvent(browser_context, std::move(event), GURL()); 274 DispatchEvent(browser_context, std::move(event), GURL());
274 } 275 }
275 276
276 } // namespace web_navigation_api_helpers 277 } // namespace web_navigation_api_helpers
277 278
278 } // namespace extensions 279 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698