| 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 event->restrict_to_browser_context = |
| 91 navigation_handle->GetWebContents()->GetBrowserContext(); |
| 92 event->filter_info = info; |
| 93 |
| 94 return event; |
| 86 } | 95 } |
| 87 | 96 |
| 88 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated | 97 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated |
| 89 // event. | 98 // event. |
| 90 void DispatchOnCommitted(events::HistogramValue histogram_value, | 99 void DispatchOnCommitted(events::HistogramValue histogram_value, |
| 91 const std::string& event_name, | 100 const std::string& event_name, |
| 92 content::NavigationHandle* navigation_handle) { | 101 content::NavigationHandle* navigation_handle) { |
| 93 content::WebContents* web_contents = navigation_handle->GetWebContents(); | 102 content::WebContents* web_contents = navigation_handle->GetWebContents(); |
| 94 GURL url(navigation_handle->GetURL()); | 103 GURL url(navigation_handle->GetURL()); |
| 95 content::RenderFrameHost* frame_host = | 104 content::RenderFrameHost* frame_host = |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 ExtensionApiFrameIdMap::GetFrameId(source_frame_host); | 205 ExtensionApiFrameIdMap::GetFrameId(source_frame_host); |
| 197 details.url = target_url.possibly_invalid_spec(); | 206 details.url = target_url.possibly_invalid_spec(); |
| 198 details.tab_id = ExtensionTabUtil::GetTabId(target_web_contents); | 207 details.tab_id = ExtensionTabUtil::GetTabId(target_web_contents); |
| 199 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); | 208 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); |
| 200 | 209 |
| 201 std::unique_ptr<Event> event( | 210 std::unique_ptr<Event> event( |
| 202 new Event(events::WEB_NAVIGATION_ON_CREATED_NAVIGATION_TARGET, | 211 new Event(events::WEB_NAVIGATION_ON_CREATED_NAVIGATION_TARGET, |
| 203 web_navigation::OnCreatedNavigationTarget::kEventName, | 212 web_navigation::OnCreatedNavigationTarget::kEventName, |
| 204 web_navigation::OnCreatedNavigationTarget::Create(details))); | 213 web_navigation::OnCreatedNavigationTarget::Create(details))); |
| 205 DispatchEvent(browser_context, std::move(event), target_url); | 214 DispatchEvent(browser_context, std::move(event), target_url); |
| 215 |
| 216 // If the target WebContents already received the onBeforeNavigate event, |
| 217 // send it immediately after the onCreatedNavigationTarget above. |
| 218 WebNavigationTabObserver* target_observer = |
| 219 WebNavigationTabObserver::Get(target_web_contents); |
| 220 target_observer->DispatchCachedOnBeforeNavigate(); |
| 206 } | 221 } |
| 207 | 222 |
| 208 // Constructs and dispatches an onErrorOccurred event. | 223 // Constructs and dispatches an onErrorOccurred event. |
| 209 void DispatchOnErrorOccurred(content::WebContents* web_contents, | 224 void DispatchOnErrorOccurred(content::WebContents* web_contents, |
| 210 content::RenderFrameHost* frame_host, | 225 content::RenderFrameHost* frame_host, |
| 211 const GURL& url, | 226 const GURL& url, |
| 212 int error_code) { | 227 int error_code) { |
| 213 web_navigation::OnErrorOccurred::Details details; | 228 web_navigation::OnErrorOccurred::Details details; |
| 214 details.tab_id = ExtensionTabUtil::GetTabId(web_contents); | 229 details.tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| 215 details.url = url.spec(); | 230 details.url = url.spec(); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 std::unique_ptr<Event> event( | 273 std::unique_ptr<Event> event( |
| 259 new Event(events::WEB_NAVIGATION_ON_TAB_REPLACED, | 274 new Event(events::WEB_NAVIGATION_ON_TAB_REPLACED, |
| 260 web_navigation::OnTabReplaced::kEventName, | 275 web_navigation::OnTabReplaced::kEventName, |
| 261 web_navigation::OnTabReplaced::Create(details))); | 276 web_navigation::OnTabReplaced::Create(details))); |
| 262 DispatchEvent(browser_context, std::move(event), GURL()); | 277 DispatchEvent(browser_context, std::move(event), GURL()); |
| 263 } | 278 } |
| 264 | 279 |
| 265 } // namespace web_navigation_api_helpers | 280 } // namespace web_navigation_api_helpers |
| 266 | 281 |
| 267 } // namespace extensions | 282 } // namespace extensions |
| OLD | NEW |