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/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 namespace { | 29 namespace { |
30 | 30 |
31 // Returns |time| as milliseconds since the epoch. | 31 // Returns |time| as milliseconds since the epoch. |
32 double MilliSecondsFromTime(const base::Time& time) { | 32 double MilliSecondsFromTime(const base::Time& time) { |
33 return 1000 * time.ToDoubleT(); | 33 return 1000 * time.ToDoubleT(); |
34 } | 34 } |
35 | 35 |
36 // Dispatches events to the extension message service. | 36 // Dispatches events to the extension message service. |
37 void DispatchEvent(content::BrowserContext* browser_context, | 37 void DispatchEvent(content::BrowserContext* browser_context, |
38 const char* event_name, | 38 const char* event_name, |
39 const ListValue& args, | 39 scoped_ptr<ListValue> args, |
40 const GURL& url) { | 40 const GURL& url) { |
41 std::string json_args; | |
42 base::JSONWriter::Write(&args, &json_args); | |
43 | |
44 EventFilteringInfo info; | 41 EventFilteringInfo info; |
45 info.SetURL(url); | 42 info.SetURL(url); |
46 | 43 |
47 Profile* profile = Profile::FromBrowserContext(browser_context); | 44 Profile* profile = Profile::FromBrowserContext(browser_context); |
48 if (profile && profile->GetExtensionEventRouter()) { | 45 if (profile && profile->GetExtensionEventRouter()) { |
49 profile->GetExtensionEventRouter()->DispatchEventToRenderers( | 46 profile->GetExtensionEventRouter()->DispatchEventToRenderers( |
50 event_name, json_args, profile, GURL(), info); | 47 event_name, args.Pass(), profile, GURL(), info); |
51 } | 48 } |
52 } | 49 } |
53 | 50 |
54 } // namespace | 51 } // namespace |
55 | 52 |
56 int GetFrameId(bool is_main_frame, int64 frame_id) { | 53 int GetFrameId(bool is_main_frame, int64 frame_id) { |
57 return is_main_frame ? 0 : static_cast<int>(frame_id); | 54 return is_main_frame ? 0 : static_cast<int>(frame_id); |
58 } | 55 } |
59 | 56 |
60 // Constructs and dispatches an onBeforeNavigate event. | 57 // Constructs and dispatches an onBeforeNavigate event. |
61 void DispatchOnBeforeNavigate(content::WebContents* web_contents, | 58 void DispatchOnBeforeNavigate(content::WebContents* web_contents, |
62 int render_process_id, | 59 int render_process_id, |
63 int64 frame_id, | 60 int64 frame_id, |
64 bool is_main_frame, | 61 bool is_main_frame, |
65 const GURL& validated_url) { | 62 const GURL& validated_url) { |
66 ListValue args; | 63 scoped_ptr<ListValue> args(new ListValue()); |
67 DictionaryValue* dict = new DictionaryValue(); | 64 DictionaryValue* dict = new DictionaryValue(); |
68 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); | 65 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); |
69 dict->SetString(keys::kUrlKey, validated_url.spec()); | 66 dict->SetString(keys::kUrlKey, validated_url.spec()); |
70 dict->SetInteger(keys::kProcessIdKey, render_process_id); | 67 dict->SetInteger(keys::kProcessIdKey, render_process_id); |
71 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | 68 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); |
72 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 69 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
73 args.Append(dict); | 70 args->Append(dict); |
74 | 71 |
75 DispatchEvent(web_contents->GetBrowserContext(), | 72 DispatchEvent(web_contents->GetBrowserContext(), |
76 keys::kOnBeforeNavigate, | 73 keys::kOnBeforeNavigate, |
77 args, | 74 args.Pass(), |
78 validated_url); | 75 validated_url); |
79 } | 76 } |
80 | 77 |
81 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated | 78 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated |
82 // event. | 79 // event. |
83 void DispatchOnCommitted(const char* event_name, | 80 void DispatchOnCommitted(const char* event_name, |
84 content::WebContents* web_contents, | 81 content::WebContents* web_contents, |
85 int64 frame_id, | 82 int64 frame_id, |
86 bool is_main_frame, | 83 bool is_main_frame, |
87 const GURL& url, | 84 const GURL& url, |
88 content::PageTransition transition_type) { | 85 content::PageTransition transition_type) { |
89 ListValue args; | 86 scoped_ptr<ListValue> args(new ListValue()); |
90 DictionaryValue* dict = new DictionaryValue(); | 87 DictionaryValue* dict = new DictionaryValue(); |
91 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); | 88 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); |
92 dict->SetString(keys::kUrlKey, url.spec()); | 89 dict->SetString(keys::kUrlKey, url.spec()); |
93 dict->SetInteger(keys::kProcessIdKey, | 90 dict->SetInteger(keys::kProcessIdKey, |
94 web_contents->GetRenderViewHost()->GetProcess()->GetID()); | 91 web_contents->GetRenderViewHost()->GetProcess()->GetID()); |
95 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | 92 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); |
96 dict->SetString( | 93 dict->SetString( |
97 keys::kTransitionTypeKey, | 94 keys::kTransitionTypeKey, |
98 content::PageTransitionGetCoreTransitionString(transition_type)); | 95 content::PageTransitionGetCoreTransitionString(transition_type)); |
99 ListValue* qualifiers = new ListValue(); | 96 ListValue* qualifiers = new ListValue(); |
100 if (transition_type & content::PAGE_TRANSITION_CLIENT_REDIRECT) | 97 if (transition_type & content::PAGE_TRANSITION_CLIENT_REDIRECT) |
101 qualifiers->Append(Value::CreateStringValue("client_redirect")); | 98 qualifiers->Append(Value::CreateStringValue("client_redirect")); |
102 if (transition_type & content::PAGE_TRANSITION_SERVER_REDIRECT) | 99 if (transition_type & content::PAGE_TRANSITION_SERVER_REDIRECT) |
103 qualifiers->Append(Value::CreateStringValue("server_redirect")); | 100 qualifiers->Append(Value::CreateStringValue("server_redirect")); |
104 if (transition_type & content::PAGE_TRANSITION_FORWARD_BACK) | 101 if (transition_type & content::PAGE_TRANSITION_FORWARD_BACK) |
105 qualifiers->Append(Value::CreateStringValue("forward_back")); | 102 qualifiers->Append(Value::CreateStringValue("forward_back")); |
106 if (transition_type & content::PAGE_TRANSITION_FROM_ADDRESS_BAR) | 103 if (transition_type & content::PAGE_TRANSITION_FROM_ADDRESS_BAR) |
107 qualifiers->Append(Value::CreateStringValue("from_address_bar")); | 104 qualifiers->Append(Value::CreateStringValue("from_address_bar")); |
108 dict->Set(keys::kTransitionQualifiersKey, qualifiers); | 105 dict->Set(keys::kTransitionQualifiersKey, qualifiers); |
109 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 106 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
110 args.Append(dict); | 107 args->Append(dict); |
111 | 108 |
112 DispatchEvent(web_contents->GetBrowserContext(), event_name, args, url); | 109 DispatchEvent(web_contents->GetBrowserContext(), event_name, args.Pass(), |
| 110 url); |
113 } | 111 } |
114 | 112 |
115 // Constructs and dispatches an onDOMContentLoaded event. | 113 // Constructs and dispatches an onDOMContentLoaded event. |
116 void DispatchOnDOMContentLoaded(content::WebContents* web_contents, | 114 void DispatchOnDOMContentLoaded(content::WebContents* web_contents, |
117 const GURL& url, | 115 const GURL& url, |
118 bool is_main_frame, | 116 bool is_main_frame, |
119 int64 frame_id) { | 117 int64 frame_id) { |
120 ListValue args; | 118 scoped_ptr<ListValue> args(new ListValue()); |
121 DictionaryValue* dict = new DictionaryValue(); | 119 DictionaryValue* dict = new DictionaryValue(); |
122 dict->SetInteger(keys::kTabIdKey, | 120 dict->SetInteger(keys::kTabIdKey, |
123 ExtensionTabUtil::GetTabId(web_contents)); | 121 ExtensionTabUtil::GetTabId(web_contents)); |
124 dict->SetString(keys::kUrlKey, url.spec()); | 122 dict->SetString(keys::kUrlKey, url.spec()); |
125 dict->SetInteger(keys::kProcessIdKey, | 123 dict->SetInteger(keys::kProcessIdKey, |
126 web_contents->GetRenderViewHost()->GetProcess()->GetID()); | 124 web_contents->GetRenderViewHost()->GetProcess()->GetID()); |
127 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | 125 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); |
128 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 126 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
129 args.Append(dict); | 127 args->Append(dict); |
130 | 128 |
131 DispatchEvent(web_contents->GetBrowserContext(), | 129 DispatchEvent(web_contents->GetBrowserContext(), |
132 keys::kOnDOMContentLoaded, | 130 keys::kOnDOMContentLoaded, |
133 args, | 131 args.Pass(), |
134 url); | 132 url); |
135 } | 133 } |
136 | 134 |
137 // Constructs and dispatches an onCompleted event. | 135 // Constructs and dispatches an onCompleted event. |
138 void DispatchOnCompleted(content::WebContents* web_contents, | 136 void DispatchOnCompleted(content::WebContents* web_contents, |
139 const GURL& url, | 137 const GURL& url, |
140 bool is_main_frame, | 138 bool is_main_frame, |
141 int64 frame_id) { | 139 int64 frame_id) { |
142 ListValue args; | 140 scoped_ptr<ListValue> args(new ListValue()); |
143 DictionaryValue* dict = new DictionaryValue(); | 141 DictionaryValue* dict = new DictionaryValue(); |
144 dict->SetInteger(keys::kTabIdKey, | 142 dict->SetInteger(keys::kTabIdKey, |
145 ExtensionTabUtil::GetTabId(web_contents)); | 143 ExtensionTabUtil::GetTabId(web_contents)); |
146 dict->SetString(keys::kUrlKey, url.spec()); | 144 dict->SetString(keys::kUrlKey, url.spec()); |
147 dict->SetInteger(keys::kProcessIdKey, | 145 dict->SetInteger(keys::kProcessIdKey, |
148 web_contents->GetRenderViewHost()->GetProcess()->GetID()); | 146 web_contents->GetRenderViewHost()->GetProcess()->GetID()); |
149 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | 147 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); |
150 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 148 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
151 args.Append(dict); | 149 args->Append(dict); |
152 | 150 |
153 DispatchEvent(web_contents->GetBrowserContext(), keys::kOnCompleted, args, | 151 DispatchEvent(web_contents->GetBrowserContext(), keys::kOnCompleted, |
154 url); | 152 args.Pass(), url); |
155 } | 153 } |
156 | 154 |
157 // Constructs and dispatches an onCreatedNavigationTarget event. | 155 // Constructs and dispatches an onCreatedNavigationTarget event. |
158 void DispatchOnCreatedNavigationTarget( | 156 void DispatchOnCreatedNavigationTarget( |
159 content::WebContents* web_contents, | 157 content::WebContents* web_contents, |
160 content::BrowserContext* browser_context, | 158 content::BrowserContext* browser_context, |
161 int64 source_frame_id, | 159 int64 source_frame_id, |
162 bool source_frame_is_main_frame, | 160 bool source_frame_is_main_frame, |
163 content::WebContents* target_web_contents, | 161 content::WebContents* target_web_contents, |
164 const GURL& target_url) { | 162 const GURL& target_url) { |
165 // Check that the tab is already inserted into a tab strip model. This code | 163 // Check that the tab is already inserted into a tab strip model. This code |
166 // path is exercised by ExtensionApiTest.WebNavigationRequestOpenTab. | 164 // path is exercised by ExtensionApiTest.WebNavigationRequestOpenTab. |
167 DCHECK(ExtensionTabUtil::GetTabById( | 165 DCHECK(ExtensionTabUtil::GetTabById( |
168 ExtensionTabUtil::GetTabId(target_web_contents), | 166 ExtensionTabUtil::GetTabId(target_web_contents), |
169 Profile::FromBrowserContext(target_web_contents->GetBrowserContext()), | 167 Profile::FromBrowserContext(target_web_contents->GetBrowserContext()), |
170 false, NULL, NULL, NULL, NULL)); | 168 false, NULL, NULL, NULL, NULL)); |
171 | 169 |
172 ListValue args; | 170 scoped_ptr<ListValue> args(new ListValue()); |
173 DictionaryValue* dict = new DictionaryValue(); | 171 DictionaryValue* dict = new DictionaryValue(); |
174 dict->SetInteger(keys::kSourceTabIdKey, | 172 dict->SetInteger(keys::kSourceTabIdKey, |
175 ExtensionTabUtil::GetTabId(web_contents)); | 173 ExtensionTabUtil::GetTabId(web_contents)); |
176 dict->SetInteger(keys::kSourceProcessIdKey, | 174 dict->SetInteger(keys::kSourceProcessIdKey, |
177 web_contents->GetRenderViewHost()->GetProcess()->GetID()); | 175 web_contents->GetRenderViewHost()->GetProcess()->GetID()); |
178 dict->SetInteger(keys::kSourceFrameIdKey, | 176 dict->SetInteger(keys::kSourceFrameIdKey, |
179 GetFrameId(source_frame_is_main_frame, source_frame_id)); | 177 GetFrameId(source_frame_is_main_frame, source_frame_id)); |
180 dict->SetString(keys::kUrlKey, target_url.possibly_invalid_spec()); | 178 dict->SetString(keys::kUrlKey, target_url.possibly_invalid_spec()); |
181 dict->SetInteger(keys::kTabIdKey, | 179 dict->SetInteger(keys::kTabIdKey, |
182 ExtensionTabUtil::GetTabId(target_web_contents)); | 180 ExtensionTabUtil::GetTabId(target_web_contents)); |
183 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 181 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
184 args.Append(dict); | 182 args->Append(dict); |
185 | 183 |
186 DispatchEvent(browser_context, keys::kOnCreatedNavigationTarget, args, | 184 DispatchEvent(browser_context, keys::kOnCreatedNavigationTarget, args.Pass(), |
187 target_url); | 185 target_url); |
188 } | 186 } |
189 | 187 |
190 // Constructs and dispatches an onErrorOccurred event. | 188 // Constructs and dispatches an onErrorOccurred event. |
191 void DispatchOnErrorOccurred(content::WebContents* web_contents, | 189 void DispatchOnErrorOccurred(content::WebContents* web_contents, |
192 int render_process_id, | 190 int render_process_id, |
193 const GURL& url, | 191 const GURL& url, |
194 int64 frame_id, | 192 int64 frame_id, |
195 bool is_main_frame, | 193 bool is_main_frame, |
196 int error_code) { | 194 int error_code) { |
197 ListValue args; | 195 scoped_ptr<ListValue> args(new ListValue()); |
198 DictionaryValue* dict = new DictionaryValue(); | 196 DictionaryValue* dict = new DictionaryValue(); |
199 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); | 197 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); |
200 dict->SetString(keys::kUrlKey, url.spec()); | 198 dict->SetString(keys::kUrlKey, url.spec()); |
201 dict->SetInteger(keys::kProcessIdKey, render_process_id); | 199 dict->SetInteger(keys::kProcessIdKey, render_process_id); |
202 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | 200 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); |
203 dict->SetString(keys::kErrorKey, net::ErrorToString(error_code)); | 201 dict->SetString(keys::kErrorKey, net::ErrorToString(error_code)); |
204 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 202 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
205 args.Append(dict); | 203 args->Append(dict); |
206 | 204 |
207 DispatchEvent(web_contents->GetBrowserContext(), keys::kOnErrorOccurred, | 205 DispatchEvent(web_contents->GetBrowserContext(), keys::kOnErrorOccurred, |
208 args, url); | 206 args.Pass(), url); |
209 } | 207 } |
210 | 208 |
211 // Constructs and dispatches an onTabReplaced event. | 209 // Constructs and dispatches an onTabReplaced event. |
212 void DispatchOnTabReplaced( | 210 void DispatchOnTabReplaced( |
213 content::WebContents* old_web_contents, | 211 content::WebContents* old_web_contents, |
214 content::BrowserContext* browser_context, | 212 content::BrowserContext* browser_context, |
215 content::WebContents* new_web_contents) { | 213 content::WebContents* new_web_contents) { |
216 ListValue args; | 214 scoped_ptr<ListValue> args(new ListValue()); |
217 DictionaryValue* dict = new DictionaryValue(); | 215 DictionaryValue* dict = new DictionaryValue(); |
218 dict->SetInteger(keys::kReplacedTabIdKey, | 216 dict->SetInteger(keys::kReplacedTabIdKey, |
219 ExtensionTabUtil::GetTabId(old_web_contents)); | 217 ExtensionTabUtil::GetTabId(old_web_contents)); |
220 dict->SetInteger(keys::kTabIdKey, | 218 dict->SetInteger(keys::kTabIdKey, |
221 ExtensionTabUtil::GetTabId(new_web_contents)); | 219 ExtensionTabUtil::GetTabId(new_web_contents)); |
222 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 220 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
223 args.Append(dict); | 221 args->Append(dict); |
224 | 222 |
225 DispatchEvent(browser_context, keys::kOnTabReplaced, args, GURL()); | 223 DispatchEvent(browser_context, keys::kOnTabReplaced, args.Pass(), GURL()); |
226 } | 224 } |
227 | 225 |
228 } // namespace web_navigation_api_helpers | 226 } // namespace web_navigation_api_helpers |
229 | 227 |
230 } // namespace extensions | 228 } // namespace extensions |
OLD | NEW |