Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(631)

Side by Side Diff: chrome/browser/extensions/extension_webnavigation_api.cc

Issue 6363002: Implement the onBeforeRetarget event of the webNavigation API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: tests Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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
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 int64 frame_id = details->opener_frame_id;
298 if (frame_id == 0 || !navigation_state_.CanSendEvents(frame_id))
yzshen1 2011/01/18 21:49:24 I think it is okay to allow this notification even
299 return;
300 ListValue args;
301 DictionaryValue* dict = new DictionaryValue();
302 dict->SetInteger(keys::kSourceTabIdKey,
303 ExtensionTabUtil::GetTabId(tab_contents));
304 dict->SetString(keys::kSourceUrlKey, details->opener_url.spec());
305 dict->SetString(keys::kTargetUrlKey,
306 details->target_url.possibly_invalid_spec());
307 dict->SetReal(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
308 args.Append(dict);
309
310 std::string json_args;
311 base::JSONWriter::Write(&args, false, &json_args);
312 DispatchEvent(tab_contents->profile(), keys::kOnBeforeRetarget, json_args);
313 }
314
285 void ExtensionWebNavigationEventRouter::DispatchEvent( 315 void ExtensionWebNavigationEventRouter::DispatchEvent(
286 Profile* profile, 316 Profile* profile,
287 const char* event_name, 317 const char* event_name,
288 const std::string& json_args) { 318 const std::string& json_args) {
289 if (profile && profile->GetExtensionEventRouter()) { 319 if (profile && profile->GetExtensionEventRouter()) {
290 profile->GetExtensionEventRouter()->DispatchEventToRenderers( 320 profile->GetExtensionEventRouter()->DispatchEventToRenderers(
291 event_name, json_args, profile, GURL()); 321 event_name, json_args, profile, GURL());
292 } 322 }
293 } 323 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698