| 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> |
| 10 |
| 9 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
| 10 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 12 #include "base/values.h" | 14 #include "base/values.h" |
| 13 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api_consta
nts.h" | 15 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api_consta
nts.h" |
| 14 #include "chrome/browser/extensions/extension_tab_util.h" | 16 #include "chrome/browser/extensions/extension_tab_util.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/common/extensions/api/web_navigation.h" | 18 #include "chrome/common/extensions/api/web_navigation.h" |
| 17 #include "content/public/browser/render_frame_host.h" | 19 #include "content/public/browser/render_frame_host.h" |
| 18 #include "content/public/browser/render_process_host.h" | 20 #include "content/public/browser/render_process_host.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 43 const std::string& event_name, | 45 const std::string& event_name, |
| 44 scoped_ptr<base::ListValue> args, | 46 scoped_ptr<base::ListValue> args, |
| 45 const GURL& url) { | 47 const GURL& url) { |
| 46 EventFilteringInfo info; | 48 EventFilteringInfo info; |
| 47 info.SetURL(url); | 49 info.SetURL(url); |
| 48 | 50 |
| 49 Profile* profile = Profile::FromBrowserContext(browser_context); | 51 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 50 EventRouter* event_router = EventRouter::Get(profile); | 52 EventRouter* event_router = EventRouter::Get(profile); |
| 51 if (profile && event_router) { | 53 if (profile && event_router) { |
| 52 scoped_ptr<Event> event( | 54 scoped_ptr<Event> event( |
| 53 new Event(histogram_value, event_name, args.Pass())); | 55 new Event(histogram_value, event_name, std::move(args))); |
| 54 event->restrict_to_browser_context = profile; | 56 event->restrict_to_browser_context = profile; |
| 55 event->filter_info = info; | 57 event->filter_info = info; |
| 56 event_router->BroadcastEvent(event.Pass()); | 58 event_router->BroadcastEvent(std::move(event)); |
| 57 } | 59 } |
| 58 } | 60 } |
| 59 | 61 |
| 60 } // namespace | 62 } // namespace |
| 61 | 63 |
| 62 int GetFrameId(content::RenderFrameHost* frame_host) { | 64 int GetFrameId(content::RenderFrameHost* frame_host) { |
| 63 if (!frame_host) | 65 if (!frame_host) |
| 64 return -1; | 66 return -1; |
| 65 return !frame_host->GetParent() ? 0 : frame_host->GetRoutingID(); | 67 return !frame_host->GetParent() ? 0 : frame_host->GetRoutingID(); |
| 66 } | 68 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 78 dict->SetString(keys::kUrlKey, validated_url.spec()); | 80 dict->SetString(keys::kUrlKey, validated_url.spec()); |
| 79 dict->SetInteger(keys::kProcessIdKey, frame_host->GetProcess()->GetID()); | 81 dict->SetInteger(keys::kProcessIdKey, frame_host->GetProcess()->GetID()); |
| 80 dict->SetInteger(keys::kFrameIdKey, GetFrameId(frame_host)); | 82 dict->SetInteger(keys::kFrameIdKey, GetFrameId(frame_host)); |
| 81 dict->SetInteger(keys::kParentFrameIdKey, | 83 dict->SetInteger(keys::kParentFrameIdKey, |
| 82 GetFrameId(frame_host->GetParent())); | 84 GetFrameId(frame_host->GetParent())); |
| 83 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 85 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 84 args->Append(dict); | 86 args->Append(dict); |
| 85 | 87 |
| 86 DispatchEvent(web_contents->GetBrowserContext(), | 88 DispatchEvent(web_contents->GetBrowserContext(), |
| 87 events::WEB_NAVIGATION_ON_BEFORE_NAVIGATE, | 89 events::WEB_NAVIGATION_ON_BEFORE_NAVIGATE, |
| 88 web_navigation::OnBeforeNavigate::kEventName, args.Pass(), | 90 web_navigation::OnBeforeNavigate::kEventName, std::move(args), |
| 89 validated_url); | 91 validated_url); |
| 90 } | 92 } |
| 91 | 93 |
| 92 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated | 94 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated |
| 93 // event. | 95 // event. |
| 94 void DispatchOnCommitted(events::HistogramValue histogram_value, | 96 void DispatchOnCommitted(events::HistogramValue histogram_value, |
| 95 const std::string& event_name, | 97 const std::string& event_name, |
| 96 content::WebContents* web_contents, | 98 content::WebContents* web_contents, |
| 97 content::RenderFrameHost* frame_host, | 99 content::RenderFrameHost* frame_host, |
| 98 const GURL& url, | 100 const GURL& url, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 118 qualifiers->Append(new base::StringValue("server_redirect")); | 120 qualifiers->Append(new base::StringValue("server_redirect")); |
| 119 if (transition_type & ui::PAGE_TRANSITION_FORWARD_BACK) | 121 if (transition_type & ui::PAGE_TRANSITION_FORWARD_BACK) |
| 120 qualifiers->Append(new base::StringValue("forward_back")); | 122 qualifiers->Append(new base::StringValue("forward_back")); |
| 121 if (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR) | 123 if (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR) |
| 122 qualifiers->Append(new base::StringValue("from_address_bar")); | 124 qualifiers->Append(new base::StringValue("from_address_bar")); |
| 123 dict->Set(keys::kTransitionQualifiersKey, qualifiers); | 125 dict->Set(keys::kTransitionQualifiersKey, qualifiers); |
| 124 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 126 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 125 args->Append(dict); | 127 args->Append(dict); |
| 126 | 128 |
| 127 DispatchEvent(web_contents->GetBrowserContext(), histogram_value, event_name, | 129 DispatchEvent(web_contents->GetBrowserContext(), histogram_value, event_name, |
| 128 args.Pass(), url); | 130 std::move(args), url); |
| 129 } | 131 } |
| 130 | 132 |
| 131 // Constructs and dispatches an onDOMContentLoaded event. | 133 // Constructs and dispatches an onDOMContentLoaded event. |
| 132 void DispatchOnDOMContentLoaded(content::WebContents* web_contents, | 134 void DispatchOnDOMContentLoaded(content::WebContents* web_contents, |
| 133 content::RenderFrameHost* frame_host, | 135 content::RenderFrameHost* frame_host, |
| 134 const GURL& url) { | 136 const GURL& url) { |
| 135 scoped_ptr<base::ListValue> args(new base::ListValue()); | 137 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 136 base::DictionaryValue* dict = new base::DictionaryValue(); | 138 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 137 dict->SetInteger(keys::kTabIdKey, | 139 dict->SetInteger(keys::kTabIdKey, |
| 138 ExtensionTabUtil::GetTabId(web_contents)); | 140 ExtensionTabUtil::GetTabId(web_contents)); |
| 139 dict->SetString(keys::kUrlKey, url.spec()); | 141 dict->SetString(keys::kUrlKey, url.spec()); |
| 140 dict->SetInteger(keys::kProcessIdKey, frame_host->GetProcess()->GetID()); | 142 dict->SetInteger(keys::kProcessIdKey, frame_host->GetProcess()->GetID()); |
| 141 dict->SetInteger(keys::kFrameIdKey, GetFrameId(frame_host)); | 143 dict->SetInteger(keys::kFrameIdKey, GetFrameId(frame_host)); |
| 142 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 144 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 143 args->Append(dict); | 145 args->Append(dict); |
| 144 | 146 |
| 145 DispatchEvent(web_contents->GetBrowserContext(), | 147 DispatchEvent(web_contents->GetBrowserContext(), |
| 146 events::WEB_NAVIGATION_ON_DOM_CONTENT_LOADED, | 148 events::WEB_NAVIGATION_ON_DOM_CONTENT_LOADED, |
| 147 web_navigation::OnDOMContentLoaded::kEventName, args.Pass(), | 149 web_navigation::OnDOMContentLoaded::kEventName, std::move(args), |
| 148 url); | 150 url); |
| 149 } | 151 } |
| 150 | 152 |
| 151 // Constructs and dispatches an onCompleted event. | 153 // Constructs and dispatches an onCompleted event. |
| 152 void DispatchOnCompleted(content::WebContents* web_contents, | 154 void DispatchOnCompleted(content::WebContents* web_contents, |
| 153 content::RenderFrameHost* frame_host, | 155 content::RenderFrameHost* frame_host, |
| 154 const GURL& url) { | 156 const GURL& url) { |
| 155 scoped_ptr<base::ListValue> args(new base::ListValue()); | 157 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 156 base::DictionaryValue* dict = new base::DictionaryValue(); | 158 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 157 dict->SetInteger(keys::kTabIdKey, | 159 dict->SetInteger(keys::kTabIdKey, |
| 158 ExtensionTabUtil::GetTabId(web_contents)); | 160 ExtensionTabUtil::GetTabId(web_contents)); |
| 159 dict->SetString(keys::kUrlKey, url.spec()); | 161 dict->SetString(keys::kUrlKey, url.spec()); |
| 160 dict->SetInteger(keys::kProcessIdKey, frame_host->GetProcess()->GetID()); | 162 dict->SetInteger(keys::kProcessIdKey, frame_host->GetProcess()->GetID()); |
| 161 dict->SetInteger(keys::kFrameIdKey, GetFrameId(frame_host)); | 163 dict->SetInteger(keys::kFrameIdKey, GetFrameId(frame_host)); |
| 162 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 164 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 163 args->Append(dict); | 165 args->Append(dict); |
| 164 | 166 |
| 165 DispatchEvent(web_contents->GetBrowserContext(), | 167 DispatchEvent(web_contents->GetBrowserContext(), |
| 166 events::WEB_NAVIGATION_ON_COMPLETED, | 168 events::WEB_NAVIGATION_ON_COMPLETED, |
| 167 web_navigation::OnCompleted::kEventName, args.Pass(), url); | 169 web_navigation::OnCompleted::kEventName, std::move(args), url); |
| 168 } | 170 } |
| 169 | 171 |
| 170 // Constructs and dispatches an onCreatedNavigationTarget event. | 172 // Constructs and dispatches an onCreatedNavigationTarget event. |
| 171 void DispatchOnCreatedNavigationTarget( | 173 void DispatchOnCreatedNavigationTarget( |
| 172 content::WebContents* web_contents, | 174 content::WebContents* web_contents, |
| 173 content::BrowserContext* browser_context, | 175 content::BrowserContext* browser_context, |
| 174 content::RenderFrameHost* source_frame_host, | 176 content::RenderFrameHost* source_frame_host, |
| 175 content::WebContents* target_web_contents, | 177 content::WebContents* target_web_contents, |
| 176 const GURL& target_url) { | 178 const GURL& target_url) { |
| 177 // Check that the tab is already inserted into a tab strip model. This code | 179 // Check that the tab is already inserted into a tab strip model. This code |
| (...skipping 12 matching lines...) Expand all Loading... |
| 190 dict->SetInteger(keys::kSourceFrameIdKey, GetFrameId(source_frame_host)); | 192 dict->SetInteger(keys::kSourceFrameIdKey, GetFrameId(source_frame_host)); |
| 191 dict->SetString(keys::kUrlKey, target_url.possibly_invalid_spec()); | 193 dict->SetString(keys::kUrlKey, target_url.possibly_invalid_spec()); |
| 192 dict->SetInteger(keys::kTabIdKey, | 194 dict->SetInteger(keys::kTabIdKey, |
| 193 ExtensionTabUtil::GetTabId(target_web_contents)); | 195 ExtensionTabUtil::GetTabId(target_web_contents)); |
| 194 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 196 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 195 args->Append(dict); | 197 args->Append(dict); |
| 196 | 198 |
| 197 DispatchEvent(browser_context, | 199 DispatchEvent(browser_context, |
| 198 events::WEB_NAVIGATION_ON_CREATED_NAVIGATION_TARGET, | 200 events::WEB_NAVIGATION_ON_CREATED_NAVIGATION_TARGET, |
| 199 web_navigation::OnCreatedNavigationTarget::kEventName, | 201 web_navigation::OnCreatedNavigationTarget::kEventName, |
| 200 args.Pass(), target_url); | 202 std::move(args), target_url); |
| 201 } | 203 } |
| 202 | 204 |
| 203 // Constructs and dispatches an onErrorOccurred event. | 205 // Constructs and dispatches an onErrorOccurred event. |
| 204 void DispatchOnErrorOccurred(content::WebContents* web_contents, | 206 void DispatchOnErrorOccurred(content::WebContents* web_contents, |
| 205 content::RenderFrameHost* frame_host, | 207 content::RenderFrameHost* frame_host, |
| 206 const GURL& url, | 208 const GURL& url, |
| 207 int error_code) { | 209 int error_code) { |
| 208 scoped_ptr<base::ListValue> args(new base::ListValue()); | 210 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 209 base::DictionaryValue* dict = new base::DictionaryValue(); | 211 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 210 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); | 212 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); |
| 211 dict->SetString(keys::kUrlKey, url.spec()); | 213 dict->SetString(keys::kUrlKey, url.spec()); |
| 212 dict->SetInteger(keys::kProcessIdKey, frame_host->GetProcess()->GetID()); | 214 dict->SetInteger(keys::kProcessIdKey, frame_host->GetProcess()->GetID()); |
| 213 dict->SetInteger(keys::kFrameIdKey, GetFrameId(frame_host)); | 215 dict->SetInteger(keys::kFrameIdKey, GetFrameId(frame_host)); |
| 214 dict->SetString(keys::kErrorKey, net::ErrorToString(error_code)); | 216 dict->SetString(keys::kErrorKey, net::ErrorToString(error_code)); |
| 215 dict->SetDouble(keys::kTimeStampKey, | 217 dict->SetDouble(keys::kTimeStampKey, |
| 216 MilliSecondsFromTime(base::Time::Now())); | 218 MilliSecondsFromTime(base::Time::Now())); |
| 217 args->Append(dict); | 219 args->Append(dict); |
| 218 | 220 |
| 219 DispatchEvent(web_contents->GetBrowserContext(), | 221 DispatchEvent(web_contents->GetBrowserContext(), |
| 220 events::WEB_NAVIGATION_ON_ERROR_OCCURRED, | 222 events::WEB_NAVIGATION_ON_ERROR_OCCURRED, |
| 221 web_navigation::OnErrorOccurred::kEventName, args.Pass(), url); | 223 web_navigation::OnErrorOccurred::kEventName, std::move(args), |
| 224 url); |
| 222 } | 225 } |
| 223 | 226 |
| 224 // Constructs and dispatches an onTabReplaced event. | 227 // Constructs and dispatches an onTabReplaced event. |
| 225 void DispatchOnTabReplaced( | 228 void DispatchOnTabReplaced( |
| 226 content::WebContents* old_web_contents, | 229 content::WebContents* old_web_contents, |
| 227 content::BrowserContext* browser_context, | 230 content::BrowserContext* browser_context, |
| 228 content::WebContents* new_web_contents) { | 231 content::WebContents* new_web_contents) { |
| 229 scoped_ptr<base::ListValue> args(new base::ListValue()); | 232 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 230 base::DictionaryValue* dict = new base::DictionaryValue(); | 233 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 231 dict->SetInteger(keys::kReplacedTabIdKey, | 234 dict->SetInteger(keys::kReplacedTabIdKey, |
| 232 ExtensionTabUtil::GetTabId(old_web_contents)); | 235 ExtensionTabUtil::GetTabId(old_web_contents)); |
| 233 dict->SetInteger( | 236 dict->SetInteger( |
| 234 keys::kTabIdKey, | 237 keys::kTabIdKey, |
| 235 ExtensionTabUtil::GetTabId(new_web_contents)); | 238 ExtensionTabUtil::GetTabId(new_web_contents)); |
| 236 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 239 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 237 args->Append(dict); | 240 args->Append(dict); |
| 238 | 241 |
| 239 DispatchEvent(browser_context, events::WEB_NAVIGATION_ON_TAB_REPLACED, | 242 DispatchEvent(browser_context, events::WEB_NAVIGATION_ON_TAB_REPLACED, |
| 240 web_navigation::OnTabReplaced::kEventName, args.Pass(), GURL()); | 243 web_navigation::OnTabReplaced::kEventName, std::move(args), |
| 244 GURL()); |
| 241 } | 245 } |
| 242 | 246 |
| 243 } // namespace web_navigation_api_helpers | 247 } // namespace web_navigation_api_helpers |
| 244 | 248 |
| 245 } // namespace extensions | 249 } // namespace extensions |
| OLD | NEW |