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