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