diff options
Diffstat (limited to 'chrome/browser/host_zoom_map.cc')
| -rw-r--r-- | chrome/browser/host_zoom_map.cc | 147 |
1 files changed, 121 insertions, 26 deletions
diff --git a/chrome/browser/host_zoom_map.cc b/chrome/browser/host_zoom_map.cc index cb67a04..1a99226 100644 --- a/chrome/browser/host_zoom_map.cc +++ b/chrome/browser/host_zoom_map.cc @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include <cmath> + #include "chrome/browser/host_zoom_map.h" #include "base/string_piece.h" @@ -10,6 +12,8 @@ #include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/prefs/scoped_pref_update.h" #include "chrome/browser/profile.h" +#include "chrome/browser/renderer_host/render_process_host.h" +#include "chrome/browser/renderer_host/render_view_host.h" #include "chrome/common/notification_details.h" #include "chrome/common/notification_service.h" #include "chrome/common/notification_source.h" @@ -17,6 +21,9 @@ #include "chrome/common/pref_names.h" #include "googleurl/src/gurl.h" #include "net/base/net_util.h" +#include "third_party/WebKit/WebKit/chromium/public/WebView.h" + +using WebKit::WebView; HostZoomMap::HostZoomMap(Profile* profile) : profile_(profile), @@ -31,6 +38,10 @@ HostZoomMap::HostZoomMap(Profile* profile) pref_change_registrar_.Init(profile_->GetPrefs()); pref_change_registrar_.Add(prefs::kPerHostZoomLevels, this); } + + registrar_.Add( + this, NotificationType::RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, + NotificationService::AllSources()); } void HostZoomMap::Load() { @@ -46,9 +57,37 @@ void HostZoomMap::Load() { for (DictionaryValue::key_iterator i(host_zoom_dictionary->begin_keys()); i != host_zoom_dictionary->end_keys(); ++i) { const std::string& host(*i); - int zoom_level = 0; - bool success = host_zoom_dictionary->GetIntegerWithoutPathExpansion( + double zoom_level = 0; + + bool success = host_zoom_dictionary->GetRealWithoutPathExpansion( host, &zoom_level); + if (!success) { + // The data used to be stored as ints, so try that. + int int_zoom_level; + success = host_zoom_dictionary->GetIntegerWithoutPathExpansion( + host, &int_zoom_level); + if (success) { + zoom_level = static_cast<double>(int_zoom_level); + // Since the values were once stored as non-clamped, clamp now. +#ifdef ZOOM_LEVEL_IS_DOUBLE + double zoom_factor = WebView::zoomLevelToZoomFactor(zoom_level); + if (zoom_factor < WebView::minTextSizeMultiplier) { + zoom_level = + WebView::zoomFactorToZoomLevel(WebView::minTextSizeMultiplier); + } else if (zoom_factor > WebView::maxTextSizeMultiplier) { + zoom_level = + WebView::zoomFactorToZoomLevel(WebView::maxTextSizeMultiplier); + } +#else + double zoom_factor = std::pow(1.2, zoom_level); + if (zoom_factor < 0.5) { + zoom_level = log(0.5) / log(1.2); + } else if (zoom_factor > 3.0) { + zoom_level = log(3.0) / log(1.2); + } +#endif + } + } DCHECK(success); host_zoom_levels_[host] = zoom_level; } @@ -60,14 +99,14 @@ void HostZoomMap::RegisterUserPrefs(PrefService* prefs) { prefs->RegisterDictionaryPref(prefs::kPerHostZoomLevels); } -int HostZoomMap::GetZoomLevel(const GURL& url) const { +double HostZoomMap::GetZoomLevel(const GURL& url) const { std::string host(net::GetHostOrSpecFromURL(url)); AutoLock auto_lock(lock_); HostZoomLevels::const_iterator i(host_zoom_levels_.find(host)); return (i == host_zoom_levels_.end()) ? 0 : i->second; } -void HostZoomMap::SetZoomLevel(const GURL& url, int level) { +void HostZoomMap::SetZoomLevel(const GURL& url, double level) { DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); if (!profile_) return; @@ -82,10 +121,9 @@ void HostZoomMap::SetZoomLevel(const GURL& url, int level) { host_zoom_levels_[host] = level; } - Details<std::string> details(&host); NotificationService::current()->Notify(NotificationType::ZOOM_LEVEL_CHANGED, Source<Profile>(profile_), - details); + NotificationService::NoDetails()); // If we're in incognito mode, don't persist changes to the prefs. We'll keep // them in memory only so they will be forgotten on exiting incognito. @@ -101,12 +139,60 @@ void HostZoomMap::SetZoomLevel(const GURL& url, int level) { host_zoom_dictionary->RemoveWithoutPathExpansion(host, NULL); } else { host_zoom_dictionary->SetWithoutPathExpansion( - host, Value::CreateIntegerValue(level)); + host, Value::CreateRealValue(level)); } } updating_preferences_ = false; } +double HostZoomMap::GetTemporaryZoomLevel(int render_process_id, + int render_view_id) const { + AutoLock auto_lock(lock_); + for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { + if (temporary_zoom_levels_[i].render_process_id == render_process_id && + temporary_zoom_levels_[i].render_view_id == render_view_id) { + return temporary_zoom_levels_[i].zoom_level; + } + } + return 0; +} + +void HostZoomMap::SetTemporaryZoomLevel(int render_process_id, + int render_view_id, + double level) { + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); + if (!profile_) + return; + + { + AutoLock auto_lock(lock_); + size_t i; + for (i = 0; i < temporary_zoom_levels_.size(); ++i) { + if (temporary_zoom_levels_[i].render_process_id == render_process_id && + temporary_zoom_levels_[i].render_view_id == render_view_id) { + if (level) { + temporary_zoom_levels_[i].zoom_level = level; + } else { + temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i); + } + break; + } + } + + if (level && i == temporary_zoom_levels_.size()) { + TemporaryZoomLevel temp; + temp.render_process_id = render_process_id; + temp.render_view_id = render_view_id; + temp.zoom_level = level; + temporary_zoom_levels_.push_back(temp); + } + } + + NotificationService::current()->Notify(NotificationType::ZOOM_LEVEL_CHANGED, + Source<Profile>(profile_), + NotificationService::NoDetails()); +} + void HostZoomMap::ResetToDefaults() { DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); if (!profile_) @@ -126,9 +212,7 @@ void HostZoomMap::Shutdown() { if (!profile_) return; - registrar_.Remove(this, - NotificationType::PROFILE_DESTROYED, - Source<Profile>(profile_)); + registrar_.RemoveAll(); if (!profile_->IsOffTheRecord()) pref_change_registrar_.RemoveAll(); profile_ = NULL; @@ -140,25 +224,36 @@ void HostZoomMap::Observe( const NotificationDetails& details) { DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); - // If the profile is going away, we need to stop using it. - if (type == NotificationType::PROFILE_DESTROYED) { - Shutdown(); - return; - } + switch (type.value) { + case NotificationType::PROFILE_DESTROYED: + // If the profile is going away, we need to stop using it. + Shutdown(); + break; + case NotificationType::RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: { + AutoLock auto_lock(lock_); + int render_view_id = Source<RenderViewHost>(source)->routing_id(); + int render_process_id = Source<RenderViewHost>(source)->process()->id(); - if (type == NotificationType::PREF_CHANGED) { - // If we are updating our own preference, don't reload. - if (updating_preferences_) - return; - - std::string* name = Details<std::string>(details).ptr(); - if (prefs::kPerHostZoomLevels == *name) { - Load(); - return; + for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { + if (temporary_zoom_levels_[i].render_process_id == render_process_id && + temporary_zoom_levels_[i].render_view_id == render_view_id) { + temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i); + break; + } + } + break; + } + case NotificationType::PREF_CHANGED: { + // If we are updating our own preference, don't reload. + if (!updating_preferences_) { + std::string* name = Details<std::string>(details).ptr(); + if (prefs::kPerHostZoomLevels == *name) + Load(); + } } + default: + NOTREACHED() << "Unexpected preference observed."; } - - NOTREACHED() << "Unexpected preference observed."; } HostZoomMap::~HostZoomMap() { |
