Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(154)

Side by Side Diff: content/browser/host_zoom_map_impl.cc

Issue 224733018: Changes to content/ to facilitate new zoom extension API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "content/browser/host_zoom_map_impl.h" 5 #include "content/browser/host_zoom_map_impl.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 #include <set>
8 9
9 #include "base/strings/string_piece.h" 10 #include "base/strings/string_piece.h"
10 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h" 12 #include "base/values.h"
12 #include "content/browser/renderer_host/render_process_host_impl.h" 13 #include "content/browser/renderer_host/render_process_host_impl.h"
13 #include "content/browser/renderer_host/render_view_host_impl.h" 14 #include "content/browser/renderer_host/render_view_host_impl.h"
14 #include "content/common/view_messages.h" 15 #include "content/common/view_messages.h"
15 #include "content/public/browser/browser_context.h" 16 #include "content/public/browser/browser_context.h"
16 #include "content/public/browser/browser_thread.h" 17 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/notification_service.h" 18 #include "content/public/browser/notification_service.h"
18 #include "content/public/browser/notification_types.h" 19 #include "content/public/browser/notification_types.h"
19 #include "content/public/browser/resource_context.h" 20 #include "content/public/browser/resource_context.h"
21 #include "content/public/browser/web_contents.h"
20 #include "content/public/common/page_zoom.h" 22 #include "content/public/common/page_zoom.h"
21 #include "net/base/net_util.h" 23 #include "net/base/net_util.h"
22 24
23 static const char* kHostZoomMapKeyName = "content_host_zoom_map"; 25 static const char* kHostZoomMapKeyName = "content_host_zoom_map";
24 26
25 namespace content { 27 namespace content {
26 28
27 HostZoomMap* HostZoomMap::GetForBrowserContext(BrowserContext* context) { 29 HostZoomMap* HostZoomMap::GetForBrowserContext(BrowserContext* context) {
28 HostZoomMapImpl* rv = static_cast<HostZoomMapImpl*>( 30 HostZoomMapImpl* rv = static_cast<HostZoomMapImpl*>(
29 context->GetUserData(kHostZoomMapKeyName)); 31 context->GetUserData(kHostZoomMapKeyName));
30 if (!rv) { 32 if (!rv) {
31 rv = new HostZoomMapImpl(); 33 rv = new HostZoomMapImpl();
32 context->SetUserData(kHostZoomMapKeyName, rv); 34 context->SetUserData(kHostZoomMapKeyName, rv);
33 } 35 }
34 return rv; 36 return rv;
35 } 37 }
36 38
37 HostZoomMapImpl::HostZoomMapImpl() 39 HostZoomMapImpl::HostZoomMapImpl()
38 : default_zoom_level_(0.0) { 40 : default_zoom_level_(0.0) {
39 registrar_.Add( 41 registrar_.Add(
40 this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, 42 this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW,
41 NotificationService::AllSources()); 43 NotificationService::AllSources());
44 registrar_.Add(
45 this, NOTIFICATION_WEB_CONTENTS_NO_TEMPORARY_ZOOM_LEVEL,
46 NotificationService::AllSources());
42 } 47 }
43 48
44 void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) { 49 void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) {
45 // This can only be called on the UI thread to avoid deadlocks, otherwise 50 // This can only be called on the UI thread to avoid deadlocks, otherwise
46 // UI: a.CopyFrom(b); 51 // UI: a.CopyFrom(b);
47 // IO: b.CopyFrom(a); 52 // IO: b.CopyFrom(a);
48 // can deadlock. 53 // can deadlock.
49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
50 HostZoomMapImpl* copy = static_cast<HostZoomMapImpl*>(copy_interface); 55 HostZoomMapImpl* copy = static_cast<HostZoomMapImpl*>(copy_interface);
51 base::AutoLock auto_lock(lock_); 56 base::AutoLock auto_lock(lock_);
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 }; 120 };
116 result.push_back(change); 121 result.push_back(change);
117 } 122 }
118 } 123 }
119 } 124 }
120 return result; 125 return result;
121 } 126 }
122 127
123 void HostZoomMapImpl::SetZoomLevelForHost(const std::string& host, 128 void HostZoomMapImpl::SetZoomLevelForHost(const std::string& host,
124 double level) { 129 double level) {
130 SetZoomLevelForHost(host, 0, level);
131 }
132
133 void HostZoomMapImpl::SetZoomLevelForHost(const std::string& host,
134 int zoom_id,
135 double level) {
125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
126 137
127 { 138 {
128 base::AutoLock auto_lock(lock_); 139 base::AutoLock auto_lock(lock_);
129 140
130 if (ZoomValuesEqual(level, default_zoom_level_)) 141 if (ZoomValuesEqual(level, default_zoom_level_))
131 host_zoom_levels_.erase(host); 142 host_zoom_levels_.erase(host);
132 else 143 else
133 host_zoom_levels_[host] = level; 144 host_zoom_levels_[host] = level;
134 } 145 }
135 146
136 // Notify renderers from this browser context. 147 SendZoomLevelChange(std::string(), host, level);
137 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); 148
138 !i.IsAtEnd(); i.Advance()) {
139 RenderProcessHost* render_process_host = i.GetCurrentValue();
140 if (HostZoomMap::GetForBrowserContext(
141 render_process_host->GetBrowserContext()) == this) {
142 render_process_host->Send(
143 new ViewMsg_SetZoomLevelForCurrentURL(std::string(), host, level));
144 }
145 }
146 HostZoomMap::ZoomLevelChange change; 149 HostZoomMap::ZoomLevelChange change;
147 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_HOST; 150 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_HOST;
148 change.host = host; 151 change.host = host;
149 change.zoom_level = level; 152 change.zoom_level = level;
153 change.zoom_id = zoom_id;
150 154
151 zoom_level_changed_callbacks_.Notify(change); 155 zoom_level_changed_callbacks_.Notify(change);
152 } 156 }
153 157
154 void HostZoomMapImpl::SetZoomLevelForHostAndScheme(const std::string& scheme, 158 void HostZoomMapImpl::SetZoomLevelForHostAndScheme(const std::string& scheme,
155 const std::string& host, 159 const std::string& host,
156 double level) { 160 double level) {
157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
158 { 162 {
159 base::AutoLock auto_lock(lock_); 163 base::AutoLock auto_lock(lock_);
160 scheme_host_zoom_levels_[scheme][host] = level; 164 scheme_host_zoom_levels_[scheme][host] = level;
161 } 165 }
162 166
163 // Notify renderers from this browser context. 167 SendZoomLevelChange(scheme, host, level);
164 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
165 !i.IsAtEnd(); i.Advance()) {
166 RenderProcessHost* render_process_host = i.GetCurrentValue();
167 if (HostZoomMap::GetForBrowserContext(
168 render_process_host->GetBrowserContext()) == this) {
169 render_process_host->Send(
170 new ViewMsg_SetZoomLevelForCurrentURL(scheme, host, level));
171 }
172 }
173 168
174 HostZoomMap::ZoomLevelChange change; 169 HostZoomMap::ZoomLevelChange change;
175 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST; 170 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST;
176 change.host = host; 171 change.host = host;
177 change.scheme = scheme; 172 change.scheme = scheme;
178 change.zoom_level = level; 173 change.zoom_level = level;
179 174
180 zoom_level_changed_callbacks_.Notify(change); 175 zoom_level_changed_callbacks_.Notify(change);
181 } 176 }
182 177
(...skipping 18 matching lines...) Expand all
201 if (temporary_zoom_levels_[i].render_process_id == render_process_id && 196 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
202 temporary_zoom_levels_[i].render_view_id == render_view_id) { 197 temporary_zoom_levels_[i].render_view_id == render_view_id) {
203 return temporary_zoom_levels_[i].zoom_level; 198 return temporary_zoom_levels_[i].zoom_level;
204 } 199 }
205 } 200 }
206 return 0; 201 return 0;
207 } 202 }
208 203
209 void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id, 204 void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id,
210 int render_view_id, 205 int render_view_id,
206 const std::string& host,
207 int zoom_id,
211 double level) { 208 double level) {
212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
213 210
214 { 211 {
215 base::AutoLock auto_lock(lock_); 212 base::AutoLock auto_lock(lock_);
216 size_t i; 213 size_t i;
217 for (i = 0; i < temporary_zoom_levels_.size(); ++i) { 214 for (i = 0; i < temporary_zoom_levels_.size(); ++i) {
218 if (temporary_zoom_levels_[i].render_process_id == render_process_id && 215 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
219 temporary_zoom_levels_[i].render_view_id == render_view_id) { 216 temporary_zoom_levels_[i].render_view_id == render_view_id) {
220 if (level) { 217 temporary_zoom_levels_[i].zoom_level = level;
221 temporary_zoom_levels_[i].zoom_level = level;
222 } else {
223 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i);
224 }
225 break; 218 break;
226 } 219 }
227 } 220 }
228 221
229 if (level && i == temporary_zoom_levels_.size()) { 222 if (level && i == temporary_zoom_levels_.size()) {
230 TemporaryZoomLevel temp; 223 TemporaryZoomLevel temp;
231 temp.render_process_id = render_process_id; 224 temp.render_process_id = render_process_id;
232 temp.render_view_id = render_view_id; 225 temp.render_view_id = render_view_id;
233 temp.zoom_level = level; 226 temp.zoom_level = level;
234 temporary_zoom_levels_.push_back(temp); 227 temporary_zoom_levels_.push_back(temp);
235 } 228 }
236 } 229 }
237 230
238 HostZoomMap::ZoomLevelChange change; 231 HostZoomMap::ZoomLevelChange change;
239 change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; 232 change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM;
233 change.host = host;
240 change.zoom_level = level; 234 change.zoom_level = level;
235 change.zoom_id = zoom_id;
241 236
242 zoom_level_changed_callbacks_.Notify(change); 237 zoom_level_changed_callbacks_.Notify(change);
243 } 238 }
244 239
245 void HostZoomMapImpl::Observe(int type, 240 void HostZoomMapImpl::Observe(int type,
246 const NotificationSource& source, 241 const NotificationSource& source,
247 const NotificationDetails& details) { 242 const NotificationDetails& details) {
248 switch (type) { 243 switch (type) {
249 case NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: { 244 case NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: {
250 base::AutoLock auto_lock(lock_); 245 base::AutoLock auto_lock(lock_);
251 int render_view_id = Source<RenderViewHost>(source)->GetRoutingID(); 246 int render_view_id = Source<RenderViewHost>(source)->GetRoutingID();
252 int render_process_id = 247 int render_process_id =
253 Source<RenderViewHost>(source)->GetProcess()->GetID(); 248 Source<RenderViewHost>(source)->GetProcess()->GetID();
254 249 EraseTemporaryZoomLevel(render_process_id, render_view_id);
255 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { 250 break;
256 if (temporary_zoom_levels_[i].render_process_id == render_process_id && 251 }
257 temporary_zoom_levels_[i].render_view_id == render_view_id) { 252 case NOTIFICATION_WEB_CONTENTS_NO_TEMPORARY_ZOOM_LEVEL: {
258 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i); 253 base::AutoLock auto_lock(lock_);
259 break; 254 int render_view_id =
260 } 255 Source<WebContents>(source)->GetRenderViewHost()->GetRoutingID();
261 } 256 int render_process_id =
257 Source<WebContents>(source)->GetRenderProcessHost()->GetID();
258 EraseTemporaryZoomLevel(render_process_id, render_view_id);
262 break; 259 break;
263 } 260 }
264 default: 261 default:
265 NOTREACHED() << "Unexpected preference observed."; 262 NOTREACHED() << "Unexpected preference observed.";
266 } 263 }
267 } 264 }
268 265
266 void HostZoomMapImpl::EraseTemporaryZoomLevel(int render_process_id,
267 int render_view_id) {
268 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) {
269 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
270 temporary_zoom_levels_[i].render_view_id == render_view_id) {
271 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i);
272 break;
273 }
274 }
275 }
276
277 void HostZoomMapImpl::SendZoomLevelChange(const std::string& scheme,
278 const std::string& host,
279 double level) {
280 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
281 !i.IsAtEnd(); i.Advance()) {
282 RenderProcessHost* render_process_host = i.GetCurrentValue();
283 if (HostZoomMap::GetForBrowserContext(
284 render_process_host->GetBrowserContext()) == this) {
285 int render_process_id = render_process_host->GetID();
286 std::set<int> exceptions;
287 for (size_t i = 0; i != temporary_zoom_levels_.size(); ++i) {
288 if (temporary_zoom_levels_[i].render_process_id == render_process_id) {
289 exceptions.insert(temporary_zoom_levels_[i].render_view_id);
290 }
291 }
292 render_process_host->Send(new ViewMsg_SetZoomLevelForCurrentURL(
293 scheme, host, level, exceptions));
294 }
295 }
296 }
297
269 HostZoomMapImpl::~HostZoomMapImpl() { 298 HostZoomMapImpl::~HostZoomMapImpl() {
270 } 299 }
271 300
272 } // namespace content 301 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698