[email protected] | 3bbacc5b | 2012-04-17 17:46:15 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 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_ |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
jsbell | 71dafdfe | 2015-11-20 19:40:38 | [diff] [blame] | 10 | #include <tuple> |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 13 | #include "base/memory/raw_ptr.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 14 | #include "base/task/sequenced_task_runner_helpers.h" |
Gabriel Charette | d87f10f | 2022-03-31 00:44:22 | [diff] [blame] | 15 | #include "base/time/time.h" |
Lei Zhang | 7ab31375 | 2021-11-17 01:26:00 | [diff] [blame] | 16 | #include "content/common/content_export.h" |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 17 | #include "content/public/browser/host_zoom_map.h" |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 18 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 19 | namespace content { |
| 20 | |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 21 | class WebContentsImpl; |
| 22 | |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 23 | // HostZoomMap lives on the UI thread. |
Sam McNally | e741fd66 | 2017-08-30 02:07:50 | [diff] [blame] | 24 | class CONTENT_EXPORT HostZoomMapImpl : public HostZoomMap { |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 25 | public: |
| 26 | HostZoomMapImpl(); |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 27 | |
| 28 | HostZoomMapImpl(const HostZoomMapImpl&) = delete; |
| 29 | HostZoomMapImpl& operator=(const HostZoomMapImpl&) = delete; |
| 30 | |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 31 | ~HostZoomMapImpl() override; |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 32 | |
| 33 | // HostZoomMap implementation: |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 34 | void CopyFrom(HostZoomMap* copy) override; |
| 35 | double GetZoomLevelForHostAndScheme(const std::string& scheme, |
Lucas Furukawa Gadani | 4b4eed0 | 2019-06-04 23:12:04 | [diff] [blame] | 36 | const std::string& host) override; |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 37 | // TODO(wjmaclean) Should we use a GURL here? crbug.com/384486 |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 38 | bool HasZoomLevel(const std::string& scheme, |
Lucas Furukawa Gadani | 4b4eed0 | 2019-06-04 23:12:04 | [diff] [blame] | 39 | const std::string& host) override; |
| 40 | ZoomLevelVector GetAllZoomLevels() override; |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 41 | void SetZoomLevelForHost(const std::string& host, double level) override; |
Christian Dullweber | cc736c1 | 2017-09-04 09:27:50 | [diff] [blame] | 42 | void InitializeZoomLevelForHost(const std::string& host, |
| 43 | double level, |
| 44 | base::Time last_modified) override; |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 45 | void SetZoomLevelForHostAndScheme(const std::string& scheme, |
| 46 | const std::string& host, |
| 47 | double level) override; |
| 48 | bool UsesTemporaryZoomLevel(int render_process_id, |
Lucas Furukawa Gadani | 4b4eed0 | 2019-06-04 23:12:04 | [diff] [blame] | 49 | int render_view_id) override; |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 50 | void SetTemporaryZoomLevel(int render_process_id, |
| 51 | int render_view_id, |
| 52 | double level) override; |
Christian Dullweber | cc736c1 | 2017-09-04 09:27:50 | [diff] [blame] | 53 | void ClearZoomLevels(base::Time delete_begin, base::Time delete_end) override; |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 54 | void ClearTemporaryZoomLevel(int render_process_id, |
| 55 | int render_view_id) override; |
Lucas Furukawa Gadani | 4b4eed0 | 2019-06-04 23:12:04 | [diff] [blame] | 56 | double GetDefaultZoomLevel() override; |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 57 | void SetDefaultZoomLevel(double level) override; |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 58 | base::CallbackListSubscription AddZoomLevelChangedCallback( |
danakj | 6aa13ea | 2019-12-10 14:53:26 | [diff] [blame] | 59 | ZoomLevelChangedCallback callback) override; |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 60 | |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 61 | // Returns the current zoom level for the specified WebContents. This may |
| 62 | // be a temporary zoom level, depending on UsesTemporaryZoomLevel(). |
Lucas Furukawa Gadani | 4b4eed0 | 2019-06-04 23:12:04 | [diff] [blame] | 63 | double GetZoomLevelForWebContents(WebContentsImpl* web_contents_impl); |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 64 | |
| 65 | // Sets the zoom level for this WebContents. If this WebContents is using |
| 66 | // a temporary zoom level, then level is only applied to this WebContents. |
| 67 | // Otherwise, the level will be applied on a host level. |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 68 | void SetZoomLevelForWebContents(WebContentsImpl* web_contents_impl, |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 69 | double level); |
| 70 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 71 | // Returns the temporary zoom level that's only valid for the lifetime of |
[email protected] | 3bbacc5b | 2012-04-17 17:46:15 | [diff] [blame] | 72 | // the given WebContents (i.e. isn't saved and doesn't affect other |
| 73 | // WebContentses) if it exists, the default zoom level otherwise. |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 74 | double GetTemporaryZoomLevel(int render_process_id, |
| 75 | int render_view_id) const; |
| 76 | |
wjmaclean | de29ed5 | 2014-10-28 21:09:06 | [diff] [blame] | 77 | void SendErrorPageZoomLevelRefresh(); |
| 78 | |
Sam McNally | e741fd66 | 2017-08-30 02:07:50 | [diff] [blame] | 79 | void WillCloseRenderView(int render_process_id, int render_view_id); |
| 80 | |
tzik | 67025f67 | 2017-11-29 05:04:44 | [diff] [blame] | 81 | void SetClockForTesting(base::Clock* clock) override; |
Christian Dullweber | cc736c1 | 2017-09-04 09:27:50 | [diff] [blame] | 82 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 83 | private: |
Christian Dullweber | cc736c1 | 2017-09-04 09:27:50 | [diff] [blame] | 84 | struct ZoomLevel { |
| 85 | double level; |
| 86 | base::Time last_modified; |
| 87 | }; |
| 88 | typedef std::map<std::string, ZoomLevel> HostZoomLevels; |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 89 | typedef std::map<std::string, HostZoomLevels> SchemeHostZoomLevels; |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 90 | |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 91 | struct RenderViewKey { |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 92 | int render_process_id; |
| 93 | int render_view_id; |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 94 | RenderViewKey(int render_process_id, int render_view_id) |
| 95 | : render_process_id(render_process_id), |
| 96 | render_view_id(render_view_id) {} |
| 97 | bool operator<(const RenderViewKey& other) const { |
jsbell | 71dafdfe | 2015-11-20 19:40:38 | [diff] [blame] | 98 | return std::tie(render_process_id, render_view_id) < |
| 99 | std::tie(other.render_process_id, other.render_view_id); |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 100 | } |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 101 | }; |
| 102 | |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 103 | typedef std::map<RenderViewKey, double> TemporaryZoomLevels; |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 104 | |
| 105 | double GetZoomLevelForHost(const std::string& host) const; |
| 106 | |
Christian Dullweber | cc736c1 | 2017-09-04 09:27:50 | [diff] [blame] | 107 | // Set a zoom level for |host| and store the |last_modified| timestamp. |
| 108 | // Use only to explicitly set a timestamp. |
| 109 | void SetZoomLevelForHostInternal(const std::string& host, |
| 110 | double level, |
| 111 | base::Time last_modified); |
| 112 | |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 113 | // Notifies the renderers from this browser context to change the zoom level |
| 114 | // for the specified host and scheme. |
akaba | 3483d8f | 2018-07-10 21:43:09 | [diff] [blame] | 115 | // |zoom level| will be extracted from |host_zoom_levels_| when needed, so no |
| 116 | // need to pass them in. |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 117 | // TODO(wjmaclean) Should we use a GURL here? crbug.com/384486 |
akaba | c6bd121 | 2018-06-25 20:10:48 | [diff] [blame] | 118 | void SendZoomLevelChange(const std::string& scheme, const std::string& host); |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 119 | |
[email protected] | 89c9aca | 2013-02-07 15:08:42 | [diff] [blame] | 120 | // Callbacks called when zoom level changes. |
Fabrice de Gans-Riberi | d82d122 | 2021-03-22 22:23:11 | [diff] [blame] | 121 | base::RepeatingCallbackList<void(const ZoomLevelChange&)> |
[email protected] | 11783281 | 2013-10-02 07:06:02 | [diff] [blame] | 122 | zoom_level_changed_callbacks_; |
[email protected] | 89c9aca | 2013-02-07 15:08:42 | [diff] [blame] | 123 | |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 124 | // Copy of the pref data. |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 125 | HostZoomLevels host_zoom_levels_; |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 126 | SchemeHostZoomLevels scheme_host_zoom_levels_; |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 127 | double default_zoom_level_; |
| 128 | |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 129 | TemporaryZoomLevels temporary_zoom_levels_; |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 130 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 131 | raw_ptr<base::Clock> clock_; |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 132 | }; |
| 133 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 134 | } // namespace content |
| 135 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 136 | #endif // CONTENT_BROWSER_HOST_ZOOM_MAP_IMPL_H_ |