OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/extension_webnavigation_api.h" | 7 #include "chrome/browser/extensions/extension_webnavigation_api.h" |
8 | 8 |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/browser/extensions/extension_event_router.h" | 12 #include "chrome/browser/extensions/extension_event_router.h" |
13 #include "chrome/browser/extensions/extension_tabs_module.h" | 13 #include "chrome/browser/extensions/extension_tabs_module.h" |
14 #include "chrome/browser/extensions/extension_webnavigation_api_constants.h" | 14 #include "chrome/browser/extensions/extension_webnavigation_api_constants.h" |
15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/tab_contents/navigation_controller.h" | 16 #include "chrome/browser/tab_contents/navigation_controller.h" |
17 #include "chrome/browser/tab_contents/provisional_load_details.h" | 17 #include "chrome/browser/tab_contents/provisional_load_details.h" |
18 #include "chrome/common/notification_type.h" | 18 #include "chrome/browser/tab_contents/tab_contents.h" |
19 #include "chrome/common/notification_service.h" | 19 #include "chrome/common/notification_service.h" |
| 20 #include "chrome/common/render_messages_params.h" |
20 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
21 | 22 |
22 namespace keys = extension_webnavigation_api_constants; | 23 namespace keys = extension_webnavigation_api_constants; |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 // Returns 0 if the navigation happens in the main frame, or the frame ID | 27 // Returns 0 if the navigation happens in the main frame, or the frame ID |
27 // modulo 32 bits otherwise. | 28 // modulo 32 bits otherwise. |
28 int GetFrameId(ProvisionalLoadDetails* details) { | 29 int GetFrameId(ProvisionalLoadDetails* details) { |
29 return details->main_frame() ? 0 : static_cast<int>(details->frame_id()); | 30 return details->main_frame() ? 0 : static_cast<int>(details->frame_id()); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 registrar_.Add(this, | 120 registrar_.Add(this, |
120 NotificationType::FRAME_DOM_CONTENT_LOADED, | 121 NotificationType::FRAME_DOM_CONTENT_LOADED, |
121 NotificationService::AllSources()); | 122 NotificationService::AllSources()); |
122 registrar_.Add(this, | 123 registrar_.Add(this, |
123 NotificationType::FRAME_DID_FINISH_LOAD, | 124 NotificationType::FRAME_DID_FINISH_LOAD, |
124 NotificationService::AllSources()); | 125 NotificationService::AllSources()); |
125 registrar_.Add(this, | 126 registrar_.Add(this, |
126 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR, | 127 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR, |
127 NotificationService::AllSources()); | 128 NotificationService::AllSources()); |
128 registrar_.Add(this, | 129 registrar_.Add(this, |
| 130 NotificationType::CREATING_NEW_WINDOW, |
| 131 NotificationService::AllSources()); |
| 132 registrar_.Add(this, |
129 NotificationType::TAB_CONTENTS_DESTROYED, | 133 NotificationType::TAB_CONTENTS_DESTROYED, |
130 NotificationService::AllSources()); | 134 NotificationService::AllSources()); |
131 } | 135 } |
132 } | 136 } |
133 | 137 |
134 void ExtensionWebNavigationEventRouter::Observe( | 138 void ExtensionWebNavigationEventRouter::Observe( |
135 NotificationType type, | 139 NotificationType type, |
136 const NotificationSource& source, | 140 const NotificationSource& source, |
137 const NotificationDetails& details) { | 141 const NotificationDetails& details) { |
138 switch (type.value) { | 142 switch (type.value) { |
(...skipping 15 matching lines...) Expand all Loading... |
154 case NotificationType::FRAME_DID_FINISH_LOAD: | 158 case NotificationType::FRAME_DID_FINISH_LOAD: |
155 FrameDidFinishLoad( | 159 FrameDidFinishLoad( |
156 Source<NavigationController>(source).ptr(), | 160 Source<NavigationController>(source).ptr(), |
157 *Details<int64>(details).ptr()); | 161 *Details<int64>(details).ptr()); |
158 break; | 162 break; |
159 case NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR: | 163 case NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR: |
160 FailProvisionalLoadWithError( | 164 FailProvisionalLoadWithError( |
161 Source<NavigationController>(source).ptr(), | 165 Source<NavigationController>(source).ptr(), |
162 Details<ProvisionalLoadDetails>(details).ptr()); | 166 Details<ProvisionalLoadDetails>(details).ptr()); |
163 break; | 167 break; |
| 168 case NotificationType::CREATING_NEW_WINDOW: |
| 169 CreatingNewWindow( |
| 170 Source<TabContents>(source).ptr(), |
| 171 Details<const ViewHostMsg_CreateWindow_Params>(details).ptr()); |
| 172 break; |
164 case NotificationType::TAB_CONTENTS_DESTROYED: | 173 case NotificationType::TAB_CONTENTS_DESTROYED: |
165 navigation_state_.RemoveTabContentsState( | 174 navigation_state_.RemoveTabContentsState( |
166 Source<TabContents>(source).ptr()); | 175 Source<TabContents>(source).ptr()); |
167 break; | 176 break; |
168 | 177 |
169 default: | 178 default: |
170 NOTREACHED(); | 179 NOTREACHED(); |
171 } | 180 } |
172 } | 181 } |
173 void ExtensionWebNavigationEventRouter::FrameProvisionalLoadStart( | 182 void ExtensionWebNavigationEventRouter::FrameProvisionalLoadStart( |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 std::string(net::ErrorToString(details->error_code()))); | 284 std::string(net::ErrorToString(details->error_code()))); |
276 dict->SetReal(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 285 dict->SetReal(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
277 args.Append(dict); | 286 args.Append(dict); |
278 | 287 |
279 std::string json_args; | 288 std::string json_args; |
280 base::JSONWriter::Write(&args, false, &json_args); | 289 base::JSONWriter::Write(&args, false, &json_args); |
281 navigation_state_.ErrorOccurredInFrame(details->frame_id()); | 290 navigation_state_.ErrorOccurredInFrame(details->frame_id()); |
282 DispatchEvent(controller->profile(), keys::kOnErrorOccurred, json_args); | 291 DispatchEvent(controller->profile(), keys::kOnErrorOccurred, json_args); |
283 } | 292 } |
284 | 293 |
| 294 void ExtensionWebNavigationEventRouter::CreatingNewWindow( |
| 295 TabContents* tab_contents, |
| 296 const ViewHostMsg_CreateWindow_Params* details) { |
| 297 ListValue args; |
| 298 DictionaryValue* dict = new DictionaryValue(); |
| 299 dict->SetInteger(keys::kSourceTabIdKey, |
| 300 ExtensionTabUtil::GetTabId(tab_contents)); |
| 301 dict->SetString(keys::kSourceUrlKey, details->opener_url.spec()); |
| 302 dict->SetString(keys::kTargetUrlKey, |
| 303 details->target_url.possibly_invalid_spec()); |
| 304 dict->SetReal(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 305 args.Append(dict); |
| 306 |
| 307 std::string json_args; |
| 308 base::JSONWriter::Write(&args, false, &json_args); |
| 309 DispatchEvent(tab_contents->profile(), keys::kOnBeforeRetarget, json_args); |
| 310 } |
| 311 |
285 void ExtensionWebNavigationEventRouter::DispatchEvent( | 312 void ExtensionWebNavigationEventRouter::DispatchEvent( |
286 Profile* profile, | 313 Profile* profile, |
287 const char* event_name, | 314 const char* event_name, |
288 const std::string& json_args) { | 315 const std::string& json_args) { |
289 if (profile && profile->GetExtensionEventRouter()) { | 316 if (profile && profile->GetExtensionEventRouter()) { |
290 profile->GetExtensionEventRouter()->DispatchEventToRenderers( | 317 profile->GetExtensionEventRouter()->DispatchEventToRenderers( |
291 event_name, json_args, profile, GURL()); | 318 event_name, json_args, profile, GURL()); |
292 } | 319 } |
293 } | 320 } |
OLD | NEW |