• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/browser/infobars/infobar_service.h"
6 
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"
16 
17 DEFINE_WEB_CONTENTS_USER_DATA_KEY(InfoBarService);
18 
19 using infobars::InfoBar;
20 using infobars::InfoBarDelegate;
21 using infobars::InfoBarManager;
22 
23 namespace {
24 
IsReload(const content::LoadCommittedDetails & details)25 bool IsReload(const content::LoadCommittedDetails& details) {
26   return ui::PageTransitionStripQualifier(
27       details.entry->GetTransitionType()) == ui::PAGE_TRANSITION_RELOAD;
28 
29 }
30 
31 }  // namespace
32 
33 // static
34 InfoBarDelegate::NavigationDetails
NavigationDetailsFromLoadCommittedDetails(const content::LoadCommittedDetails & details)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;
43 
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;
48 
49   return navigation_details;
50 }
51 
52 // static
WebContentsFromInfoBar(InfoBar * infobar)53 content::WebContents* InfoBarService::WebContentsFromInfoBar(InfoBar* infobar) {
54   if (!infobar || !infobar->owner())
55     return NULL;
56   InfoBarService* infobar_service =
57       static_cast<InfoBarService*>(infobar->owner());
58   return infobar_service->web_contents();
59 }
60 
InfoBarService(content::WebContents * web_contents)61 InfoBarService::InfoBarService(content::WebContents* web_contents)
62     : content::WebContentsObserver(web_contents),
63       ignore_next_reload_(false) {
64   DCHECK(web_contents);
65 }
66 
~InfoBarService()67 InfoBarService::~InfoBarService() {
68   ShutDown();
69 }
70 
GetActiveEntryID()71 int InfoBarService::GetActiveEntryID() {
72   content::NavigationEntry* active_entry =
73       web_contents()->GetController().GetActiveEntry();
74   return active_entry ? active_entry->GetUniqueID() : 0;
75 }
76 
NotifyInfoBarAdded(InfoBar * infobar)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));
85 }
86 
NotifyInfoBarRemoved(InfoBar * infobar,bool animate)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));
96 }
97 
RenderProcessGone(base::TerminationStatus status)98 void InfoBarService::RenderProcessGone(base::TerminationStatus status) {
99   RemoveAllInfoBars(true);
100 }
101 
DidStartNavigationToPendingEntry(const GURL & url,content::NavigationController::ReloadType reload_type)102 void InfoBarService::DidStartNavigationToPendingEntry(
103     const GURL& url,
104     content::NavigationController::ReloadType reload_type) {
105   ignore_next_reload_ = false;
106 }
107 
NavigationEntryCommitted(const content::LoadCommittedDetails & load_details)108 void InfoBarService::NavigationEntryCommitted(
109     const content::LoadCommittedDetails& load_details) {
110   const bool ignore = ignore_next_reload_ && IsReload(load_details);
111   ignore_next_reload_ = false;
112   if (!ignore)
113     OnNavigation(NavigationDetailsFromLoadCommittedDetails(load_details));
114 }
115 
WebContentsDestroyed()116 void InfoBarService::WebContentsDestroyed() {
117   // The WebContents is going away; be aggressively paranoid and delete
118   // ourselves lest other parts of the system attempt to add infobars or use
119   // us otherwise during the destruction.
120   web_contents()->RemoveUserData(UserDataKey());
121   // That was the equivalent of "delete this". This object is now destroyed;
122   // returning from this function is the only safe thing to do.
123 }
124 
OnMessageReceived(const IPC::Message & message)125 bool InfoBarService::OnMessageReceived(const IPC::Message& message) {
126   bool handled = true;
127   IPC_BEGIN_MESSAGE_MAP(InfoBarService, message)
128     IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockDisplayingInsecureContent,
129                         OnDidBlockDisplayingInsecureContent)
130     IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockRunningInsecureContent,
131                         OnDidBlockRunningInsecureContent)
132     IPC_MESSAGE_UNHANDLED(handled = false)
133   IPC_END_MESSAGE_MAP()
134   return handled;
135 }
136 
OnDidBlockDisplayingInsecureContent()137 void InfoBarService::OnDidBlockDisplayingInsecureContent() {
138   InsecureContentInfoBarDelegate::Create(
139       this, InsecureContentInfoBarDelegate::DISPLAY);
140 }
141 
OnDidBlockRunningInsecureContent()142 void InfoBarService::OnDidBlockRunningInsecureContent() {
143   InsecureContentInfoBarDelegate::Create(this,
144                                          InsecureContentInfoBarDelegate::RUN);
145 }
146