[email protected] | 7f070d4 | 2011-03-09 20:25:32 | [diff] [blame] | 1 | // Copyright (c) 2011 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] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 7 | #include "content/browser/host_zoom_map.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] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 13 | #include "content/browser/renderer_host/render_view_host.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 14 | #include "content/public/browser/browser_thread.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 15 | #include "content/public/browser/notification_service.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 16 | #include "content/public/browser/notification_types.h" |
[email protected] | 0f08340 | 2011-11-22 02:59:01 | [diff] [blame^] | 17 | #include "content/public/common/page_zoom.h" |
[email protected] | 9d797f3 | 2010-04-23 07:17:54 | [diff] [blame] | 18 | #include "googleurl/src/gurl.h" |
| 19 | #include "net/base/net_util.h" |
[email protected] | 8bd0fe6 | 2011-01-17 06:44:37 | [diff] [blame] | 20 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 21 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 22 | using content::BrowserThread; |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 23 | using WebKit::WebView; |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 24 | |
[email protected] | d407cc0 | 2011-09-13 16:01:46 | [diff] [blame] | 25 | HostZoomMap::HostZoomMap() |
| 26 | : default_zoom_level_(0.0), |
| 27 | original_(this) { |
| 28 | Init(); |
| 29 | } |
| 30 | |
| 31 | HostZoomMap::HostZoomMap(HostZoomMap* original) |
| 32 | : default_zoom_level_(0.0), |
| 33 | original_(original) { |
| 34 | DCHECK(original); |
| 35 | Init(); |
| 36 | base::AutoLock auto_lock(original->lock_); |
| 37 | for (HostZoomLevels::const_iterator i(original->host_zoom_levels_.begin()); |
| 38 | i != original->host_zoom_levels_.end(); ++i) { |
| 39 | host_zoom_levels_[i->first] = i->second; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | void HostZoomMap::Init() { |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 44 | registrar_.Add( |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 45 | this, content::NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 46 | content::NotificationService::AllSources()); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 47 | } |
| 48 | |
[email protected] | 0008e3f | 2011-06-22 21:02:36 | [diff] [blame] | 49 | double HostZoomMap::GetZoomLevel(const std::string& host) const { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 50 | base::AutoLock auto_lock(lock_); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 51 | HostZoomLevels::const_iterator i(host_zoom_levels_.find(host)); |
[email protected] | d0b8d09 | 2010-10-25 04:05:17 | [diff] [blame] | 52 | return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second; |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 53 | } |
| 54 | |
[email protected] | 13ffa32e | 2011-05-27 16:37:01 | [diff] [blame] | 55 | void HostZoomMap::SetZoomLevel(std::string host, double level) { |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 56 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 57 | |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 58 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 59 | base::AutoLock auto_lock(lock_); |
[email protected] | 0f08340 | 2011-11-22 02:59:01 | [diff] [blame^] | 60 | |
| 61 | if (content::ZoomValuesEqual(level, default_zoom_level_)) |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 62 | host_zoom_levels_.erase(host); |
| 63 | else |
| 64 | host_zoom_levels_[host] = level; |
| 65 | } |
| 66 | |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 67 | content::NotificationService::current()->Notify( |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 68 | content::NOTIFICATION_ZOOM_LEVEL_CHANGED, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 69 | content::Source<HostZoomMap>(this), |
| 70 | content::Details<const std::string>(&host)); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 71 | } |
| 72 | |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 73 | double HostZoomMap::GetTemporaryZoomLevel(int render_process_id, |
| 74 | int render_view_id) const { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 75 | base::AutoLock auto_lock(lock_); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 76 | for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { |
| 77 | if (temporary_zoom_levels_[i].render_process_id == render_process_id && |
| 78 | temporary_zoom_levels_[i].render_view_id == render_view_id) { |
| 79 | return temporary_zoom_levels_[i].zoom_level; |
| 80 | } |
| 81 | } |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | void HostZoomMap::SetTemporaryZoomLevel(int render_process_id, |
| 86 | int render_view_id, |
| 87 | double level) { |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 88 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 89 | |
| 90 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 91 | base::AutoLock auto_lock(lock_); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 92 | size_t i; |
| 93 | for (i = 0; i < temporary_zoom_levels_.size(); ++i) { |
| 94 | if (temporary_zoom_levels_[i].render_process_id == render_process_id && |
| 95 | temporary_zoom_levels_[i].render_view_id == render_view_id) { |
| 96 | if (level) { |
| 97 | temporary_zoom_levels_[i].zoom_level = level; |
| 98 | } else { |
| 99 | temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i); |
| 100 | } |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | if (level && i == temporary_zoom_levels_.size()) { |
| 106 | TemporaryZoomLevel temp; |
| 107 | temp.render_process_id = render_process_id; |
| 108 | temp.render_view_id = render_view_id; |
| 109 | temp.zoom_level = level; |
| 110 | temporary_zoom_levels_.push_back(temp); |
| 111 | } |
| 112 | } |
| 113 | |
[email protected] | a5f2695d | 2011-05-24 07:45:16 | [diff] [blame] | 114 | std::string host; |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 115 | content::NotificationService::current()->Notify( |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 116 | content::NOTIFICATION_ZOOM_LEVEL_CHANGED, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 117 | content::Source<HostZoomMap>(this), |
| 118 | content::Details<const std::string>(&host)); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 119 | } |
| 120 | |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 121 | void HostZoomMap::Observe( |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 122 | int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 123 | const content::NotificationSource& source, |
| 124 | const content::NotificationDetails& details) { |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 125 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 126 | |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 127 | switch (type) { |
| 128 | case content::NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 129 | base::AutoLock auto_lock(lock_); |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 130 | int render_view_id = |
| 131 | content::Source<RenderViewHost>(source)->routing_id(); |
| 132 | int render_process_id = |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 133 | content::Source<RenderViewHost>(source)->process()->GetID(); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 134 | |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 135 | for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { |
| 136 | if (temporary_zoom_levels_[i].render_process_id == render_process_id && |
| 137 | temporary_zoom_levels_[i].render_view_id == render_view_id) { |
| 138 | temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i); |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | break; |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 143 | } |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 144 | default: |
| 145 | NOTREACHED() << "Unexpected preference observed."; |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 146 | } |
[email protected] | 779c336 | 2010-01-30 01:09:33 | [diff] [blame] | 147 | } |
| 148 | |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 149 | HostZoomMap::~HostZoomMap() { |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 150 | } |