host_zoom_map.h revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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 3// found in the LICENSE file. 4 5#ifndef CONTENT_PUBLIC_BROWSER_HOST_ZOOM_MAP_H_ 6#define CONTENT_PUBLIC_BROWSER_HOST_ZOOM_MAP_H_ 7 8#include <map> 9#include <string> 10#include <vector> 11 12#include "base/basictypes.h" 13#include "base/callback.h" 14#include "base/callback_list.h" 15#include "content/common/content_export.h" 16 17namespace content { 18 19class BrowserContext; 20class ResourceContext; 21class WebContents; 22 23// Maps hostnames to custom zoom levels. Written on the UI thread and read on 24// any thread. One instance per browser context. Must be created on the UI 25// thread, and it'll delete itself on the UI thread as well. 26// Zoom can be defined at three levels: default zoom, zoom for host, and zoom 27// for host with specific scheme. Setting any of the levels leaves settings 28// for other settings intact. Getting the zoom level starts at the most 29// specific setting and progresses to the less specific: first the zoom for the 30// host and scheme pair is checked, secondly the zoom for the host only and 31// lastly default zoom. 32 33class HostZoomMap { 34 public: 35 // Enum that indicates what was the scope of zoom level change. 36 enum ZoomLevelChangeMode { 37 ZOOM_CHANGED_FOR_HOST, // Zoom level changed for host. 38 ZOOM_CHANGED_FOR_SCHEME_AND_HOST, // Zoom level changed for scheme/host 39 // pair. 40 ZOOM_CHANGED_TEMPORARY_ZOOM, // Temporary zoom change for specific 41 // renderer, no scheme/host is specified. 42 }; 43 44 // Structure used to notify about zoom changes. Host and/or scheme are empty 45 // if not applicable to |mode|. 46 struct ZoomLevelChange { 47 ZoomLevelChangeMode mode; 48 std::string host; 49 std::string scheme; 50 double zoom_level; 51 }; 52 53 typedef std::vector<ZoomLevelChange> ZoomLevelVector; 54 55 CONTENT_EXPORT static HostZoomMap* GetForBrowserContext( 56 BrowserContext* browser_context); 57 58 // Returns the current zoom level for the specified WebContents. May be 59 // temporary or host-specific. 60 CONTENT_EXPORT static double GetZoomLevel(const WebContents* web_contents); 61 62 // Sets the current zoom level for the specified WebContents. The level may 63 // be temporary or host-specific depending on the particular WebContents. 64 CONTENT_EXPORT static void SetZoomLevel(const WebContents* web_contents, 65 double level); 66 67 // Copy the zoom levels from the given map. Can only be called on the UI 68 // thread. 69 virtual void CopyFrom(HostZoomMap* copy) = 0; 70 71 // Here |host| is the host portion of URL, or (in the absence of a host) 72 // the complete spec of the URL. 73 // Returns the zoom for the specified |scheme| and |host|. See class 74 // description for details. 75 // 76 // This may be called on any thread. 77 virtual double GetZoomLevelForHostAndScheme( 78 const std::string& scheme, 79 const std::string& host) const = 0; 80 81 // Returns all non-temporary zoom levels. Can only be called on any thread. 82 virtual ZoomLevelVector GetAllZoomLevels() const = 0; 83 84 // Here |host| is the host portion of URL, or (in the absence of a host) 85 // the complete spec of the URL. 86 // Sets the zoom level for the |host| to |level|. If the level matches the 87 // current default zoom level, the host is erased from the saved preferences; 88 // otherwise the new value is written out. 89 // Zoom levels specified for both scheme and host are not affected. 90 // 91 // This should only be called on the UI thread. 92 virtual void SetZoomLevelForHost(const std::string& host, double level) = 0; 93 94 // Here |host| is the host portion of URL, or (in the absence of a host) 95 // the complete spec of the URL. 96 // Sets the zoom level for the |scheme|/|host| pair to |level|. No values 97 // will be erased during this operation, and this value will not be stored in 98 // the preferences. 99 // 100 // This should only be called on the UI thread. 101 virtual void SetZoomLevelForHostAndScheme(const std::string& scheme, 102 const std::string& host, 103 double level) = 0; 104 105 // Get/Set the default zoom level for pages that don't override it. 106 virtual double GetDefaultZoomLevel() const = 0; 107 virtual void SetDefaultZoomLevel(double level) = 0;; 108 109 typedef base::Callback<void(const ZoomLevelChange&)> ZoomLevelChangedCallback; 110 typedef base::CallbackList<void(const ZoomLevelChange&)>::Subscription 111 Subscription; 112 // Add and remove zoom level changed callbacks. 113 virtual scoped_ptr<Subscription> AddZoomLevelChangedCallback( 114 const ZoomLevelChangedCallback& callback) = 0; 115 116 protected: 117 virtual ~HostZoomMap() {} 118}; 119 120} // namespace content 121 122#endif // CONTENT_PUBLIC_BROWSER_HOST_ZOOM_MAP_H_ 123