• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_BROWSER_HOST_ZOOM_MAP_IMPL_H_
6 #define CONTENT_BROWSER_HOST_ZOOM_MAP_IMPL_H_
7 
8 #include <map>
9 #include <string>
10 #include <vector>
11 
12 #include "base/compiler_specific.h"
13 #include "base/sequenced_task_runner_helpers.h"
14 #include "base/supports_user_data.h"
15 #include "base/synchronization/lock.h"
16 #include "content/public/browser/host_zoom_map.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
19 
20 namespace content {
21 
22 class WebContentsImpl;
23 
24 // HostZoomMap needs to be deleted on the UI thread because it listens
25 // to notifications on there (and holds a NotificationRegistrar).
NON_EXPORTED_BASE(HostZoomMap)26 class CONTENT_EXPORT HostZoomMapImpl : public NON_EXPORTED_BASE(HostZoomMap),
27                                        public NotificationObserver,
28                                        public base::SupportsUserData::Data {
29  public:
30   HostZoomMapImpl();
31   virtual ~HostZoomMapImpl();
32 
33   // HostZoomMap implementation:
34   virtual void CopyFrom(HostZoomMap* copy) OVERRIDE;
35   virtual double GetZoomLevelForHostAndScheme(
36       const std::string& scheme,
37       const std::string& host) const OVERRIDE;
38   // TODO(wjmaclean) Should we use a GURL here? crbug.com/384486
39   virtual bool HasZoomLevel(const std::string& scheme,
40                             const std::string& host) const OVERRIDE;
41   virtual ZoomLevelVector GetAllZoomLevels() const OVERRIDE;
42   virtual void SetZoomLevelForHost(
43       const std::string& host,
44       double level) OVERRIDE;
45   virtual void SetZoomLevelForHostAndScheme(
46       const std::string& scheme,
47       const std::string& host,
48       double level) OVERRIDE;
49   virtual bool UsesTemporaryZoomLevel(int render_process_id,
50                                       int render_view_id) const OVERRIDE;
51   virtual void SetTemporaryZoomLevel(int render_process_id,
52                                      int render_view_id,
53                                      double level) OVERRIDE;
54 
55   virtual void ClearTemporaryZoomLevel(int render_process_id,
56                                        int render_view_id) OVERRIDE;
57   virtual double GetDefaultZoomLevel() const OVERRIDE;
58   virtual void SetDefaultZoomLevel(double level) OVERRIDE;
59   virtual scoped_ptr<Subscription> AddZoomLevelChangedCallback(
60       const ZoomLevelChangedCallback& callback) OVERRIDE;
61 
62   // Returns the current zoom level for the specified WebContents. This may
63   // be a temporary zoom level, depending on UsesTemporaryZoomLevel().
64   double GetZoomLevelForWebContents(
65       const WebContentsImpl& web_contents_impl) const;
66 
67   // Sets the zoom level for this WebContents. If this WebContents is using
68   // a temporary zoom level, then level is only applied to this WebContents.
69   // Otherwise, the level will be applied on a host level.
70   void SetZoomLevelForWebContents(const WebContentsImpl& web_contents_impl,
71                                   double level);
72 
73   // Sets the zoom level for the specified view. The level may be set for only
74   // this view, or for the host, depending on UsesTemporaryZoomLevel().
75   void SetZoomLevelForView(int render_process_id,
76                            int render_view_id,
77                            double level,
78                            const std::string& host);
79 
80   // Returns the temporary zoom level that's only valid for the lifetime of
81   // the given WebContents (i.e. isn't saved and doesn't affect other
82   // WebContentses) if it exists, the default zoom level otherwise.
83   //
84   // This may be called on any thread.
85   double GetTemporaryZoomLevel(int render_process_id,
86                                int render_view_id) const;
87 
88   // NotificationObserver implementation.
89   virtual void Observe(int type,
90                        const NotificationSource& source,
91                        const NotificationDetails& details) OVERRIDE;
92 
93  private:
94   typedef std::map<std::string, double> HostZoomLevels;
95   typedef std::map<std::string, HostZoomLevels> SchemeHostZoomLevels;
96 
97   struct RenderViewKey {
98     int render_process_id;
99     int render_view_id;
100     RenderViewKey(int render_process_id, int render_view_id)
101         : render_process_id(render_process_id),
102           render_view_id(render_view_id) {}
103     bool operator<(const RenderViewKey& other) const {
104       return render_process_id < other.render_process_id ||
105              ((render_process_id == other.render_process_id) &&
106               (render_view_id < other.render_view_id));
107     }
108   };
109 
110   typedef std::map<RenderViewKey, double> TemporaryZoomLevels;
111 
112   double GetZoomLevelForHost(const std::string& host) const;
113 
114   // Notifies the renderers from this browser context to change the zoom level
115   // for the specified host and scheme.
116   // TODO(wjmaclean) Should we use a GURL here? crbug.com/384486
117   void SendZoomLevelChange(const std::string& scheme,
118                            const std::string& host,
119                            double level);
120 
121   // Callbacks called when zoom level changes.
122   base::CallbackList<void(const ZoomLevelChange&)>
123       zoom_level_changed_callbacks_;
124 
125   // Copy of the pref data, so that we can read it on the IO thread.
126   HostZoomLevels host_zoom_levels_;
127   SchemeHostZoomLevels scheme_host_zoom_levels_;
128   double default_zoom_level_;
129 
130   // Don't expect more than a couple of tabs that are using a temporary zoom
131   // level, so vector is fine for now.
132   TemporaryZoomLevels temporary_zoom_levels_;
133 
134   // Used around accesses to |host_zoom_levels_|, |default_zoom_level_| and
135   // |temporary_zoom_levels_| to guarantee thread safety.
136   mutable base::Lock lock_;
137 
138   NotificationRegistrar registrar_;
139 
140   DISALLOW_COPY_AND_ASSIGN(HostZoomMapImpl);
141 };
142 
143 }  // namespace content
144 
145 #endif  // CONTENT_BROWSER_HOST_ZOOM_MAP_IMPL_H_
146