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_PUBLIC_BROWSER_HOST_ZOOM_MAP_H_
      6 #define CONTENT_PUBLIC_BROWSER_HOST_ZOOM_MAP_H_
      7 
      8 #include <map>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/basictypes.h"
     13 #include "base/callback.h"
     14 #include "base/callback_list.h"
     15 #include "content/common/content_export.h"
     16 
     17 namespace content {
     18 
     19 class BrowserContext;
     20 class ResourceContext;
     21 class WebContents;
     22 
     23 // Maps hostnames to custom zoom levels.  Written on the UI thread and read on
     24 // any thread.  One instance per browser context. Must be created on the UI
     25 // thread, and it'll delete itself on the UI thread as well.
     26 // Zoom can be defined at three levels: default zoom, zoom for host, and zoom
     27 // for host with specific scheme. Setting any of the levels leaves settings
     28 // for other settings intact. Getting the zoom level starts at the most
     29 // specific setting and progresses to the less specific: first the zoom for the
     30 // host and scheme pair is checked, secondly the zoom for the host only and
     31 // lastly default zoom.
     32 
     33 class HostZoomMap {
     34  public:
     35   // Enum that indicates what was the scope of zoom level change.
     36   enum ZoomLevelChangeMode {
     37     ZOOM_CHANGED_FOR_HOST,            // Zoom level changed for host.
     38     ZOOM_CHANGED_FOR_SCHEME_AND_HOST, // Zoom level changed for scheme/host
     39                                       // pair.
     40     ZOOM_CHANGED_TEMPORARY_ZOOM,      // Temporary zoom change for specific
     41                                       // renderer, no scheme/host is specified.
     42   };
     43 
     44   // Structure used to notify about zoom changes. Host and/or scheme are empty
     45   // if not applicable to |mode|.
     46   struct ZoomLevelChange {
     47     ZoomLevelChangeMode mode;
     48     std::string host;
     49     std::string scheme;
     50     double zoom_level;
     51   };
     52 
     53   typedef std::vector<ZoomLevelChange> ZoomLevelVector;
     54 
     55   CONTENT_EXPORT static HostZoomMap* GetForBrowserContext(
     56       BrowserContext* browser_context);
     57 
     58   // Returns the current zoom level for the specified WebContents. May be
     59   // temporary or host-specific.
     60   CONTENT_EXPORT static double GetZoomLevel(const WebContents* web_contents);
     61 
     62   // Sets the current zoom level for the specified WebContents. The level may
     63   // be temporary or host-specific depending on the particular WebContents.
     64   CONTENT_EXPORT static void SetZoomLevel(const WebContents* web_contents,
     65                                           double level);
     66 
     67   // Copy the zoom levels from the given map. Can only be called on the UI
     68   // thread.
     69   virtual void CopyFrom(HostZoomMap* copy) = 0;
     70 
     71   // Here |host| is the host portion of URL, or (in the absence of a host)
     72   // the complete spec of the URL.
     73   // Returns the zoom for the specified |scheme| and |host|. See class
     74   // description for details.
     75   //
     76   // This may be called on any thread.
     77   virtual double GetZoomLevelForHostAndScheme(
     78       const std::string& scheme,
     79       const std::string& host) const = 0;
     80 
     81   // Returns true if the specified |scheme| and/or |host| has a zoom level
     82   // currently set.
     83   //
     84   // This may be called on any thread.
     85   virtual bool HasZoomLevel(const std::string& scheme,
     86                             const std::string& host) const = 0;
     87 
     88   // Returns all non-temporary zoom levels. Can be called on any thread.
     89   virtual ZoomLevelVector GetAllZoomLevels() const = 0;
     90 
     91   // Here |host| is the host portion of URL, or (in the absence of a host)
     92   // the complete spec of the URL.
     93   // Sets the zoom level for the |host| to |level|.  If the level matches the
     94   // current default zoom level, the host is erased from the saved preferences;
     95   // otherwise the new value is written out.
     96   // Zoom levels specified for both scheme and host are not affected.
     97   //
     98   // This should only be called on the UI thread.
     99   virtual void SetZoomLevelForHost(const std::string& host, double level) = 0;
    100 
    101   // Here |host| is the host portion of URL, or (in the absence of a host)
    102   // the complete spec of the URL.
    103   // Sets the zoom level for the |scheme|/|host| pair to |level|. No values
    104   // will be erased during this operation, and this value will not be stored in
    105   // the preferences.
    106   //
    107   // This should only be called on the UI thread.
    108   virtual void SetZoomLevelForHostAndScheme(const std::string& scheme,
    109                                             const std::string& host,
    110                                             double level) = 0;
    111 
    112   // Returns whether the view manages its zoom level independently of other
    113   // views displaying content from the same host.
    114   virtual bool UsesTemporaryZoomLevel(int render_process_id,
    115                                       int render_view_id) const = 0;
    116 
    117   // Sets the temporary zoom level that's only valid for the lifetime of this
    118   // WebContents.
    119   //
    120   // This should only be called on the UI thread.
    121   virtual void SetTemporaryZoomLevel(int render_process_id,
    122                                      int render_view_id,
    123                                      double level) = 0;
    124 
    125   // Clears the temporary zoom level stored for this WebContents.
    126   //
    127   // This should only be called on the UI thread.
    128   virtual void ClearTemporaryZoomLevel(int render_process_id,
    129                                        int render_view_id) = 0;
    130 
    131   // Get/Set the default zoom level for pages that don't override it.
    132   virtual double GetDefaultZoomLevel() const = 0;
    133   virtual void SetDefaultZoomLevel(double level) = 0;;
    134 
    135   typedef base::Callback<void(const ZoomLevelChange&)> ZoomLevelChangedCallback;
    136   typedef base::CallbackList<void(const ZoomLevelChange&)>::Subscription
    137       Subscription;
    138   // Add and remove zoom level changed callbacks.
    139   virtual scoped_ptr<Subscription> AddZoomLevelChangedCallback(
    140       const ZoomLevelChangedCallback& callback) = 0;
    141 
    142  protected:
    143   virtual ~HostZoomMap() {}
    144 };
    145 
    146 }  // namespace content
    147 
    148 #endif  // CONTENT_PUBLIC_BROWSER_HOST_ZOOM_MAP_H_
    149