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
3 // found in the LICENSE file.
4
5 // Implements the Chrome Extensions WebNavigation API.
6
7 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api_helpers.h"
8
9 #include "base/json/json_writer.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/time/time.h"
12 #include "base/values.h"
13 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api_constants.h"
14 #include "chrome/browser/extensions/extension_system.h"
15 #include "chrome/browser/extensions/extension_tab_util.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/common/extensions/api/web_navigation.h"
18 #include "content/public/browser/render_process_host.h"
19 #include "content/public/browser/render_view_host.h"
20 #include "content/public/browser/web_contents.h"
21 #include "extensions/browser/event_router.h"
22 #include "extensions/common/event_filtering_info.h"
23 #include "net/base/net_errors.h"
24
25 namespace extensions {
26
27 namespace keys = web_navigation_api_constants;
28 namespace web_navigation = api::web_navigation;
29
30 namespace web_navigation_api_helpers {
31
32 namespace {
33
34 // Returns |time| as milliseconds since the epoch.
MilliSecondsFromTime(const base::Time & time)35 double MilliSecondsFromTime(const base::Time& time) {
36 return 1000 * time.ToDoubleT();
37 }
38
39 // Dispatches events to the extension message service.
DispatchEvent(content::BrowserContext * browser_context,const std::string & event_name,scoped_ptr<base::ListValue> args,const GURL & url)40 void DispatchEvent(content::BrowserContext* browser_context,
41 const std::string& event_name,
42 scoped_ptr<base::ListValue> args,
43 const GURL& url) {
44 EventFilteringInfo info;
45 info.SetURL(url);
46
47 Profile* profile = Profile::FromBrowserContext(browser_context);
48 if (profile && extensions::ExtensionSystem::Get(profile)->event_router()) {
49 scoped_ptr<Event> event(new Event(event_name, args.Pass()));
50 event->restrict_to_browser_context = profile;
51 event->filter_info = info;
52 ExtensionSystem::Get(profile)->event_router()->BroadcastEvent(event.Pass());
53 }
54 }
55
56 } // namespace
57
GetFrameId(bool is_main_frame,int64 frame_id)58 int GetFrameId(bool is_main_frame, int64 frame_id) {
59 return is_main_frame ? 0 : static_cast<int>(frame_id);
60 }
61
62 // Constructs and dispatches an onBeforeNavigate event.
DispatchOnBeforeNavigate(content::WebContents * web_contents,int render_process_id,int64 frame_id,bool is_main_frame,int64 parent_frame_id,bool parent_is_main_frame,const GURL & validated_url)63 void DispatchOnBeforeNavigate(content::WebContents* web_contents,
64 int render_process_id,
65 int64 frame_id,
66 bool is_main_frame,
67 int64 parent_frame_id,
68 bool parent_is_main_frame,
69 const GURL& validated_url) {
70 scoped_ptr<base::ListValue> args(new base::ListValue());
71 base::DictionaryValue* dict = new base::DictionaryValue();
72 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents));
73 dict->SetString(keys::kUrlKey, validated_url.spec());
74 dict->SetInteger(keys::kProcessIdKey, render_process_id);
75 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id));
76 dict->SetInteger(keys::kParentFrameIdKey,
77 GetFrameId(parent_is_main_frame, parent_frame_id));
78 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
79 args->Append(dict);
80
81 DispatchEvent(web_contents->GetBrowserContext(),
82 web_navigation::OnBeforeNavigate::kEventName,
83 args.Pass(),
84 validated_url);
85 }
86
87 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated
88 // event.
DispatchOnCommitted(const std::string & event_name,content::WebContents * web_contents,int64 frame_id,bool is_main_frame,const GURL & url,content::PageTransition transition_type)89 void DispatchOnCommitted(const std::string& event_name,
90 content::WebContents* web_contents,
91 int64 frame_id,
92 bool is_main_frame,
93 const GURL& url,
94 content::PageTransition transition_type) {
95 scoped_ptr<base::ListValue> args(new base::ListValue());
96 base::DictionaryValue* dict = new base::DictionaryValue();
97 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents));
98 dict->SetString(keys::kUrlKey, url.spec());
99 dict->SetInteger(keys::kProcessIdKey,
100 web_contents->GetRenderViewHost()->GetProcess()->GetID());
101 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id));
102 dict->SetString(
103 keys::kTransitionTypeKey,
104 content::PageTransitionGetCoreTransitionString(transition_type));
105 base::ListValue* qualifiers = new base::ListValue();
106 if (transition_type & content::PAGE_TRANSITION_CLIENT_REDIRECT)
107 qualifiers->Append(new base::StringValue("client_redirect"));
108 if (transition_type & content::PAGE_TRANSITION_SERVER_REDIRECT)
109 qualifiers->Append(new base::StringValue("server_redirect"));
110 if (transition_type & content::PAGE_TRANSITION_FORWARD_BACK)
111 qualifiers->Append(new base::StringValue("forward_back"));
112 if (transition_type & content::PAGE_TRANSITION_FROM_ADDRESS_BAR)
113 qualifiers->Append(new base::StringValue("from_address_bar"));
114 dict->Set(keys::kTransitionQualifiersKey, qualifiers);
115 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
116 args->Append(dict);
117
118 DispatchEvent(web_contents->GetBrowserContext(), event_name, args.Pass(),
119 url);
120 }
121
122 // Constructs and dispatches an onDOMContentLoaded event.
DispatchOnDOMContentLoaded(content::WebContents * web_contents,const GURL & url,bool is_main_frame,int64 frame_id)123 void DispatchOnDOMContentLoaded(content::WebContents* web_contents,
124 const GURL& url,
125 bool is_main_frame,
126 int64 frame_id) {
127 scoped_ptr<base::ListValue> args(new base::ListValue());
128 base::DictionaryValue* dict = new base::DictionaryValue();
129 dict->SetInteger(keys::kTabIdKey,
130 ExtensionTabUtil::GetTabId(web_contents));
131 dict->SetString(keys::kUrlKey, url.spec());
132 dict->SetInteger(keys::kProcessIdKey,
133 web_contents->GetRenderViewHost()->GetProcess()->GetID());
134 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id));
135 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
136 args->Append(dict);
137
138 DispatchEvent(web_contents->GetBrowserContext(),
139 web_navigation::OnDOMContentLoaded::kEventName,
140 args.Pass(),
141 url);
142 }
143
144 // Constructs and dispatches an onCompleted event.
DispatchOnCompleted(content::WebContents * web_contents,const GURL & url,bool is_main_frame,int64 frame_id)145 void DispatchOnCompleted(content::WebContents* web_contents,
146 const GURL& url,
147 bool is_main_frame,
148 int64 frame_id) {
149 scoped_ptr<base::ListValue> args(new base::ListValue());
150 base::DictionaryValue* dict = new base::DictionaryValue();
151 dict->SetInteger(keys::kTabIdKey,
152 ExtensionTabUtil::GetTabId(web_contents));
153 dict->SetString(keys::kUrlKey, url.spec());
154 dict->SetInteger(keys::kProcessIdKey,
155 web_contents->GetRenderViewHost()->GetProcess()->GetID());
156 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id));
157 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
158 args->Append(dict);
159
160 DispatchEvent(web_contents->GetBrowserContext(),
161 web_navigation::OnCompleted::kEventName,
162 args.Pass(), url);
163 }
164
165 // Constructs and dispatches an onCreatedNavigationTarget event.
DispatchOnCreatedNavigationTarget(content::WebContents * web_contents,content::BrowserContext * browser_context,int64 source_frame_id,bool source_frame_is_main_frame,content::WebContents * target_web_contents,const GURL & target_url)166 void DispatchOnCreatedNavigationTarget(
167 content::WebContents* web_contents,
168 content::BrowserContext* browser_context,
169 int64 source_frame_id,
170 bool source_frame_is_main_frame,
171 content::WebContents* target_web_contents,
172 const GURL& target_url) {
173 // Check that the tab is already inserted into a tab strip model. This code
174 // path is exercised by ExtensionApiTest.WebNavigationRequestOpenTab.
175 DCHECK(ExtensionTabUtil::GetTabById(
176 ExtensionTabUtil::GetTabId(target_web_contents),
177 Profile::FromBrowserContext(target_web_contents->GetBrowserContext()),
178 false, NULL, NULL, NULL, NULL));
179
180 scoped_ptr<base::ListValue> args(new base::ListValue());
181 base::DictionaryValue* dict = new base::DictionaryValue();
182 dict->SetInteger(keys::kSourceTabIdKey,
183 ExtensionTabUtil::GetTabId(web_contents));
184 dict->SetInteger(keys::kSourceProcessIdKey,
185 web_contents->GetRenderViewHost()->GetProcess()->GetID());
186 dict->SetInteger(keys::kSourceFrameIdKey,
187 GetFrameId(source_frame_is_main_frame, source_frame_id));
188 dict->SetString(keys::kUrlKey, target_url.possibly_invalid_spec());
189 dict->SetInteger(keys::kTabIdKey,
190 ExtensionTabUtil::GetTabId(target_web_contents));
191 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
192 args->Append(dict);
193
194 DispatchEvent(browser_context,
195 web_navigation::OnCreatedNavigationTarget::kEventName,
196 args.Pass(),
197 target_url);
198 }
199
200 // Constructs and dispatches an onErrorOccurred event.
DispatchOnErrorOccurred(content::WebContents * web_contents,int render_process_id,const GURL & url,int64 frame_id,bool is_main_frame,int error_code)201 void DispatchOnErrorOccurred(content::WebContents* web_contents,
202 int render_process_id,
203 const GURL& url,
204 int64 frame_id,
205 bool is_main_frame,
206 int error_code) {
207 scoped_ptr<base::ListValue> args(new base::ListValue());
208 base::DictionaryValue* dict = new base::DictionaryValue();
209 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents));
210 dict->SetString(keys::kUrlKey, url.spec());
211 dict->SetInteger(keys::kProcessIdKey, render_process_id);
212 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id));
213 dict->SetString(keys::kErrorKey, net::ErrorToString(error_code));
214 dict->SetDouble(keys::kTimeStampKey,
215 MilliSecondsFromTime(base::Time::Now()));
216 args->Append(dict);
217
218 DispatchEvent(web_contents->GetBrowserContext(),
219 web_navigation::OnErrorOccurred::kEventName,
220 args.Pass(), url);
221 }
222
223 // Constructs and dispatches an onTabReplaced event.
DispatchOnTabReplaced(content::WebContents * old_web_contents,content::BrowserContext * browser_context,content::WebContents * new_web_contents)224 void DispatchOnTabReplaced(
225 content::WebContents* old_web_contents,
226 content::BrowserContext* browser_context,
227 content::WebContents* new_web_contents) {
228 scoped_ptr<base::ListValue> args(new base::ListValue());
229 base::DictionaryValue* dict = new base::DictionaryValue();
230 dict->SetInteger(keys::kReplacedTabIdKey,
231 ExtensionTabUtil::GetTabId(old_web_contents));
232 dict->SetInteger(
233 keys::kTabIdKey,
234 ExtensionTabUtil::GetTabId(new_web_contents));
235 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
236 args->Append(dict);
237
238 DispatchEvent(browser_context,
239 web_navigation::OnTabReplaced::kEventName,
240 args.Pass(),
241 GURL());
242 }
243
244 } // namespace web_navigation_api_helpers
245
246 } // namespace extensions
247