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/infobars/core/infobar.h"
12 #include "content/public/browser/navigation_details.h"
13 #include "content/public/browser/navigation_entry.h"
14 #include "content/public/browser/notification_service.h"
15 #include "content/public/browser/web_contents.h"
17 DEFINE_WEB_CONTENTS_USER_DATA_KEY(InfoBarService
);
19 using infobars::InfoBar
;
20 using infobars::InfoBarDelegate
;
21 using infobars::InfoBarManager
;
24 InfoBarDelegate::NavigationDetails
25 InfoBarService::NavigationDetailsFromLoadCommittedDetails(
26 const content::LoadCommittedDetails
& details
) {
27 InfoBarDelegate::NavigationDetails navigation_details
;
28 navigation_details
.entry_id
= details
.entry
->GetUniqueID();
29 navigation_details
.is_navigation_to_different_page
=
30 details
.is_navigation_to_different_page();
31 navigation_details
.did_replace_entry
= details
.did_replace_entry
;
32 navigation_details
.is_main_frame
= details
.is_main_frame
;
34 const content::PageTransition transition
= details
.entry
->GetTransitionType();
35 navigation_details
.is_reload
=
36 content::PageTransitionStripQualifier(transition
) ==
37 content::PAGE_TRANSITION_RELOAD
;
38 navigation_details
.is_redirect
=
39 (transition
& content::PAGE_TRANSITION_IS_REDIRECT_MASK
) != 0;
41 return navigation_details
;
45 content::WebContents
* InfoBarService::WebContentsFromInfoBar(InfoBar
* infobar
) {
46 if (!infobar
|| !infobar
->owner())
48 InfoBarService
* infobar_service
=
49 static_cast<InfoBarService
*>(infobar
->owner());
50 return infobar_service
->web_contents();
53 InfoBarService::InfoBarService(content::WebContents
* web_contents
)
54 : content::WebContentsObserver(web_contents
) {
58 InfoBarService::~InfoBarService() {
62 int InfoBarService::GetActiveEntryID() {
63 content::NavigationEntry
* active_entry
=
64 web_contents()->GetController().GetActiveEntry();
65 return active_entry
? active_entry
->GetUniqueID() : 0;
68 void InfoBarService::NotifyInfoBarAdded(InfoBar
* infobar
) {
69 InfoBarManager::NotifyInfoBarAdded(infobar
);
70 // TODO(droger): Remove the notifications and have listeners change to be
71 // NavigationManager::Observers instead. See http://crbug.com/354380
72 content::NotificationService::current()->Notify(
73 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED
,
74 content::Source
<InfoBarService
>(this),
75 content::Details
<InfoBar::AddedDetails
>(infobar
));
78 void InfoBarService::NotifyInfoBarRemoved(InfoBar
* infobar
, bool animate
) {
79 InfoBarManager::NotifyInfoBarRemoved(infobar
, animate
);
80 // TODO(droger): Remove the notifications and have listeners change to be
81 // NavigationManager::Observers instead. See http://crbug.com/354380
82 InfoBar::RemovedDetails
removed_details(infobar
, animate
);
83 content::NotificationService::current()->Notify(
84 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED
,
85 content::Source
<InfoBarService
>(this),
86 content::Details
<InfoBar::RemovedDetails
>(&removed_details
));
89 void InfoBarService::NotifyInfoBarReplaced(InfoBar
* old_infobar
,
90 InfoBar
* new_infobar
) {
91 InfoBarManager::NotifyInfoBarReplaced(old_infobar
, new_infobar
);
92 // TODO(droger): Remove the notifications and have listeners change to be
93 // NavigationManager::Observers instead. See http://crbug.com/354380
94 InfoBar::ReplacedDetails
replaced_details(old_infobar
, new_infobar
);
95 content::NotificationService::current()->Notify(
96 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED
,
97 content::Source
<InfoBarService
>(this),
98 content::Details
<InfoBar::ReplacedDetails
>(&replaced_details
));
101 void InfoBarService::RenderProcessGone(base::TerminationStatus status
) {
102 RemoveAllInfoBars(true);
105 void InfoBarService::NavigationEntryCommitted(
106 const content::LoadCommittedDetails
& load_details
) {
107 OnNavigation(NavigationDetailsFromLoadCommittedDetails(load_details
));
110 void InfoBarService::WebContentsDestroyed(content::WebContents
* web_contents
) {
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_HANDLER(ChromeViewHostMsg_DidBlockRunningInsecureContent
,
125 OnDidBlockRunningInsecureContent
)
126 IPC_MESSAGE_UNHANDLED(handled
= false)
127 IPC_END_MESSAGE_MAP()
131 void InfoBarService::OnDidBlockDisplayingInsecureContent() {
132 InsecureContentInfoBarDelegate::Create(
133 this, InsecureContentInfoBarDelegate::DISPLAY
);
136 void InfoBarService::OnDidBlockRunningInsecureContent() {
137 InsecureContentInfoBarDelegate::Create(this,
138 InsecureContentInfoBarDelegate::RUN
);