[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / content / browser / host_zoom_map_impl.h
blob7840188c0d747399cd59fbb42d0871237aafe74f
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_
8 #include <map>
9 #include <string>
10 #include <vector>
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"
20 namespace content {
22 // HostZoomMap needs to be deleted on the UI thread because it listens
23 // to notifications on there (and holds a NotificationRegistrar).
24 class CONTENT_EXPORT HostZoomMapImpl : public NON_EXPORTED_BASE(HostZoomMap),
25 public NotificationObserver,
26 public base::SupportsUserData::Data {
27 public:
28 HostZoomMapImpl();
29 virtual ~HostZoomMapImpl();
31 // HostZoomMap implementation:
32 virtual void CopyFrom(HostZoomMap* copy) OVERRIDE;
33 virtual double GetZoomLevel(const std::string& host) const OVERRIDE;
34 virtual void SetZoomLevel(const std::string& host, double level) OVERRIDE;
35 virtual double GetDefaultZoomLevel() const OVERRIDE;
36 virtual void SetDefaultZoomLevel(double level) OVERRIDE;
38 // Returns the temporary zoom level that's only valid for the lifetime of
39 // the given WebContents (i.e. isn't saved and doesn't affect other
40 // WebContentses) if it exists, the default zoom level otherwise.
42 // This may be called on any thread.
43 double GetTemporaryZoomLevel(int render_process_id,
44 int render_view_id) const;
46 // Sets the temporary zoom level that's only valid for the lifetime of this
47 // WebContents.
49 // This should only be called on the UI thread.
50 void SetTemporaryZoomLevel(int render_process_id,
51 int render_view_id,
52 double level);
54 // NotificationObserver implementation.
55 virtual void Observe(int type,
56 const NotificationSource& source,
57 const NotificationDetails& details) OVERRIDE;
59 private:
60 typedef std::map<std::string, double> HostZoomLevels;
62 // Copy of the pref data, so that we can read it on the IO thread.
63 HostZoomLevels host_zoom_levels_;
64 double default_zoom_level_;
66 struct TemporaryZoomLevel {
67 int render_process_id;
68 int render_view_id;
69 double zoom_level;
72 // Don't expect more than a couple of tabs that are using a temporary zoom
73 // level, so vector is fine for now.
74 std::vector<TemporaryZoomLevel> temporary_zoom_levels_;
76 // Used around accesses to |host_zoom_levels_|, |default_zoom_level_| and
77 // |temporary_zoom_levels_| to guarantee thread safety.
78 mutable base::Lock lock_;
80 NotificationRegistrar registrar_;
82 DISALLOW_COPY_AND_ASSIGN(HostZoomMapImpl);
85 } // namespace content
87 #endif // CONTENT_BROWSER_HOST_ZOOM_MAP_IMPL_H_