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_BROWSER_HOST_ZOOM_MAP_IMPL_H_
6 #define CONTENT_BROWSER_HOST_ZOOM_MAP_IMPL_H_
12 #include "base/compiler_specific.h"
13 #include "base/sequenced_task_runner_helpers.h"
14 #include "base/synchronization/lock.h"
15 #include "content/public/browser/host_zoom_map.h"
16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
21 class WebContentsImpl
;
23 // HostZoomMap needs to be deleted on the UI thread because it listens
24 // to notifications on there (and holds a NotificationRegistrar).
25 class CONTENT_EXPORT HostZoomMapImpl
: public NON_EXPORTED_BASE(HostZoomMap
),
26 public NotificationObserver
{
29 ~HostZoomMapImpl() override
;
31 // HostZoomMap implementation:
32 void SetPageScaleFactorIsOneForView(
33 int render_process_id
, int render_view_id
, bool is_one
) override
;
34 void ClearPageScaleFactorIsOneForView(
35 int render_process_id
, int render_view_id
) override
;
36 void CopyFrom(HostZoomMap
* copy
) override
;
37 double GetZoomLevelForHostAndScheme(const std::string
& scheme
,
38 const std::string
& host
) const override
;
39 // TODO(wjmaclean) Should we use a GURL here? crbug.com/384486
40 bool HasZoomLevel(const std::string
& scheme
,
41 const std::string
& host
) const override
;
42 ZoomLevelVector
GetAllZoomLevels() const override
;
43 void SetZoomLevelForHost(const std::string
& host
, double level
) override
;
44 void SetZoomLevelForHostAndScheme(const std::string
& scheme
,
45 const std::string
& host
,
46 double level
) override
;
47 bool UsesTemporaryZoomLevel(int render_process_id
,
48 int render_view_id
) const override
;
49 void SetTemporaryZoomLevel(int render_process_id
,
51 double level
) override
;
53 void ClearTemporaryZoomLevel(int render_process_id
,
54 int render_view_id
) override
;
55 double GetDefaultZoomLevel() const override
;
56 void SetDefaultZoomLevel(double level
) override
;
57 scoped_ptr
<Subscription
> AddZoomLevelChangedCallback(
58 const ZoomLevelChangedCallback
& callback
) override
;
60 // Returns the current zoom level for the specified WebContents. This may
61 // be a temporary zoom level, depending on UsesTemporaryZoomLevel().
62 double GetZoomLevelForWebContents(
63 const WebContentsImpl
& web_contents_impl
) const;
65 bool PageScaleFactorIsOneForWebContents(
66 const WebContentsImpl
& web_contents_impl
) const;
68 // Sets the zoom level for this WebContents. If this WebContents is using
69 // a temporary zoom level, then level is only applied to this WebContents.
70 // Otherwise, the level will be applied on a host level.
71 void SetZoomLevelForWebContents(const WebContentsImpl
& web_contents_impl
,
74 // Sets the zoom level for the specified view. The level may be set for only
75 // this view, or for the host, depending on UsesTemporaryZoomLevel().
76 void SetZoomLevelForView(int render_process_id
,
79 const std::string
& host
);
81 // Returns the temporary zoom level that's only valid for the lifetime of
82 // the given WebContents (i.e. isn't saved and doesn't affect other
83 // WebContentses) if it exists, the default zoom level otherwise.
85 // This may be called on any thread.
86 double GetTemporaryZoomLevel(int render_process_id
,
87 int render_view_id
) const;
89 // Returns the zoom level regardless of whether it's temporary, host-keyed or
92 // This may be called on any thread.
93 double GetZoomLevelForView(const GURL
& url
,
94 int render_process_id
,
95 int render_view_id
) const;
97 // NotificationObserver implementation.
98 void Observe(int type
,
99 const NotificationSource
& source
,
100 const NotificationDetails
& details
) override
;
102 void SendErrorPageZoomLevelRefresh();
105 typedef std::map
<std::string
, double> HostZoomLevels
;
106 typedef std::map
<std::string
, HostZoomLevels
> SchemeHostZoomLevels
;
108 struct RenderViewKey
{
109 int render_process_id
;
111 RenderViewKey(int render_process_id
, int render_view_id
)
112 : render_process_id(render_process_id
),
113 render_view_id(render_view_id
) {}
114 bool operator<(const RenderViewKey
& other
) const {
115 return render_process_id
< other
.render_process_id
||
116 ((render_process_id
== other
.render_process_id
) &&
117 (render_view_id
< other
.render_view_id
));
121 typedef std::map
<RenderViewKey
, double> TemporaryZoomLevels
;
122 typedef std::map
<RenderViewKey
, bool> ViewPageScaleFactorsAreOne
;
124 double GetZoomLevelForHost(const std::string
& host
) const;
126 // Non-locked versions for internal use. These should only be called within
127 // a scope where a lock has been acquired.
128 double GetZoomLevelForHostInternal(const std::string
& host
) const;
129 double GetZoomLevelForHostAndSchemeInternal(const std::string
& scheme
,
130 const std::string
& host
) const;
132 // Notifies the renderers from this browser context to change the zoom level
133 // for the specified host and scheme.
134 // TODO(wjmaclean) Should we use a GURL here? crbug.com/384486
135 void SendZoomLevelChange(const std::string
& scheme
,
136 const std::string
& host
,
139 // Callbacks called when zoom level changes.
140 base::CallbackList
<void(const ZoomLevelChange
&)>
141 zoom_level_changed_callbacks_
;
143 // Copy of the pref data, so that we can read it on the IO thread.
144 HostZoomLevels host_zoom_levels_
;
145 SchemeHostZoomLevels scheme_host_zoom_levels_
;
146 double default_zoom_level_
;
148 // Page scale factor data for each renderer.
149 ViewPageScaleFactorsAreOne view_page_scale_factors_are_one_
;
151 // Don't expect more than a couple of tabs that are using a temporary zoom
152 // level, so vector is fine for now.
153 TemporaryZoomLevels temporary_zoom_levels_
;
155 // Used around accesses to |host_zoom_levels_|, |default_zoom_level_|,
156 // |temporary_zoom_levels_|, and |view_page_scale_factors_are_one_| to
157 // guarantee thread safety.
158 mutable base::Lock lock_
;
160 NotificationRegistrar registrar_
;
162 DISALLOW_COPY_AND_ASSIGN(HostZoomMapImpl
);
165 } // namespace content
167 #endif // CONTENT_BROWSER_HOST_ZOOM_MAP_IMPL_H_