[email protected] | 9f76c1e | 2012-03-05 15:15:58 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 5 | #include "content/browser/host_zoom_map_impl.h" |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 6 | |
[email protected] | b9e7c479f | 2013-04-12 04:33:24 | [diff] [blame^] | 7 | #include <cmath> |
| 8 | |
| 9 | #include "base/strings/string_piece.h" |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 10 | #include "base/utf_string_conversions.h" |
[email protected] | d407cc0 | 2011-09-13 16:01:46 | [diff] [blame] | 11 | #include "base/values.h" |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 12 | #include "content/browser/renderer_host/render_process_host_impl.h" |
[email protected] | b3c41c0b | 2012-03-06 15:48:32 | [diff] [blame] | 13 | #include "content/browser/renderer_host/render_view_host_impl.h" |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 14 | #include "content/common/view_messages.h" |
| 15 | #include "content/public/browser/browser_context.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 16 | #include "content/public/browser/browser_thread.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 17 | #include "content/public/browser/notification_service.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 18 | #include "content/public/browser/notification_types.h" |
[email protected] | 5fe3713a | 2012-02-22 08:31:56 | [diff] [blame] | 19 | #include "content/public/browser/resource_context.h" |
[email protected] | 0f08340 | 2011-11-22 02:59:01 | [diff] [blame] | 20 | #include "content/public/common/page_zoom.h" |
[email protected] | 9d797f3 | 2010-04-23 07:17:54 | [diff] [blame] | 21 | #include "googleurl/src/gurl.h" |
| 22 | #include "net/base/net_util.h" |
[email protected] | 8bd0fe6 | 2011-01-17 06:44:37 | [diff] [blame] | 23 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 24 | |
| 25 | using WebKit::WebView; |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 26 | |
[email protected] | 5fe3713a | 2012-02-22 08:31:56 | [diff] [blame] | 27 | static const char* kHostZoomMapKeyName = "content_host_zoom_map"; |
| 28 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 29 | namespace content { |
| 30 | |
[email protected] | 5fe3713a | 2012-02-22 08:31:56 | [diff] [blame] | 31 | HostZoomMap* HostZoomMap::GetForBrowserContext(BrowserContext* context) { |
| 32 | HostZoomMapImpl* rv = static_cast<HostZoomMapImpl*>( |
| 33 | context->GetUserData(kHostZoomMapKeyName)); |
| 34 | if (!rv) { |
| 35 | rv = new HostZoomMapImpl(); |
| 36 | context->SetUserData(kHostZoomMapKeyName, rv); |
| 37 | } |
| 38 | return rv; |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 39 | } |
| 40 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 41 | HostZoomMapImpl::HostZoomMapImpl() |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 42 | : default_zoom_level_(0.0) { |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 43 | registrar_.Add( |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 44 | this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, |
| 45 | NotificationService::AllSources()); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 46 | } |
| 47 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 48 | void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) { |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 49 | // This can only be called on the UI thread to avoid deadlocks, otherwise |
| 50 | // UI: a.CopyFrom(b); |
| 51 | // IO: b.CopyFrom(a); |
| 52 | // can deadlock. |
| 53 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 54 | HostZoomMapImpl* copy = static_cast<HostZoomMapImpl*>(copy_interface); |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 55 | base::AutoLock auto_lock(lock_); |
| 56 | base::AutoLock copy_auto_lock(copy->lock_); |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 57 | host_zoom_levels_. |
| 58 | insert(copy->host_zoom_levels_.begin(), copy->host_zoom_levels_.end()); |
| 59 | for (SchemeHostZoomLevels::const_iterator i(copy-> |
| 60 | scheme_host_zoom_levels_.begin()); |
| 61 | i != copy->scheme_host_zoom_levels_.end(); ++i) { |
| 62 | scheme_host_zoom_levels_[i->first] = HostZoomLevels(); |
| 63 | scheme_host_zoom_levels_[i->first]. |
| 64 | insert(i->second.begin(), i->second.end()); |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 65 | } |
[email protected] | 19d2669 | 2013-01-12 19:56:33 | [diff] [blame] | 66 | default_zoom_level_ = copy->default_zoom_level_; |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 67 | } |
| 68 | |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 69 | double HostZoomMapImpl::GetZoomLevelForHost(const std::string& host) const { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 70 | base::AutoLock auto_lock(lock_); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 71 | HostZoomLevels::const_iterator i(host_zoom_levels_.find(host)); |
[email protected] | d0b8d09 | 2010-10-25 04:05:17 | [diff] [blame] | 72 | return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second; |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 73 | } |
| 74 | |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 75 | double HostZoomMapImpl::GetZoomLevelForHostAndScheme( |
| 76 | const std::string& scheme, |
| 77 | const std::string& host) const { |
| 78 | { |
| 79 | base::AutoLock auto_lock(lock_); |
| 80 | SchemeHostZoomLevels::const_iterator scheme_iterator( |
| 81 | scheme_host_zoom_levels_.find(scheme)); |
| 82 | if (scheme_iterator != scheme_host_zoom_levels_.end()) { |
| 83 | HostZoomLevels::const_iterator i(scheme_iterator->second.find(host)); |
| 84 | if (i != scheme_iterator->second.end()) |
| 85 | return i->second; |
| 86 | } |
| 87 | } |
| 88 | return GetZoomLevelForHost(host); |
| 89 | } |
| 90 | |
| 91 | void HostZoomMapImpl::SetZoomLevelForHost(const std::string& host, |
| 92 | double level) { |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 93 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 94 | |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 95 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 96 | base::AutoLock auto_lock(lock_); |
[email protected] | 0f08340 | 2011-11-22 02:59:01 | [diff] [blame] | 97 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 98 | if (ZoomValuesEqual(level, default_zoom_level_)) |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 99 | host_zoom_levels_.erase(host); |
| 100 | else |
| 101 | host_zoom_levels_[host] = level; |
| 102 | } |
| 103 | |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 104 | // Notify renderers from this browser context. |
| 105 | for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
| 106 | !i.IsAtEnd(); i.Advance()) { |
| 107 | RenderProcessHost* render_process_host = i.GetCurrentValue(); |
[email protected] | 5fe3713a | 2012-02-22 08:31:56 | [diff] [blame] | 108 | if (HostZoomMap::GetForBrowserContext( |
| 109 | render_process_host->GetBrowserContext()) == this) { |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 110 | render_process_host->Send( |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 111 | new ViewMsg_SetZoomLevelForCurrentURL(std::string(), host, level)); |
| 112 | } |
| 113 | } |
| 114 | HostZoomMap::ZoomLevelChange change; |
| 115 | change.mode = HostZoomMap::ZOOM_CHANGED_FOR_HOST; |
| 116 | change.host = host; |
| 117 | change.zoom_level = level; |
| 118 | |
| 119 | for (size_t i = 0; i < zoom_level_changed_callbacks_.size(); i++) |
| 120 | zoom_level_changed_callbacks_[i].Run(change); |
| 121 | } |
| 122 | |
| 123 | void HostZoomMapImpl::SetZoomLevelForHostAndScheme(const std::string& scheme, |
| 124 | const std::string& host, |
| 125 | double level) { |
| 126 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 127 | { |
| 128 | base::AutoLock auto_lock(lock_); |
| 129 | scheme_host_zoom_levels_[scheme][host] = level; |
| 130 | } |
| 131 | |
| 132 | // Notify renderers from this browser context. |
| 133 | for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
| 134 | !i.IsAtEnd(); i.Advance()) { |
| 135 | RenderProcessHost* render_process_host = i.GetCurrentValue(); |
| 136 | if (HostZoomMap::GetForBrowserContext( |
| 137 | render_process_host->GetBrowserContext()) == this) { |
| 138 | render_process_host->Send( |
| 139 | new ViewMsg_SetZoomLevelForCurrentURL(scheme, host, level)); |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 143 | HostZoomMap::ZoomLevelChange change; |
| 144 | change.mode = HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST; |
| 145 | change.host = host; |
| 146 | change.scheme = scheme; |
| 147 | change.zoom_level = level; |
| 148 | |
[email protected] | 89c9aca | 2013-02-07 15:08:42 | [diff] [blame] | 149 | for (size_t i = 0; i < zoom_level_changed_callbacks_.size(); i++) |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 150 | zoom_level_changed_callbacks_[i].Run(change); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 151 | } |
| 152 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 153 | double HostZoomMapImpl::GetDefaultZoomLevel() const { |
| 154 | return default_zoom_level_; |
| 155 | } |
| 156 | |
| 157 | void HostZoomMapImpl::SetDefaultZoomLevel(double level) { |
| 158 | default_zoom_level_ = level; |
| 159 | } |
| 160 | |
[email protected] | 89c9aca | 2013-02-07 15:08:42 | [diff] [blame] | 161 | void HostZoomMapImpl::AddZoomLevelChangedCallback( |
| 162 | const ZoomLevelChangedCallback& callback) { |
| 163 | zoom_level_changed_callbacks_.push_back(callback); |
| 164 | } |
| 165 | |
| 166 | void HostZoomMapImpl::RemoveZoomLevelChangedCallback( |
| 167 | const ZoomLevelChangedCallback& callback) { |
| 168 | for (size_t i = 0; i < zoom_level_changed_callbacks_.size(); i++) { |
| 169 | if (zoom_level_changed_callbacks_[i].Equals(callback)) { |
| 170 | zoom_level_changed_callbacks_.erase( |
| 171 | zoom_level_changed_callbacks_.begin() + i); |
| 172 | return; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 177 | double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id, |
| 178 | int render_view_id) const { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 179 | base::AutoLock auto_lock(lock_); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 180 | for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { |
| 181 | if (temporary_zoom_levels_[i].render_process_id == render_process_id && |
| 182 | temporary_zoom_levels_[i].render_view_id == render_view_id) { |
| 183 | return temporary_zoom_levels_[i].zoom_level; |
| 184 | } |
| 185 | } |
| 186 | return 0; |
| 187 | } |
| 188 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 189 | void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id, |
| 190 | int render_view_id, |
| 191 | double level) { |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 192 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 193 | |
| 194 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 195 | base::AutoLock auto_lock(lock_); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 196 | size_t i; |
| 197 | for (i = 0; i < temporary_zoom_levels_.size(); ++i) { |
| 198 | if (temporary_zoom_levels_[i].render_process_id == render_process_id && |
| 199 | temporary_zoom_levels_[i].render_view_id == render_view_id) { |
| 200 | if (level) { |
| 201 | temporary_zoom_levels_[i].zoom_level = level; |
| 202 | } else { |
| 203 | temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i); |
| 204 | } |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | if (level && i == temporary_zoom_levels_.size()) { |
| 210 | TemporaryZoomLevel temp; |
| 211 | temp.render_process_id = render_process_id; |
| 212 | temp.render_view_id = render_view_id; |
| 213 | temp.zoom_level = level; |
| 214 | temporary_zoom_levels_.push_back(temp); |
| 215 | } |
| 216 | } |
| 217 | |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 218 | HostZoomMap::ZoomLevelChange change; |
| 219 | change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; |
| 220 | change.zoom_level = level; |
| 221 | |
[email protected] | 89c9aca | 2013-02-07 15:08:42 | [diff] [blame] | 222 | for (size_t i = 0; i < zoom_level_changed_callbacks_.size(); i++) |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 223 | zoom_level_changed_callbacks_[i].Run(change); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 224 | } |
| 225 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 226 | void HostZoomMapImpl::Observe(int type, |
| 227 | const NotificationSource& source, |
| 228 | const NotificationDetails& details) { |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 229 | switch (type) { |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 230 | case NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 231 | base::AutoLock auto_lock(lock_); |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 232 | int render_view_id = Source<RenderViewHost>(source)->GetRoutingID(); |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 233 | int render_process_id = |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 234 | Source<RenderViewHost>(source)->GetProcess()->GetID(); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 235 | |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 236 | for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { |
| 237 | if (temporary_zoom_levels_[i].render_process_id == render_process_id && |
| 238 | temporary_zoom_levels_[i].render_view_id == render_view_id) { |
| 239 | temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i); |
| 240 | break; |
| 241 | } |
| 242 | } |
| 243 | break; |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 244 | } |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 245 | default: |
| 246 | NOTREACHED() << "Unexpected preference observed."; |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 247 | } |
[email protected] | 779c336 | 2010-01-30 01:09:33 | [diff] [blame] | 248 | } |
| 249 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 250 | HostZoomMapImpl::~HostZoomMapImpl() { |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 251 | } |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 252 | |
| 253 | } // namespace content |