| 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 <cmath> | 5 #include <cmath> |
| 6 | 6 |
| 7 #include "content/browser/host_zoom_map.h" | 7 #include "content/browser/host_zoom_map.h" |
| 8 | 8 |
| 9 #include "base/string_piece.h" | 9 #include "base/string_piece.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/browser/prefs/pref_service.h" | |
| 12 #include "chrome/browser/prefs/scoped_user_pref_update.h" | |
| 13 #include "chrome/browser/profiles/profile.h" | |
| 14 #include "chrome/common/pref_names.h" | |
| 15 #include "content/browser/browser_thread.h" | 11 #include "content/browser/browser_thread.h" |
| 16 #include "content/browser/renderer_host/render_process_host.h" | 12 #include "content/browser/renderer_host/render_process_host.h" |
| 17 #include "content/browser/renderer_host/render_view_host.h" | 13 #include "content/browser/renderer_host/render_view_host.h" |
| 18 #include "content/common/notification_service.h" | 14 #include "content/common/notification_service.h" |
| 19 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
| 20 #include "net/base/net_util.h" | 16 #include "net/base/net_util.h" |
| 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 22 | 18 |
| 23 using WebKit::WebView; | 19 using WebKit::WebView; |
| 24 | 20 |
| 25 HostZoomMap::HostZoomMap(Profile* profile) | 21 HostZoomMap::HostZoomMap() : default_zoom_level_(0.0) { |
| 26 : profile_(profile), default_zoom_level_(0.0) { | |
| 27 | |
| 28 registrar_.Add( | 22 registrar_.Add( |
| 29 this, NotificationType::RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, | 23 this, NotificationType::RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, |
| 30 NotificationService::AllSources()); | 24 NotificationService::AllSources()); |
| 31 | |
| 32 base::AutoLock auto_lock(lock_); | |
| 33 host_zoom_levels_.clear(); | |
| 34 const DictionaryValue* host_zoom_dictionary = | |
| 35 profile_->GetPrefs()->GetDictionary(prefs::kPerHostZoomLevels); | |
| 36 // Careful: The returned value could be NULL if the pref has never been set. | |
| 37 if (host_zoom_dictionary != NULL) { | |
| 38 for (DictionaryValue::key_iterator i(host_zoom_dictionary->begin_keys()); | |
| 39 i != host_zoom_dictionary->end_keys(); ++i) { | |
| 40 const std::string& host(*i); | |
| 41 double zoom_level = 0; | |
| 42 | |
| 43 bool success = host_zoom_dictionary->GetDoubleWithoutPathExpansion( | |
| 44 host, &zoom_level); | |
| 45 DCHECK(success); | |
| 46 host_zoom_levels_[host] = zoom_level; | |
| 47 } | |
| 48 } | |
| 49 } | 25 } |
| 50 | 26 |
| 51 double HostZoomMap::GetZoomLevel(const GURL& url) const { | 27 double HostZoomMap::GetZoomLevel(const GURL& url) const { |
| 52 std::string host(net::GetHostOrSpecFromURL(url)); | 28 std::string host(net::GetHostOrSpecFromURL(url)); |
| 53 base::AutoLock auto_lock(lock_); | 29 base::AutoLock auto_lock(lock_); |
| 54 HostZoomLevels::const_iterator i(host_zoom_levels_.find(host)); | 30 HostZoomLevels::const_iterator i(host_zoom_levels_.find(host)); |
| 55 return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second; | 31 return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second; |
| 56 } | 32 } |
| 57 | 33 |
| 58 void HostZoomMap::SetZoomLevel(const GURL& url, double level) { | 34 void HostZoomMap::SetZoomLevel(const GURL& url, double level) { |
| 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 60 | 36 |
| 61 std::string host(net::GetHostOrSpecFromURL(url)); | 37 std::string host(net::GetHostOrSpecFromURL(url)); |
| 62 | 38 |
| 63 { | 39 { |
| 64 base::AutoLock auto_lock(lock_); | 40 base::AutoLock auto_lock(lock_); |
| 65 if (level == default_zoom_level_) | 41 if (level == default_zoom_level_) |
| 66 host_zoom_levels_.erase(host); | 42 host_zoom_levels_.erase(host); |
| 67 else | 43 else |
| 68 host_zoom_levels_[host] = level; | 44 host_zoom_levels_[host] = level; |
| 69 } | 45 } |
| 70 | 46 |
| 71 NotificationService::current()->Notify(NotificationType::ZOOM_LEVEL_CHANGED, | 47 NotificationService::current()->Notify(NotificationType::ZOOM_LEVEL_CHANGED, |
| 72 Source<Profile>(profile_), | 48 Source<HostZoomMap>(this), |
| 73 NotificationService::NoDetails()); | 49 Details<const std::string>(&host)); |
| 74 | |
| 75 // If we're in incognito mode, don't persist changes to the prefs. We'll keep | |
| 76 // them in memory only so they will be forgotten on exiting incognito. | |
| 77 if (profile_->IsOffTheRecord()) | |
| 78 return; | |
| 79 | |
| 80 DictionaryPrefUpdate update(profile_->GetPrefs(), prefs::kPerHostZoomLevels); | |
| 81 DictionaryValue* host_zoom_dictionary = update.Get(); | |
| 82 if (level == default_zoom_level_) { | |
| 83 host_zoom_dictionary->RemoveWithoutPathExpansion(host, NULL); | |
| 84 } else { | |
| 85 host_zoom_dictionary->SetWithoutPathExpansion( | |
| 86 host, Value::CreateDoubleValue(level)); | |
| 87 } | |
| 88 } | 50 } |
| 89 | 51 |
| 90 double HostZoomMap::GetTemporaryZoomLevel(int render_process_id, | 52 double HostZoomMap::GetTemporaryZoomLevel(int render_process_id, |
| 91 int render_view_id) const { | 53 int render_view_id) const { |
| 92 base::AutoLock auto_lock(lock_); | 54 base::AutoLock auto_lock(lock_); |
| 93 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { | 55 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { |
| 94 if (temporary_zoom_levels_[i].render_process_id == render_process_id && | 56 if (temporary_zoom_levels_[i].render_process_id == render_process_id && |
| 95 temporary_zoom_levels_[i].render_view_id == render_view_id) { | 57 temporary_zoom_levels_[i].render_view_id == render_view_id) { |
| 96 return temporary_zoom_levels_[i].zoom_level; | 58 return temporary_zoom_levels_[i].zoom_level; |
| 97 } | 59 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 122 if (level && i == temporary_zoom_levels_.size()) { | 84 if (level && i == temporary_zoom_levels_.size()) { |
| 123 TemporaryZoomLevel temp; | 85 TemporaryZoomLevel temp; |
| 124 temp.render_process_id = render_process_id; | 86 temp.render_process_id = render_process_id; |
| 125 temp.render_view_id = render_view_id; | 87 temp.render_view_id = render_view_id; |
| 126 temp.zoom_level = level; | 88 temp.zoom_level = level; |
| 127 temporary_zoom_levels_.push_back(temp); | 89 temporary_zoom_levels_.push_back(temp); |
| 128 } | 90 } |
| 129 } | 91 } |
| 130 | 92 |
| 131 NotificationService::current()->Notify(NotificationType::ZOOM_LEVEL_CHANGED, | 93 NotificationService::current()->Notify(NotificationType::ZOOM_LEVEL_CHANGED, |
| 132 Source<Profile>(profile_), | 94 Source<HostZoomMap>(this), |
| 133 NotificationService::NoDetails()); | 95 NotificationService::NoDetails()); |
| 134 } | 96 } |
| 135 | 97 |
| 136 void HostZoomMap::Observe( | 98 void HostZoomMap::Observe( |
| 137 NotificationType type, | 99 NotificationType type, |
| 138 const NotificationSource& source, | 100 const NotificationSource& source, |
| 139 const NotificationDetails& details) { | 101 const NotificationDetails& details) { |
| 140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 141 | 103 |
| 142 switch (type.value) { | 104 switch (type.value) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 154 } | 116 } |
| 155 break; | 117 break; |
| 156 } | 118 } |
| 157 default: | 119 default: |
| 158 NOTREACHED() << "Unexpected preference observed."; | 120 NOTREACHED() << "Unexpected preference observed."; |
| 159 } | 121 } |
| 160 } | 122 } |
| 161 | 123 |
| 162 HostZoomMap::~HostZoomMap() { | 124 HostZoomMap::~HostZoomMap() { |
| 163 } | 125 } |
| OLD | NEW |