File: host_zoom_map_impl.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (167 lines) | stat: -rw-r--r-- 7,257 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_HOST_ZOOM_MAP_IMPL_H_
#define CONTENT_BROWSER_HOST_ZOOM_MAP_IMPL_H_

#include <map>
#include <set>
#include <string>
#include <tuple>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "base/task/sequenced_task_runner_helpers.h"
#include "base/time/time.h"
#include "content/common/content_export.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/browser/host_zoom_map.h"

namespace content {

class RenderFrameHostImpl;
class WebContentsImpl;

// HostZoomMap lives on the UI thread.
class CONTENT_EXPORT HostZoomMapImpl : public HostZoomMap {
 public:
  HostZoomMapImpl();

  HostZoomMapImpl(const HostZoomMapImpl&) = delete;
  HostZoomMapImpl& operator=(const HostZoomMapImpl&) = delete;

  ~HostZoomMapImpl() override;

  // HostZoomMap implementation:
  void CopyFrom(HostZoomMap* copy) override;
  double GetZoomLevelForHostAndScheme(const std::string& scheme,
                                      const std::string& host) override;
  // TODO(wjmaclean) Should we use a GURL here? crbug.com/384486
  bool HasZoomLevel(const std::string& scheme,
                    const std::string& host) override;
  ZoomLevelVector GetAllZoomLevels() override;
  void SetZoomLevelForHost(const std::string& host, double level) override;
  void InitializeZoomLevelForHost(const std::string& host,
                                  double level,
                                  base::Time last_modified) override;
  void SetZoomLevelForHostAndScheme(const std::string& scheme,
                                    const std::string& host,
                                    double level) override;
  bool UsesTemporaryZoomLevel(const GlobalRenderFrameHostId& rfh_id) override;
  void SetNoLongerUsesTemporaryZoomLevel(const GlobalRenderFrameHostId& rfh_id);
  void SetTemporaryZoomLevel(const GlobalRenderFrameHostId& rfh_id,
                             double level) override;
  void ClearZoomLevels(base::Time delete_begin, base::Time delete_end) override;
  void ClearTemporaryZoomLevel(const GlobalRenderFrameHostId& rfh_id) override;
  double GetDefaultZoomLevel() override;
  void SetDefaultZoomLevel(double level) override;
  base::CallbackListSubscription AddZoomLevelChangedCallback(
      ZoomLevelChangedCallback callback) override;

  // Returns the current zoom level for the specified WebContents. This may
  // be a temporary zoom level, depending on UsesTemporaryZoomLevel().
  double GetZoomLevelForWebContents(WebContentsImpl* web_contents_impl);
  double GetZoomLevelForWebContents(WebContentsImpl* web_contents_impl,
                                    GlobalRenderFrameHostId rfh_id);

  // Sets the zoom level for this WebContents. If this WebContents is using
  // a temporary zoom level, then level is only applied to this WebContents.
  // Otherwise, the level will be applied on a host level.
  void SetZoomLevelForWebContents(WebContentsImpl* web_contents_impl,
                                  double level);
  void SetZoomLevelForWebContents(WebContentsImpl* web_contents_impl,
                                  GlobalRenderFrameHostId rfh_id,
                                  double level);

  // Returns the temporary zoom level that's only valid for the lifetime of
  // the given RenderFrameHost identified by `rfh_id` (i.e. isn't saved and
  // doesn't affect other RenderFrameHosts) if it exists, the default zoom
  // level otherwise.
  double GetTemporaryZoomLevel(const GlobalRenderFrameHostId& rfh_id) const;

  void SendErrorPageZoomLevelRefresh();

  void SetClockForTesting(base::Clock* clock) override;

#if BUILDFLAG(IS_ANDROID)
  void SetDefaultZoomLevelPrefCallback(
      HostZoomMap::DefaultZoomChangedCallback callback) override;
  HostZoomMap::DefaultZoomChangedCallback* GetDefaultZoomLevelPrefCallback();
  double GetZoomLevelForHostAndSchemeAndroid(const std::string& scheme,
                                             const std::string& host) override;
  void SetSystemFontScaleForTesting(float scale);
  void SetShouldAdjustForOSLevelForTesting(bool shouldAdjustForOSLevel);
#endif

  double GetZoomLevelForPreviewAndHost(const std::string& host) override;
  void SetZoomLevelForPreviewAndHost(const std::string& host,
                                     double level) override;

  void SetIndependentZoomForFrameTreeNode(WebContents* web_contents,
                                          FrameTreeNodeId ftn_id) override;
  void ClearIndependentZoomForFrameTreeNode(FrameTreeNodeId ftn_id) override;
  bool IsIndependentZoomFrameTreeNode(FrameTreeNodeId ftn_id) const;

 private:
  struct ZoomLevel {
    double level;
    base::Time last_modified;
  };
  typedef std::map<std::string, ZoomLevel> HostZoomLevels;
  typedef std::map<std::string, HostZoomLevels> SchemeHostZoomLevels;

  typedef std::map<GlobalRenderFrameHostId, double> TemporaryZoomLevels;
  typedef std::set<FrameTreeNodeId> IndependentZoomFrameTreeNodes;

  double GetZoomLevelForHost(const std::string& host) const;

  // Set a zoom level for |host| and store the |last_modified| timestamp.
  // Use only to explicitly set a timestamp.
  void SetZoomLevelForHostInternal(const std::string& host,
                                   double level,
                                   base::Time last_modified);

  // Internal helper for SetDefaultZoomLevel().
  void SetDefaultZoomLevelInternal(double level,
                                   WebContentsImpl* web_contents,
                                   RenderFrameHostImpl* rfh);

  // Notifies the renderers from this browser context to change the zoom level
  // for the specified host and scheme.
  // |zoom level| will be extracted from |host_zoom_levels_| when needed, so no
  // need to pass them in.
  // TODO(wjmaclean) Should we use a GURL here? crbug.com/384486
  void SendZoomLevelChange(const std::string& scheme, const std::string& host);

  // Callbacks called when zoom level changes.
  base::RepeatingCallbackList<void(const ZoomLevelChange&)>
      zoom_level_changed_callbacks_;

#if BUILDFLAG(IS_ANDROID)
  // Callback called when Java-side UI updates the default zoom level.
  HostZoomMap::DefaultZoomChangedCallback default_zoom_level_pref_callback_;
#endif

  // Copy of the pref data.
  HostZoomLevels host_zoom_levels_;
  SchemeHostZoomLevels scheme_host_zoom_levels_;
  double default_zoom_level_;

  TemporaryZoomLevels temporary_zoom_levels_;
  // Used to track which FrameTreeNodes have independent zoom. A FrameTreeNode
  // can have a zoom level that is independent from the main frame when it is
  // displaying content in a GuestView (or possibly a PDF in a OOPIF without a
  // GuestView), and features::kGuestViewMPArch is enabled. When this feature is
  // not enabled it means that GuestViews will have their own WebContents, and
  // so the use of a single zoom level for an entire WebContents suffices.
  IndependentZoomFrameTreeNodes independent_zoom_frame_tree_nodes_;

  HostZoomLevels host_zoom_levels_for_preview_;

  raw_ptr<base::Clock> clock_;
};

}  // namespace content

#endif  // CONTENT_BROWSER_HOST_ZOOM_MAP_IMPL_H_