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" |
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/api/web_navigation/web_navigation_api_consta
nts.h" | 14 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api_consta
nts.h" |
15 #include "chrome/browser/extensions/extension_event_router.h" | 15 #include "chrome/browser/extensions/extension_event_router.h" |
16 #include "chrome/browser/extensions/extension_tab_util.h" | 16 #include "chrome/browser/extensions/extension_tab_util.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/tab_contents/retargeting_details.h" |
| 19 #include "chrome/browser/ui/browser.h" |
19 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 20 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
20 #include "chrome/browser/view_type_utils.h" | 21 #include "chrome/browser/view_type_utils.h" |
21 #include "chrome/common/chrome_notification_types.h" | 22 #include "chrome/common/chrome_notification_types.h" |
22 #include "chrome/common/extensions/api/web_navigation.h" | 23 #include "chrome/common/extensions/api/web_navigation.h" |
23 #include "chrome/common/extensions/event_filtering_info.h" | 24 #include "chrome/common/extensions/event_filtering_info.h" |
24 #include "chrome/common/url_constants.h" | 25 #include "chrome/common/url_constants.h" |
25 #include "content/public/browser/resource_request_details.h" | 26 #include "content/public/browser/resource_request_details.h" |
26 #include "content/public/browser/navigation_details.h" | 27 #include "content/public/browser/navigation_details.h" |
27 #include "content/public/browser/notification_service.h" | 28 #include "content/public/browser/notification_service.h" |
28 #include "content/public/browser/notification_types.h" | 29 #include "content/public/browser/notification_types.h" |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 dict->SetString(keys::kUrlKey, url.spec()); | 223 dict->SetString(keys::kUrlKey, url.spec()); |
223 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | 224 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); |
224 dict->SetString(keys::kErrorKey, net::ErrorToString(error_code)); | 225 dict->SetString(keys::kErrorKey, net::ErrorToString(error_code)); |
225 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 226 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
226 args.Append(dict); | 227 args.Append(dict); |
227 | 228 |
228 DispatchEvent(web_contents->GetBrowserContext(), keys::kOnErrorOccurred, | 229 DispatchEvent(web_contents->GetBrowserContext(), keys::kOnErrorOccurred, |
229 args, url); | 230 args, url); |
230 } | 231 } |
231 | 232 |
| 233 // Constructs and dispatches an onTabReplaced event. |
| 234 void DispatchOnTabReplaced( |
| 235 WebContents* old_web_contents, |
| 236 BrowserContext* browser_context, |
| 237 WebContents* new_web_contents) { |
| 238 ListValue args; |
| 239 DictionaryValue* dict = new DictionaryValue(); |
| 240 dict->SetInteger(keys::kReplacedTabIdKey, |
| 241 ExtensionTabUtil::GetTabId(old_web_contents)); |
| 242 dict->SetInteger(keys::kTabIdKey, |
| 243 ExtensionTabUtil::GetTabId(new_web_contents)); |
| 244 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 245 args.Append(dict); |
| 246 |
| 247 DispatchEvent(browser_context, keys::kOnTabReplaced, args, GURL()); |
| 248 } |
| 249 |
232 } // namespace | 250 } // namespace |
233 | 251 |
234 | 252 |
235 // FrameNavigationState ------------------------------------------------------- | 253 // FrameNavigationState ------------------------------------------------------- |
236 | 254 |
237 // static | 255 // static |
238 bool FrameNavigationState::allow_extension_scheme_ = false; | 256 bool FrameNavigationState::allow_extension_scheme_ = false; |
239 | 257 |
240 FrameNavigationState::FrameNavigationState() | 258 FrameNavigationState::FrameNavigationState() |
241 : main_frame_id_(-1) { | 259 : main_frame_id_(-1) { |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 source_frame_is_main_frame(source_frame_is_main_frame), | 408 source_frame_is_main_frame(source_frame_is_main_frame), |
391 target_web_contents(target_web_contents), | 409 target_web_contents(target_web_contents), |
392 target_url(target_url) { | 410 target_url(target_url) { |
393 } | 411 } |
394 | 412 |
395 WebNavigationEventRouter::PendingWebContents::~PendingWebContents() {} | 413 WebNavigationEventRouter::PendingWebContents::~PendingWebContents() {} |
396 | 414 |
397 WebNavigationEventRouter::WebNavigationEventRouter(Profile* profile) | 415 WebNavigationEventRouter::WebNavigationEventRouter(Profile* profile) |
398 : profile_(profile) {} | 416 : profile_(profile) {} |
399 | 417 |
400 WebNavigationEventRouter::~WebNavigationEventRouter() {} | 418 WebNavigationEventRouter::~WebNavigationEventRouter() { |
| 419 BrowserList::RemoveObserver(this); |
| 420 } |
401 | 421 |
402 void WebNavigationEventRouter::Init() { | 422 void WebNavigationEventRouter::Init() { |
403 if (registrar_.IsEmpty()) { | 423 if (!registrar_.IsEmpty()) |
404 registrar_.Add(this, | 424 return; |
405 chrome::NOTIFICATION_RETARGETING, | 425 registrar_.Add(this, |
406 content::NotificationService::AllSources()); | 426 chrome::NOTIFICATION_RETARGETING, |
407 registrar_.Add(this, | 427 content::NotificationService::AllSources()); |
408 chrome::NOTIFICATION_TAB_ADDED, | 428 registrar_.Add(this, |
409 content::NotificationService::AllSources()); | 429 chrome::NOTIFICATION_TAB_ADDED, |
410 registrar_.Add(this, | 430 content::NotificationService::AllSources()); |
411 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 431 registrar_.Add(this, |
412 content::NotificationService::AllSources()); | 432 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
| 433 content::NotificationService::AllSources()); |
| 434 |
| 435 BrowserList::AddObserver(this); |
| 436 for (BrowserList::const_iterator iter = BrowserList::begin(); |
| 437 iter != BrowserList::end(); ++iter) { |
| 438 OnBrowserAdded(*iter); |
413 } | 439 } |
414 } | 440 } |
415 | 441 |
| 442 void WebNavigationEventRouter::OnBrowserAdded(Browser* browser) { |
| 443 if (!profile_->IsSameProfile(browser->profile())) |
| 444 return; |
| 445 browser->tab_strip_model()->AddObserver(this); |
| 446 } |
| 447 |
| 448 void WebNavigationEventRouter::OnBrowserRemoved(Browser* browser) { |
| 449 if (!profile_->IsSameProfile(browser->profile())) |
| 450 return; |
| 451 browser->tab_strip_model()->RemoveObserver(this); |
| 452 } |
| 453 |
| 454 void WebNavigationEventRouter::TabReplacedAt( |
| 455 TabStripModel* tab_strip_model, |
| 456 TabContents* old_contents, |
| 457 TabContents* new_contents, |
| 458 int index) { |
| 459 WebNavigationTabObserver* tab_observer = |
| 460 WebNavigationTabObserver::Get(old_contents->web_contents()); |
| 461 if (!tab_observer) { |
| 462 // If you hit this DCHECK(), please add reproduction steps to |
| 463 // http://crbug.com/109464. |
| 464 DCHECK(chrome::GetViewType(old_contents->web_contents()) != |
| 465 chrome::VIEW_TYPE_TAB_CONTENTS); |
| 466 return; |
| 467 } |
| 468 const FrameNavigationState& frame_navigation_state = |
| 469 tab_observer->frame_navigation_state(); |
| 470 |
| 471 if (!frame_navigation_state.IsValidUrl( |
| 472 old_contents->web_contents()->GetURL()) || |
| 473 !frame_navigation_state.IsValidUrl( |
| 474 new_contents->web_contents()->GetURL())) |
| 475 return; |
| 476 |
| 477 DispatchOnTabReplaced( |
| 478 old_contents->web_contents(), |
| 479 profile_, |
| 480 new_contents->web_contents()); |
| 481 } |
| 482 |
416 void WebNavigationEventRouter::Observe( | 483 void WebNavigationEventRouter::Observe( |
417 int type, | 484 int type, |
418 const content::NotificationSource& source, | 485 const content::NotificationSource& source, |
419 const content::NotificationDetails& details) { | 486 const content::NotificationDetails& details) { |
420 switch (type) { | 487 switch (type) { |
421 case chrome::NOTIFICATION_RETARGETING: { | 488 case chrome::NOTIFICATION_RETARGETING: { |
422 Profile* profile = content::Source<Profile>(source).ptr(); | 489 Profile* profile = content::Source<Profile>(source).ptr(); |
423 if (profile->GetOriginalProfile() == profile_) { | 490 if (profile->GetOriginalProfile() == profile_) { |
424 Retargeting( | 491 Retargeting( |
425 content::Details<const RetargetingDetails>(details).ptr()); | 492 content::Details<const RetargetingDetails>(details).ptr()); |
(...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
801 frame->frame_id = GetFrameId(navigation_state.IsMainFrame(frame_id), | 868 frame->frame_id = GetFrameId(navigation_state.IsMainFrame(frame_id), |
802 frame_id); | 869 frame_id); |
803 frame->error_occurred = navigation_state.GetErrorOccurredInFrame(frame_id); | 870 frame->error_occurred = navigation_state.GetErrorOccurredInFrame(frame_id); |
804 result_list.push_back(frame); | 871 result_list.push_back(frame); |
805 } | 872 } |
806 result_.reset(GetAllFrames::Result::Create(result_list)); | 873 result_.reset(GetAllFrames::Result::Create(result_list)); |
807 return true; | 874 return true; |
808 } | 875 } |
809 | 876 |
810 } // namespace extensions | 877 } // namespace extensions |
OLD | NEW |