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 // static |
51 return infobar_manager_.AddInfoBar(infobar.Pass()); | 41 content::WebContents* InfoBarService::WebContentsFromInfoBar(InfoBar* infobar) { |
52 } | 42 if (!infobar || !infobar->owner()) |
53 | 43 return NULL; |
54 InfoBar* InfoBarService::ReplaceInfoBar(InfoBar* old_infobar, | 44 InfoBarService* infobar_service = |
55 scoped_ptr<InfoBar> new_infobar) { | 45 static_cast<InfoBarService*>(infobar->owner()); |
56 return infobar_manager_.ReplaceInfoBar(old_infobar, new_infobar.Pass()); | 46 return infobar_service->web_contents(); |
57 } | 47 } |
58 | 48 |
59 InfoBarService::InfoBarService(content::WebContents* web_contents) | 49 InfoBarService::InfoBarService(content::WebContents* web_contents) |
60 : content::WebContentsObserver(web_contents), | 50 : content::WebContentsObserver(web_contents) { |
61 infobar_manager_(web_contents) { | |
62 DCHECK(web_contents); | 51 DCHECK(web_contents); |
63 infobar_manager_.AddObserver(this); | |
64 } | 52 } |
65 | 53 |
66 InfoBarService::~InfoBarService() {} | 54 InfoBarService::~InfoBarService() { |
| 55 ShutDown(); |
| 56 } |
| 57 |
| 58 void InfoBarService::NotifyInfoBarAdded(InfoBar* infobar) { |
| 59 InfoBarManager::NotifyInfoBarAdded(infobar); |
| 60 // TODO(droger): Remove the notifications and have listeners change to be |
| 61 // NavigationManager::Observers instead. See http://crbug.com/354380 |
| 62 content::NotificationService::current()->Notify( |
| 63 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, |
| 64 content::Source<InfoBarService>(this), |
| 65 content::Details<InfoBar::AddedDetails>(infobar)); |
| 66 } |
| 67 |
| 68 void InfoBarService::NotifyInfoBarRemoved(InfoBar* infobar, bool animate) { |
| 69 InfoBarManager::NotifyInfoBarRemoved(infobar, animate); |
| 70 // TODO(droger): Remove the notifications and have listeners change to be |
| 71 // NavigationManager::Observers instead. See http://crbug.com/354380 |
| 72 InfoBar::RemovedDetails removed_details(infobar, animate); |
| 73 content::NotificationService::current()->Notify( |
| 74 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, |
| 75 content::Source<InfoBarService>(this), |
| 76 content::Details<InfoBar::RemovedDetails>(&removed_details)); |
| 77 } |
| 78 |
| 79 void InfoBarService::NotifyInfoBarReplaced(InfoBar* old_infobar, |
| 80 InfoBar* new_infobar) { |
| 81 InfoBarManager::NotifyInfoBarReplaced(old_infobar, new_infobar); |
| 82 // TODO(droger): Remove the notifications and have listeners change to be |
| 83 // NavigationManager::Observers instead. See http://crbug.com/354380 |
| 84 InfoBar::ReplacedDetails replaced_details(old_infobar, new_infobar); |
| 85 content::NotificationService::current()->Notify( |
| 86 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED, |
| 87 content::Source<InfoBarService>(this), |
| 88 content::Details<InfoBar::ReplacedDetails>(&replaced_details)); |
| 89 } |
67 | 90 |
68 void InfoBarService::RenderProcessGone(base::TerminationStatus status) { | 91 void InfoBarService::RenderProcessGone(base::TerminationStatus status) { |
69 infobar_manager_.RemoveAllInfoBars(true); | 92 RemoveAllInfoBars(true); |
70 } | 93 } |
71 | 94 |
72 void InfoBarService::NavigationEntryCommitted( | 95 void InfoBarService::NavigationEntryCommitted( |
73 const content::LoadCommittedDetails& load_details) { | 96 const content::LoadCommittedDetails& load_details) { |
74 infobar_manager_.OnNavigation( | 97 OnNavigation(NavigationDetailsFromLoadCommittedDetails(load_details)); |
75 NavigationDetailsFromLoadCommittedDetails(load_details)); | |
76 } | 98 } |
77 | 99 |
78 void InfoBarService::WebContentsDestroyed(content::WebContents* web_contents) { | 100 void InfoBarService::WebContentsDestroyed(content::WebContents* web_contents) { |
79 infobar_manager_.OnWebContentsDestroyed(); | |
80 | |
81 // The WebContents is going away; be aggressively paranoid and delete | 101 // The WebContents is going away; be aggressively paranoid and delete |
82 // ourselves lest other parts of the system attempt to add infobars or use | 102 // ourselves lest other parts of the system attempt to add infobars or use |
83 // us otherwise during the destruction. | 103 // us otherwise during the destruction. |
84 web_contents->RemoveUserData(UserDataKey()); | 104 web_contents->RemoveUserData(UserDataKey()); |
85 // That was the equivalent of "delete this". This object is now destroyed; | 105 // That was the equivalent of "delete this". This object is now destroyed; |
86 // returning from this function is the only safe thing to do. | 106 // returning from this function is the only safe thing to do. |
87 } | 107 } |
88 | 108 |
89 bool InfoBarService::OnMessageReceived(const IPC::Message& message) { | 109 bool InfoBarService::OnMessageReceived(const IPC::Message& message) { |
90 bool handled = true; | 110 bool handled = true; |
91 IPC_BEGIN_MESSAGE_MAP(InfoBarService, message) | 111 IPC_BEGIN_MESSAGE_MAP(InfoBarService, message) |
92 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockDisplayingInsecureContent, | 112 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockDisplayingInsecureContent, |
93 OnDidBlockDisplayingInsecureContent) | 113 OnDidBlockDisplayingInsecureContent) |
94 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockRunningInsecureContent, | 114 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockRunningInsecureContent, |
95 OnDidBlockRunningInsecureContent) | 115 OnDidBlockRunningInsecureContent) |
96 IPC_MESSAGE_UNHANDLED(handled = false) | 116 IPC_MESSAGE_UNHANDLED(handled = false) |
97 IPC_END_MESSAGE_MAP() | 117 IPC_END_MESSAGE_MAP() |
98 return handled; | 118 return handled; |
99 } | 119 } |
100 | 120 |
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() { | 121 void InfoBarService::OnDidBlockDisplayingInsecureContent() { |
136 InsecureContentInfoBarDelegate::Create( | 122 InsecureContentInfoBarDelegate::Create( |
137 this, InsecureContentInfoBarDelegate::DISPLAY); | 123 this, InsecureContentInfoBarDelegate::DISPLAY); |
138 } | 124 } |
139 | 125 |
140 void InfoBarService::OnDidBlockRunningInsecureContent() { | 126 void InfoBarService::OnDidBlockRunningInsecureContent() { |
141 InsecureContentInfoBarDelegate::Create(this, | 127 InsecureContentInfoBarDelegate::Create(this, |
142 InsecureContentInfoBarDelegate::RUN); | 128 InsecureContentInfoBarDelegate::RUN); |
143 } | 129 } |
OLD | NEW |