| 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 <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 | 37 |
| 38 namespace { | 38 namespace { |
| 39 | 39 |
| 40 // Returns |time| as milliseconds since the epoch. | 40 // Returns |time| as milliseconds since the epoch. |
| 41 double MilliSecondsFromTime(const base::Time& time) { | 41 double MilliSecondsFromTime(const base::Time& time) { |
| 42 return 1000 * time.ToDoubleT(); | 42 return 1000 * time.ToDoubleT(); |
| 43 } | 43 } |
| 44 | 44 |
| 45 // Dispatches events to the extension message service. | 45 // Dispatches events to the extension message service. |
| 46 void DispatchEvent(content::BrowserContext* browser_context, | 46 void DispatchEvent(content::BrowserContext* browser_context, |
| 47 scoped_ptr<Event> event, | 47 std::unique_ptr<Event> event, |
| 48 const GURL& url) { | 48 const GURL& url) { |
| 49 EventFilteringInfo info; | 49 EventFilteringInfo info; |
| 50 info.SetURL(url); | 50 info.SetURL(url); |
| 51 | 51 |
| 52 Profile* profile = Profile::FromBrowserContext(browser_context); | 52 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 53 EventRouter* event_router = EventRouter::Get(profile); | 53 EventRouter* event_router = EventRouter::Get(profile); |
| 54 if (profile && event_router) { | 54 if (profile && event_router) { |
| 55 event->restrict_to_browser_context = profile; | 55 event->restrict_to_browser_context = profile; |
| 56 event->filter_info = info; | 56 event->filter_info = info; |
| 57 event_router->BroadcastEvent(std::move(event)); | 57 event_router->BroadcastEvent(std::move(event)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 69 web_navigation::OnBeforeNavigate::Details details; | 69 web_navigation::OnBeforeNavigate::Details details; |
| 70 details.tab_id = | 70 details.tab_id = |
| 71 ExtensionTabUtil::GetTabId(navigation_handle->GetWebContents()); | 71 ExtensionTabUtil::GetTabId(navigation_handle->GetWebContents()); |
| 72 details.url = url.spec(); | 72 details.url = url.spec(); |
| 73 details.process_id = -1; | 73 details.process_id = -1; |
| 74 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(navigation_handle); | 74 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(navigation_handle); |
| 75 details.parent_frame_id = | 75 details.parent_frame_id = |
| 76 ExtensionApiFrameIdMap::GetParentFrameId(navigation_handle); | 76 ExtensionApiFrameIdMap::GetParentFrameId(navigation_handle); |
| 77 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); | 77 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); |
| 78 | 78 |
| 79 scoped_ptr<Event> event( | 79 std::unique_ptr<Event> event( |
| 80 new Event(events::WEB_NAVIGATION_ON_BEFORE_NAVIGATE, | 80 new Event(events::WEB_NAVIGATION_ON_BEFORE_NAVIGATE, |
| 81 web_navigation::OnBeforeNavigate::kEventName, | 81 web_navigation::OnBeforeNavigate::kEventName, |
| 82 web_navigation::OnBeforeNavigate::Create(details))); | 82 web_navigation::OnBeforeNavigate::Create(details))); |
| 83 DispatchEvent(navigation_handle->GetWebContents()->GetBrowserContext(), | 83 DispatchEvent(navigation_handle->GetWebContents()->GetBrowserContext(), |
| 84 std::move(event), url); | 84 std::move(event), url); |
| 85 } | 85 } |
| 86 | 86 |
| 87 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated | 87 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated |
| 88 // event. | 88 // event. |
| 89 void DispatchOnCommitted(events::HistogramValue histogram_value, | 89 void DispatchOnCommitted(events::HistogramValue histogram_value, |
| 90 const std::string& event_name, | 90 const std::string& event_name, |
| 91 content::NavigationHandle* navigation_handle) { | 91 content::NavigationHandle* navigation_handle) { |
| 92 content::WebContents* web_contents = navigation_handle->GetWebContents(); | 92 content::WebContents* web_contents = navigation_handle->GetWebContents(); |
| 93 GURL url(navigation_handle->GetURL()); | 93 GURL url(navigation_handle->GetURL()); |
| 94 content::RenderFrameHost* frame_host = | 94 content::RenderFrameHost* frame_host = |
| 95 navigation_handle->GetRenderFrameHost(); | 95 navigation_handle->GetRenderFrameHost(); |
| 96 ui::PageTransition transition_type = navigation_handle->GetPageTransition(); | 96 ui::PageTransition transition_type = navigation_handle->GetPageTransition(); |
| 97 | 97 |
| 98 if (navigation_handle->IsSrcdoc()) | 98 if (navigation_handle->IsSrcdoc()) |
| 99 url = GURL(content::kAboutSrcDocURL); | 99 url = GURL(content::kAboutSrcDocURL); |
| 100 | 100 |
| 101 scoped_ptr<base::ListValue> args(new base::ListValue()); | 101 std::unique_ptr<base::ListValue> args(new base::ListValue()); |
| 102 base::DictionaryValue* dict = new base::DictionaryValue(); | 102 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 103 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); | 103 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); |
| 104 dict->SetString(keys::kUrlKey, url.spec()); | 104 dict->SetString(keys::kUrlKey, url.spec()); |
| 105 dict->SetInteger(keys::kProcessIdKey, frame_host->GetProcess()->GetID()); | 105 dict->SetInteger(keys::kProcessIdKey, frame_host->GetProcess()->GetID()); |
| 106 dict->SetInteger(keys::kFrameIdKey, | 106 dict->SetInteger(keys::kFrameIdKey, |
| 107 ExtensionApiFrameIdMap::GetFrameId(frame_host)); | 107 ExtensionApiFrameIdMap::GetFrameId(frame_host)); |
| 108 | 108 |
| 109 if (navigation_handle->WasServerRedirect()) { | 109 if (navigation_handle->WasServerRedirect()) { |
| 110 transition_type = ui::PageTransitionFromInt( | 110 transition_type = ui::PageTransitionFromInt( |
| 111 transition_type | ui::PAGE_TRANSITION_SERVER_REDIRECT); | 111 transition_type | ui::PAGE_TRANSITION_SERVER_REDIRECT); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 125 if (transition_type & ui::PAGE_TRANSITION_SERVER_REDIRECT) | 125 if (transition_type & ui::PAGE_TRANSITION_SERVER_REDIRECT) |
| 126 qualifiers->Append(new base::StringValue("server_redirect")); | 126 qualifiers->Append(new base::StringValue("server_redirect")); |
| 127 if (transition_type & ui::PAGE_TRANSITION_FORWARD_BACK) | 127 if (transition_type & ui::PAGE_TRANSITION_FORWARD_BACK) |
| 128 qualifiers->Append(new base::StringValue("forward_back")); | 128 qualifiers->Append(new base::StringValue("forward_back")); |
| 129 if (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR) | 129 if (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR) |
| 130 qualifiers->Append(new base::StringValue("from_address_bar")); | 130 qualifiers->Append(new base::StringValue("from_address_bar")); |
| 131 dict->Set(keys::kTransitionQualifiersKey, qualifiers); | 131 dict->Set(keys::kTransitionQualifiersKey, qualifiers); |
| 132 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 132 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 133 args->Append(dict); | 133 args->Append(dict); |
| 134 | 134 |
| 135 scoped_ptr<Event> event( | 135 std::unique_ptr<Event> event( |
| 136 new Event(histogram_value, event_name, std::move(args))); | 136 new Event(histogram_value, event_name, std::move(args))); |
| 137 DispatchEvent(navigation_handle->GetWebContents()->GetBrowserContext(), | 137 DispatchEvent(navigation_handle->GetWebContents()->GetBrowserContext(), |
| 138 std::move(event), url); | 138 std::move(event), url); |
| 139 } | 139 } |
| 140 | 140 |
| 141 // Constructs and dispatches an onDOMContentLoaded event. | 141 // Constructs and dispatches an onDOMContentLoaded event. |
| 142 void DispatchOnDOMContentLoaded(content::WebContents* web_contents, | 142 void DispatchOnDOMContentLoaded(content::WebContents* web_contents, |
| 143 content::RenderFrameHost* frame_host, | 143 content::RenderFrameHost* frame_host, |
| 144 const GURL& url) { | 144 const GURL& url) { |
| 145 web_navigation::OnDOMContentLoaded::Details details; | 145 web_navigation::OnDOMContentLoaded::Details details; |
| 146 details.tab_id = ExtensionTabUtil::GetTabId(web_contents); | 146 details.tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| 147 details.url = url.spec(); | 147 details.url = url.spec(); |
| 148 details.process_id = frame_host->GetProcess()->GetID(); | 148 details.process_id = frame_host->GetProcess()->GetID(); |
| 149 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(frame_host); | 149 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(frame_host); |
| 150 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); | 150 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); |
| 151 | 151 |
| 152 scoped_ptr<Event> event( | 152 std::unique_ptr<Event> event( |
| 153 new Event(events::WEB_NAVIGATION_ON_DOM_CONTENT_LOADED, | 153 new Event(events::WEB_NAVIGATION_ON_DOM_CONTENT_LOADED, |
| 154 web_navigation::OnDOMContentLoaded::kEventName, | 154 web_navigation::OnDOMContentLoaded::kEventName, |
| 155 web_navigation::OnDOMContentLoaded::Create(details))); | 155 web_navigation::OnDOMContentLoaded::Create(details))); |
| 156 DispatchEvent(web_contents->GetBrowserContext(), std::move(event), url); | 156 DispatchEvent(web_contents->GetBrowserContext(), std::move(event), url); |
| 157 } | 157 } |
| 158 | 158 |
| 159 // Constructs and dispatches an onCompleted event. | 159 // Constructs and dispatches an onCompleted event. |
| 160 void DispatchOnCompleted(content::WebContents* web_contents, | 160 void DispatchOnCompleted(content::WebContents* web_contents, |
| 161 content::RenderFrameHost* frame_host, | 161 content::RenderFrameHost* frame_host, |
| 162 const GURL& url) { | 162 const GURL& url) { |
| 163 web_navigation::OnCompleted::Details details; | 163 web_navigation::OnCompleted::Details details; |
| 164 details.tab_id = ExtensionTabUtil::GetTabId(web_contents); | 164 details.tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| 165 details.url = url.spec(); | 165 details.url = url.spec(); |
| 166 details.process_id = frame_host->GetProcess()->GetID(); | 166 details.process_id = frame_host->GetProcess()->GetID(); |
| 167 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(frame_host); | 167 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(frame_host); |
| 168 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); | 168 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); |
| 169 | 169 |
| 170 scoped_ptr<Event> event( | 170 std::unique_ptr<Event> event( |
| 171 new Event(events::WEB_NAVIGATION_ON_COMPLETED, | 171 new Event(events::WEB_NAVIGATION_ON_COMPLETED, |
| 172 web_navigation::OnCompleted::kEventName, | 172 web_navigation::OnCompleted::kEventName, |
| 173 web_navigation::OnCompleted::Create(details))); | 173 web_navigation::OnCompleted::Create(details))); |
| 174 DispatchEvent(web_contents->GetBrowserContext(), std::move(event), url); | 174 DispatchEvent(web_contents->GetBrowserContext(), std::move(event), url); |
| 175 } | 175 } |
| 176 | 176 |
| 177 // Constructs and dispatches an onCreatedNavigationTarget event. | 177 // Constructs and dispatches an onCreatedNavigationTarget event. |
| 178 void DispatchOnCreatedNavigationTarget( | 178 void DispatchOnCreatedNavigationTarget( |
| 179 content::WebContents* web_contents, | 179 content::WebContents* web_contents, |
| 180 content::BrowserContext* browser_context, | 180 content::BrowserContext* browser_context, |
| 181 content::RenderFrameHost* source_frame_host, | 181 content::RenderFrameHost* source_frame_host, |
| 182 content::WebContents* target_web_contents, | 182 content::WebContents* target_web_contents, |
| 183 const GURL& target_url) { | 183 const GURL& target_url) { |
| 184 // Check that the tab is already inserted into a tab strip model. This code | 184 // Check that the tab is already inserted into a tab strip model. This code |
| 185 // path is exercised by ExtensionApiTest.WebNavigationRequestOpenTab. | 185 // path is exercised by ExtensionApiTest.WebNavigationRequestOpenTab. |
| 186 DCHECK(ExtensionTabUtil::GetTabById( | 186 DCHECK(ExtensionTabUtil::GetTabById( |
| 187 ExtensionTabUtil::GetTabId(target_web_contents), | 187 ExtensionTabUtil::GetTabId(target_web_contents), |
| 188 Profile::FromBrowserContext(target_web_contents->GetBrowserContext()), | 188 Profile::FromBrowserContext(target_web_contents->GetBrowserContext()), |
| 189 false, NULL, NULL, NULL, NULL)); | 189 false, NULL, NULL, NULL, NULL)); |
| 190 | 190 |
| 191 web_navigation::OnCreatedNavigationTarget::Details details; | 191 web_navigation::OnCreatedNavigationTarget::Details details; |
| 192 details.source_tab_id = ExtensionTabUtil::GetTabId(web_contents); | 192 details.source_tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| 193 details.source_process_id = source_frame_host->GetProcess()->GetID(); | 193 details.source_process_id = source_frame_host->GetProcess()->GetID(); |
| 194 details.source_frame_id = | 194 details.source_frame_id = |
| 195 ExtensionApiFrameIdMap::GetFrameId(source_frame_host); | 195 ExtensionApiFrameIdMap::GetFrameId(source_frame_host); |
| 196 details.url = target_url.possibly_invalid_spec(); | 196 details.url = target_url.possibly_invalid_spec(); |
| 197 details.tab_id = ExtensionTabUtil::GetTabId(target_web_contents); | 197 details.tab_id = ExtensionTabUtil::GetTabId(target_web_contents); |
| 198 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); | 198 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); |
| 199 | 199 |
| 200 scoped_ptr<Event> event( | 200 std::unique_ptr<Event> event( |
| 201 new Event(events::WEB_NAVIGATION_ON_CREATED_NAVIGATION_TARGET, | 201 new Event(events::WEB_NAVIGATION_ON_CREATED_NAVIGATION_TARGET, |
| 202 web_navigation::OnCreatedNavigationTarget::kEventName, | 202 web_navigation::OnCreatedNavigationTarget::kEventName, |
| 203 web_navigation::OnCreatedNavigationTarget::Create(details))); | 203 web_navigation::OnCreatedNavigationTarget::Create(details))); |
| 204 DispatchEvent(browser_context, std::move(event), target_url); | 204 DispatchEvent(browser_context, std::move(event), target_url); |
| 205 } | 205 } |
| 206 | 206 |
| 207 // Constructs and dispatches an onErrorOccurred event. | 207 // Constructs and dispatches an onErrorOccurred event. |
| 208 void DispatchOnErrorOccurred(content::WebContents* web_contents, | 208 void DispatchOnErrorOccurred(content::WebContents* web_contents, |
| 209 content::RenderFrameHost* frame_host, | 209 content::RenderFrameHost* frame_host, |
| 210 const GURL& url, | 210 const GURL& url, |
| 211 int error_code) { | 211 int error_code) { |
| 212 web_navigation::OnErrorOccurred::Details details; | 212 web_navigation::OnErrorOccurred::Details details; |
| 213 details.tab_id = ExtensionTabUtil::GetTabId(web_contents); | 213 details.tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| 214 details.url = url.spec(); | 214 details.url = url.spec(); |
| 215 details.process_id = frame_host->GetProcess()->GetID(); | 215 details.process_id = frame_host->GetProcess()->GetID(); |
| 216 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(frame_host); | 216 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(frame_host); |
| 217 details.error = net::ErrorToString(error_code); | 217 details.error = net::ErrorToString(error_code); |
| 218 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); | 218 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); |
| 219 | 219 |
| 220 scoped_ptr<Event> event( | 220 std::unique_ptr<Event> event( |
| 221 new Event(events::WEB_NAVIGATION_ON_ERROR_OCCURRED, | 221 new Event(events::WEB_NAVIGATION_ON_ERROR_OCCURRED, |
| 222 web_navigation::OnErrorOccurred::kEventName, | 222 web_navigation::OnErrorOccurred::kEventName, |
| 223 web_navigation::OnErrorOccurred::Create(details))); | 223 web_navigation::OnErrorOccurred::Create(details))); |
| 224 DispatchEvent(web_contents->GetBrowserContext(), std::move(event), url); | 224 DispatchEvent(web_contents->GetBrowserContext(), std::move(event), url); |
| 225 } | 225 } |
| 226 | 226 |
| 227 void DispatchOnErrorOccurred(content::NavigationHandle* navigation_handle) { | 227 void DispatchOnErrorOccurred(content::NavigationHandle* navigation_handle) { |
| 228 web_navigation::OnErrorOccurred::Details details; | 228 web_navigation::OnErrorOccurred::Details details; |
| 229 details.tab_id = | 229 details.tab_id = |
| 230 ExtensionTabUtil::GetTabId(navigation_handle->GetWebContents()); | 230 ExtensionTabUtil::GetTabId(navigation_handle->GetWebContents()); |
| 231 details.url = navigation_handle->GetURL().spec(); | 231 details.url = navigation_handle->GetURL().spec(); |
| 232 details.process_id = -1; | 232 details.process_id = -1; |
| 233 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(navigation_handle); | 233 details.frame_id = ExtensionApiFrameIdMap::GetFrameId(navigation_handle); |
| 234 details.error = (navigation_handle->GetNetErrorCode() != net::OK) | 234 details.error = (navigation_handle->GetNetErrorCode() != net::OK) |
| 235 ? net::ErrorToString(navigation_handle->GetNetErrorCode()) | 235 ? net::ErrorToString(navigation_handle->GetNetErrorCode()) |
| 236 : net::ErrorToString(net::ERR_ABORTED); | 236 : net::ErrorToString(net::ERR_ABORTED); |
| 237 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); | 237 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); |
| 238 | 238 |
| 239 scoped_ptr<Event> event( | 239 std::unique_ptr<Event> event( |
| 240 new Event(events::WEB_NAVIGATION_ON_ERROR_OCCURRED, | 240 new Event(events::WEB_NAVIGATION_ON_ERROR_OCCURRED, |
| 241 web_navigation::OnErrorOccurred::kEventName, | 241 web_navigation::OnErrorOccurred::kEventName, |
| 242 web_navigation::OnErrorOccurred::Create(details))); | 242 web_navigation::OnErrorOccurred::Create(details))); |
| 243 DispatchEvent(navigation_handle->GetWebContents()->GetBrowserContext(), | 243 DispatchEvent(navigation_handle->GetWebContents()->GetBrowserContext(), |
| 244 std::move(event), navigation_handle->GetURL()); | 244 std::move(event), navigation_handle->GetURL()); |
| 245 } | 245 } |
| 246 | 246 |
| 247 // Constructs and dispatches an onTabReplaced event. | 247 // Constructs and dispatches an onTabReplaced event. |
| 248 void DispatchOnTabReplaced( | 248 void DispatchOnTabReplaced( |
| 249 content::WebContents* old_web_contents, | 249 content::WebContents* old_web_contents, |
| 250 content::BrowserContext* browser_context, | 250 content::BrowserContext* browser_context, |
| 251 content::WebContents* new_web_contents) { | 251 content::WebContents* new_web_contents) { |
| 252 web_navigation::OnTabReplaced::Details details; | 252 web_navigation::OnTabReplaced::Details details; |
| 253 details.replaced_tab_id = ExtensionTabUtil::GetTabId(old_web_contents); | 253 details.replaced_tab_id = ExtensionTabUtil::GetTabId(old_web_contents); |
| 254 details.tab_id = ExtensionTabUtil::GetTabId(new_web_contents); | 254 details.tab_id = ExtensionTabUtil::GetTabId(new_web_contents); |
| 255 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); | 255 details.time_stamp = MilliSecondsFromTime(base::Time::Now()); |
| 256 | 256 |
| 257 scoped_ptr<Event> event( | 257 std::unique_ptr<Event> event( |
| 258 new Event(events::WEB_NAVIGATION_ON_TAB_REPLACED, | 258 new Event(events::WEB_NAVIGATION_ON_TAB_REPLACED, |
| 259 web_navigation::OnTabReplaced::kEventName, | 259 web_navigation::OnTabReplaced::kEventName, |
| 260 web_navigation::OnTabReplaced::Create(details))); | 260 web_navigation::OnTabReplaced::Create(details))); |
| 261 DispatchEvent(browser_context, std::move(event), GURL()); | 261 DispatchEvent(browser_context, std::move(event), GURL()); |
| 262 } | 262 } |
| 263 | 263 |
| 264 } // namespace web_navigation_api_helpers | 264 } // namespace web_navigation_api_helpers |
| 265 | 265 |
| 266 } // namespace extensions | 266 } // namespace extensions |
| OLD | NEW |