1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/host_zoom_map_impl.h"
#include <algorithm>
#include <cmath>
#include "base/strings/string_piece.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "content/browser/frame_host/navigation_entry_impl.h"
#include "content/browser/renderer_host/render_process_host_impl.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/common/view_messages.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/resource_context.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/common/page_zoom.h"
#include "content/public/common/url_constants.h"
#include "net/base/net_util.h"
namespace content {
namespace {
std::string GetHostFromProcessView(int render_process_id, int render_view_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
RenderViewHost* render_view_host =
RenderViewHost::FromID(render_process_id, render_view_id);
if (!render_view_host)
return std::string();
WebContents* web_contents = WebContents::FromRenderViewHost(render_view_host);
NavigationEntry* entry =
web_contents->GetController().GetLastCommittedEntry();
if (!entry)
return std::string();
return net::GetHostOrSpecFromURL(HostZoomMap::GetURLFromEntry(entry));
}
} // namespace
GURL HostZoomMap::GetURLFromEntry(const NavigationEntry* entry) {
switch (entry->GetPageType()) {
case PAGE_TYPE_ERROR:
return GURL(kUnreachableWebDataURL);
// TODO(wjmaclean): In future, give interstitial pages special treatment as
// well.
default:
return entry->GetURL();
}
}
HostZoomMap* HostZoomMap::GetDefaultForBrowserContext(BrowserContext* context) {
StoragePartition* partition =
BrowserContext::GetDefaultStoragePartition(context);
DCHECK(partition);
return partition->GetHostZoomMap();
}
HostZoomMap* HostZoomMap::Get(SiteInstance* instance) {
StoragePartition* partition = BrowserContext::GetStoragePartition(
instance->GetBrowserContext(), instance);
DCHECK(partition);
return partition->GetHostZoomMap();
}
HostZoomMap* HostZoomMap::GetForWebContents(const WebContents* contents) {
StoragePartition* partition =
BrowserContext::GetStoragePartition(contents->GetBrowserContext(),
contents->GetSiteInstance());
DCHECK(partition);
return partition->GetHostZoomMap();
}
// Helper function for setting/getting zoom levels for WebContents without
// having to import HostZoomMapImpl everywhere.
double HostZoomMap::GetZoomLevel(const WebContents* web_contents) {
HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>(
HostZoomMap::GetForWebContents(web_contents));
return host_zoom_map->GetZoomLevelForWebContents(
*static_cast<const WebContentsImpl*>(web_contents));
}
void HostZoomMap::SetZoomLevel(const WebContents* web_contents, double level) {
HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>(
HostZoomMap::GetForWebContents(web_contents));
host_zoom_map->SetZoomLevelForWebContents(
*static_cast<const WebContentsImpl*>(web_contents), level);
}
void HostZoomMap::SendErrorPageZoomLevelRefresh(
const WebContents* web_contents) {
HostZoomMapImpl* host_zoom_map =
static_cast<HostZoomMapImpl*>(HostZoomMap::GetDefaultForBrowserContext(
web_contents->GetBrowserContext()));
host_zoom_map->SendErrorPageZoomLevelRefresh();
}
HostZoomMapImpl::HostZoomMapImpl()
: default_zoom_level_(0.0) {
registrar_.Add(
this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW,
NotificationService::AllSources());
}
void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) {
// This can only be called on the UI thread to avoid deadlocks, otherwise
// UI: a.CopyFrom(b);
// IO: b.CopyFrom(a);
// can deadlock.
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
HostZoomMapImpl* copy = static_cast<HostZoomMapImpl*>(copy_interface);
base::AutoLock auto_lock(lock_);
base::AutoLock copy_auto_lock(copy->lock_);
host_zoom_levels_.
insert(copy->host_zoom_levels_.begin(), copy->host_zoom_levels_.end());
for (SchemeHostZoomLevels::const_iterator i(copy->
scheme_host_zoom_levels_.begin());
i != copy->scheme_host_zoom_levels_.end(); ++i) {
scheme_host_zoom_levels_[i->first] = HostZoomLevels();
scheme_host_zoom_levels_[i->first].
insert(i->second.begin(), i->second.end());
}
default_zoom_level_ = copy->default_zoom_level_;
}
double HostZoomMapImpl::GetZoomLevelForHost(const std::string& host) const {
base::AutoLock auto_lock(lock_);
HostZoomLevels::const_iterator i(host_zoom_levels_.find(host));
return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second;
}
bool HostZoomMapImpl::HasZoomLevel(const std::string& scheme,
const std::string& host) const {
base::AutoLock auto_lock(lock_);
SchemeHostZoomLevels::const_iterator scheme_iterator(
scheme_host_zoom_levels_.find(scheme));
const HostZoomLevels& zoom_levels =
(scheme_iterator != scheme_host_zoom_levels_.end())
? scheme_iterator->second
: host_zoom_levels_;
HostZoomLevels::const_iterator i(zoom_levels.find(host));
return i != zoom_levels.end();
}
double HostZoomMapImpl::GetZoomLevelForHostAndScheme(
const std::string& scheme,
const std::string& host) const {
{
base::AutoLock auto_lock(lock_);
SchemeHostZoomLevels::const_iterator scheme_iterator(
scheme_host_zoom_levels_.find(scheme));
if (scheme_iterator != scheme_host_zoom_levels_.end()) {
HostZoomLevels::const_iterator i(scheme_iterator->second.find(host));
if (i != scheme_iterator->second.end())
return i->second;
}
}
return GetZoomLevelForHost(host);
}
HostZoomMap::ZoomLevelVector HostZoomMapImpl::GetAllZoomLevels() const {
HostZoomMap::ZoomLevelVector result;
{
base::AutoLock auto_lock(lock_);
result.reserve(host_zoom_levels_.size() + scheme_host_zoom_levels_.size());
for (HostZoomLevels::const_iterator i = host_zoom_levels_.begin();
i != host_zoom_levels_.end();
++i) {
ZoomLevelChange change = {HostZoomMap::ZOOM_CHANGED_FOR_HOST,
i->first, // host
std::string(), // scheme
i->second // zoom level
};
result.push_back(change);
}
for (SchemeHostZoomLevels::const_iterator i =
scheme_host_zoom_levels_.begin();
i != scheme_host_zoom_levels_.end();
++i) {
const std::string& scheme = i->first;
const HostZoomLevels& host_zoom_levels = i->second;
for (HostZoomLevels::const_iterator j = host_zoom_levels.begin();
j != host_zoom_levels.end();
++j) {
ZoomLevelChange change = {HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST,
j->first, // host
scheme, // scheme
j->second // zoom level
};
result.push_back(change);
}
}
}
return result;
}
void HostZoomMapImpl::SetZoomLevelForHost(const std::string& host,
double level) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
{
base::AutoLock auto_lock(lock_);
if (ZoomValuesEqual(level, default_zoom_level_))
host_zoom_levels_.erase(host);
else
host_zoom_levels_[host] = level;
}
// TODO(wjmaclean) Should we use a GURL here? crbug.com/384486
SendZoomLevelChange(std::string(), host, level);
HostZoomMap::ZoomLevelChange change;
change.mode = HostZoomMap::ZOOM_CHANGED_FOR_HOST;
change.host = host;
change.zoom_level = level;
zoom_level_changed_callbacks_.Notify(change);
}
void HostZoomMapImpl::SetZoomLevelForHostAndScheme(const std::string& scheme,
const std::string& host,
double level) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
{
base::AutoLock auto_lock(lock_);
scheme_host_zoom_levels_[scheme][host] = level;
}
SendZoomLevelChange(scheme, host, level);
HostZoomMap::ZoomLevelChange change;
change.mode = HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST;
change.host = host;
change.scheme = scheme;
change.zoom_level = level;
zoom_level_changed_callbacks_.Notify(change);
}
double HostZoomMapImpl::GetDefaultZoomLevel() const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
return default_zoom_level_;
}
void HostZoomMapImpl::SetDefaultZoomLevel(double level) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
default_zoom_level_ = level;
}
scoped_ptr<HostZoomMap::Subscription>
HostZoomMapImpl::AddZoomLevelChangedCallback(
const ZoomLevelChangedCallback& callback) {
return zoom_level_changed_callbacks_.Add(callback);
}
double HostZoomMapImpl::GetZoomLevelForWebContents(
const WebContentsImpl& web_contents_impl) const {
int render_process_id = web_contents_impl.GetRenderProcessHost()->GetID();
int routing_id = web_contents_impl.GetRenderViewHost()->GetRoutingID();
if (UsesTemporaryZoomLevel(render_process_id, routing_id))
return GetTemporaryZoomLevel(render_process_id, routing_id);
// Get the url from the navigation controller directly, as calling
// WebContentsImpl::GetLastCommittedURL() may give us a virtual url that
// is different than is stored in the map.
GURL url;
NavigationEntry* entry =
web_contents_impl.GetController().GetLastCommittedEntry();
// It is possible for a WebContent's zoom level to be queried before
// a navigation has occurred.
if (entry)
url = GetURLFromEntry(entry);
return GetZoomLevelForHostAndScheme(url.scheme(),
net::GetHostOrSpecFromURL(url));
}
void HostZoomMapImpl::SetZoomLevelForWebContents(
const WebContentsImpl& web_contents_impl,
double level) {
int render_process_id = web_contents_impl.GetRenderProcessHost()->GetID();
int render_view_id = web_contents_impl.GetRenderViewHost()->GetRoutingID();
if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) {
SetTemporaryZoomLevel(render_process_id, render_view_id, level);
} else {
// Get the url from the navigation controller directly, as calling
// WebContentsImpl::GetLastCommittedURL() may give us a virtual url that
// is different than what the render view is using. If the two don't match,
// the attempt to set the zoom will fail.
NavigationEntry* entry =
web_contents_impl.GetController().GetLastCommittedEntry();
// Tests may invoke this function with a null entry, but we don't
// want to save zoom levels in this case.
if (!entry)
return;
GURL url = GetURLFromEntry(entry);
SetZoomLevelForHost(net::GetHostOrSpecFromURL(url), level);
}
}
void HostZoomMapImpl::SetZoomLevelForView(int render_process_id,
int render_view_id,
double level,
const std::string& host) {
if (UsesTemporaryZoomLevel(render_process_id, render_view_id))
SetTemporaryZoomLevel(render_process_id, render_view_id, level);
else
SetZoomLevelForHost(host, level);
}
bool HostZoomMapImpl::UsesTemporaryZoomLevel(int render_process_id,
int render_view_id) const {
RenderViewKey key(render_process_id, render_view_id);
base::AutoLock auto_lock(lock_);
return ContainsKey(temporary_zoom_levels_, key);
}
double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id,
int render_view_id) const {
base::AutoLock auto_lock(lock_);
RenderViewKey key(render_process_id, render_view_id);
if (!ContainsKey(temporary_zoom_levels_, key))
return 0;
return temporary_zoom_levels_.find(key)->second;
}
void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id,
int render_view_id,
double level) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
{
RenderViewKey key(render_process_id, render_view_id);
base::AutoLock auto_lock(lock_);
temporary_zoom_levels_[key] = level;
}
RenderViewHost* host =
RenderViewHost::FromID(render_process_id, render_view_id);
host->Send(new ViewMsg_SetZoomLevelForView(render_view_id, true, level));
HostZoomMap::ZoomLevelChange change;
change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM;
change.host = GetHostFromProcessView(render_process_id, render_view_id);
change.zoom_level = level;
zoom_level_changed_callbacks_.Notify(change);
}
void HostZoomMapImpl::Observe(int type,
const NotificationSource& source,
const NotificationDetails& details) {
switch (type) {
case NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: {
int render_view_id = Source<RenderViewHost>(source)->GetRoutingID();
int render_process_id =
Source<RenderViewHost>(source)->GetProcess()->GetID();
ClearTemporaryZoomLevel(render_process_id, render_view_id);
break;
}
default:
NOTREACHED() << "Unexpected preference observed.";
}
}
void HostZoomMapImpl::ClearTemporaryZoomLevel(int render_process_id,
int render_view_id) {
{
base::AutoLock auto_lock(lock_);
RenderViewKey key(render_process_id, render_view_id);
TemporaryZoomLevels::iterator it = temporary_zoom_levels_.find(key);
if (it == temporary_zoom_levels_.end())
return;
temporary_zoom_levels_.erase(it);
}
RenderViewHost* host =
RenderViewHost::FromID(render_process_id, render_view_id);
DCHECK(host);
// Send a new zoom level, host-specific if one exists.
host->Send(new ViewMsg_SetZoomLevelForView(
render_view_id,
false,
GetZoomLevelForHost(
GetHostFromProcessView(render_process_id, render_view_id))));
}
void HostZoomMapImpl::SendZoomLevelChange(const std::string& scheme,
const std::string& host,
double level) {
for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
!i.IsAtEnd(); i.Advance()) {
RenderProcessHost* render_process_host = i.GetCurrentValue();
// TODO(wjmaclean) This will need to be cleaned up when
// RenderProcessHost::GetStoragePartition() goes away. Perhaps have
// RenderProcessHost expose a GetHostZoomMap() function?
if (render_process_host->GetStoragePartition()->GetHostZoomMap() == this) {
render_process_host->Send(
new ViewMsg_SetZoomLevelForCurrentURL(scheme, host, level));
}
}
}
void HostZoomMapImpl::SendErrorPageZoomLevelRefresh() {
GURL error_url(kUnreachableWebDataURL);
std::string host = net::GetHostOrSpecFromURL(error_url);
double error_page_zoom_level = GetZoomLevelForHost(host);
SendZoomLevelChange(std::string(), host, error_page_zoom_level);
}
HostZoomMapImpl::~HostZoomMapImpl() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}
} // namespace content
|