[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] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 5 | #include <cmath> |
| 6 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 7 | #include "content/browser/host_zoom_map_impl.h" |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 8 | |
[email protected] | 864b136 | 2010-08-19 03:49:38 | [diff] [blame] | 9 | #include "base/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] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 26 | using content::BrowserThread; |
| 27 | using content::RenderProcessHost; |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 28 | |
[email protected] | 5fe3713a | 2012-02-22 08:31:56 | [diff] [blame] | 29 | static const char* kHostZoomMapKeyName = "content_host_zoom_map"; |
| 30 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 31 | namespace content { |
| 32 | |
[email protected] | 5fe3713a | 2012-02-22 08:31:56 | [diff] [blame] | 33 | HostZoomMap* HostZoomMap::GetForBrowserContext(BrowserContext* context) { |
| 34 | HostZoomMapImpl* rv = static_cast<HostZoomMapImpl*>( |
| 35 | context->GetUserData(kHostZoomMapKeyName)); |
| 36 | if (!rv) { |
| 37 | rv = new HostZoomMapImpl(); |
| 38 | context->SetUserData(kHostZoomMapKeyName, rv); |
| 39 | } |
| 40 | return rv; |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | } // namespace content |
| 44 | |
| 45 | HostZoomMapImpl::HostZoomMapImpl() |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 46 | : default_zoom_level_(0.0) { |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 47 | registrar_.Add( |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 48 | this, content::NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 49 | content::NotificationService::AllSources()); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 50 | } |
| 51 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 52 | void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) { |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 53 | // This can only be called on the UI thread to avoid deadlocks, otherwise |
| 54 | // UI: a.CopyFrom(b); |
| 55 | // IO: b.CopyFrom(a); |
| 56 | // can deadlock. |
| 57 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 58 | HostZoomMapImpl* copy = static_cast<HostZoomMapImpl*>(copy_interface); |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 59 | base::AutoLock auto_lock(lock_); |
| 60 | base::AutoLock copy_auto_lock(copy->lock_); |
| 61 | for (HostZoomLevels::const_iterator i(copy->host_zoom_levels_.begin()); |
| 62 | i != copy->host_zoom_levels_.end(); ++i) { |
| 63 | host_zoom_levels_[i->first] = i->second; |
| 64 | } |
| 65 | } |
| 66 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 67 | double HostZoomMapImpl::GetZoomLevel(const std::string& host) const { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 68 | base::AutoLock auto_lock(lock_); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 69 | HostZoomLevels::const_iterator i(host_zoom_levels_.find(host)); |
[email protected] | d0b8d09 | 2010-10-25 04:05:17 | [diff] [blame] | 70 | return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second; |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 71 | } |
| 72 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 73 | void HostZoomMapImpl::SetZoomLevel(std::string host, double level) { |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 74 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 75 | |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 76 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 77 | base::AutoLock auto_lock(lock_); |
[email protected] | 0f08340 | 2011-11-22 02:59:01 | [diff] [blame] | 78 | |
| 79 | if (content::ZoomValuesEqual(level, default_zoom_level_)) |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 80 | host_zoom_levels_.erase(host); |
| 81 | else |
| 82 | host_zoom_levels_[host] = level; |
| 83 | } |
| 84 | |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 85 | // Notify renderers from this browser context. |
| 86 | for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
| 87 | !i.IsAtEnd(); i.Advance()) { |
| 88 | RenderProcessHost* render_process_host = i.GetCurrentValue(); |
[email protected] | 5fe3713a | 2012-02-22 08:31:56 | [diff] [blame] | 89 | if (HostZoomMap::GetForBrowserContext( |
| 90 | render_process_host->GetBrowserContext()) == this) { |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 91 | render_process_host->Send( |
| 92 | new ViewMsg_SetZoomLevelForCurrentURL(host, level)); |
| 93 | } |
| 94 | } |
| 95 | |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 96 | content::NotificationService::current()->Notify( |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 97 | content::NOTIFICATION_ZOOM_LEVEL_CHANGED, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 98 | content::Source<HostZoomMap>(this), |
| 99 | content::Details<const std::string>(&host)); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 100 | } |
| 101 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 102 | double HostZoomMapImpl::GetDefaultZoomLevel() const { |
| 103 | return default_zoom_level_; |
| 104 | } |
| 105 | |
| 106 | void HostZoomMapImpl::SetDefaultZoomLevel(double level) { |
| 107 | default_zoom_level_ = level; |
| 108 | } |
| 109 | |
| 110 | double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id, |
| 111 | int render_view_id) const { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 112 | base::AutoLock auto_lock(lock_); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 113 | for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { |
| 114 | if (temporary_zoom_levels_[i].render_process_id == render_process_id && |
| 115 | temporary_zoom_levels_[i].render_view_id == render_view_id) { |
| 116 | return temporary_zoom_levels_[i].zoom_level; |
| 117 | } |
| 118 | } |
| 119 | return 0; |
| 120 | } |
| 121 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 122 | void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id, |
| 123 | int render_view_id, |
| 124 | double level) { |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 125 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 126 | |
| 127 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 128 | base::AutoLock auto_lock(lock_); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 129 | size_t i; |
| 130 | for (i = 0; i < temporary_zoom_levels_.size(); ++i) { |
| 131 | if (temporary_zoom_levels_[i].render_process_id == render_process_id && |
| 132 | temporary_zoom_levels_[i].render_view_id == render_view_id) { |
| 133 | if (level) { |
| 134 | temporary_zoom_levels_[i].zoom_level = level; |
| 135 | } else { |
| 136 | temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i); |
| 137 | } |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | if (level && i == temporary_zoom_levels_.size()) { |
| 143 | TemporaryZoomLevel temp; |
| 144 | temp.render_process_id = render_process_id; |
| 145 | temp.render_view_id = render_view_id; |
| 146 | temp.zoom_level = level; |
| 147 | temporary_zoom_levels_.push_back(temp); |
| 148 | } |
| 149 | } |
| 150 | |
[email protected] | a5f2695d | 2011-05-24 07:45:16 | [diff] [blame] | 151 | std::string host; |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 152 | content::NotificationService::current()->Notify( |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 153 | content::NOTIFICATION_ZOOM_LEVEL_CHANGED, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 154 | content::Source<HostZoomMap>(this), |
| 155 | content::Details<const std::string>(&host)); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 158 | void HostZoomMapImpl::Observe( |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 159 | int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 160 | const content::NotificationSource& source, |
| 161 | const content::NotificationDetails& details) { |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 162 | switch (type) { |
| 163 | case content::NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 164 | base::AutoLock auto_lock(lock_); |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 165 | int render_view_id = |
[email protected] | 9f76c1e | 2012-03-05 15:15:58 | [diff] [blame] | 166 | content::Source<RenderViewHost>(source)->GetRoutingID(); |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 167 | int render_process_id = |
[email protected] | 9f76c1e | 2012-03-05 15:15:58 | [diff] [blame] | 168 | content::Source<RenderViewHost>(source)->GetProcess()->GetID(); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 169 | |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 170 | for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { |
| 171 | if (temporary_zoom_levels_[i].render_process_id == render_process_id && |
| 172 | temporary_zoom_levels_[i].render_view_id == render_view_id) { |
| 173 | temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i); |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | break; |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 178 | } |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 179 | default: |
| 180 | NOTREACHED() << "Unexpected preference observed."; |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 181 | } |
[email protected] | 779c336 | 2010-01-30 01:09:33 | [diff] [blame] | 182 | } |
| 183 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 184 | HostZoomMapImpl::~HostZoomMapImpl() { |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 185 | } |