Chromium Code Reviews| 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" |
| 11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api_consta nts.h" | 13 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api_consta nts.h" |
| 14 #include "chrome/browser/extensions/extension_tab_util.h" | 14 #include "chrome/browser/extensions/extension_tab_util.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/common/extensions/api/web_navigation.h" | 16 #include "chrome/common/extensions/api/web_navigation.h" |
| 17 #include "content/public/browser/render_frame_host.h" | |
| 17 #include "content/public/browser/render_process_host.h" | 18 #include "content/public/browser/render_process_host.h" |
| 18 #include "content/public/browser/render_view_host.h" | 19 #include "content/public/browser/render_view_host.h" |
| 19 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
| 20 #include "content/public/common/page_transition_types.h" | 21 #include "content/public/common/page_transition_types.h" |
| 21 #include "extensions/browser/event_router.h" | 22 #include "extensions/browser/event_router.h" |
| 22 #include "extensions/common/event_filtering_info.h" | 23 #include "extensions/common/event_filtering_info.h" |
| 23 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
| 24 | 25 |
| 25 namespace extensions { | 26 namespace extensions { |
| 26 | 27 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 49 if (profile && event_router) { | 50 if (profile && event_router) { |
| 50 scoped_ptr<Event> event(new Event(event_name, args.Pass())); | 51 scoped_ptr<Event> event(new Event(event_name, args.Pass())); |
| 51 event->restrict_to_browser_context = profile; | 52 event->restrict_to_browser_context = profile; |
| 52 event->filter_info = info; | 53 event->filter_info = info; |
| 53 event_router->BroadcastEvent(event.Pass()); | 54 event_router->BroadcastEvent(event.Pass()); |
| 54 } | 55 } |
| 55 } | 56 } |
| 56 | 57 |
| 57 } // namespace | 58 } // namespace |
| 58 | 59 |
| 59 int GetFrameId(bool is_main_frame, int64 frame_id) { | 60 int GetFrameId(content::RenderFrameHost* frame_host) { |
| 60 return is_main_frame ? 0 : static_cast<int>(frame_id); | 61 if (!frame_host) |
| 62 return -1; | |
| 63 return !frame_host->GetParent() ? 0 : frame_host->GetRoutingID(); | |
| 61 } | 64 } |
| 62 | 65 |
| 63 // Constructs and dispatches an onBeforeNavigate event. | 66 // Constructs and dispatches an onBeforeNavigate event. |
| 67 // TODO(dcheng): Is it actually OK to omit the parent process ID here? | |
|
jochen (gone - plz use gerrit)
2014/07/14 09:38:36
can you file a bug for this (exposing the parent p
dcheng
2014/07/14 20:12:56
I actually wasn't sure if we needed this. My recol
| |
| 68 // Collisions are probably possible... but maybe this won't ever happen because | |
| 69 // of the SiteInstance grouping policies. | |
| 64 void DispatchOnBeforeNavigate(content::WebContents* web_contents, | 70 void DispatchOnBeforeNavigate(content::WebContents* web_contents, |
| 65 int render_process_id, | 71 content::RenderFrameHost* frame_host, |
| 66 int64 frame_id, | |
| 67 bool is_main_frame, | |
| 68 int64 parent_frame_id, | |
| 69 bool parent_is_main_frame, | |
| 70 const GURL& validated_url) { | 72 const GURL& validated_url) { |
| 71 scoped_ptr<base::ListValue> args(new base::ListValue()); | 73 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 72 base::DictionaryValue* dict = new base::DictionaryValue(); | 74 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 73 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); | 75 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); |
| 74 dict->SetString(keys::kUrlKey, validated_url.spec()); | 76 dict->SetString(keys::kUrlKey, validated_url.spec()); |
| 75 dict->SetInteger(keys::kProcessIdKey, render_process_id); | 77 dict->SetInteger(keys::kProcessIdKey, frame_host->GetProcess()->GetID()); |
| 76 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | 78 dict->SetInteger(keys::kFrameIdKey, GetFrameId(frame_host)); |
| 77 dict->SetInteger(keys::kParentFrameIdKey, | 79 dict->SetInteger(keys::kParentFrameIdKey, |
| 78 GetFrameId(parent_is_main_frame, parent_frame_id)); | 80 GetFrameId(frame_host->GetParent())); |
| 79 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 81 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 80 args->Append(dict); | 82 args->Append(dict); |
| 81 | 83 |
| 82 DispatchEvent(web_contents->GetBrowserContext(), | 84 DispatchEvent(web_contents->GetBrowserContext(), |
| 83 web_navigation::OnBeforeNavigate::kEventName, | 85 web_navigation::OnBeforeNavigate::kEventName, |
| 84 args.Pass(), | 86 args.Pass(), |
| 85 validated_url); | 87 validated_url); |
| 86 } | 88 } |
| 87 | 89 |
| 88 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated | 90 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated |
| 89 // event. | 91 // event. |
| 90 void DispatchOnCommitted(const std::string& event_name, | 92 void DispatchOnCommitted(const std::string& event_name, |
| 91 content::WebContents* web_contents, | 93 content::WebContents* web_contents, |
| 92 int64 frame_id, | 94 content::RenderFrameHost* frame_host, |
| 93 bool is_main_frame, | |
| 94 const GURL& url, | 95 const GURL& url, |
| 95 content::PageTransition transition_type) { | 96 content::PageTransition transition_type) { |
| 96 scoped_ptr<base::ListValue> args(new base::ListValue()); | 97 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 97 base::DictionaryValue* dict = new base::DictionaryValue(); | 98 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 98 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); | 99 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); |
| 99 dict->SetString(keys::kUrlKey, url.spec()); | 100 dict->SetString(keys::kUrlKey, url.spec()); |
| 100 dict->SetInteger(keys::kProcessIdKey, | 101 dict->SetInteger(keys::kProcessIdKey, frame_host->GetProcess()->GetID()); |
| 101 web_contents->GetRenderViewHost()->GetProcess()->GetID()); | 102 dict->SetInteger(keys::kFrameIdKey, GetFrameId(frame_host)); |
| 102 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | |
| 103 std::string transition_type_string = | 103 std::string transition_type_string = |
| 104 content::PageTransitionGetCoreTransitionString(transition_type); | 104 content::PageTransitionGetCoreTransitionString(transition_type); |
| 105 // For webNavigation API backward compatibility, keep "start_page" even after | 105 // For webNavigation API backward compatibility, keep "start_page" even after |
| 106 // renamed to "auto_toplevel". | 106 // renamed to "auto_toplevel". |
| 107 if (PageTransitionStripQualifier(transition_type) == | 107 if (PageTransitionStripQualifier(transition_type) == |
| 108 content::PAGE_TRANSITION_AUTO_TOPLEVEL) | 108 content::PAGE_TRANSITION_AUTO_TOPLEVEL) |
| 109 transition_type_string = "start_page"; | 109 transition_type_string = "start_page"; |
| 110 dict->SetString(keys::kTransitionTypeKey, transition_type_string); | 110 dict->SetString(keys::kTransitionTypeKey, transition_type_string); |
| 111 base::ListValue* qualifiers = new base::ListValue(); | 111 base::ListValue* qualifiers = new base::ListValue(); |
| 112 if (transition_type & content::PAGE_TRANSITION_CLIENT_REDIRECT) | 112 if (transition_type & content::PAGE_TRANSITION_CLIENT_REDIRECT) |
| 113 qualifiers->Append(new base::StringValue("client_redirect")); | 113 qualifiers->Append(new base::StringValue("client_redirect")); |
| 114 if (transition_type & content::PAGE_TRANSITION_SERVER_REDIRECT) | 114 if (transition_type & content::PAGE_TRANSITION_SERVER_REDIRECT) |
| 115 qualifiers->Append(new base::StringValue("server_redirect")); | 115 qualifiers->Append(new base::StringValue("server_redirect")); |
| 116 if (transition_type & content::PAGE_TRANSITION_FORWARD_BACK) | 116 if (transition_type & content::PAGE_TRANSITION_FORWARD_BACK) |
| 117 qualifiers->Append(new base::StringValue("forward_back")); | 117 qualifiers->Append(new base::StringValue("forward_back")); |
| 118 if (transition_type & content::PAGE_TRANSITION_FROM_ADDRESS_BAR) | 118 if (transition_type & content::PAGE_TRANSITION_FROM_ADDRESS_BAR) |
| 119 qualifiers->Append(new base::StringValue("from_address_bar")); | 119 qualifiers->Append(new base::StringValue("from_address_bar")); |
| 120 dict->Set(keys::kTransitionQualifiersKey, qualifiers); | 120 dict->Set(keys::kTransitionQualifiersKey, qualifiers); |
| 121 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 121 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 122 args->Append(dict); | 122 args->Append(dict); |
| 123 | 123 |
| 124 DispatchEvent(web_contents->GetBrowserContext(), event_name, args.Pass(), | 124 DispatchEvent(web_contents->GetBrowserContext(), event_name, args.Pass(), |
| 125 url); | 125 url); |
| 126 } | 126 } |
| 127 | 127 |
| 128 // Constructs and dispatches an onDOMContentLoaded event. | 128 // Constructs and dispatches an onDOMContentLoaded event. |
| 129 void DispatchOnDOMContentLoaded(content::WebContents* web_contents, | 129 void DispatchOnDOMContentLoaded(content::WebContents* web_contents, |
| 130 const GURL& url, | 130 content::RenderFrameHost* frame_host, |
| 131 bool is_main_frame, | 131 const GURL& url) { |
| 132 int64 frame_id) { | |
| 133 scoped_ptr<base::ListValue> args(new base::ListValue()); | 132 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 134 base::DictionaryValue* dict = new base::DictionaryValue(); | 133 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 135 dict->SetInteger(keys::kTabIdKey, | 134 dict->SetInteger(keys::kTabIdKey, |
| 136 ExtensionTabUtil::GetTabId(web_contents)); | 135 ExtensionTabUtil::GetTabId(web_contents)); |
| 137 dict->SetString(keys::kUrlKey, url.spec()); | 136 dict->SetString(keys::kUrlKey, url.spec()); |
| 138 dict->SetInteger(keys::kProcessIdKey, | 137 dict->SetInteger(keys::kProcessIdKey, frame_host->GetProcess()->GetID()); |
| 139 web_contents->GetRenderViewHost()->GetProcess()->GetID()); | 138 dict->SetInteger(keys::kFrameIdKey, GetFrameId(frame_host)); |
| 140 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | |
| 141 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 139 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 142 args->Append(dict); | 140 args->Append(dict); |
| 143 | 141 |
| 144 DispatchEvent(web_contents->GetBrowserContext(), | 142 DispatchEvent(web_contents->GetBrowserContext(), |
| 145 web_navigation::OnDOMContentLoaded::kEventName, | 143 web_navigation::OnDOMContentLoaded::kEventName, |
| 146 args.Pass(), | 144 args.Pass(), |
| 147 url); | 145 url); |
| 148 } | 146 } |
| 149 | 147 |
| 150 // Constructs and dispatches an onCompleted event. | 148 // Constructs and dispatches an onCompleted event. |
| 151 void DispatchOnCompleted(content::WebContents* web_contents, | 149 void DispatchOnCompleted(content::WebContents* web_contents, |
| 152 const GURL& url, | 150 content::RenderFrameHost* frame_host, |
| 153 bool is_main_frame, | 151 const GURL& url) { |
| 154 int64 frame_id) { | |
| 155 scoped_ptr<base::ListValue> args(new base::ListValue()); | 152 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 156 base::DictionaryValue* dict = new base::DictionaryValue(); | 153 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 157 dict->SetInteger(keys::kTabIdKey, | 154 dict->SetInteger(keys::kTabIdKey, |
| 158 ExtensionTabUtil::GetTabId(web_contents)); | 155 ExtensionTabUtil::GetTabId(web_contents)); |
| 159 dict->SetString(keys::kUrlKey, url.spec()); | 156 dict->SetString(keys::kUrlKey, url.spec()); |
| 160 dict->SetInteger(keys::kProcessIdKey, | 157 dict->SetInteger(keys::kProcessIdKey, frame_host->GetProcess()->GetID()); |
| 161 web_contents->GetRenderViewHost()->GetProcess()->GetID()); | 158 dict->SetInteger(keys::kFrameIdKey, GetFrameId(frame_host)); |
| 162 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | |
| 163 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 159 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 164 args->Append(dict); | 160 args->Append(dict); |
| 165 | 161 |
| 166 DispatchEvent(web_contents->GetBrowserContext(), | 162 DispatchEvent(web_contents->GetBrowserContext(), |
| 167 web_navigation::OnCompleted::kEventName, | 163 web_navigation::OnCompleted::kEventName, |
| 168 args.Pass(), url); | 164 args.Pass(), url); |
| 169 } | 165 } |
| 170 | 166 |
| 171 // Constructs and dispatches an onCreatedNavigationTarget event. | 167 // Constructs and dispatches an onCreatedNavigationTarget event. |
| 172 void DispatchOnCreatedNavigationTarget( | 168 void DispatchOnCreatedNavigationTarget( |
| 173 content::WebContents* web_contents, | 169 content::WebContents* web_contents, |
| 174 content::BrowserContext* browser_context, | 170 content::BrowserContext* browser_context, |
| 175 int64 source_frame_id, | 171 content::RenderFrameHost* source_frame_host, |
| 176 bool source_frame_is_main_frame, | |
| 177 content::WebContents* target_web_contents, | 172 content::WebContents* target_web_contents, |
| 178 const GURL& target_url) { | 173 const GURL& target_url) { |
| 179 // Check that the tab is already inserted into a tab strip model. This code | 174 // Check that the tab is already inserted into a tab strip model. This code |
| 180 // path is exercised by ExtensionApiTest.WebNavigationRequestOpenTab. | 175 // path is exercised by ExtensionApiTest.WebNavigationRequestOpenTab. |
| 181 DCHECK(ExtensionTabUtil::GetTabById( | 176 DCHECK(ExtensionTabUtil::GetTabById( |
| 182 ExtensionTabUtil::GetTabId(target_web_contents), | 177 ExtensionTabUtil::GetTabId(target_web_contents), |
| 183 Profile::FromBrowserContext(target_web_contents->GetBrowserContext()), | 178 Profile::FromBrowserContext(target_web_contents->GetBrowserContext()), |
| 184 false, NULL, NULL, NULL, NULL)); | 179 false, NULL, NULL, NULL, NULL)); |
| 185 | 180 |
| 186 scoped_ptr<base::ListValue> args(new base::ListValue()); | 181 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 187 base::DictionaryValue* dict = new base::DictionaryValue(); | 182 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 188 dict->SetInteger(keys::kSourceTabIdKey, | 183 dict->SetInteger(keys::kSourceTabIdKey, |
| 189 ExtensionTabUtil::GetTabId(web_contents)); | 184 ExtensionTabUtil::GetTabId(web_contents)); |
| 190 dict->SetInteger(keys::kSourceProcessIdKey, | 185 dict->SetInteger(keys::kSourceProcessIdKey, |
| 191 web_contents->GetRenderViewHost()->GetProcess()->GetID()); | 186 source_frame_host->GetProcess()->GetID()); |
| 192 dict->SetInteger(keys::kSourceFrameIdKey, | 187 dict->SetInteger(keys::kSourceFrameIdKey, GetFrameId(source_frame_host)); |
| 193 GetFrameId(source_frame_is_main_frame, source_frame_id)); | |
| 194 dict->SetString(keys::kUrlKey, target_url.possibly_invalid_spec()); | 188 dict->SetString(keys::kUrlKey, target_url.possibly_invalid_spec()); |
| 195 dict->SetInteger(keys::kTabIdKey, | 189 dict->SetInteger(keys::kTabIdKey, |
| 196 ExtensionTabUtil::GetTabId(target_web_contents)); | 190 ExtensionTabUtil::GetTabId(target_web_contents)); |
| 197 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 191 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 198 args->Append(dict); | 192 args->Append(dict); |
| 199 | 193 |
| 200 DispatchEvent(browser_context, | 194 DispatchEvent(browser_context, |
| 201 web_navigation::OnCreatedNavigationTarget::kEventName, | 195 web_navigation::OnCreatedNavigationTarget::kEventName, |
| 202 args.Pass(), | 196 args.Pass(), |
| 203 target_url); | 197 target_url); |
| 204 } | 198 } |
| 205 | 199 |
| 206 // Constructs and dispatches an onErrorOccurred event. | 200 // Constructs and dispatches an onErrorOccurred event. |
| 207 void DispatchOnErrorOccurred(content::WebContents* web_contents, | 201 void DispatchOnErrorOccurred(content::WebContents* web_contents, |
| 208 int render_process_id, | 202 content::RenderFrameHost* frame_host, |
| 209 const GURL& url, | 203 const GURL& url, |
| 210 int64 frame_id, | |
| 211 bool is_main_frame, | |
| 212 int error_code) { | 204 int error_code) { |
| 213 scoped_ptr<base::ListValue> args(new base::ListValue()); | 205 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 214 base::DictionaryValue* dict = new base::DictionaryValue(); | 206 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 215 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); | 207 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); |
| 216 dict->SetString(keys::kUrlKey, url.spec()); | 208 dict->SetString(keys::kUrlKey, url.spec()); |
| 217 dict->SetInteger(keys::kProcessIdKey, render_process_id); | 209 dict->SetInteger(keys::kProcessIdKey, frame_host->GetProcess()->GetID()); |
| 218 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | 210 dict->SetInteger(keys::kFrameIdKey, GetFrameId(frame_host)); |
| 219 dict->SetString(keys::kErrorKey, net::ErrorToString(error_code)); | 211 dict->SetString(keys::kErrorKey, net::ErrorToString(error_code)); |
| 220 dict->SetDouble(keys::kTimeStampKey, | 212 dict->SetDouble(keys::kTimeStampKey, |
| 221 MilliSecondsFromTime(base::Time::Now())); | 213 MilliSecondsFromTime(base::Time::Now())); |
| 222 args->Append(dict); | 214 args->Append(dict); |
| 223 | 215 |
| 224 DispatchEvent(web_contents->GetBrowserContext(), | 216 DispatchEvent(web_contents->GetBrowserContext(), |
| 225 web_navigation::OnErrorOccurred::kEventName, | 217 web_navigation::OnErrorOccurred::kEventName, |
| 226 args.Pass(), url); | 218 args.Pass(), url); |
| 227 } | 219 } |
| 228 | 220 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 243 | 235 |
| 244 DispatchEvent(browser_context, | 236 DispatchEvent(browser_context, |
| 245 web_navigation::OnTabReplaced::kEventName, | 237 web_navigation::OnTabReplaced::kEventName, |
| 246 args.Pass(), | 238 args.Pass(), |
| 247 GURL()); | 239 GURL()); |
| 248 } | 240 } |
| 249 | 241 |
| 250 } // namespace web_navigation_api_helpers | 242 } // namespace web_navigation_api_helpers |
| 251 | 243 |
| 252 } // namespace extensions | 244 } // namespace extensions |
| OLD | NEW |