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