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
;
25 bool IsReload(const content::LoadCommittedDetails
& details
) {
26 return ui::PageTransitionStripQualifier(
27 details
.entry
->GetTransitionType()) == ui::PAGE_TRANSITION_RELOAD
;
34 InfoBarDelegate::NavigationDetails
35 InfoBarService::NavigationDetailsFromLoadCommittedDetails(
36 const content::LoadCommittedDetails
& details
) {
37 InfoBarDelegate::NavigationDetails navigation_details
;
38 navigation_details
.entry_id
= details
.entry
->GetUniqueID();
39 navigation_details
.is_navigation_to_different_page
=
40 details
.is_navigation_to_different_page();
41 navigation_details
.did_replace_entry
= details
.did_replace_entry
;
42 navigation_details
.is_main_frame
= details
.is_main_frame
;
44 const ui::PageTransition transition
= details
.entry
->GetTransitionType();
45 navigation_details
.is_reload
= IsReload(details
);
46 navigation_details
.is_redirect
=
47 (transition
& ui::PAGE_TRANSITION_IS_REDIRECT_MASK
) != 0;
49 return navigation_details
;
53 content::WebContents
* InfoBarService::WebContentsFromInfoBar(InfoBar
* infobar
) {
54 if (!infobar
|| !infobar
->owner())
56 InfoBarService
* infobar_service
=
57 static_cast<InfoBarService
*>(infobar
->owner());
58 return infobar_service
->web_contents();
61 InfoBarService::InfoBarService(content::WebContents
* web_contents
)
62 : content::WebContentsObserver(web_contents
),
63 ignore_next_reload_(false) {
67 InfoBarService::~InfoBarService() {
71 int InfoBarService::GetActiveEntryID() {
72 content::NavigationEntry
* active_entry
=
73 web_contents()->GetController().GetActiveEntry();
74 return active_entry
? active_entry
->GetUniqueID() : 0;
77 void InfoBarService::NotifyInfoBarAdded(InfoBar
* infobar
) {
78 InfoBarManager::NotifyInfoBarAdded(infobar
);
79 // TODO(droger): Remove the notifications and have listeners change to be
80 // InfoBarManager::Observers instead. See http://crbug.com/354380
81 content::NotificationService::current()->Notify(
82 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED
,
83 content::Source
<InfoBarService
>(this),
84 content::Details
<InfoBar::AddedDetails
>(infobar
));
87 void InfoBarService::NotifyInfoBarRemoved(InfoBar
* infobar
, bool animate
) {
88 InfoBarManager::NotifyInfoBarRemoved(infobar
, animate
);
89 // TODO(droger): Remove the notifications and have listeners change to be
90 // InfoBarManager::Observers instead. See http://crbug.com/354380
91 InfoBar::RemovedDetails
removed_details(infobar
, animate
);
92 content::NotificationService::current()->Notify(
93 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED
,
94 content::Source
<InfoBarService
>(this),
95 content::Details
<InfoBar::RemovedDetails
>(&removed_details
));
98 // InfoBarService::CreateConfirmInfoBar() is implemented in platform-specific
101 void InfoBarService::RenderProcessGone(base::TerminationStatus status
) {
102 RemoveAllInfoBars(true);
105 void InfoBarService::DidStartNavigationToPendingEntry(
107 content::NavigationController::ReloadType reload_type
) {
108 ignore_next_reload_
= false;
111 void InfoBarService::NavigationEntryCommitted(
112 const content::LoadCommittedDetails
& load_details
) {
113 const bool ignore
= ignore_next_reload_
&& IsReload(load_details
);
114 ignore_next_reload_
= false;
116 OnNavigation(NavigationDetailsFromLoadCommittedDetails(load_details
));
119 void InfoBarService::WebContentsDestroyed() {
120 // The WebContents is going away; be aggressively paranoid and delete
121 // ourselves lest other parts of the system attempt to add infobars or use
122 // us otherwise during the destruction.
123 web_contents()->RemoveUserData(UserDataKey());
124 // That was the equivalent of "delete this". This object is now destroyed;
125 // returning from this function is the only safe thing to do.
128 bool InfoBarService::OnMessageReceived(const IPC::Message
& message
) {
130 IPC_BEGIN_MESSAGE_MAP(InfoBarService
, message
)
131 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockDisplayingInsecureContent
,
132 OnDidBlockDisplayingInsecureContent
)
133 IPC_MESSAGE_UNHANDLED(handled
= false)
134 IPC_END_MESSAGE_MAP()
138 void InfoBarService::OnDidBlockDisplayingInsecureContent() {
139 InsecureContentInfoBarDelegate::Create(this);