Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Side by Side Diff: chrome/browser/extensions/api/web_navigation/web_navigation_api_helpers.cc

Issue 1670673003: Refactor the implementation of the webNavigation extension API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@Bug-532666-NavigationHandleAPI
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
11 #include "base/json/json_writer.h" 11 #include "base/json/json_writer.h"
12 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
13 #include "base/time/time.h" 13 #include "base/time/time.h"
14 #include "base/values.h" 14 #include "base/values.h"
15 #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"
16 #include "chrome/browser/extensions/extension_tab_util.h" 16 #include "chrome/browser/extensions/extension_tab_util.h"
17 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/common/extensions/api/web_navigation.h" 18 #include "chrome/common/extensions/api/web_navigation.h"
19 #include "content/public/browser/navigation_handle.h"
19 #include "content/public/browser/render_frame_host.h" 20 #include "content/public/browser/render_frame_host.h"
20 #include "content/public/browser/render_process_host.h" 21 #include "content/public/browser/render_process_host.h"
21 #include "content/public/browser/render_view_host.h" 22 #include "content/public/browser/render_view_host.h"
22 #include "content/public/browser/web_contents.h" 23 #include "content/public/browser/web_contents.h"
24 #include "content/public/common/url_constants.h"
23 #include "extensions/browser/event_router.h" 25 #include "extensions/browser/event_router.h"
24 #include "extensions/browser/extension_api_frame_id_map.h" 26 #include "extensions/browser/extension_api_frame_id_map.h"
25 #include "extensions/common/event_filtering_info.h" 27 #include "extensions/common/event_filtering_info.h"
26 #include "net/base/net_errors.h" 28 #include "net/base/net_errors.h"
27 #include "ui/base/page_transition_types.h" 29 #include "ui/base/page_transition_types.h"
28 30
29 namespace extensions { 31 namespace extensions {
30 32
31 namespace keys = web_navigation_api_constants; 33 namespace keys = web_navigation_api_constants;
32 namespace web_navigation = api::web_navigation; 34 namespace web_navigation = api::web_navigation;
(...skipping 26 matching lines...) Expand all
59 event_router->BroadcastEvent(std::move(event)); 61 event_router->BroadcastEvent(std::move(event));
60 } 62 }
61 } 63 }
62 64
63 } // namespace 65 } // namespace
64 66
65 int GetFrameId(content::RenderFrameHost* frame_host) { 67 int GetFrameId(content::RenderFrameHost* frame_host) {
66 return ExtensionApiFrameIdMap::GetFrameId(frame_host); 68 return ExtensionApiFrameIdMap::GetFrameId(frame_host);
67 } 69 }
68 70
71 int GetFrameId(content::NavigationHandle* navigation_handle) {
Devlin 2016/02/05 18:30:28 It's a shame that we duplicate the logic between h
nasko 2016/02/05 23:41:22 Totally makes sense. Done.
72 if (navigation_handle->IsInMainFrame()) {
Devlin 2016/02/05 18:30:28 nit: If we keep this, I'd prefer a ternary here.
nasko 2016/02/05 23:41:22 Done.
73 return 0;
74 }
75 return navigation_handle->GetFrameTreeNodeId();
76 }
77
78 int GetParentFrameId(content::NavigationHandle* navigation_handle) {
79 if (navigation_handle->IsInMainFrame()) {
80 return -1;
81 }
82 if (navigation_handle->IsParentMainFrame()) {
83 return 0;
84 }
85 return navigation_handle->GetParentFrameTreeNodeId();
86 }
87
69 // Constructs and dispatches an onBeforeNavigate event. 88 // Constructs and dispatches an onBeforeNavigate event.
70 void DispatchOnBeforeNavigate(content::WebContents* web_contents, 89 void DispatchOnBeforeNavigate(content::NavigationHandle* navigation_handle) {
71 content::RenderFrameHost* frame_host,
72 const GURL& validated_url) {
73 scoped_ptr<base::ListValue> args(new base::ListValue()); 90 scoped_ptr<base::ListValue> args(new base::ListValue());
74 base::DictionaryValue* dict = new base::DictionaryValue(); 91 base::DictionaryValue* dict = new base::DictionaryValue();
75 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); 92
76 dict->SetString(keys::kUrlKey, validated_url.spec()); 93 GURL url(navigation_handle->GetURL());
77 dict->SetInteger(keys::kProcessIdKey, frame_host->GetProcess()->GetID()); 94 if (navigation_handle->IsSrcdoc())
78 dict->SetInteger(keys::kFrameIdKey, GetFrameId(frame_host)); 95 url = GURL(content::kAboutSrcDocURL);
96
97 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(
Devlin 2016/02/05 18:30:28 This is typically prettier when we use api::web_na
nasko 2016/02/05 23:41:22 You asked for it! :)
98 navigation_handle->GetWebContents()));
99 dict->SetString(keys::kUrlKey, url.spec());
100 dict->SetInteger(keys::kProcessIdKey, -1);
101 dict->SetInteger(keys::kFrameIdKey, GetFrameId(navigation_handle));
79 dict->SetInteger(keys::kParentFrameIdKey, 102 dict->SetInteger(keys::kParentFrameIdKey,
80 GetFrameId(frame_host->GetParent())); 103 GetParentFrameId(navigation_handle));
81 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); 104 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
82 args->Append(dict); 105 args->Append(dict);
83 106 DispatchEvent(navigation_handle->GetWebContents()->GetBrowserContext(),
84 DispatchEvent(web_contents->GetBrowserContext(),
85 events::WEB_NAVIGATION_ON_BEFORE_NAVIGATE, 107 events::WEB_NAVIGATION_ON_BEFORE_NAVIGATE,
86 web_navigation::OnBeforeNavigate::kEventName, std::move(args), 108 web_navigation::OnBeforeNavigate::kEventName, std::move(args),
87 validated_url); 109 url);
88 } 110 }
89 111
90 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated 112 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated
91 // event. 113 // event.
92 void DispatchOnCommitted(events::HistogramValue histogram_value, 114 void DispatchOnCommitted(events::HistogramValue histogram_value,
93 const std::string& event_name, 115 const std::string& event_name,
94 content::WebContents* web_contents, 116 content::NavigationHandle* navigation_handle) {
95 content::RenderFrameHost* frame_host, 117 content::WebContents* web_contents = navigation_handle->GetWebContents();
96 const GURL& url, 118 GURL url(navigation_handle->GetURL());
97 ui::PageTransition transition_type) { 119 content::RenderFrameHost* frame_host =
120 navigation_handle->GetRenderFrameHost();
121 ui::PageTransition transition_type = navigation_handle->GetPageTransition();
122
123 if (navigation_handle->IsSrcdoc())
124 url = GURL(content::kAboutSrcDocURL);
125
98 scoped_ptr<base::ListValue> args(new base::ListValue()); 126 scoped_ptr<base::ListValue> args(new base::ListValue());
99 base::DictionaryValue* dict = new base::DictionaryValue(); 127 base::DictionaryValue* dict = new base::DictionaryValue();
100 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); 128 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents));
101 dict->SetString(keys::kUrlKey, url.spec()); 129 dict->SetString(keys::kUrlKey, url.spec());
102 dict->SetInteger(keys::kProcessIdKey, frame_host->GetProcess()->GetID()); 130 dict->SetInteger(keys::kProcessIdKey, frame_host->GetProcess()->GetID());
103 dict->SetInteger(keys::kFrameIdKey, GetFrameId(frame_host)); 131 dict->SetInteger(keys::kFrameIdKey, GetFrameId(frame_host));
132
133 if (navigation_handle->WasServerRedirect()) {
134 transition_type = ui::PageTransitionFromInt(
Devlin 2016/02/05 18:30:28 Out of curiosity, why isn't this done as part of t
nasko 2016/02/05 23:41:22 Not sure. I'd rather not touch that though :), or
135 transition_type | ui::PAGE_TRANSITION_SERVER_REDIRECT);
136 }
137
104 std::string transition_type_string = 138 std::string transition_type_string =
105 ui::PageTransitionGetCoreTransitionString(transition_type); 139 ui::PageTransitionGetCoreTransitionString(transition_type);
106 // For webNavigation API backward compatibility, keep "start_page" even after 140 // For webNavigation API backward compatibility, keep "start_page" even after
107 // renamed to "auto_toplevel". 141 // renamed to "auto_toplevel".
108 if (ui::PageTransitionStripQualifier(transition_type) == 142 if (ui::PageTransitionStripQualifier(transition_type) ==
109 ui::PAGE_TRANSITION_AUTO_TOPLEVEL) 143 ui::PAGE_TRANSITION_AUTO_TOPLEVEL)
110 transition_type_string = "start_page"; 144 transition_type_string = "start_page";
111 dict->SetString(keys::kTransitionTypeKey, transition_type_string); 145 dict->SetString(keys::kTransitionTypeKey, transition_type_string);
112 base::ListValue* qualifiers = new base::ListValue(); 146 base::ListValue* qualifiers = new base::ListValue();
113 if (transition_type & ui::PAGE_TRANSITION_CLIENT_REDIRECT) 147 if (transition_type & ui::PAGE_TRANSITION_CLIENT_REDIRECT)
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 dict->SetDouble(keys::kTimeStampKey, 247 dict->SetDouble(keys::kTimeStampKey,
214 MilliSecondsFromTime(base::Time::Now())); 248 MilliSecondsFromTime(base::Time::Now()));
215 args->Append(dict); 249 args->Append(dict);
216 250
217 DispatchEvent(web_contents->GetBrowserContext(), 251 DispatchEvent(web_contents->GetBrowserContext(),
218 events::WEB_NAVIGATION_ON_ERROR_OCCURRED, 252 events::WEB_NAVIGATION_ON_ERROR_OCCURRED,
219 web_navigation::OnErrorOccurred::kEventName, std::move(args), 253 web_navigation::OnErrorOccurred::kEventName, std::move(args),
220 url); 254 url);
221 } 255 }
222 256
257 void DispatchOnErrorOccurred(content::NavigationHandle* navigation_handle) {
258 scoped_ptr<base::ListValue> args(new base::ListValue());
259 base::DictionaryValue* dict = new base::DictionaryValue();
260
261 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(
262 navigation_handle->GetWebContents()));
263 dict->SetString(keys::kUrlKey, navigation_handle->GetURL().spec());
264 dict->SetInteger(keys::kProcessIdKey, -1);
265 dict->SetInteger(keys::kFrameIdKey, GetFrameId(navigation_handle));
266 if (navigation_handle->GetNetErrorCode() != net::OK) {
267 dict->SetString(keys::kErrorKey,
268 net::ErrorToString(navigation_handle->GetNetErrorCode()));
269 } else {
270 dict->SetString(keys::kErrorKey, net::ErrorToString(net::ERR_ABORTED));
271 }
272 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
273 args->Append(dict);
274
275 DispatchEvent(navigation_handle->GetWebContents()->GetBrowserContext(),
276 events::WEB_NAVIGATION_ON_ERROR_OCCURRED,
277 web_navigation::OnErrorOccurred::kEventName, std::move(args),
278 navigation_handle->GetURL());
279 }
280
223 // Constructs and dispatches an onTabReplaced event. 281 // Constructs and dispatches an onTabReplaced event.
224 void DispatchOnTabReplaced( 282 void DispatchOnTabReplaced(
225 content::WebContents* old_web_contents, 283 content::WebContents* old_web_contents,
226 content::BrowserContext* browser_context, 284 content::BrowserContext* browser_context,
227 content::WebContents* new_web_contents) { 285 content::WebContents* new_web_contents) {
228 scoped_ptr<base::ListValue> args(new base::ListValue()); 286 scoped_ptr<base::ListValue> args(new base::ListValue());
229 base::DictionaryValue* dict = new base::DictionaryValue(); 287 base::DictionaryValue* dict = new base::DictionaryValue();
230 dict->SetInteger(keys::kReplacedTabIdKey, 288 dict->SetInteger(keys::kReplacedTabIdKey,
231 ExtensionTabUtil::GetTabId(old_web_contents)); 289 ExtensionTabUtil::GetTabId(old_web_contents));
232 dict->SetInteger( 290 dict->SetInteger(
233 keys::kTabIdKey, 291 keys::kTabIdKey,
234 ExtensionTabUtil::GetTabId(new_web_contents)); 292 ExtensionTabUtil::GetTabId(new_web_contents));
235 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); 293 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
236 args->Append(dict); 294 args->Append(dict);
237 295
238 DispatchEvent(browser_context, events::WEB_NAVIGATION_ON_TAB_REPLACED, 296 DispatchEvent(browser_context, events::WEB_NAVIGATION_ON_TAB_REPLACED,
239 web_navigation::OnTabReplaced::kEventName, std::move(args), 297 web_navigation::OnTabReplaced::kEventName, std::move(args),
240 GURL()); 298 GURL());
241 } 299 }
242 300
243 } // namespace web_navigation_api_helpers 301 } // namespace web_navigation_api_helpers
244 302
245 } // namespace extensions 303 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698