OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/tab_contents/core_tab_helper.h" |
| 6 |
| 7 #include "chrome/browser/google/google_util.h" |
| 8 #include "chrome/browser/pdf_unsupported_feature.h" |
| 9 #include "chrome/browser/prefs/pref_service.h" |
| 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/renderer_host/web_cache_manager.h" |
| 12 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 13 #include "chrome/common/chrome_notification_types.h" |
| 14 #include "chrome/common/pref_names.h" |
| 15 #include "chrome/common/render_messages.h" |
| 16 #include "content/browser/renderer_host/render_view_host.h" |
| 17 #include "content/public/browser/notification_service.h" |
| 18 #include "grit/generated_resources.h" |
| 19 #include "ui/base/l10n/l10n_util.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 // The list of prefs we want to observe. |
| 24 const char* kPrefsToObserve[] = { |
| 25 prefs::kAlternateErrorPagesEnabled, |
| 26 prefs::kDefaultZoomLevel, |
| 27 }; |
| 28 |
| 29 const int kPrefsToObserveLength = arraysize(kPrefsToObserve); |
| 30 |
| 31 } // namespace |
| 32 |
| 33 CoreTabHelper::CoreTabHelper(TabContentsWrapper* wrapper) |
| 34 : TabContentsObserver(wrapper->tab_contents()), |
| 35 delegate_(NULL), |
| 36 wrapper_(wrapper) { |
| 37 PrefService* prefs = wrapper_->profile()->GetPrefs(); |
| 38 pref_change_registrar_.Init(prefs); |
| 39 if (prefs) { |
| 40 for (int i = 0; i < kPrefsToObserveLength; ++i) |
| 41 pref_change_registrar_.Add(kPrefsToObserve[i], this); |
| 42 } |
| 43 |
| 44 registrar_.Add(this, chrome::NOTIFICATION_GOOGLE_URL_UPDATED, |
| 45 content::NotificationService::AllSources()); |
| 46 } |
| 47 |
| 48 CoreTabHelper::~CoreTabHelper() { |
| 49 } |
| 50 |
| 51 // static |
| 52 void CoreTabHelper::RegisterUserPrefs(PrefService* prefs) { |
| 53 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, |
| 54 true, |
| 55 PrefService::SYNCABLE_PREF); |
| 56 } |
| 57 |
| 58 string16 CoreTabHelper::GetDefaultTitle() { |
| 59 return l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE); |
| 60 } |
| 61 |
| 62 string16 CoreTabHelper::GetStatusText() const { |
| 63 if (!tab_contents()->IsLoading() || |
| 64 tab_contents()->load_state().state == net::LOAD_STATE_IDLE) { |
| 65 return string16(); |
| 66 } |
| 67 |
| 68 switch (tab_contents()->load_state().state) { |
| 69 case net::LOAD_STATE_WAITING_FOR_DELEGATE: |
| 70 return l10n_util::GetStringFUTF16(IDS_LOAD_STATE_WAITING_FOR_DELEGATE, |
| 71 tab_contents()->load_state().param); |
| 72 case net::LOAD_STATE_WAITING_FOR_CACHE: |
| 73 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_WAITING_FOR_CACHE); |
| 74 case net::LOAD_STATE_WAITING_FOR_APPCACHE: |
| 75 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_WAITING_FOR_APPCACHE); |
| 76 case net::LOAD_STATE_ESTABLISHING_PROXY_TUNNEL: |
| 77 return |
| 78 l10n_util::GetStringUTF16(IDS_LOAD_STATE_ESTABLISHING_PROXY_TUNNEL); |
| 79 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL: |
| 80 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL); |
| 81 case net::LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT: |
| 82 return l10n_util::GetStringUTF16( |
| 83 IDS_LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT); |
| 84 case net::LOAD_STATE_RESOLVING_HOST: |
| 85 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_RESOLVING_HOST); |
| 86 case net::LOAD_STATE_CONNECTING: |
| 87 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_CONNECTING); |
| 88 case net::LOAD_STATE_SSL_HANDSHAKE: |
| 89 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_SSL_HANDSHAKE); |
| 90 case net::LOAD_STATE_SENDING_REQUEST: |
| 91 if (tab_contents()->upload_size()) |
| 92 return l10n_util::GetStringFUTF16Int( |
| 93 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS, |
| 94 static_cast<int>((100 * tab_contents()->upload_position()) / |
| 95 tab_contents()->upload_size())); |
| 96 else |
| 97 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_SENDING_REQUEST); |
| 98 case net::LOAD_STATE_WAITING_FOR_RESPONSE: |
| 99 return l10n_util::GetStringFUTF16(IDS_LOAD_STATE_WAITING_FOR_RESPONSE, |
| 100 tab_contents()->load_state_host()); |
| 101 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE |
| 102 case net::LOAD_STATE_IDLE: |
| 103 case net::LOAD_STATE_READING_RESPONSE: |
| 104 break; |
| 105 } |
| 106 |
| 107 return string16(); |
| 108 } |
| 109 |
| 110 void CoreTabHelper::CaptureSnapshot() { |
| 111 Send(new ChromeViewMsg_CaptureSnapshot(routing_id())); |
| 112 } |
| 113 |
| 114 void CoreTabHelper::ExitFullscreenMode() { |
| 115 if (tab_contents() && tab_contents()->render_view_host()) |
| 116 tab_contents()->render_view_host()->ExitFullscreen(); |
| 117 } |
| 118 |
| 119 //////////////////////////////////////////////////////////////////////////////// |
| 120 // TabContentsObserver overrides |
| 121 |
| 122 void CoreTabHelper::RenderViewCreated(RenderViewHost* render_view_host) { |
| 123 UpdateAlternateErrorPageURL(render_view_host); |
| 124 } |
| 125 |
| 126 void CoreTabHelper::DidBecomeSelected() { |
| 127 // TODO(avi): Does this belong here or in some other tab helper? |
| 128 WebCacheManager::GetInstance()->ObserveActivity( |
| 129 tab_contents()->GetRenderProcessHost()->GetID()); |
| 130 } |
| 131 |
| 132 bool CoreTabHelper::OnMessageReceived(const IPC::Message& message) { |
| 133 bool handled = true; |
| 134 IPC_BEGIN_MESSAGE_MAP(CoreTabHelper, message) |
| 135 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_Snapshot, OnSnapshot) |
| 136 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PDFHasUnsupportedFeature, |
| 137 OnPDFHasUnsupportedFeature) |
| 138 IPC_MESSAGE_UNHANDLED(handled = false) |
| 139 IPC_END_MESSAGE_MAP() |
| 140 return handled; |
| 141 } |
| 142 |
| 143 //////////////////////////////////////////////////////////////////////////////// |
| 144 // content::NotificationObserver overrides |
| 145 |
| 146 void CoreTabHelper::Observe(int type, |
| 147 const content::NotificationSource& source, |
| 148 const content::NotificationDetails& details) { |
| 149 switch (type) { |
| 150 case chrome::NOTIFICATION_GOOGLE_URL_UPDATED: |
| 151 UpdateAlternateErrorPageURL(tab_contents()->render_view_host()); |
| 152 break; |
| 153 case chrome::NOTIFICATION_PREF_CHANGED: { |
| 154 std::string* pref_name = content::Details<std::string>(details).ptr(); |
| 155 DCHECK(content::Source<PrefService>(source).ptr() == |
| 156 wrapper_->profile()->GetPrefs()); |
| 157 if (*pref_name == prefs::kAlternateErrorPagesEnabled) { |
| 158 UpdateAlternateErrorPageURL(tab_contents()->render_view_host()); |
| 159 } else if (*pref_name == prefs::kDefaultZoomLevel) { |
| 160 tab_contents()->render_view_host()->SetZoomLevel( |
| 161 tab_contents()->GetZoomLevel()); |
| 162 } else { |
| 163 NOTREACHED() << "unexpected pref change notification" << *pref_name; |
| 164 } |
| 165 break; |
| 166 } |
| 167 default: |
| 168 NOTREACHED(); |
| 169 } |
| 170 } |
| 171 |
| 172 //////////////////////////////////////////////////////////////////////////////// |
| 173 // Internal helpers |
| 174 |
| 175 void CoreTabHelper::OnSnapshot(const SkBitmap& bitmap) { |
| 176 content::NotificationService::current()->Notify( |
| 177 chrome::NOTIFICATION_TAB_SNAPSHOT_TAKEN, |
| 178 content::Source<TabContentsWrapper>(wrapper_), |
| 179 content::Details<const SkBitmap>(&bitmap)); |
| 180 } |
| 181 |
| 182 void CoreTabHelper::OnPDFHasUnsupportedFeature() { |
| 183 PDFHasUnsupportedFeature(wrapper_); |
| 184 } |
| 185 |
| 186 GURL CoreTabHelper::GetAlternateErrorPageURL() const { |
| 187 GURL url; |
| 188 // Disable alternate error pages when in Incognito mode. |
| 189 if (wrapper_->profile()->IsOffTheRecord()) |
| 190 return url; |
| 191 |
| 192 PrefService* prefs = wrapper_->profile()->GetPrefs(); |
| 193 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) { |
| 194 url = google_util::AppendGoogleLocaleParam( |
| 195 GURL(google_util::kLinkDoctorBaseURL)); |
| 196 url = google_util::AppendGoogleTLDParam(url); |
| 197 } |
| 198 return url; |
| 199 } |
| 200 |
| 201 void CoreTabHelper::UpdateAlternateErrorPageURL(RenderViewHost* rvh) { |
| 202 rvh->SetAltErrorPageURL(GetAlternateErrorPageURL()); |
| 203 } |
OLD | NEW |