| 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> |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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; | 58 DCHECK_EQ(profile, event->restrict_to_browser_context); |
| 59 event->filter_info = info; | 59 event->filter_info = info; |
| 60 event_router->BroadcastEvent(std::move(event)); | 60 event_router->BroadcastEvent(std::move(event)); |
| 61 } | 61 } |
| 62 } | 62 } |
| 63 | 63 |
| 64 } // namespace | 64 } // namespace |
| 65 | 65 |
| 66 // Constructs an onBeforeNavigate event. | 66 // Constructs an onBeforeNavigate event. |
| 67 std::unique_ptr<Event> CreateOnBeforeNavigateEvent( | 67 std::unique_ptr<Event> CreateOnBeforeNavigateEvent( |
| 68 content::NavigationHandle* navigation_handle) { | 68 content::NavigationHandle* navigation_handle) { |
| 69 GURL url(navigation_handle->GetURL()); | 69 GURL url(navigation_handle->GetURL()); |
| 70 | 70 |
| 71 web_navigation::OnBeforeNavigate::Details details; | 71 web_navigation::OnBeforeNavigate::Details details; |
| 72 details.tab_id = | 72 details.tab_id = |
| 73 ExtensionTabUtil::GetTabId(navigation_handle->GetWebContents()); | 73 ExtensionTabUtil::GetTabId(navigation_handle->GetWebContents()); |
| 74 details.url = url.spec(); | 74 details.url = url.spec(); |
| 75 details.process_id = -1; | 75 details.process_id = -1; |
| 76 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(navigation_handle); | 76 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(navigation_handle); |
| 77 details.parent_frame_id = | 77 details.parent_frame_id = |
| 78 ExtensionApiFrameIdMap::GetParentFrameId(navigation_handle); | 78 ExtensionApiFrameIdMap::GetParentFrameId(navigation_handle); |
| 79 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); | 79 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); |
| 80 | 80 |
| 81 std::unique_ptr<Event> event( | 81 auto event = base::MakeUnique<Event>( |
| 82 new Event(events::WEB_NAVIGATION_ON_BEFORE_NAVIGATE, | 82 events::WEB_NAVIGATION_ON_BEFORE_NAVIGATE, |
| 83 web_navigation::OnBeforeNavigate::kEventName, | 83 web_navigation::OnBeforeNavigate::kEventName, |
| 84 web_navigation::OnBeforeNavigate::Create(details))); | 84 web_navigation::OnBeforeNavigate::Create(details), |
| 85 navigation_handle->GetWebContents()->GetBrowserContext()); |
| 85 | 86 |
| 86 EventFilteringInfo info; | 87 EventFilteringInfo info; |
| 87 info.SetURL(navigation_handle->GetURL()); | 88 info.SetURL(navigation_handle->GetURL()); |
| 88 | |
| 89 event->restrict_to_browser_context = | |
| 90 navigation_handle->GetWebContents()->GetBrowserContext(); | |
| 91 event->filter_info = info; | 89 event->filter_info = info; |
| 92 | 90 |
| 93 return event; | 91 return event; |
| 94 } | 92 } |
| 95 | 93 |
| 96 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated | 94 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated |
| 97 // event. | 95 // event. |
| 98 void DispatchOnCommitted(events::HistogramValue histogram_value, | 96 void DispatchOnCommitted(events::HistogramValue histogram_value, |
| 99 const std::string& event_name, | 97 const std::string& event_name, |
| 100 content::NavigationHandle* navigation_handle) { | 98 content::NavigationHandle* navigation_handle) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 131 if (transition_type & ui::PAGE_TRANSITION_SERVER_REDIRECT) | 129 if (transition_type & ui::PAGE_TRANSITION_SERVER_REDIRECT) |
| 132 qualifiers->AppendString("server_redirect"); | 130 qualifiers->AppendString("server_redirect"); |
| 133 if (transition_type & ui::PAGE_TRANSITION_FORWARD_BACK) | 131 if (transition_type & ui::PAGE_TRANSITION_FORWARD_BACK) |
| 134 qualifiers->AppendString("forward_back"); | 132 qualifiers->AppendString("forward_back"); |
| 135 if (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR) | 133 if (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR) |
| 136 qualifiers->AppendString("from_address_bar"); | 134 qualifiers->AppendString("from_address_bar"); |
| 137 dict->Set(keys::kTransitionQualifiersKey, std::move(qualifiers)); | 135 dict->Set(keys::kTransitionQualifiersKey, std::move(qualifiers)); |
| 138 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 136 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 139 args->Append(std::move(dict)); | 137 args->Append(std::move(dict)); |
| 140 | 138 |
| 141 std::unique_ptr<Event> event( | 139 content::BrowserContext* browser_context = |
| 142 new Event(histogram_value, event_name, std::move(args))); | 140 navigation_handle->GetWebContents()->GetBrowserContext(); |
| 143 DispatchEvent(navigation_handle->GetWebContents()->GetBrowserContext(), | 141 auto event = base::MakeUnique<Event>(histogram_value, event_name, |
| 144 std::move(event), url); | 142 std::move(args), browser_context); |
| 143 DispatchEvent(browser_context, std::move(event), url); |
| 145 } | 144 } |
| 146 | 145 |
| 147 // Constructs and dispatches an onDOMContentLoaded event. | 146 // Constructs and dispatches an onDOMContentLoaded event. |
| 148 void DispatchOnDOMContentLoaded(content::WebContents* web_contents, | 147 void DispatchOnDOMContentLoaded(content::WebContents* web_contents, |
| 149 content::RenderFrameHost* frame_host, | 148 content::RenderFrameHost* frame_host, |
| 150 const GURL& url) { | 149 const GURL& url) { |
| 151 web_navigation::OnDOMContentLoaded::Details details; | 150 web_navigation::OnDOMContentLoaded::Details details; |
| 152 details.tab_id = ExtensionTabUtil::GetTabId(web_contents); | 151 details.tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| 153 details.url = url.spec(); | 152 details.url = url.spec(); |
| 154 details.process_id = frame_host->GetProcess()->GetID(); | 153 details.process_id = frame_host->GetProcess()->GetID(); |
| 155 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(frame_host); | 154 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(frame_host); |
| 156 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); | 155 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); |
| 157 | 156 |
| 158 std::unique_ptr<Event> event( | 157 content::BrowserContext* browser_context = web_contents->GetBrowserContext(); |
| 159 new Event(events::WEB_NAVIGATION_ON_DOM_CONTENT_LOADED, | 158 auto event = base::MakeUnique<Event>( |
| 160 web_navigation::OnDOMContentLoaded::kEventName, | 159 events::WEB_NAVIGATION_ON_DOM_CONTENT_LOADED, |
| 161 web_navigation::OnDOMContentLoaded::Create(details))); | 160 web_navigation::OnDOMContentLoaded::kEventName, |
| 162 DispatchEvent(web_contents->GetBrowserContext(), std::move(event), url); | 161 web_navigation::OnDOMContentLoaded::Create(details), browser_context); |
| 162 DispatchEvent(browser_context, std::move(event), url); |
| 163 } | 163 } |
| 164 | 164 |
| 165 // Constructs and dispatches an onCompleted event. | 165 // Constructs and dispatches an onCompleted event. |
| 166 void DispatchOnCompleted(content::WebContents* web_contents, | 166 void DispatchOnCompleted(content::WebContents* web_contents, |
| 167 content::RenderFrameHost* frame_host, | 167 content::RenderFrameHost* frame_host, |
| 168 const GURL& url) { | 168 const GURL& url) { |
| 169 web_navigation::OnCompleted::Details details; | 169 web_navigation::OnCompleted::Details details; |
| 170 details.tab_id = ExtensionTabUtil::GetTabId(web_contents); | 170 details.tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| 171 details.url = url.spec(); | 171 details.url = url.spec(); |
| 172 details.process_id = frame_host->GetProcess()->GetID(); | 172 details.process_id = frame_host->GetProcess()->GetID(); |
| 173 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(frame_host); | 173 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(frame_host); |
| 174 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); | 174 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); |
| 175 | 175 |
| 176 std::unique_ptr<Event> event( | 176 content::BrowserContext* browser_context = web_contents->GetBrowserContext(); |
| 177 new Event(events::WEB_NAVIGATION_ON_COMPLETED, | 177 auto event = base::MakeUnique<Event>( |
| 178 web_navigation::OnCompleted::kEventName, | 178 events::WEB_NAVIGATION_ON_COMPLETED, |
| 179 web_navigation::OnCompleted::Create(details))); | 179 web_navigation::OnCompleted::kEventName, |
| 180 DispatchEvent(web_contents->GetBrowserContext(), std::move(event), url); | 180 web_navigation::OnCompleted::Create(details), browser_context); |
| 181 DispatchEvent(browser_context, std::move(event), url); |
| 181 } | 182 } |
| 182 | 183 |
| 183 // Constructs and dispatches an onCreatedNavigationTarget event. | 184 // Constructs and dispatches an onCreatedNavigationTarget event. |
| 184 void DispatchOnCreatedNavigationTarget( | 185 void DispatchOnCreatedNavigationTarget( |
| 185 content::WebContents* web_contents, | 186 content::WebContents* web_contents, |
| 186 content::BrowserContext* browser_context, | 187 content::BrowserContext* browser_context, |
| 187 content::RenderFrameHost* source_frame_host, | 188 content::RenderFrameHost* source_frame_host, |
| 188 content::WebContents* target_web_contents, | 189 content::WebContents* target_web_contents, |
| 189 const GURL& target_url) { | 190 const GURL& target_url) { |
| 190 // Check that the tab is already inserted into a tab strip model. This code | 191 // Check that the tab is already inserted into a tab strip model. This code |
| 191 // path is exercised by ExtensionApiTest.WebNavigationRequestOpenTab. | 192 // path is exercised by ExtensionApiTest.WebNavigationRequestOpenTab. |
| 192 DCHECK(ExtensionTabUtil::GetTabById( | 193 DCHECK(ExtensionTabUtil::GetTabById( |
| 193 ExtensionTabUtil::GetTabId(target_web_contents), | 194 ExtensionTabUtil::GetTabId(target_web_contents), |
| 194 Profile::FromBrowserContext(target_web_contents->GetBrowserContext()), | 195 Profile::FromBrowserContext(target_web_contents->GetBrowserContext()), |
| 195 false, NULL, NULL, NULL, NULL)); | 196 false, NULL, NULL, NULL, NULL)); |
| 196 | 197 |
| 197 web_navigation::OnCreatedNavigationTarget::Details details; | 198 web_navigation::OnCreatedNavigationTarget::Details details; |
| 198 details.source_tab_id = ExtensionTabUtil::GetTabId(web_contents); | 199 details.source_tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| 199 details.source_process_id = source_frame_host->GetProcess()->GetID(); | 200 details.source_process_id = source_frame_host->GetProcess()->GetID(); |
| 200 details.source_frame_id = | 201 details.source_frame_id = |
| 201 ExtensionApiFrameIdMap::GetFrameId(source_frame_host); | 202 ExtensionApiFrameIdMap::GetFrameId(source_frame_host); |
| 202 details.url = target_url.possibly_invalid_spec(); | 203 details.url = target_url.possibly_invalid_spec(); |
| 203 details.tab_id = ExtensionTabUtil::GetTabId(target_web_contents); | 204 details.tab_id = ExtensionTabUtil::GetTabId(target_web_contents); |
| 204 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); | 205 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); |
| 205 | 206 |
| 206 std::unique_ptr<Event> event( | 207 auto event = base::MakeUnique<Event>( |
| 207 new Event(events::WEB_NAVIGATION_ON_CREATED_NAVIGATION_TARGET, | 208 events::WEB_NAVIGATION_ON_CREATED_NAVIGATION_TARGET, |
| 208 web_navigation::OnCreatedNavigationTarget::kEventName, | 209 web_navigation::OnCreatedNavigationTarget::kEventName, |
| 209 web_navigation::OnCreatedNavigationTarget::Create(details))); | 210 web_navigation::OnCreatedNavigationTarget::Create(details), |
| 211 browser_context); |
| 210 DispatchEvent(browser_context, std::move(event), target_url); | 212 DispatchEvent(browser_context, std::move(event), target_url); |
| 211 | 213 |
| 212 // If the target WebContents already received the onBeforeNavigate event, | 214 // If the target WebContents already received the onBeforeNavigate event, |
| 213 // send it immediately after the onCreatedNavigationTarget above. | 215 // send it immediately after the onCreatedNavigationTarget above. |
| 214 WebNavigationTabObserver* target_observer = | 216 WebNavigationTabObserver* target_observer = |
| 215 WebNavigationTabObserver::Get(target_web_contents); | 217 WebNavigationTabObserver::Get(target_web_contents); |
| 216 target_observer->DispatchCachedOnBeforeNavigate(); | 218 target_observer->DispatchCachedOnBeforeNavigate(); |
| 217 } | 219 } |
| 218 | 220 |
| 219 // Constructs and dispatches an onErrorOccurred event. | 221 // Constructs and dispatches an onErrorOccurred event. |
| 220 void DispatchOnErrorOccurred(content::WebContents* web_contents, | 222 void DispatchOnErrorOccurred(content::WebContents* web_contents, |
| 221 content::RenderFrameHost* frame_host, | 223 content::RenderFrameHost* frame_host, |
| 222 const GURL& url, | 224 const GURL& url, |
| 223 int error_code) { | 225 int error_code) { |
| 224 web_navigation::OnErrorOccurred::Details details; | 226 web_navigation::OnErrorOccurred::Details details; |
| 225 details.tab_id = ExtensionTabUtil::GetTabId(web_contents); | 227 details.tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| 226 details.url = url.spec(); | 228 details.url = url.spec(); |
| 227 details.process_id = frame_host->GetProcess()->GetID(); | 229 details.process_id = frame_host->GetProcess()->GetID(); |
| 228 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(frame_host); | 230 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(frame_host); |
| 229 details.error = net::ErrorToString(error_code); | 231 details.error = net::ErrorToString(error_code); |
| 230 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); | 232 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); |
| 231 | 233 |
| 232 std::unique_ptr<Event> event( | 234 content::BrowserContext* browser_context = web_contents->GetBrowserContext(); |
| 233 new Event(events::WEB_NAVIGATION_ON_ERROR_OCCURRED, | 235 auto event = |
| 234 web_navigation::OnErrorOccurred::kEventName, | 236 base::MakeUnique<Event>(events::WEB_NAVIGATION_ON_ERROR_OCCURRED, |
| 235 web_navigation::OnErrorOccurred::Create(details))); | 237 web_navigation::OnErrorOccurred::kEventName, |
| 236 DispatchEvent(web_contents->GetBrowserContext(), std::move(event), url); | 238 web_navigation::OnErrorOccurred::Create(details), |
| 239 web_contents->GetBrowserContext()); |
| 240 DispatchEvent(browser_context, std::move(event), url); |
| 237 } | 241 } |
| 238 | 242 |
| 239 void DispatchOnErrorOccurred(content::NavigationHandle* navigation_handle) { | 243 void DispatchOnErrorOccurred(content::NavigationHandle* navigation_handle) { |
| 240 web_navigation::OnErrorOccurred::Details details; | 244 web_navigation::OnErrorOccurred::Details details; |
| 241 details.tab_id = | 245 details.tab_id = |
| 242 ExtensionTabUtil::GetTabId(navigation_handle->GetWebContents()); | 246 ExtensionTabUtil::GetTabId(navigation_handle->GetWebContents()); |
| 243 details.url = navigation_handle->GetURL().spec(); | 247 details.url = navigation_handle->GetURL().spec(); |
| 244 details.process_id = -1; | 248 details.process_id = -1; |
| 245 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(navigation_handle); | 249 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(navigation_handle); |
| 246 details.error = (navigation_handle->GetNetErrorCode() != net::OK) | 250 details.error = (navigation_handle->GetNetErrorCode() != net::OK) |
| 247 ? net::ErrorToString(navigation_handle->GetNetErrorCode()) | 251 ? net::ErrorToString(navigation_handle->GetNetErrorCode()) |
| 248 : net::ErrorToString(net::ERR_ABORTED); | 252 : net::ErrorToString(net::ERR_ABORTED); |
| 249 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); | 253 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); |
| 250 | 254 |
| 251 std::unique_ptr<Event> event( | 255 content::BrowserContext* browser_context = |
| 252 new Event(events::WEB_NAVIGATION_ON_ERROR_OCCURRED, | 256 navigation_handle->GetWebContents()->GetBrowserContext(); |
| 253 web_navigation::OnErrorOccurred::kEventName, | 257 auto event = base::MakeUnique<Event>( |
| 254 web_navigation::OnErrorOccurred::Create(details))); | 258 events::WEB_NAVIGATION_ON_ERROR_OCCURRED, |
| 255 DispatchEvent(navigation_handle->GetWebContents()->GetBrowserContext(), | 259 web_navigation::OnErrorOccurred::kEventName, |
| 256 std::move(event), navigation_handle->GetURL()); | 260 web_navigation::OnErrorOccurred::Create(details), browser_context); |
| 261 DispatchEvent(browser_context, std::move(event), navigation_handle->GetURL()); |
| 257 } | 262 } |
| 258 | 263 |
| 259 // Constructs and dispatches an onTabReplaced event. | 264 // Constructs and dispatches an onTabReplaced event. |
| 260 void DispatchOnTabReplaced( | 265 void DispatchOnTabReplaced( |
| 261 content::WebContents* old_web_contents, | 266 content::WebContents* old_web_contents, |
| 262 content::BrowserContext* browser_context, | 267 content::BrowserContext* browser_context, |
| 263 content::WebContents* new_web_contents) { | 268 content::WebContents* new_web_contents) { |
| 264 web_navigation::OnTabReplaced::Details details; | 269 web_navigation::OnTabReplaced::Details details; |
| 265 details.replaced_tab_id = ExtensionTabUtil::GetTabId(old_web_contents); | 270 details.replaced_tab_id = ExtensionTabUtil::GetTabId(old_web_contents); |
| 266 details.tab_id = ExtensionTabUtil::GetTabId(new_web_contents); | 271 details.tab_id = ExtensionTabUtil::GetTabId(new_web_contents); |
| 267 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); | 272 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); |
| 268 | 273 |
| 269 std::unique_ptr<Event> event( | 274 auto event = base::MakeUnique<Event>( |
| 270 new Event(events::WEB_NAVIGATION_ON_TAB_REPLACED, | 275 events::WEB_NAVIGATION_ON_TAB_REPLACED, |
| 271 web_navigation::OnTabReplaced::kEventName, | 276 web_navigation::OnTabReplaced::kEventName, |
| 272 web_navigation::OnTabReplaced::Create(details))); | 277 web_navigation::OnTabReplaced::Create(details), browser_context); |
| 273 DispatchEvent(browser_context, std::move(event), GURL()); | 278 DispatchEvent(browser_context, std::move(event), GURL()); |
| 274 } | 279 } |
| 275 | 280 |
| 276 } // namespace web_navigation_api_helpers | 281 } // namespace web_navigation_api_helpers |
| 277 | 282 |
| 278 } // namespace extensions | 283 } // namespace extensions |
| OLD | NEW |