OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
12 #include "base/time.h" | 12 #include "base/time.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "chrome/browser/extensions/extension_event_router.h" | 14 #include "chrome/browser/extensions/extension_event_router.h" |
15 #include "chrome/browser/extensions/extension_tab_util.h" | 15 #include "chrome/browser/extensions/extension_tab_util.h" |
16 #include "chrome/browser/extensions/extension_webnavigation_api_constants.h" | 16 #include "chrome/browser/extensions/extension_webnavigation_api_constants.h" |
17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/tab_contents/retargeting_details.h" |
18 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 19 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 20 #include "chrome/common/chrome_notification_types.h" |
19 #include "chrome/common/url_constants.h" | 21 #include "chrome/common/url_constants.h" |
20 #include "content/browser/tab_contents/navigation_details.h" | 22 #include "content/browser/tab_contents/navigation_details.h" |
21 #include "content/browser/tab_contents/tab_contents.h" | 23 #include "content/browser/tab_contents/tab_contents.h" |
22 #include "content/public/browser/notification_service.h" | 24 #include "content/public/browser/notification_service.h" |
23 #include "content/public/browser/notification_types.h" | 25 #include "content/public/browser/notification_types.h" |
24 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
25 | 27 |
26 namespace keys = extension_webnavigation_api_constants; | 28 namespace keys = extension_webnavigation_api_constants; |
27 | 29 |
28 namespace { | 30 namespace { |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 } | 166 } |
165 | 167 |
166 // Constructs and dispatches an onCreatedNavigationTarget event. | 168 // Constructs and dispatches an onCreatedNavigationTarget event. |
167 void DispatchOnCreatedNavigationTarget( | 169 void DispatchOnCreatedNavigationTarget( |
168 TabContents* tab_contents, | 170 TabContents* tab_contents, |
169 content::BrowserContext* browser_context, | 171 content::BrowserContext* browser_context, |
170 int64 source_frame_id, | 172 int64 source_frame_id, |
171 bool source_frame_is_main_frame, | 173 bool source_frame_is_main_frame, |
172 TabContents* target_tab_contents, | 174 TabContents* target_tab_contents, |
173 const GURL& target_url) { | 175 const GURL& target_url) { |
| 176 // Check that the tab is already inserted into a tab strip model. This code |
| 177 // path is exercised by ExtensionApiTest.WebNavigationRequestOpenTab. |
| 178 DCHECK(ExtensionTabUtil::GetTabById( |
| 179 ExtensionTabUtil::GetTabId(target_tab_contents), |
| 180 Profile::FromBrowserContext(target_tab_contents->browser_context()), |
| 181 false, NULL, NULL, NULL, NULL)); |
| 182 |
174 ListValue args; | 183 ListValue args; |
175 DictionaryValue* dict = new DictionaryValue(); | 184 DictionaryValue* dict = new DictionaryValue(); |
176 dict->SetInteger(keys::kSourceTabIdKey, | 185 dict->SetInteger(keys::kSourceTabIdKey, |
177 ExtensionTabUtil::GetTabId(tab_contents)); | 186 ExtensionTabUtil::GetTabId(tab_contents)); |
178 dict->SetInteger(keys::kSourceFrameIdKey, | 187 dict->SetInteger(keys::kSourceFrameIdKey, |
179 GetFrameId(source_frame_is_main_frame, source_frame_id)); | 188 GetFrameId(source_frame_is_main_frame, source_frame_id)); |
180 dict->SetString(keys::kUrlKey, target_url.possibly_invalid_spec()); | 189 dict->SetString(keys::kUrlKey, target_url.possibly_invalid_spec()); |
181 dict->SetInteger(keys::kTabIdKey, | 190 dict->SetInteger(keys::kTabIdKey, |
182 ExtensionTabUtil::GetTabId(target_tab_contents)); | 191 ExtensionTabUtil::GetTabId(target_tab_contents)); |
183 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 192 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 ExtensionWebNavigationEventRouter::PendingTabContents::~PendingTabContents() {} | 365 ExtensionWebNavigationEventRouter::PendingTabContents::~PendingTabContents() {} |
357 | 366 |
358 ExtensionWebNavigationEventRouter::ExtensionWebNavigationEventRouter( | 367 ExtensionWebNavigationEventRouter::ExtensionWebNavigationEventRouter( |
359 Profile* profile) : profile_(profile) {} | 368 Profile* profile) : profile_(profile) {} |
360 | 369 |
361 ExtensionWebNavigationEventRouter::~ExtensionWebNavigationEventRouter() {} | 370 ExtensionWebNavigationEventRouter::~ExtensionWebNavigationEventRouter() {} |
362 | 371 |
363 void ExtensionWebNavigationEventRouter::Init() { | 372 void ExtensionWebNavigationEventRouter::Init() { |
364 if (registrar_.IsEmpty()) { | 373 if (registrar_.IsEmpty()) { |
365 registrar_.Add(this, | 374 registrar_.Add(this, |
366 content::NOTIFICATION_RETARGETING, | 375 chrome::NOTIFICATION_RETARGETING, |
367 content::Source<content::BrowserContext>(profile_)); | 376 content::Source<Profile>(profile_)); |
368 registrar_.Add(this, | 377 registrar_.Add(this, |
369 content::NOTIFICATION_TAB_ADDED, | 378 content::NOTIFICATION_TAB_ADDED, |
370 content::NotificationService::AllSources()); | 379 content::NotificationService::AllSources()); |
371 registrar_.Add(this, | 380 registrar_.Add(this, |
372 content::NOTIFICATION_TAB_CONTENTS_DESTROYED, | 381 content::NOTIFICATION_TAB_CONTENTS_DESTROYED, |
373 content::NotificationService::AllSources()); | 382 content::NotificationService::AllSources()); |
374 } | 383 } |
375 } | 384 } |
376 | 385 |
377 void ExtensionWebNavigationEventRouter::Observe( | 386 void ExtensionWebNavigationEventRouter::Observe( |
378 int type, | 387 int type, |
379 const content::NotificationSource& source, | 388 const content::NotificationSource& source, |
380 const content::NotificationDetails& details) { | 389 const content::NotificationDetails& details) { |
381 switch (type) { | 390 switch (type) { |
382 case content::NOTIFICATION_RETARGETING: | 391 case chrome::NOTIFICATION_RETARGETING: |
383 Retargeting( | 392 Retargeting( |
384 content::Details<const content::RetargetingDetails>(details).ptr()); | 393 content::Details<const RetargetingDetails>(details).ptr()); |
385 break; | 394 break; |
386 | 395 |
387 case content::NOTIFICATION_TAB_ADDED: | 396 case content::NOTIFICATION_TAB_ADDED: |
388 TabAdded(content::Details<TabContents>(details).ptr()); | 397 TabAdded(content::Details<TabContents>(details).ptr()); |
389 break; | 398 break; |
390 | 399 |
391 case content::NOTIFICATION_TAB_CONTENTS_DESTROYED: | 400 case content::NOTIFICATION_TAB_CONTENTS_DESTROYED: |
392 TabDestroyed(content::Source<TabContents>(source).ptr()); | 401 TabDestroyed(content::Source<TabContents>(source).ptr()); |
393 break; | 402 break; |
394 | 403 |
395 default: | 404 default: |
396 NOTREACHED(); | 405 NOTREACHED(); |
397 } | 406 } |
398 } | 407 } |
399 | 408 |
400 void ExtensionWebNavigationEventRouter::Retargeting( | 409 void ExtensionWebNavigationEventRouter::Retargeting( |
401 const content::RetargetingDetails* details) { | 410 const RetargetingDetails* details) { |
402 if (details->source_frame_id == 0) | 411 if (details->source_frame_id == 0) |
403 return; | 412 return; |
404 ExtensionWebNavigationTabObserver* tab_observer = | 413 ExtensionWebNavigationTabObserver* tab_observer = |
405 ExtensionWebNavigationTabObserver::Get(details->source_tab_contents); | 414 ExtensionWebNavigationTabObserver::Get(details->source_tab_contents); |
406 if (!tab_observer) { | 415 if (!tab_observer) { |
407 CHECK(details->source_tab_contents->GetRenderViewType() != | 416 CHECK(details->source_tab_contents->GetRenderViewType() != |
408 content::VIEW_TYPE_TAB_CONTENTS); | 417 content::VIEW_TYPE_TAB_CONTENTS); |
409 return; | 418 return; |
410 } | 419 } |
411 const FrameNavigationState& frame_navigation_state = | 420 const FrameNavigationState& frame_navigation_state = |
412 tab_observer->frame_navigation_state(); | 421 tab_observer->frame_navigation_state(); |
413 | 422 |
414 if (!frame_navigation_state.CanSendEvents(details->source_frame_id)) | 423 if (!frame_navigation_state.CanSendEvents(details->source_frame_id)) |
415 return; | 424 return; |
416 | 425 |
417 // If the TabContents was created as a response to an IPC from a renderer, it | 426 // If the TabContents was created as a response to an IPC from a renderer |
418 // doesn't yet have a wrapper, and we need to delay the extension event until | 427 // (and therefore doesn't yet have a wrapper), or if it isn't yet inserted |
419 // the TabContents is fully initialized. | 428 // into a tab strip, we need to delay the extension event until the |
420 if (TabContentsWrapper::GetCurrentWrapperForContents( | 429 // TabContents is fully initialized. |
421 details->target_tab_contents) == NULL) { | 430 if ((TabContentsWrapper::GetCurrentWrapperForContents( |
| 431 details->target_tab_contents) == NULL) || |
| 432 details->not_yet_in_tabstrip) { |
422 pending_tab_contents_[details->target_tab_contents] = | 433 pending_tab_contents_[details->target_tab_contents] = |
423 PendingTabContents( | 434 PendingTabContents( |
424 details->source_tab_contents, | 435 details->source_tab_contents, |
425 details->source_frame_id, | 436 details->source_frame_id, |
426 frame_navigation_state.IsMainFrame(details->source_frame_id), | 437 frame_navigation_state.IsMainFrame(details->source_frame_id), |
427 details->target_tab_contents, | 438 details->target_tab_contents, |
428 details->target_url); | 439 details->target_url); |
429 } else { | 440 } else { |
430 DispatchOnCreatedNavigationTarget( | 441 DispatchOnCreatedNavigationTarget( |
431 details->source_tab_contents, | 442 details->source_tab_contents, |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
728 keys::kFrameIdKey, | 739 keys::kFrameIdKey, |
729 GetFrameId(navigation_state.IsMainFrame(*frame), *frame)); | 740 GetFrameId(navigation_state.IsMainFrame(*frame), *frame)); |
730 frameDict->SetBoolean( | 741 frameDict->SetBoolean( |
731 keys::kErrorOccurredKey, | 742 keys::kErrorOccurredKey, |
732 navigation_state.GetErrorOccurredInFrame(*frame)); | 743 navigation_state.GetErrorOccurredInFrame(*frame)); |
733 resultList->Append(frameDict); | 744 resultList->Append(frameDict); |
734 } | 745 } |
735 result_.reset(resultList); | 746 result_.reset(resultList); |
736 return true; | 747 return true; |
737 } | 748 } |
OLD | NEW |