Home | History | Annotate | Download | only in browser
      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 // 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();
     30 
     31   // HostZoomMap implementation:
     32   virtual void CopyFrom(HostZoomMap* copy) OVERRIDE;
     33   virtual double GetZoomLevelForHostAndScheme(
     34       const std::string& scheme,
     35       const std::string& host) const OVERRIDE;
     36   virtual void SetZoomLevelForHost(
     37       const std::string& host,
     38       double level) OVERRIDE;
     39   virtual void SetZoomLevelForHostAndScheme(
     40       const std::string& scheme,
     41       const std::string& host,
     42       double level) OVERRIDE;
     43   virtual double GetDefaultZoomLevel() const OVERRIDE;
     44   virtual void SetDefaultZoomLevel(double level) OVERRIDE;
     45   virtual void AddZoomLevelChangedCallback(
     46       const ZoomLevelChangedCallback& callback) OVERRIDE;
     47   virtual void RemoveZoomLevelChangedCallback(
     48       const ZoomLevelChangedCallback& callback) OVERRIDE;
     49 
     50   // Returns the temporary zoom level that's only valid for the lifetime of
     51   // the given WebContents (i.e. isn't saved and doesn't affect other
     52   // WebContentses) if it exists, the default zoom level otherwise.
     53   //
     54   // This may be called on any thread.
     55   double GetTemporaryZoomLevel(int render_process_id,
     56                                int render_view_id) const;
     57 
     58   // Sets the temporary zoom level that's only valid for the lifetime of this
     59   // WebContents.
     60   //
     61   // This should only be called on the UI thread.
     62   void SetTemporaryZoomLevel(int render_process_id,
     63                              int render_view_id,
     64                              double level);
     65 
     66   // NotificationObserver implementation.
     67   virtual void Observe(int type,
     68                        const NotificationSource& source,
     69                        const NotificationDetails& details) OVERRIDE;
     70 
     71  private:
     72   double GetZoomLevelForHost(const std::string& host) const;
     73 
     74   typedef std::map<std::string, double> HostZoomLevels;
     75   typedef std::map<std::string, HostZoomLevels> SchemeHostZoomLevels;
     76 
     77   // Callbacks called when zoom level changes.
     78   std::vector<ZoomLevelChangedCallback> zoom_level_changed_callbacks_;
     79 
     80   // Copy of the pref data, so that we can read it on the IO thread.
     81   HostZoomLevels host_zoom_levels_;
     82   SchemeHostZoomLevels scheme_host_zoom_levels_;
     83   double default_zoom_level_;
     84 
     85   struct TemporaryZoomLevel {
     86     int render_process_id;
     87     int render_view_id;
     88     double zoom_level;
     89   };
     90 
     91   // Don't expect more than a couple of tabs that are using a temporary zoom
     92   // level, so vector is fine for now.
     93   std::vector<TemporaryZoomLevel> temporary_zoom_levels_;
     94 
     95   // Used around accesses to |host_zoom_levels_|, |default_zoom_level_| and
     96   // |temporary_zoom_levels_| to guarantee thread safety.
     97   mutable base::Lock lock_;
     98 
     99   NotificationRegistrar registrar_;
    100 
    101   DISALLOW_COPY_AND_ASSIGN(HostZoomMapImpl);
    102 };
    103 
    104 }  // namespace content
    105 
    106 #endif  // CONTENT_BROWSER_HOST_ZOOM_MAP_IMPL_H_
    107