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.
5 #ifndef CONTENT_PUBLIC_BROWSER_HOST_ZOOM_MAP_H_
6 #define CONTENT_PUBLIC_BROWSER_HOST_ZOOM_MAP_H_
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/callback_list.h"
15 #include "content/common/content_export.h"
20 class NavigationEntry;
22 class ResourceContext;
25 // Maps hostnames to custom zoom levels. Written on the UI thread and read on
26 // any thread. One instance per browser context. Must be created on the UI
27 // thread, and it'll delete itself on the UI thread as well.
28 // Zoom can be defined at three levels: default zoom, zoom for host, and zoom
29 // for host with specific scheme. Setting any of the levels leaves settings
30 // for other settings intact. Getting the zoom level starts at the most
31 // specific setting and progresses to the less specific: first the zoom for the
32 // host and scheme pair is checked, secondly the zoom for the host only and
33 // lastly default zoom.
37 // Enum that indicates what was the scope of zoom level change.
38 enum ZoomLevelChangeMode {
39 ZOOM_CHANGED_FOR_HOST, // Zoom level changed for host.
40 ZOOM_CHANGED_FOR_SCHEME_AND_HOST, // Zoom level changed for scheme/host
42 ZOOM_CHANGED_TEMPORARY_ZOOM, // Temporary zoom change for specific
43 // renderer, no scheme/host is specified.
46 // Structure used to notify about zoom changes. Host and/or scheme are empty
47 // if not applicable to |mode|.
48 struct ZoomLevelChange {
49 ZoomLevelChangeMode mode;
55 typedef std::vector<ZoomLevelChange> ZoomLevelVector;
57 // Extracts the URL from NavigationEntry, substituting the error page
58 // URL in the event that the error page is showing.
59 CONTENT_EXPORT static GURL GetURLFromEntry(const NavigationEntry* entry);
61 CONTENT_EXPORT static HostZoomMap* GetDefaultForBrowserContext(
62 BrowserContext* browser_context);
64 // Returns the current zoom level for the specified WebContents. May be
65 // temporary or host-specific.
66 CONTENT_EXPORT static double GetZoomLevel(const WebContents* web_contents);
68 // Sets the current zoom level for the specified WebContents. The level may
69 // be temporary or host-specific depending on the particular WebContents.
70 CONTENT_EXPORT static void SetZoomLevel(const WebContents* web_contents,
73 // Send an IPC to refresh any displayed error page's zoom levels. Needs to
74 // be called since error pages don't get loaded via the normal channel.
75 CONTENT_EXPORT static void SendErrorPageZoomLevelRefresh(
76 const WebContents* web_contents);
78 // Copy the zoom levels from the given map. Can only be called on the UI
80 virtual void CopyFrom(HostZoomMap* copy) = 0;
82 // Here |host| is the host portion of URL, or (in the absence of a host)
83 // the complete spec of the URL.
84 // Returns the zoom for the specified |scheme| and |host|. See class
85 // description for details.
87 // This may be called on any thread.
88 virtual double GetZoomLevelForHostAndScheme(
89 const std::string& scheme,
90 const std::string& host) const = 0;
92 // Returns true if the specified |scheme| and/or |host| has a zoom level
95 // This may be called on any thread.
96 virtual bool HasZoomLevel(const std::string& scheme,
97 const std::string& host) const = 0;
99 // Returns all non-temporary zoom levels. Can be called on any thread.
100 virtual ZoomLevelVector GetAllZoomLevels() const = 0;
102 // Here |host| is the host portion of URL, or (in the absence of a host)
103 // the complete spec of the URL.
104 // Sets the zoom level for the |host| to |level|. If the level matches the
105 // current default zoom level, the host is erased from the saved preferences;
106 // otherwise the new value is written out.
107 // Zoom levels specified for both scheme and host are not affected.
109 // This should only be called on the UI thread.
110 virtual void SetZoomLevelForHost(const std::string& host, double level) = 0;
112 // Here |host| is the host portion of URL, or (in the absence of a host)
113 // the complete spec of the URL.
114 // Sets the zoom level for the |scheme|/|host| pair to |level|. No values
115 // will be erased during this operation, and this value will not be stored in
118 // This should only be called on the UI thread.
119 virtual void SetZoomLevelForHostAndScheme(const std::string& scheme,
120 const std::string& host,
123 // Returns whether the view manages its zoom level independently of other
124 // views displaying content from the same host.
125 virtual bool UsesTemporaryZoomLevel(int render_process_id,
126 int render_view_id) const = 0;
128 // Sets the temporary zoom level that's only valid for the lifetime of this
131 // This should only be called on the UI thread.
132 virtual void SetTemporaryZoomLevel(int render_process_id,
136 // Clears the temporary zoom level stored for this WebContents.
138 // This should only be called on the UI thread.
139 virtual void ClearTemporaryZoomLevel(int render_process_id,
140 int render_view_id) = 0;
142 // Get/Set the default zoom level for pages that don't override it.
143 virtual double GetDefaultZoomLevel() const = 0;
144 virtual void SetDefaultZoomLevel(double level) = 0;;
146 typedef base::Callback<void(const ZoomLevelChange&)> ZoomLevelChangedCallback;
147 typedef base::CallbackList<void(const ZoomLevelChange&)>::Subscription
149 // Add and remove zoom level changed callbacks.
150 virtual scoped_ptr<Subscription> AddZoomLevelChangedCallback(
151 const ZoomLevelChangedCallback& callback) = 0;
154 virtual ~HostZoomMap() {}
157 } // namespace content
159 #endif // CONTENT_PUBLIC_BROWSER_HOST_ZOOM_MAP_H_