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