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.h" | 7 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h" |
8 | 8 |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 } | 67 } |
68 | 68 |
69 // Returns |time| as milliseconds since the epoch. | 69 // Returns |time| as milliseconds since the epoch. |
70 double MilliSecondsFromTime(const base::Time& time) { | 70 double MilliSecondsFromTime(const base::Time& time) { |
71 return 1000 * time.ToDoubleT(); | 71 return 1000 * time.ToDoubleT(); |
72 } | 72 } |
73 | 73 |
74 // Dispatches events to the extension message service. | 74 // Dispatches events to the extension message service. |
75 void DispatchEvent(BrowserContext* browser_context, | 75 void DispatchEvent(BrowserContext* browser_context, |
76 const char* event_name, | 76 const char* event_name, |
77 const std::string& json_args) { | 77 base::Value* args) { |
| 78 ListValue *event_args = new ListValue(); |
| 79 event_args->Append(args); |
| 80 |
78 Profile* profile = Profile::FromBrowserContext(browser_context); | 81 Profile* profile = Profile::FromBrowserContext(browser_context); |
79 if (profile && profile->GetExtensionEventRouter()) { | 82 if (profile && profile->GetExtensionEventRouter()) { |
80 profile->GetExtensionEventRouter()->DispatchEventToRenderers( | 83 profile->GetExtensionEventRouter()->DispatchEventToRenderers( |
81 event_name, json_args, profile, GURL()); | 84 event_name, event_args, profile, GURL()); |
82 } | 85 } |
83 } | 86 } |
84 | 87 |
85 // Constructs and dispatches an onBeforeNavigate event. | 88 // Constructs and dispatches an onBeforeNavigate event. |
86 void DispatchOnBeforeNavigate(WebContents* web_contents, | 89 void DispatchOnBeforeNavigate(WebContents* web_contents, |
87 int64 frame_id, | 90 int64 frame_id, |
88 bool is_main_frame, | 91 bool is_main_frame, |
89 const GURL& validated_url) { | 92 const GURL& validated_url) { |
90 ListValue args; | |
91 DictionaryValue* dict = new DictionaryValue(); | 93 DictionaryValue* dict = new DictionaryValue(); |
92 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); | 94 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); |
93 dict->SetString(keys::kUrlKey, validated_url.spec()); | 95 dict->SetString(keys::kUrlKey, validated_url.spec()); |
94 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | 96 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); |
95 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 97 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
96 args.Append(dict); | |
97 | 98 |
98 std::string json_args; | |
99 base::JSONWriter::Write(&args, &json_args); | |
100 DispatchEvent(web_contents->GetBrowserContext(), | 99 DispatchEvent(web_contents->GetBrowserContext(), |
101 keys::kOnBeforeNavigate, | 100 keys::kOnBeforeNavigate, |
102 json_args); | 101 dict); |
103 } | 102 } |
104 | 103 |
105 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated | 104 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated |
106 // event. | 105 // event. |
107 void DispatchOnCommitted(const char* event_name, | 106 void DispatchOnCommitted(const char* event_name, |
108 WebContents* web_contents, | 107 WebContents* web_contents, |
109 int64 frame_id, | 108 int64 frame_id, |
110 bool is_main_frame, | 109 bool is_main_frame, |
111 const GURL& url, | 110 const GURL& url, |
112 content::PageTransition transition_type) { | 111 content::PageTransition transition_type) { |
113 ListValue args; | |
114 DictionaryValue* dict = new DictionaryValue(); | 112 DictionaryValue* dict = new DictionaryValue(); |
115 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); | 113 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); |
116 dict->SetString(keys::kUrlKey, url.spec()); | 114 dict->SetString(keys::kUrlKey, url.spec()); |
117 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | 115 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); |
118 dict->SetString( | 116 dict->SetString( |
119 keys::kTransitionTypeKey, | 117 keys::kTransitionTypeKey, |
120 content::PageTransitionGetCoreTransitionString(transition_type)); | 118 content::PageTransitionGetCoreTransitionString(transition_type)); |
121 ListValue* qualifiers = new ListValue(); | 119 ListValue* qualifiers = new ListValue(); |
122 if (transition_type & content::PAGE_TRANSITION_CLIENT_REDIRECT) | 120 if (transition_type & content::PAGE_TRANSITION_CLIENT_REDIRECT) |
123 qualifiers->Append(Value::CreateStringValue("client_redirect")); | 121 qualifiers->Append(Value::CreateStringValue("client_redirect")); |
124 if (transition_type & content::PAGE_TRANSITION_SERVER_REDIRECT) | 122 if (transition_type & content::PAGE_TRANSITION_SERVER_REDIRECT) |
125 qualifiers->Append(Value::CreateStringValue("server_redirect")); | 123 qualifiers->Append(Value::CreateStringValue("server_redirect")); |
126 if (transition_type & content::PAGE_TRANSITION_FORWARD_BACK) | 124 if (transition_type & content::PAGE_TRANSITION_FORWARD_BACK) |
127 qualifiers->Append(Value::CreateStringValue("forward_back")); | 125 qualifiers->Append(Value::CreateStringValue("forward_back")); |
128 if (transition_type & content::PAGE_TRANSITION_FROM_ADDRESS_BAR) | 126 if (transition_type & content::PAGE_TRANSITION_FROM_ADDRESS_BAR) |
129 qualifiers->Append(Value::CreateStringValue("from_address_bar")); | 127 qualifiers->Append(Value::CreateStringValue("from_address_bar")); |
130 dict->Set(keys::kTransitionQualifiersKey, qualifiers); | 128 dict->Set(keys::kTransitionQualifiersKey, qualifiers); |
131 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 129 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
132 args.Append(dict); | |
133 | 130 |
134 std::string json_args; | 131 DispatchEvent(web_contents->GetBrowserContext(), event_name, dict); |
135 base::JSONWriter::Write(&args, &json_args); | |
136 DispatchEvent(web_contents->GetBrowserContext(), event_name, json_args); | |
137 } | 132 } |
138 | 133 |
139 // Constructs and dispatches an onDOMContentLoaded event. | 134 // Constructs and dispatches an onDOMContentLoaded event. |
140 void DispatchOnDOMContentLoaded(WebContents* web_contents, | 135 void DispatchOnDOMContentLoaded(WebContents* web_contents, |
141 const GURL& url, | 136 const GURL& url, |
142 bool is_main_frame, | 137 bool is_main_frame, |
143 int64 frame_id) { | 138 int64 frame_id) { |
144 ListValue args; | |
145 DictionaryValue* dict = new DictionaryValue(); | 139 DictionaryValue* dict = new DictionaryValue(); |
146 dict->SetInteger(keys::kTabIdKey, | 140 dict->SetInteger(keys::kTabIdKey, |
147 ExtensionTabUtil::GetTabId(web_contents)); | 141 ExtensionTabUtil::GetTabId(web_contents)); |
148 dict->SetString(keys::kUrlKey, url.spec()); | 142 dict->SetString(keys::kUrlKey, url.spec()); |
149 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | 143 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); |
150 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 144 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
151 args.Append(dict); | |
152 | 145 |
153 std::string json_args; | |
154 base::JSONWriter::Write(&args, &json_args); | |
155 DispatchEvent(web_contents->GetBrowserContext(), | 146 DispatchEvent(web_contents->GetBrowserContext(), |
156 keys::kOnDOMContentLoaded, | 147 keys::kOnDOMContentLoaded, |
157 json_args); | 148 dict); |
158 } | 149 } |
159 | 150 |
160 // Constructs and dispatches an onCompleted event. | 151 // Constructs and dispatches an onCompleted event. |
161 void DispatchOnCompleted(WebContents* web_contents, | 152 void DispatchOnCompleted(WebContents* web_contents, |
162 const GURL& url, | 153 const GURL& url, |
163 bool is_main_frame, | 154 bool is_main_frame, |
164 int64 frame_id) { | 155 int64 frame_id) { |
165 ListValue args; | |
166 DictionaryValue* dict = new DictionaryValue(); | 156 DictionaryValue* dict = new DictionaryValue(); |
167 dict->SetInteger(keys::kTabIdKey, | 157 dict->SetInteger(keys::kTabIdKey, |
168 ExtensionTabUtil::GetTabId(web_contents)); | 158 ExtensionTabUtil::GetTabId(web_contents)); |
169 dict->SetString(keys::kUrlKey, url.spec()); | 159 dict->SetString(keys::kUrlKey, url.spec()); |
170 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | 160 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); |
171 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 161 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
172 args.Append(dict); | |
173 | 162 |
174 std::string json_args; | 163 DispatchEvent(web_contents->GetBrowserContext(), keys::kOnCompleted, dict); |
175 base::JSONWriter::Write(&args, &json_args); | |
176 DispatchEvent(web_contents->GetBrowserContext(), | |
177 keys::kOnCompleted, json_args); | |
178 } | 164 } |
179 | 165 |
180 // Constructs and dispatches an onCreatedNavigationTarget event. | 166 // Constructs and dispatches an onCreatedNavigationTarget event. |
181 void DispatchOnCreatedNavigationTarget( | 167 void DispatchOnCreatedNavigationTarget( |
182 WebContents* web_contents, | 168 WebContents* web_contents, |
183 BrowserContext* browser_context, | 169 BrowserContext* browser_context, |
184 int64 source_frame_id, | 170 int64 source_frame_id, |
185 bool source_frame_is_main_frame, | 171 bool source_frame_is_main_frame, |
186 WebContents* target_web_contents, | 172 WebContents* target_web_contents, |
187 const GURL& target_url) { | 173 const GURL& target_url) { |
188 // 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 |
189 // path is exercised by ExtensionApiTest.WebNavigationRequestOpenTab. | 175 // path is exercised by ExtensionApiTest.WebNavigationRequestOpenTab. |
190 DCHECK(ExtensionTabUtil::GetTabById( | 176 DCHECK(ExtensionTabUtil::GetTabById( |
191 ExtensionTabUtil::GetTabId(target_web_contents), | 177 ExtensionTabUtil::GetTabId(target_web_contents), |
192 Profile::FromBrowserContext(target_web_contents->GetBrowserContext()), | 178 Profile::FromBrowserContext(target_web_contents->GetBrowserContext()), |
193 false, NULL, NULL, NULL, NULL)); | 179 false, NULL, NULL, NULL, NULL)); |
194 | 180 |
195 ListValue args; | |
196 DictionaryValue* dict = new DictionaryValue(); | 181 DictionaryValue* dict = new DictionaryValue(); |
197 dict->SetInteger(keys::kSourceTabIdKey, | 182 dict->SetInteger(keys::kSourceTabIdKey, |
198 ExtensionTabUtil::GetTabId(web_contents)); | 183 ExtensionTabUtil::GetTabId(web_contents)); |
199 dict->SetInteger(keys::kSourceFrameIdKey, | 184 dict->SetInteger(keys::kSourceFrameIdKey, |
200 GetFrameId(source_frame_is_main_frame, source_frame_id)); | 185 GetFrameId(source_frame_is_main_frame, source_frame_id)); |
201 dict->SetString(keys::kUrlKey, target_url.possibly_invalid_spec()); | 186 dict->SetString(keys::kUrlKey, target_url.possibly_invalid_spec()); |
202 dict->SetInteger(keys::kTabIdKey, | 187 dict->SetInteger(keys::kTabIdKey, |
203 ExtensionTabUtil::GetTabId(target_web_contents)); | 188 ExtensionTabUtil::GetTabId(target_web_contents)); |
204 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 189 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
205 args.Append(dict); | |
206 | 190 |
207 std::string json_args; | 191 DispatchEvent(browser_context, keys::kOnCreatedNavigationTarget, dict); |
208 base::JSONWriter::Write(&args, &json_args); | |
209 DispatchEvent( | |
210 browser_context, keys::kOnCreatedNavigationTarget, json_args); | |
211 } | 192 } |
212 | 193 |
213 // Constructs and dispatches an onErrorOccurred event. | 194 // Constructs and dispatches an onErrorOccurred event. |
214 void DispatchOnErrorOccurred(WebContents* web_contents, | 195 void DispatchOnErrorOccurred(WebContents* web_contents, |
215 const GURL& url, | 196 const GURL& url, |
216 int64 frame_id, | 197 int64 frame_id, |
217 bool is_main_frame, | 198 bool is_main_frame, |
218 int error_code) { | 199 int error_code) { |
219 ListValue args; | |
220 DictionaryValue* dict = new DictionaryValue(); | 200 DictionaryValue* dict = new DictionaryValue(); |
221 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); | 201 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); |
222 dict->SetString(keys::kUrlKey, url.spec()); | 202 dict->SetString(keys::kUrlKey, url.spec()); |
223 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | 203 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); |
224 dict->SetString(keys::kErrorKey, net::ErrorToString(error_code)); | 204 dict->SetString(keys::kErrorKey, net::ErrorToString(error_code)); |
225 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 205 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
226 args.Append(dict); | |
227 | 206 |
228 std::string json_args; | |
229 base::JSONWriter::Write(&args, &json_args); | |
230 DispatchEvent(web_contents->GetBrowserContext(), | 207 DispatchEvent(web_contents->GetBrowserContext(), |
231 keys::kOnErrorOccurred, | 208 keys::kOnErrorOccurred, |
232 json_args); | 209 dict); |
233 } | 210 } |
234 | 211 |
235 } // namespace | 212 } // namespace |
236 | 213 |
237 | 214 |
238 // FrameNavigationState ------------------------------------------------------- | 215 // FrameNavigationState ------------------------------------------------------- |
239 | 216 |
240 // static | 217 // static |
241 bool FrameNavigationState::allow_extension_scheme_ = false; | 218 bool FrameNavigationState::allow_extension_scheme_ = false; |
242 | 219 |
(...skipping 561 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
804 frame->frame_id = GetFrameId(navigation_state.IsMainFrame(frame_id), | 781 frame->frame_id = GetFrameId(navigation_state.IsMainFrame(frame_id), |
805 frame_id); | 782 frame_id); |
806 frame->error_occurred = navigation_state.GetErrorOccurredInFrame(frame_id); | 783 frame->error_occurred = navigation_state.GetErrorOccurredInFrame(frame_id); |
807 result_list.push_back(frame); | 784 result_list.push_back(frame); |
808 } | 785 } |
809 result_.reset(GetAllFrames::Result::Create(result_list)); | 786 result_.reset(GetAllFrames::Result::Create(result_list)); |
810 return true; | 787 return true; |
811 } | 788 } |
812 | 789 |
813 } // namespace extensions | 790 } // namespace extensions |
OLD | NEW |