Chromium Code Reviews| OLD | NEW |
|---|---|
| 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> |
| 11 | 11 |
| 12 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h" | |
| 16 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api_consta nts.h" | 17 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api_consta nts.h" |
| 17 #include "chrome/browser/extensions/extension_tab_util.h" | 18 #include "chrome/browser/extensions/extension_tab_util.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/common/extensions/api/web_navigation.h" | 20 #include "chrome/common/extensions/api/web_navigation.h" |
| 20 #include "content/public/browser/navigation_handle.h" | 21 #include "content/public/browser/navigation_handle.h" |
| 21 #include "content/public/browser/render_frame_host.h" | 22 #include "content/public/browser/render_frame_host.h" |
| 22 #include "content/public/browser/render_process_host.h" | 23 #include "content/public/browser/render_process_host.h" |
| 23 #include "content/public/browser/render_view_host.h" | 24 #include "content/public/browser/render_view_host.h" |
| 24 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
| 25 #include "content/public/common/url_constants.h" | 26 #include "content/public/common/url_constants.h" |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 54 EventRouter* event_router = EventRouter::Get(profile); | 55 EventRouter* event_router = EventRouter::Get(profile); |
| 55 if (profile && event_router) { | 56 if (profile && event_router) { |
| 56 event->restrict_to_browser_context = profile; | 57 event->restrict_to_browser_context = profile; |
| 57 event->filter_info = info; | 58 event->filter_info = info; |
| 58 event_router->BroadcastEvent(std::move(event)); | 59 event_router->BroadcastEvent(std::move(event)); |
| 59 } | 60 } |
| 60 } | 61 } |
| 61 | 62 |
| 62 } // namespace | 63 } // namespace |
| 63 | 64 |
| 64 // Constructs and dispatches an onBeforeNavigate event. | 65 // Constructs an onBeforeNavigate event. |
| 65 void DispatchOnBeforeNavigate(content::NavigationHandle* navigation_handle) { | 66 std::unique_ptr<Event> CreateOnBeforeNavigateEvent( |
| 67 content::NavigationHandle* navigation_handle) { | |
| 66 GURL url(navigation_handle->GetURL()); | 68 GURL url(navigation_handle->GetURL()); |
| 67 if (navigation_handle->IsSrcdoc()) | 69 if (navigation_handle->IsSrcdoc()) |
| 68 url = GURL(content::kAboutSrcDocURL); | 70 url = GURL(content::kAboutSrcDocURL); |
| 69 | 71 |
| 70 web_navigation::OnBeforeNavigate::Details details; | 72 web_navigation::OnBeforeNavigate::Details details; |
| 71 details.tab_id = | 73 details.tab_id = |
| 72 ExtensionTabUtil::GetTabId(navigation_handle->GetWebContents()); | 74 ExtensionTabUtil::GetTabId(navigation_handle->GetWebContents()); |
| 73 details.url = url.spec(); | 75 details.url = url.spec(); |
| 74 details.process_id = -1; | 76 details.process_id = -1; |
| 75 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(navigation_handle); | 77 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(navigation_handle); |
| 76 details.parent_frame_id = | 78 details.parent_frame_id = |
| 77 ExtensionApiFrameIdMap::GetParentFrameId(navigation_handle); | 79 ExtensionApiFrameIdMap::GetParentFrameId(navigation_handle); |
| 78 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); | 80 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); |
| 79 | 81 |
| 80 std::unique_ptr<Event> event( | 82 std::unique_ptr<Event> event( |
| 81 new Event(events::WEB_NAVIGATION_ON_BEFORE_NAVIGATE, | 83 new Event(events::WEB_NAVIGATION_ON_BEFORE_NAVIGATE, |
| 82 web_navigation::OnBeforeNavigate::kEventName, | 84 web_navigation::OnBeforeNavigate::kEventName, |
| 83 web_navigation::OnBeforeNavigate::Create(details))); | 85 web_navigation::OnBeforeNavigate::Create(details))); |
| 84 DispatchEvent(navigation_handle->GetWebContents()->GetBrowserContext(), | 86 |
| 85 std::move(event), url); | 87 EventFilteringInfo info; |
| 88 info.SetURL(navigation_handle->GetURL()); | |
| 89 | |
| 90 Profile* profile = Profile::FromBrowserContext( | |
| 91 navigation_handle->GetWebContents()->GetBrowserContext()); | |
| 92 EventRouter* event_router = EventRouter::Get(profile); | |
|
Devlin
2016/12/02 20:17:11
Why do we need event router?
nasko
2016/12/02 21:40:45
Because of copy/paste :).
| |
| 93 if (profile && event_router) { | |
| 94 event->restrict_to_browser_context = profile; | |
| 95 event->filter_info = info; | |
| 96 } | |
| 97 | |
| 98 return event; | |
| 86 } | 99 } |
| 87 | 100 |
| 88 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated | 101 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated |
| 89 // event. | 102 // event. |
| 90 void DispatchOnCommitted(events::HistogramValue histogram_value, | 103 void DispatchOnCommitted(events::HistogramValue histogram_value, |
| 91 const std::string& event_name, | 104 const std::string& event_name, |
| 92 content::NavigationHandle* navigation_handle) { | 105 content::NavigationHandle* navigation_handle) { |
| 93 content::WebContents* web_contents = navigation_handle->GetWebContents(); | 106 content::WebContents* web_contents = navigation_handle->GetWebContents(); |
| 94 GURL url(navigation_handle->GetURL()); | 107 GURL url(navigation_handle->GetURL()); |
| 95 content::RenderFrameHost* frame_host = | 108 content::RenderFrameHost* frame_host = |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 196 ExtensionApiFrameIdMap::GetFrameId(source_frame_host); | 209 ExtensionApiFrameIdMap::GetFrameId(source_frame_host); |
| 197 details.url = target_url.possibly_invalid_spec(); | 210 details.url = target_url.possibly_invalid_spec(); |
| 198 details.tab_id = ExtensionTabUtil::GetTabId(target_web_contents); | 211 details.tab_id = ExtensionTabUtil::GetTabId(target_web_contents); |
| 199 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); | 212 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); |
| 200 | 213 |
| 201 std::unique_ptr<Event> event( | 214 std::unique_ptr<Event> event( |
| 202 new Event(events::WEB_NAVIGATION_ON_CREATED_NAVIGATION_TARGET, | 215 new Event(events::WEB_NAVIGATION_ON_CREATED_NAVIGATION_TARGET, |
| 203 web_navigation::OnCreatedNavigationTarget::kEventName, | 216 web_navigation::OnCreatedNavigationTarget::kEventName, |
| 204 web_navigation::OnCreatedNavigationTarget::Create(details))); | 217 web_navigation::OnCreatedNavigationTarget::Create(details))); |
| 205 DispatchEvent(browser_context, std::move(event), target_url); | 218 DispatchEvent(browser_context, std::move(event), target_url); |
| 219 | |
| 220 // If the target WebContents already received the onBeforeNavigate event, | |
| 221 // send it immediately after the onCreatedNavigationTarget above. | |
| 222 WebNavigationTabObserver* target_observer = | |
| 223 WebNavigationTabObserver::Get(target_web_contents); | |
| 224 target_observer->DispatchOnBeforeNavigate(); | |
| 206 } | 225 } |
| 207 | 226 |
| 208 // Constructs and dispatches an onErrorOccurred event. | 227 // Constructs and dispatches an onErrorOccurred event. |
| 209 void DispatchOnErrorOccurred(content::WebContents* web_contents, | 228 void DispatchOnErrorOccurred(content::WebContents* web_contents, |
| 210 content::RenderFrameHost* frame_host, | 229 content::RenderFrameHost* frame_host, |
| 211 const GURL& url, | 230 const GURL& url, |
| 212 int error_code) { | 231 int error_code) { |
| 213 web_navigation::OnErrorOccurred::Details details; | 232 web_navigation::OnErrorOccurred::Details details; |
| 214 details.tab_id = ExtensionTabUtil::GetTabId(web_contents); | 233 details.tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| 215 details.url = url.spec(); | 234 details.url = url.spec(); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 258 std::unique_ptr<Event> event( | 277 std::unique_ptr<Event> event( |
| 259 new Event(events::WEB_NAVIGATION_ON_TAB_REPLACED, | 278 new Event(events::WEB_NAVIGATION_ON_TAB_REPLACED, |
| 260 web_navigation::OnTabReplaced::kEventName, | 279 web_navigation::OnTabReplaced::kEventName, |
| 261 web_navigation::OnTabReplaced::Create(details))); | 280 web_navigation::OnTabReplaced::Create(details))); |
| 262 DispatchEvent(browser_context, std::move(event), GURL()); | 281 DispatchEvent(browser_context, std::move(event), GURL()); |
| 263 } | 282 } |
| 264 | 283 |
| 265 } // namespace web_navigation_api_helpers | 284 } // namespace web_navigation_api_helpers |
| 266 | 285 |
| 267 } // namespace extensions | 286 } // namespace extensions |
| OLD | NEW |