OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/ui/tab_contents/tab_contents_wrapper.h" | 5 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "chrome/browser/autocomplete_history_manager.h" | 9 #include "chrome/browser/autocomplete_history_manager.h" |
10 #include "chrome/browser/autofill/autofill_external_delegate.h" | 10 #include "chrome/browser/autofill/autofill_external_delegate.h" |
11 #include "chrome/browser/autofill/autofill_manager.h" | 11 #include "chrome/browser/autofill/autofill_manager.h" |
12 #include "chrome/browser/automation/automation_tab_helper.h" | 12 #include "chrome/browser/automation/automation_tab_helper.h" |
13 #include "chrome/browser/browser_process.h" | |
14 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | 13 #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
15 #include "chrome/browser/download/download_request_limiter_observer.h" | 14 #include "chrome/browser/download/download_request_limiter_observer.h" |
16 #include "chrome/browser/extensions/extension_tab_helper.h" | 15 #include "chrome/browser/extensions/extension_tab_helper.h" |
17 #include "chrome/browser/extensions/extension_webnavigation_api.h" | 16 #include "chrome/browser/extensions/extension_webnavigation_api.h" |
18 #include "chrome/browser/external_protocol/external_protocol_observer.h" | 17 #include "chrome/browser/external_protocol/external_protocol_observer.h" |
19 #include "chrome/browser/favicon/favicon_tab_helper.h" | 18 #include "chrome/browser/favicon/favicon_tab_helper.h" |
20 #include "chrome/browser/google/google_util.h" | |
21 #include "chrome/browser/history/history_tab_helper.h" | 19 #include "chrome/browser/history/history_tab_helper.h" |
22 #include "chrome/browser/infobars/infobar_tab_helper.h" | 20 #include "chrome/browser/infobars/infobar_tab_helper.h" |
23 #include "chrome/browser/omnibox_search_hint.h" | 21 #include "chrome/browser/omnibox_search_hint.h" |
24 #include "chrome/browser/password_manager/password_manager.h" | 22 #include "chrome/browser/password_manager/password_manager.h" |
25 #include "chrome/browser/password_manager_delegate_impl.h" | 23 #include "chrome/browser/password_manager_delegate_impl.h" |
26 #include "chrome/browser/pdf_unsupported_feature.h" | |
27 #include "chrome/browser/plugin_observer.h" | 24 #include "chrome/browser/plugin_observer.h" |
28 #include "chrome/browser/prefs/pref_service.h" | |
29 #include "chrome/browser/prerender/prerender_tab_helper.h" | 25 #include "chrome/browser/prerender/prerender_tab_helper.h" |
30 #include "chrome/browser/printing/print_preview_message_handler.h" | 26 #include "chrome/browser/printing/print_preview_message_handler.h" |
31 #include "chrome/browser/printing/print_view_manager.h" | 27 #include "chrome/browser/printing/print_view_manager.h" |
32 #include "chrome/browser/profiles/profile.h" | 28 #include "chrome/browser/safe_browsing/safe_browsing_tab_observer.h" |
33 #include "chrome/browser/renderer_host/web_cache_manager.h" | |
34 #include "chrome/browser/sessions/restore_tab_helper.h" | 29 #include "chrome/browser/sessions/restore_tab_helper.h" |
35 #include "chrome/browser/safe_browsing/client_side_detection_host.h" | |
36 #include "chrome/browser/tab_contents/tab_contents_ssl_helper.h" | 30 #include "chrome/browser/tab_contents/tab_contents_ssl_helper.h" |
37 #include "chrome/browser/tab_contents/thumbnail_generator.h" | 31 #include "chrome/browser/tab_contents/thumbnail_generator.h" |
38 #include "chrome/browser/translate/translate_tab_helper.h" | 32 #include "chrome/browser/translate/translate_tab_helper.h" |
| 33 #include "chrome/browser/ui/alternate_error_tab_observer.h" |
39 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h" | 34 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h" |
40 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h" | 35 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h" |
41 #include "chrome/browser/ui/constrained_window_tab_helper.h" | 36 #include "chrome/browser/ui/constrained_window_tab_helper.h" |
42 #include "chrome/browser/ui/find_bar/find_tab_helper.h" | 37 #include "chrome/browser/ui/find_bar/find_tab_helper.h" |
43 #include "chrome/browser/ui/intents/web_intent_picker_factory_impl.h" | 38 #include "chrome/browser/ui/intents/web_intent_picker_factory_impl.h" |
44 #include "chrome/browser/ui/intents/web_intent_picker_controller.h" | 39 #include "chrome/browser/ui/intents/web_intent_picker_controller.h" |
| 40 #include "chrome/browser/ui/pdf/pdf_tab_observer.h" |
45 #include "chrome/browser/ui/prefs/prefs_tab_helper.h" | 41 #include "chrome/browser/ui/prefs/prefs_tab_helper.h" |
46 #include "chrome/browser/ui/sad_tab_observer.h" | 42 #include "chrome/browser/ui/sad_tab_observer.h" |
47 #include "chrome/browser/ui/search_engines/search_engine_tab_helper.h" | 43 #include "chrome/browser/ui/search_engines/search_engine_tab_helper.h" |
| 44 #include "chrome/browser/ui/snapshot_tab_helper.h" |
48 #include "chrome/browser/ui/sync/tab_contents_wrapper_synced_tab_delegate.h" | 45 #include "chrome/browser/ui/sync/tab_contents_wrapper_synced_tab_delegate.h" |
49 #include "chrome/common/chrome_notification_types.h" | 46 #include "chrome/browser/ui/tab_contents/core_tab_helper.h" |
50 #include "chrome/common/chrome_switches.h" | 47 #include "chrome/common/chrome_switches.h" |
51 #include "chrome/common/pref_names.h" | |
52 #include "chrome/common/render_messages.h" | |
53 #include "content/browser/renderer_host/render_view_host.h" | |
54 #include "content/public/browser/notification_details.h" | |
55 #include "content/public/browser/notification_service.h" | |
56 #include "grit/generated_resources.h" | |
57 #include "grit/locale_settings.h" | |
58 #include "grit/platform_locale_settings.h" | |
59 #include "ui/base/l10n/l10n_util.h" | |
60 | 48 |
61 namespace { | 49 namespace { |
62 | 50 |
63 static base::LazyInstance<base::PropertyAccessor<TabContentsWrapper*> > | 51 static base::LazyInstance<base::PropertyAccessor<TabContentsWrapper*> > |
64 g_tab_contents_wrapper_property_accessor = LAZY_INSTANCE_INITIALIZER; | 52 g_tab_contents_wrapper_property_accessor = LAZY_INSTANCE_INITIALIZER; |
65 | 53 |
66 // The list of prefs we want to observe. | |
67 const char* kPrefsToObserve[] = { | |
68 prefs::kAlternateErrorPagesEnabled, | |
69 prefs::kDefaultZoomLevel, | |
70 #if defined (ENABLE_SAFE_BROWSING) | |
71 prefs::kSafeBrowsingEnabled, | |
72 #endif | |
73 }; | |
74 | |
75 const int kPrefsToObserveLength = arraysize(kPrefsToObserve); | |
76 | |
77 } // namespace | 54 } // namespace |
78 | 55 |
79 //////////////////////////////////////////////////////////////////////////////// | 56 //////////////////////////////////////////////////////////////////////////////// |
80 // TabContentsWrapper, public: | 57 // TabContentsWrapper, public: |
81 | 58 |
82 TabContentsWrapper::TabContentsWrapper(TabContents* contents) | 59 TabContentsWrapper::TabContentsWrapper(TabContents* contents) |
83 : TabContentsObserver(contents), | 60 : TabContentsObserver(contents), |
84 delegate_(NULL), | |
85 in_destructor_(false), | 61 in_destructor_(false), |
86 tab_contents_(contents) { | 62 tab_contents_(contents) { |
87 DCHECK(contents); | 63 DCHECK(contents); |
88 DCHECK(!GetCurrentWrapperForContents(contents)); | 64 DCHECK(!GetCurrentWrapperForContents(contents)); |
| 65 |
89 // Stash this in the property bag so it can be retrieved without having to | 66 // Stash this in the property bag so it can be retrieved without having to |
90 // go to a Browser. | 67 // go to a Browser. |
91 property_accessor()->SetProperty(contents->property_bag(), this); | 68 property_accessor()->SetProperty(contents->property_bag(), this); |
92 | 69 |
93 PrefService* prefs = profile()->GetPrefs(); | |
94 pref_change_registrar_.Init(prefs); | |
95 if (prefs) { | |
96 for (int i = 0; i < kPrefsToObserveLength; ++i) | |
97 pref_change_registrar_.Add(kPrefsToObserve[i], this); | |
98 } | |
99 | |
100 // Create the tab helpers. | 70 // Create the tab helpers. |
101 autocomplete_history_manager_.reset(new AutocompleteHistoryManager(contents)); | 71 autocomplete_history_manager_.reset(new AutocompleteHistoryManager(contents)); |
102 autofill_manager_ = new AutofillManager(this); | 72 autofill_manager_ = new AutofillManager(this); |
103 if (CommandLine::ForCurrentProcess()->HasSwitch( | 73 if (CommandLine::ForCurrentProcess()->HasSwitch( |
104 switches::kExternalAutofillPopup)) { | 74 switches::kExternalAutofillPopup)) { |
105 autofill_external_delegate_.reset( | 75 autofill_external_delegate_.reset( |
106 AutofillExternalDelegate::Create(this, autofill_manager_.get())); | 76 AutofillExternalDelegate::Create(this, autofill_manager_.get())); |
107 autofill_manager_->SetExternalDelegate(autofill_external_delegate_.get()); | 77 autofill_manager_->SetExternalDelegate(autofill_external_delegate_.get()); |
108 autocomplete_history_manager_->SetExternalDelegate( | 78 autocomplete_history_manager_->SetExternalDelegate( |
109 autofill_external_delegate_.get()); | 79 autofill_external_delegate_.get()); |
110 } | 80 } |
111 automation_tab_helper_.reset(new AutomationTabHelper(contents)); | 81 automation_tab_helper_.reset(new AutomationTabHelper(contents)); |
112 blocked_content_tab_helper_.reset(new BlockedContentTabHelper(this)); | 82 blocked_content_tab_helper_.reset(new BlockedContentTabHelper(this)); |
113 bookmark_tab_helper_.reset(new BookmarkTabHelper(this)); | 83 bookmark_tab_helper_.reset(new BookmarkTabHelper(this)); |
114 constrained_window_tab_helper_.reset(new ConstrainedWindowTabHelper(this)); | 84 constrained_window_tab_helper_.reset(new ConstrainedWindowTabHelper(this)); |
| 85 core_tab_helper_.reset(new CoreTabHelper(this)); |
115 extension_tab_helper_.reset(new ExtensionTabHelper(this)); | 86 extension_tab_helper_.reset(new ExtensionTabHelper(this)); |
116 favicon_tab_helper_.reset(new FaviconTabHelper(contents)); | 87 favicon_tab_helper_.reset(new FaviconTabHelper(contents)); |
117 find_tab_helper_.reset(new FindTabHelper(contents)); | 88 find_tab_helper_.reset(new FindTabHelper(contents)); |
118 history_tab_helper_.reset(new HistoryTabHelper(contents)); | 89 history_tab_helper_.reset(new HistoryTabHelper(contents)); |
119 infobar_tab_helper_.reset(new InfoBarTabHelper(contents)); | 90 infobar_tab_helper_.reset(new InfoBarTabHelper(contents)); |
120 password_manager_delegate_.reset(new PasswordManagerDelegateImpl(this)); | 91 password_manager_delegate_.reset(new PasswordManagerDelegateImpl(this)); |
121 password_manager_.reset( | 92 password_manager_.reset( |
122 new PasswordManager(contents, password_manager_delegate_.get())); | 93 new PasswordManager(contents, password_manager_delegate_.get())); |
123 prefs_tab_helper_.reset(new PrefsTabHelper(this)); | 94 prefs_tab_helper_.reset(new PrefsTabHelper(this)); |
124 prerender_tab_helper_.reset(new prerender::PrerenderTabHelper(this)); | 95 prerender_tab_helper_.reset(new prerender::PrerenderTabHelper(this)); |
125 print_view_manager_.reset(new printing::PrintViewManager(this)); | 96 print_view_manager_.reset(new printing::PrintViewManager(this)); |
126 restore_tab_helper_.reset(new RestoreTabHelper(this)); | 97 restore_tab_helper_.reset(new RestoreTabHelper(this)); |
127 #if defined(ENABLE_SAFE_BROWSING) | |
128 if (profile()->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && | |
129 g_browser_process->safe_browsing_detection_service()) { | |
130 safebrowsing_detection_host_.reset( | |
131 safe_browsing::ClientSideDetectionHost::Create(contents)); | |
132 } | |
133 #endif | |
134 search_engine_tab_helper_.reset(new SearchEngineTabHelper(contents)); | 98 search_engine_tab_helper_.reset(new SearchEngineTabHelper(contents)); |
| 99 snapshot_tab_helper_.reset(new SnapshotTabHelper(this)); |
135 ssl_helper_.reset(new TabContentsSSLHelper(this)); | 100 ssl_helper_.reset(new TabContentsSSLHelper(this)); |
136 synced_tab_delegate_.reset(new TabContentsWrapperSyncedTabDelegate(this)); | 101 synced_tab_delegate_.reset(new TabContentsWrapperSyncedTabDelegate(this)); |
137 content_settings_.reset(new TabSpecificContentSettings(contents)); | 102 content_settings_.reset(new TabSpecificContentSettings(contents)); |
138 translate_tab_helper_.reset(new TranslateTabHelper(contents)); | 103 translate_tab_helper_.reset(new TranslateTabHelper(contents)); |
139 web_intent_picker_controller_.reset(new WebIntentPickerController( | 104 web_intent_picker_controller_.reset(new WebIntentPickerController( |
140 this, new WebIntentPickerFactoryImpl())); | 105 this, new WebIntentPickerFactoryImpl())); |
141 | 106 |
142 // Create the per-tab observers. | 107 // Create the per-tab observers. |
| 108 alternate_error_page_tab_observer_.reset( |
| 109 new AlternateErrorPageTabObserver(this)); |
143 download_request_limiter_observer_.reset( | 110 download_request_limiter_observer_.reset( |
144 new DownloadRequestLimiterObserver(contents)); | 111 new DownloadRequestLimiterObserver(contents)); |
145 webnavigation_observer_.reset( | 112 webnavigation_observer_.reset( |
146 new ExtensionWebNavigationTabObserver(contents)); | 113 new ExtensionWebNavigationTabObserver(contents)); |
147 external_protocol_observer_.reset(new ExternalProtocolObserver(contents)); | 114 external_protocol_observer_.reset(new ExternalProtocolObserver(contents)); |
| 115 if (OmniboxSearchHint::IsEnabled(profile())) |
| 116 omnibox_search_hint_.reset(new OmniboxSearchHint(this)); |
| 117 pdf_tab_observer_.reset(new PDFTabObserver(this)); |
148 plugin_observer_.reset(new PluginObserver(this)); | 118 plugin_observer_.reset(new PluginObserver(this)); |
149 print_preview_.reset(new printing::PrintPreviewMessageHandler(contents)); | 119 print_preview_.reset(new printing::PrintPreviewMessageHandler(contents)); |
150 sad_tab_observer_.reset(new SadTabObserver(contents)); | 120 sad_tab_observer_.reset(new SadTabObserver(contents)); |
| 121 safe_browsing_tab_observer_.reset( |
| 122 new safe_browsing::SafeBrowsingTabObserver(this)); |
| 123 |
151 // Start the in-browser thumbnailing if the feature is enabled. | 124 // Start the in-browser thumbnailing if the feature is enabled. |
152 if (switches::IsInBrowserThumbnailingEnabled()) { | 125 if (switches::IsInBrowserThumbnailingEnabled()) { |
153 thumbnail_generation_observer_.reset(new ThumbnailGenerator); | 126 thumbnail_generation_observer_.reset(new ThumbnailGenerator); |
154 thumbnail_generation_observer_->StartThumbnailing(tab_contents_.get()); | 127 thumbnail_generation_observer_->StartThumbnailing(tab_contents_.get()); |
155 } | 128 } |
156 | |
157 // Set-up the showing of the omnibox search infobar if applicable. | |
158 if (OmniboxSearchHint::IsEnabled(profile())) | |
159 omnibox_search_hint_.reset(new OmniboxSearchHint(this)); | |
160 | |
161 registrar_.Add(this, chrome::NOTIFICATION_GOOGLE_URL_UPDATED, | |
162 content::NotificationService::AllSources()); | |
163 } | 129 } |
164 | 130 |
165 TabContentsWrapper::~TabContentsWrapper() { | 131 TabContentsWrapper::~TabContentsWrapper() { |
166 in_destructor_ = true; | 132 in_destructor_ = true; |
167 | 133 |
168 // Need to tear down infobars before the TabContents goes away. | 134 // Need to tear down infobars before the TabContents goes away. |
| 135 // TODO(avi): Can we get this handled by the tab helper itself? |
169 infobar_tab_helper_.reset(); | 136 infobar_tab_helper_.reset(); |
170 } | 137 } |
171 | 138 |
172 base::PropertyAccessor<TabContentsWrapper*>* | 139 base::PropertyAccessor<TabContentsWrapper*>* |
173 TabContentsWrapper::property_accessor() { | 140 TabContentsWrapper::property_accessor() { |
174 return g_tab_contents_wrapper_property_accessor.Pointer(); | 141 return g_tab_contents_wrapper_property_accessor.Pointer(); |
175 } | 142 } |
176 | 143 |
177 void TabContentsWrapper::RegisterUserPrefs(PrefService* prefs) { | |
178 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, | |
179 true, | |
180 PrefService::SYNCABLE_PREF); | |
181 } | |
182 | |
183 string16 TabContentsWrapper::GetDefaultTitle() { | |
184 return l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE); | |
185 } | |
186 | |
187 string16 TabContentsWrapper::GetStatusText() const { | |
188 if (!tab_contents()->IsLoading() || | |
189 tab_contents()->load_state().state == net::LOAD_STATE_IDLE) { | |
190 return string16(); | |
191 } | |
192 | |
193 switch (tab_contents()->load_state().state) { | |
194 case net::LOAD_STATE_WAITING_FOR_DELEGATE: | |
195 return l10n_util::GetStringFUTF16(IDS_LOAD_STATE_WAITING_FOR_DELEGATE, | |
196 tab_contents()->load_state().param); | |
197 case net::LOAD_STATE_WAITING_FOR_CACHE: | |
198 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_WAITING_FOR_CACHE); | |
199 case net::LOAD_STATE_WAITING_FOR_APPCACHE: | |
200 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_WAITING_FOR_APPCACHE); | |
201 case net::LOAD_STATE_ESTABLISHING_PROXY_TUNNEL: | |
202 return | |
203 l10n_util::GetStringUTF16(IDS_LOAD_STATE_ESTABLISHING_PROXY_TUNNEL); | |
204 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL: | |
205 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL); | |
206 case net::LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT: | |
207 return l10n_util::GetStringUTF16( | |
208 IDS_LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT); | |
209 case net::LOAD_STATE_RESOLVING_HOST: | |
210 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_RESOLVING_HOST); | |
211 case net::LOAD_STATE_CONNECTING: | |
212 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_CONNECTING); | |
213 case net::LOAD_STATE_SSL_HANDSHAKE: | |
214 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_SSL_HANDSHAKE); | |
215 case net::LOAD_STATE_SENDING_REQUEST: | |
216 if (tab_contents()->upload_size()) | |
217 return l10n_util::GetStringFUTF16Int( | |
218 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS, | |
219 static_cast<int>((100 * tab_contents()->upload_position()) / | |
220 tab_contents()->upload_size())); | |
221 else | |
222 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_SENDING_REQUEST); | |
223 case net::LOAD_STATE_WAITING_FOR_RESPONSE: | |
224 return l10n_util::GetStringFUTF16(IDS_LOAD_STATE_WAITING_FOR_RESPONSE, | |
225 tab_contents()->load_state_host()); | |
226 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE | |
227 case net::LOAD_STATE_IDLE: | |
228 case net::LOAD_STATE_READING_RESPONSE: | |
229 break; | |
230 } | |
231 | |
232 return string16(); | |
233 } | |
234 | |
235 TabContentsWrapper* TabContentsWrapper::Clone() { | 144 TabContentsWrapper* TabContentsWrapper::Clone() { |
236 TabContents* new_contents = tab_contents()->Clone(); | 145 TabContents* new_contents = tab_contents()->Clone(); |
237 TabContentsWrapper* new_wrapper = new TabContentsWrapper(new_contents); | 146 TabContentsWrapper* new_wrapper = new TabContentsWrapper(new_contents); |
238 | 147 |
| 148 // TODO(avi): Can we generalize this so that knowledge of the functionings of |
| 149 // the tab helpers isn't required here? |
239 new_wrapper->extension_tab_helper()->CopyStateFrom( | 150 new_wrapper->extension_tab_helper()->CopyStateFrom( |
240 *extension_tab_helper_.get()); | 151 *extension_tab_helper_.get()); |
241 return new_wrapper; | 152 return new_wrapper; |
242 } | 153 } |
243 | 154 |
244 void TabContentsWrapper::CaptureSnapshot() { | |
245 Send(new ChromeViewMsg_CaptureSnapshot(routing_id())); | |
246 } | |
247 | |
248 // static | 155 // static |
249 TabContentsWrapper* TabContentsWrapper::GetCurrentWrapperForContents( | 156 TabContentsWrapper* TabContentsWrapper::GetCurrentWrapperForContents( |
250 TabContents* contents) { | 157 TabContents* contents) { |
251 TabContentsWrapper** wrapper = | 158 TabContentsWrapper** wrapper = |
252 property_accessor()->GetProperty(contents->property_bag()); | 159 property_accessor()->GetProperty(contents->property_bag()); |
253 | 160 |
254 return wrapper ? *wrapper : NULL; | 161 return wrapper ? *wrapper : NULL; |
255 } | 162 } |
256 | 163 |
257 // static | 164 // static |
258 const TabContentsWrapper* TabContentsWrapper::GetCurrentWrapperForContents( | 165 const TabContentsWrapper* TabContentsWrapper::GetCurrentWrapperForContents( |
259 const TabContents* contents) { | 166 const TabContents* contents) { |
260 TabContentsWrapper* const* wrapper = | 167 TabContentsWrapper* const* wrapper = |
261 property_accessor()->GetProperty(contents->property_bag()); | 168 property_accessor()->GetProperty(contents->property_bag()); |
262 | 169 |
263 return wrapper ? *wrapper : NULL; | 170 return wrapper ? *wrapper : NULL; |
264 } | 171 } |
265 | 172 |
266 Profile* TabContentsWrapper::profile() const { | 173 Profile* TabContentsWrapper::profile() const { |
267 return Profile::FromBrowserContext(tab_contents()->browser_context()); | 174 return Profile::FromBrowserContext(tab_contents()->browser_context()); |
268 } | 175 } |
269 | 176 |
270 //////////////////////////////////////////////////////////////////////////////// | 177 //////////////////////////////////////////////////////////////////////////////// |
271 // TabContentsWrapper implementation: | 178 // TabContentsObserver overrides |
272 | |
273 void TabContentsWrapper::RenderViewCreated(RenderViewHost* render_view_host) { | |
274 UpdateAlternateErrorPageURL(render_view_host); | |
275 } | |
276 | |
277 void TabContentsWrapper::DidBecomeSelected() { | |
278 WebCacheManager::GetInstance()->ObserveActivity( | |
279 tab_contents()->GetRenderProcessHost()->GetID()); | |
280 } | |
281 | |
282 bool TabContentsWrapper::OnMessageReceived(const IPC::Message& message) { | |
283 bool handled = true; | |
284 IPC_BEGIN_MESSAGE_MAP(TabContentsWrapper, message) | |
285 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_Snapshot, OnSnapshot) | |
286 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PDFHasUnsupportedFeature, | |
287 OnPDFHasUnsupportedFeature) | |
288 IPC_MESSAGE_UNHANDLED(handled = false) | |
289 IPC_END_MESSAGE_MAP() | |
290 return handled; | |
291 } | |
292 | 179 |
293 void TabContentsWrapper::TabContentsDestroyed(TabContents* tab) { | 180 void TabContentsWrapper::TabContentsDestroyed(TabContents* tab) { |
294 // Destruction of the TabContents should only be done by us from our | 181 // Destruction of the TabContents should only be done by us from our |
295 // destructor. Otherwise it's very likely we (or one of the helpers we own) | 182 // destructor. Otherwise it's very likely we (or one of the helpers we own) |
296 // will attempt to access the TabContents and we'll crash. | 183 // will attempt to access the TabContents and we'll crash. |
297 DCHECK(in_destructor_); | 184 DCHECK(in_destructor_); |
298 } | 185 } |
299 | |
300 void TabContentsWrapper::Observe(int type, | |
301 const content::NotificationSource& source, | |
302 const content::NotificationDetails& details) { | |
303 switch (type) { | |
304 case chrome::NOTIFICATION_GOOGLE_URL_UPDATED: | |
305 UpdateAlternateErrorPageURL(render_view_host()); | |
306 break; | |
307 case chrome::NOTIFICATION_PREF_CHANGED: { | |
308 std::string* pref_name_in = content::Details<std::string>(details).ptr(); | |
309 DCHECK(content::Source<PrefService>(source).ptr() == | |
310 profile()->GetPrefs()); | |
311 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) { | |
312 UpdateAlternateErrorPageURL(render_view_host()); | |
313 } else if (*pref_name_in == prefs::kDefaultZoomLevel) { | |
314 tab_contents()->render_view_host()->SetZoomLevel( | |
315 tab_contents()->GetZoomLevel()); | |
316 } else if (*pref_name_in == prefs::kSafeBrowsingEnabled) { | |
317 UpdateSafebrowsingDetectionHost(); | |
318 } else { | |
319 NOTREACHED() << "unexpected pref change notification" << *pref_name_in; | |
320 } | |
321 break; | |
322 } | |
323 default: | |
324 NOTREACHED(); | |
325 } | |
326 } | |
327 | |
328 //////////////////////////////////////////////////////////////////////////////// | |
329 // Internal helpers | |
330 | |
331 void TabContentsWrapper::OnSnapshot(const SkBitmap& bitmap) { | |
332 content::NotificationService::current()->Notify( | |
333 chrome::NOTIFICATION_TAB_SNAPSHOT_TAKEN, | |
334 content::Source<TabContentsWrapper>(this), | |
335 content::Details<const SkBitmap>(&bitmap)); | |
336 } | |
337 | |
338 void TabContentsWrapper::OnPDFHasUnsupportedFeature() { | |
339 PDFHasUnsupportedFeature(this); | |
340 } | |
341 | |
342 GURL TabContentsWrapper::GetAlternateErrorPageURL() const { | |
343 GURL url; | |
344 // Disable alternate error pages when in Incognito mode. | |
345 if (profile()->IsOffTheRecord()) | |
346 return url; | |
347 | |
348 PrefService* prefs = profile()->GetPrefs(); | |
349 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) { | |
350 url = google_util::AppendGoogleLocaleParam( | |
351 GURL(google_util::kLinkDoctorBaseURL)); | |
352 url = google_util::AppendGoogleTLDParam(url); | |
353 } | |
354 return url; | |
355 } | |
356 | |
357 void TabContentsWrapper::UpdateAlternateErrorPageURL(RenderViewHost* rvh) { | |
358 rvh->SetAltErrorPageURL(GetAlternateErrorPageURL()); | |
359 } | |
360 | |
361 void TabContentsWrapper::UpdateSafebrowsingDetectionHost() { | |
362 #if defined(ENABLE_SAFE_BROWSING) | |
363 PrefService* prefs = profile()->GetPrefs(); | |
364 bool safe_browsing = prefs->GetBoolean(prefs::kSafeBrowsingEnabled); | |
365 if (safe_browsing && | |
366 g_browser_process->safe_browsing_detection_service()) { | |
367 if (!safebrowsing_detection_host_.get()) { | |
368 safebrowsing_detection_host_.reset( | |
369 safe_browsing::ClientSideDetectionHost::Create(tab_contents())); | |
370 } | |
371 } else { | |
372 safebrowsing_detection_host_.reset(); | |
373 } | |
374 render_view_host()->Send( | |
375 new ChromeViewMsg_SetClientSidePhishingDetection(routing_id(), | |
376 safe_browsing)); | |
377 #endif | |
378 } | |
379 | |
380 void TabContentsWrapper::ExitFullscreenMode() { | |
381 if (tab_contents() && render_view_host()) | |
382 tab_contents()->render_view_host()->ExitFullscreen(); | |
383 } | |
OLD | NEW |