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.
5 // Implements the Chrome Extensions WebNavigation API.
7 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api_helpers.h"
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/event_router.h"
15 #include "chrome/browser/extensions/extension_system.h"
16 #include "chrome/browser/extensions/extension_tab_util.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/common/extensions/api/web_navigation.h"
19 #include "content/public/browser/render_process_host.h"
20 #include "content/public/browser/render_view_host.h"
21 #include "content/public/browser/web_contents.h"
22 #include "extensions/common/event_filtering_info.h"
23 #include "net/base/net_errors.h"
25 namespace extensions {
27 namespace keys = web_navigation_api_constants;
28 namespace web_navigation = api::web_navigation;
30 namespace web_navigation_api_helpers {
34 // Returns |time| as milliseconds since the epoch.
35 double MilliSecondsFromTime(const base::Time& time) {
36 return 1000 * time.ToDoubleT();
39 // Dispatches events to the extension message service.
40 void DispatchEvent(content::BrowserContext* browser_context,
41 const std::string& event_name,
42 scoped_ptr<base::ListValue> args,
44 EventFilteringInfo info;
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_profile = profile;
51 event->filter_info = info;
52 ExtensionSystem::Get(profile)->event_router()->BroadcastEvent(event.Pass());
58 int GetFrameId(bool is_main_frame, int64 frame_id) {
59 return is_main_frame ? 0 : static_cast<int>(frame_id);
62 // Constructs and dispatches an onBeforeNavigate event.
63 void DispatchOnBeforeNavigate(content::WebContents* web_contents,
64 int render_process_id,
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()));
81 DispatchEvent(web_contents->GetBrowserContext(),
82 web_navigation::OnBeforeNavigate::kEventName,
87 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated
89 void DispatchOnCommitted(const std::string& event_name,
90 content::WebContents* web_contents,
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));
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()));
118 DispatchEvent(web_contents->GetBrowserContext(), event_name, args.Pass(),
122 // Constructs and dispatches an onDOMContentLoaded event.
123 void DispatchOnDOMContentLoaded(content::WebContents* web_contents,
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()));
138 DispatchEvent(web_contents->GetBrowserContext(),
139 web_navigation::OnDOMContentLoaded::kEventName,
144 // Constructs and dispatches an onCompleted event.
145 void DispatchOnCompleted(content::WebContents* web_contents,
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()));
160 DispatchEvent(web_contents->GetBrowserContext(),
161 web_navigation::OnCompleted::kEventName,
165 // Constructs and dispatches an onCreatedNavigationTarget event.
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));
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()));
194 DispatchEvent(browser_context,
195 web_navigation::OnCreatedNavigationTarget::kEventName,
200 // Constructs and dispatches an onErrorOccurred event.
201 void DispatchOnErrorOccurred(content::WebContents* web_contents,
202 int render_process_id,
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()));
218 DispatchEvent(web_contents->GetBrowserContext(),
219 web_navigation::OnErrorOccurred::kEventName,
223 // Constructs and dispatches an onTabReplaced event.
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));
234 ExtensionTabUtil::GetTabId(new_web_contents));
235 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
238 DispatchEvent(browser_context,
239 web_navigation::OnTabReplaced::kEventName,
244 } // namespace web_navigation_api_helpers
246 } // namespace extensions