OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 #include "chrome/browser/infobars/infobar_service.h" | 5 #include "chrome/browser/infobars/infobar_service.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "chrome/browser/chrome_notification_types.h" | 8 #include "chrome/browser/chrome_notification_types.h" |
9 #include "chrome/browser/infobars/infobar.h" | 9 #include "chrome/browser/infobars/infobar.h" |
10 #include "chrome/browser/infobars/insecure_content_infobar_delegate.h" | 10 #include "chrome/browser/infobars/insecure_content_infobar_delegate.h" |
11 #include "chrome/common/render_messages.h" | 11 #include "chrome/common/render_messages.h" |
12 #include "content/public/browser/navigation_details.h" | 12 #include "content/public/browser/navigation_details.h" |
13 #include "content/public/browser/navigation_entry.h" | 13 #include "content/public/browser/navigation_entry.h" |
14 #include "content/public/browser/notification_service.h" | 14 #include "content/public/browser/notification_service.h" |
15 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
16 | 16 |
17 DEFINE_WEB_CONTENTS_USER_DATA_KEY(InfoBarService); | 17 DEFINE_WEB_CONTENTS_USER_DATA_KEY(InfoBarService); |
18 | 18 |
19 // static | 19 // static |
20 InfoBarManager* InfoBarService::InfoBarManagerFromWebContents( | |
21 content::WebContents* web_contents) { | |
22 InfoBarService* infobar_service = FromWebContents(web_contents); | |
23 // |infobar_service| may be NULL during shutdown. | |
24 if (!infobar_service) | |
25 return NULL; | |
26 return infobar_service->infobar_manager(); | |
27 } | |
28 | |
29 // static | |
30 InfoBarDelegate::NavigationDetails | 20 InfoBarDelegate::NavigationDetails |
31 InfoBarService::NavigationDetailsFromLoadCommittedDetails( | 21 InfoBarService::NavigationDetailsFromLoadCommittedDetails( |
32 const content::LoadCommittedDetails& details) { | 22 const content::LoadCommittedDetails& details) { |
33 InfoBarDelegate::NavigationDetails navigation_details; | 23 InfoBarDelegate::NavigationDetails navigation_details; |
34 navigation_details.entry_id = details.entry->GetUniqueID(); | 24 navigation_details.entry_id = details.entry->GetUniqueID(); |
35 navigation_details.is_navigation_to_different_page = | 25 navigation_details.is_navigation_to_different_page = |
36 details.is_navigation_to_different_page(); | 26 details.is_navigation_to_different_page(); |
37 navigation_details.did_replace_entry = details.did_replace_entry; | 27 navigation_details.did_replace_entry = details.did_replace_entry; |
38 navigation_details.is_main_frame = details.is_main_frame; | 28 navigation_details.is_main_frame = details.is_main_frame; |
39 | 29 |
40 const content::PageTransition transition = details.entry->GetTransitionType(); | 30 const content::PageTransition transition = details.entry->GetTransitionType(); |
41 navigation_details.is_reload = | 31 navigation_details.is_reload = |
42 content::PageTransitionStripQualifier(transition) == | 32 content::PageTransitionStripQualifier(transition) == |
43 content::PAGE_TRANSITION_RELOAD; | 33 content::PAGE_TRANSITION_RELOAD; |
44 navigation_details.is_redirect = | 34 navigation_details.is_redirect = |
45 (transition & content::PAGE_TRANSITION_IS_REDIRECT_MASK) != 0; | 35 (transition & content::PAGE_TRANSITION_IS_REDIRECT_MASK) != 0; |
46 | 36 |
47 return navigation_details; | 37 return navigation_details; |
48 } | 38 } |
49 | 39 |
50 InfoBar* InfoBarService::AddInfoBar(scoped_ptr<InfoBar> infobar) { | 40 InfoBarService::InfoBarService(content::WebContents* web_contents) |
51 return infobar_manager_.AddInfoBar(infobar.Pass()); | 41 : InfoBarManager(web_contents), |
| 42 content::WebContentsObserver(web_contents) { |
| 43 DCHECK(web_contents); |
52 } | 44 } |
53 | 45 |
54 InfoBar* InfoBarService::ReplaceInfoBar(InfoBar* old_infobar, | 46 InfoBarService::~InfoBarService() { |
55 scoped_ptr<InfoBar> new_infobar) { | 47 ShutDown(); |
56 return infobar_manager_.ReplaceInfoBar(old_infobar, new_infobar.Pass()); | |
57 } | 48 } |
58 | 49 |
59 InfoBarService::InfoBarService(content::WebContents* web_contents) | 50 void InfoBarService::NotifyInfoBarAdded(InfoBar* infobar) { |
60 : content::WebContentsObserver(web_contents), | 51 InfoBarManager::NotifyInfoBarAdded(infobar); |
61 infobar_manager_(web_contents) { | 52 // TODO(droger): Remove the notifications and have listeners change to be |
62 DCHECK(web_contents); | 53 // NavigationManager::Observers instead. See http://crbug.com/354380 |
63 infobar_manager_.AddObserver(this); | 54 content::NotificationService::current()->Notify( |
| 55 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, |
| 56 content::Source<InfoBarService>(this), |
| 57 content::Details<InfoBar::AddedDetails>(infobar)); |
64 } | 58 } |
65 | 59 |
66 InfoBarService::~InfoBarService() {} | 60 void InfoBarService::NotifyInfoBarRemoved(InfoBar* infobar, bool animate) { |
| 61 InfoBarManager::NotifyInfoBarRemoved(infobar, animate); |
| 62 // TODO(droger): Remove the notifications and have listeners change to be |
| 63 // NavigationManager::Observers instead. See http://crbug.com/354380 |
| 64 InfoBar::RemovedDetails removed_details(infobar, animate); |
| 65 content::NotificationService::current()->Notify( |
| 66 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, |
| 67 content::Source<InfoBarService>(this), |
| 68 content::Details<InfoBar::RemovedDetails>(&removed_details)); |
| 69 } |
| 70 |
| 71 void InfoBarService::NotifyInfoBarReplaced(InfoBar* old_infobar, |
| 72 InfoBar* new_infobar) { |
| 73 InfoBarManager::NotifyInfoBarReplaced(old_infobar, new_infobar); |
| 74 // TODO(droger): Remove the notifications and have listeners change to be |
| 75 // NavigationManager::Observers instead. See http://crbug.com/354380 |
| 76 InfoBar::ReplacedDetails replaced_details(old_infobar, new_infobar); |
| 77 content::NotificationService::current()->Notify( |
| 78 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED, |
| 79 content::Source<InfoBarService>(this), |
| 80 content::Details<InfoBar::ReplacedDetails>(&replaced_details)); |
| 81 } |
67 | 82 |
68 void InfoBarService::RenderProcessGone(base::TerminationStatus status) { | 83 void InfoBarService::RenderProcessGone(base::TerminationStatus status) { |
69 infobar_manager_.RemoveAllInfoBars(true); | 84 RemoveAllInfoBars(true); |
70 } | 85 } |
71 | 86 |
72 void InfoBarService::NavigationEntryCommitted( | 87 void InfoBarService::NavigationEntryCommitted( |
73 const content::LoadCommittedDetails& load_details) { | 88 const content::LoadCommittedDetails& load_details) { |
74 infobar_manager_.OnNavigation( | 89 OnNavigation(NavigationDetailsFromLoadCommittedDetails(load_details)); |
75 NavigationDetailsFromLoadCommittedDetails(load_details)); | |
76 } | 90 } |
77 | 91 |
78 void InfoBarService::WebContentsDestroyed(content::WebContents* web_contents) { | 92 void InfoBarService::WebContentsDestroyed(content::WebContents* web_contents) { |
79 infobar_manager_.OnWebContentsDestroyed(); | 93 OnWebContentsDestroyed(); |
80 | 94 |
81 // The WebContents is going away; be aggressively paranoid and delete | 95 // The WebContents is going away; be aggressively paranoid and delete |
82 // ourselves lest other parts of the system attempt to add infobars or use | 96 // ourselves lest other parts of the system attempt to add infobars or use |
83 // us otherwise during the destruction. | 97 // us otherwise during the destruction. |
84 web_contents->RemoveUserData(UserDataKey()); | 98 web_contents->RemoveUserData(UserDataKey()); |
85 // That was the equivalent of "delete this". This object is now destroyed; | 99 // That was the equivalent of "delete this". This object is now destroyed; |
86 // returning from this function is the only safe thing to do. | 100 // returning from this function is the only safe thing to do. |
87 } | 101 } |
88 | 102 |
89 bool InfoBarService::OnMessageReceived(const IPC::Message& message) { | 103 bool InfoBarService::OnMessageReceived(const IPC::Message& message) { |
90 bool handled = true; | 104 bool handled = true; |
91 IPC_BEGIN_MESSAGE_MAP(InfoBarService, message) | 105 IPC_BEGIN_MESSAGE_MAP(InfoBarService, message) |
92 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockDisplayingInsecureContent, | 106 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockDisplayingInsecureContent, |
93 OnDidBlockDisplayingInsecureContent) | 107 OnDidBlockDisplayingInsecureContent) |
94 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockRunningInsecureContent, | 108 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockRunningInsecureContent, |
95 OnDidBlockRunningInsecureContent) | 109 OnDidBlockRunningInsecureContent) |
96 IPC_MESSAGE_UNHANDLED(handled = false) | 110 IPC_MESSAGE_UNHANDLED(handled = false) |
97 IPC_END_MESSAGE_MAP() | 111 IPC_END_MESSAGE_MAP() |
98 return handled; | 112 return handled; |
99 } | 113 } |
100 | 114 |
101 void InfoBarService::OnInfoBarAdded(InfoBar* infobar) { | |
102 // TODO(droger): Remove the notifications and have listeners change to be | |
103 // NavigationManager::Observers instead. See http://crbug.com/354380 | |
104 content::NotificationService::current()->Notify( | |
105 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, | |
106 content::Source<InfoBarService>(this), | |
107 content::Details<InfoBar::AddedDetails>(infobar)); | |
108 } | |
109 | |
110 void InfoBarService::OnInfoBarReplaced(InfoBar* old_infobar, | |
111 InfoBar* new_infobar) { | |
112 // TODO(droger): Remove the notifications and have listeners change to be | |
113 // NavigationManager::Observers instead. See http://crbug.com/354380 | |
114 InfoBar::ReplacedDetails replaced_details(old_infobar, new_infobar); | |
115 content::NotificationService::current()->Notify( | |
116 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED, | |
117 content::Source<InfoBarService>(this), | |
118 content::Details<InfoBar::ReplacedDetails>(&replaced_details)); | |
119 } | |
120 | |
121 void InfoBarService::OnInfoBarRemoved(InfoBar* infobar, bool animate) { | |
122 // TODO(droger): Remove the notifications and have listeners change to be | |
123 // NavigationManager::Observers instead. See http://crbug.com/354380 | |
124 InfoBar::RemovedDetails removed_details(infobar, animate); | |
125 content::NotificationService::current()->Notify( | |
126 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | |
127 content::Source<InfoBarService>(this), | |
128 content::Details<InfoBar::RemovedDetails>(&removed_details)); | |
129 } | |
130 | |
131 void InfoBarService::OnManagerShuttingDown(InfoBarManager* manager) { | |
132 infobar_manager_.RemoveObserver(this); | |
133 } | |
134 | |
135 void InfoBarService::OnDidBlockDisplayingInsecureContent() { | 115 void InfoBarService::OnDidBlockDisplayingInsecureContent() { |
136 InsecureContentInfoBarDelegate::Create( | 116 InsecureContentInfoBarDelegate::Create( |
137 this, InsecureContentInfoBarDelegate::DISPLAY); | 117 this, InsecureContentInfoBarDelegate::DISPLAY); |
138 } | 118 } |
139 | 119 |
140 void InfoBarService::OnDidBlockRunningInsecureContent() { | 120 void InfoBarService::OnDidBlockRunningInsecureContent() { |
141 InsecureContentInfoBarDelegate::Create(this, | 121 InsecureContentInfoBarDelegate::Create(this, |
142 InsecureContentInfoBarDelegate::RUN); | 122 InsecureContentInfoBarDelegate::RUN); |
143 } | 123 } |
OLD | NEW |