Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / public / browser / host_zoom_map.h
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 #include "url/gurl.h"
17
18 namespace content {
19
20 class NavigationEntry;
21 class BrowserContext;
22 class ResourceContext;
23 class WebContents;
24
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.
34
35 class HostZoomMap {
36  public:
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
41                                       // pair.
42     ZOOM_CHANGED_TEMPORARY_ZOOM,      // Temporary zoom change for specific
43                                       // renderer, no scheme/host is specified.
44   };
45
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;
50     std::string host;
51     std::string scheme;
52     double zoom_level;
53   };
54
55   typedef std::vector<ZoomLevelChange> ZoomLevelVector;
56
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);
60
61   CONTENT_EXPORT static HostZoomMap* GetDefaultForBrowserContext(
62       BrowserContext* browser_context);
63
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);
67
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,
71                                           double level);
72
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);
77
78   // Copy the zoom levels from the given map. Can only be called on the UI
79   // thread.
80   virtual void CopyFrom(HostZoomMap* copy) = 0;
81
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.
86   //
87   // This may be called on any thread.
88   virtual double GetZoomLevelForHostAndScheme(
89       const std::string& scheme,
90       const std::string& host) const = 0;
91
92   // Returns true if the specified |scheme| and/or |host| has a zoom level
93   // currently set.
94   //
95   // This may be called on any thread.
96   virtual bool HasZoomLevel(const std::string& scheme,
97                             const std::string& host) const = 0;
98
99   // Returns all non-temporary zoom levels. Can be called on any thread.
100   virtual ZoomLevelVector GetAllZoomLevels() const = 0;
101
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.
108   //
109   // This should only be called on the UI thread.
110   virtual void SetZoomLevelForHost(const std::string& host, double level) = 0;
111
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
116   // the preferences.
117   //
118   // This should only be called on the UI thread.
119   virtual void SetZoomLevelForHostAndScheme(const std::string& scheme,
120                                             const std::string& host,
121                                             double level) = 0;
122
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;
127
128   // Sets the temporary zoom level that's only valid for the lifetime of this
129   // WebContents.
130   //
131   // This should only be called on the UI thread.
132   virtual void SetTemporaryZoomLevel(int render_process_id,
133                                      int render_view_id,
134                                      double level) = 0;
135
136   // Clears the temporary zoom level stored for this WebContents.
137   //
138   // This should only be called on the UI thread.
139   virtual void ClearTemporaryZoomLevel(int render_process_id,
140                                        int render_view_id) = 0;
141
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;;
145
146   typedef base::Callback<void(const ZoomLevelChange&)> ZoomLevelChangedCallback;
147   typedef base::CallbackList<void(const ZoomLevelChange&)>::Subscription
148       Subscription;
149   // Add and remove zoom level changed callbacks.
150   virtual scoped_ptr<Subscription> AddZoomLevelChangedCallback(
151       const ZoomLevelChangedCallback& callback) = 0;
152
153  protected:
154   virtual ~HostZoomMap() {}
155 };
156
157 }  // namespace content
158
159 #endif  // CONTENT_PUBLIC_BROWSER_HOST_ZOOM_MAP_H_