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