1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/infobars/infobar_service.h"
7 #include "base/command_line.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/infobars/insecure_content_infobar_delegate.h"
10 #include "chrome/common/render_messages.h"
11 #include "components/content_settings/content/common/content_settings_messages.h"
12 #include "components/infobars/core/infobar.h"
13 #include "content/public/browser/navigation_details.h"
14 #include "content/public/browser/navigation_entry.h"
15 #include "content/public/browser/notification_service.h"
16 #include "content/public/browser/web_contents.h"
17 #include "ui/base/page_transition_types.h"
20 DEFINE_WEB_CONTENTS_USER_DATA_KEY(InfoBarService
);
23 infobars::InfoBarDelegate::NavigationDetails
24 InfoBarService::NavigationDetailsFromLoadCommittedDetails(
25 const content::LoadCommittedDetails
& details
) {
26 infobars::InfoBarDelegate::NavigationDetails navigation_details
;
27 navigation_details
.entry_id
= details
.entry
->GetUniqueID();
28 navigation_details
.is_navigation_to_different_page
=
29 details
.is_navigation_to_different_page();
30 navigation_details
.did_replace_entry
= details
.did_replace_entry
;
31 const ui::PageTransition transition
= details
.entry
->GetTransitionType();
32 navigation_details
.is_reload
=
33 ui::PageTransitionCoreTypeIs(transition
, ui::PAGE_TRANSITION_RELOAD
);
34 navigation_details
.is_redirect
= ui::PageTransitionIsRedirect(transition
);
35 return navigation_details
;
39 content::WebContents
* InfoBarService::WebContentsFromInfoBar(
40 infobars::InfoBar
* infobar
) {
41 if (!infobar
|| !infobar
->owner())
43 InfoBarService
* infobar_service
=
44 static_cast<InfoBarService
*>(infobar
->owner());
45 return infobar_service
->web_contents();
48 InfoBarService::InfoBarService(content::WebContents
* web_contents
)
49 : content::WebContentsObserver(web_contents
),
50 ignore_next_reload_(false) {
54 InfoBarService::~InfoBarService() {
58 int InfoBarService::GetActiveEntryID() {
59 content::NavigationEntry
* active_entry
=
60 web_contents()->GetController().GetActiveEntry();
61 return active_entry
? active_entry
->GetUniqueID() : 0;
64 void InfoBarService::NotifyInfoBarAdded(infobars::InfoBar
* infobar
) {
65 infobars::InfoBarManager::NotifyInfoBarAdded(infobar
);
66 // TODO(droger): Remove the notifications and have listeners change to be
67 // InfoBarManager::Observers instead. See http://crbug.com/354380
68 content::NotificationService::current()->Notify(
69 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED
,
70 content::Source
<InfoBarService
>(this),
71 content::Details
<infobars::InfoBar::AddedDetails
>(infobar
));
74 void InfoBarService::NotifyInfoBarRemoved(infobars::InfoBar
* infobar
,
76 infobars::InfoBarManager::NotifyInfoBarRemoved(infobar
, animate
);
77 // TODO(droger): Remove the notifications and have listeners change to be
78 // InfoBarManager::Observers instead. See http://crbug.com/354380
79 infobars::InfoBar::RemovedDetails
removed_details(infobar
, animate
);
80 content::NotificationService::current()->Notify(
81 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED
,
82 content::Source
<InfoBarService
>(this),
83 content::Details
<infobars::InfoBar::RemovedDetails
>(&removed_details
));
86 // InfoBarService::CreateConfirmInfoBar() is implemented in platform-specific
89 void InfoBarService::RenderProcessGone(base::TerminationStatus status
) {
90 RemoveAllInfoBars(true);
93 void InfoBarService::DidStartNavigationToPendingEntry(
95 content::NavigationController::ReloadType reload_type
) {
96 ignore_next_reload_
= false;
99 void InfoBarService::NavigationEntryCommitted(
100 const content::LoadCommittedDetails
& load_details
) {
101 const bool ignore
= ignore_next_reload_
&&
102 (ui::PageTransitionStripQualifier(
103 load_details
.entry
->GetTransitionType()) ==
104 ui::PAGE_TRANSITION_RELOAD
);
105 ignore_next_reload_
= false;
107 OnNavigation(NavigationDetailsFromLoadCommittedDetails(load_details
));
110 void InfoBarService::WebContentsDestroyed() {
111 // The WebContents is going away; be aggressively paranoid and delete
112 // ourselves lest other parts of the system attempt to add infobars or use
113 // us otherwise during the destruction.
114 web_contents()->RemoveUserData(UserDataKey());
115 // That was the equivalent of "delete this". This object is now destroyed;
116 // returning from this function is the only safe thing to do.
119 bool InfoBarService::OnMessageReceived(const IPC::Message
& message
) {
121 IPC_BEGIN_MESSAGE_MAP(InfoBarService
, message
)
122 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockDisplayingInsecureContent
,
123 OnDidBlockDisplayingInsecureContent
)
124 IPC_MESSAGE_UNHANDLED(handled
= false)
125 IPC_END_MESSAGE_MAP()
129 void InfoBarService::OnDidBlockDisplayingInsecureContent() {
130 InsecureContentInfoBarDelegate::Create(this);