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 
     22 // Maps hostnames to custom zoom levels.  Written on the UI thread and read on
     23 // any thread.  One instance per browser context. Must be created on the UI
     24 // thread, and it'll delete itself on the UI thread as well.
     25 // Zoom can be defined at three levels: default zoom, zoom for host, and zoom
     26 // for host with specific scheme. Setting any of the levels leaves settings
     27 // for other settings intact. Getting the zoom level starts at the most
     28 // specific setting and progresses to the less specific: first the zoom for the
     29 // host and scheme pair is checked, secondly the zoom for the host only and
     30 // lastly default zoom.
     31 
     32 class HostZoomMap {
     33  public:
     34   // Enum that indicates what was the scope of zoom level change.
     35   enum ZoomLevelChangeMode {
     36     ZOOM_CHANGED_FOR_HOST,            // Zoom level changed for host.
     37     ZOOM_CHANGED_FOR_SCHEME_AND_HOST, // Zoom level changed for scheme/host
     38                                       // pair.
     39     ZOOM_CHANGED_TEMPORARY_ZOOM,      // Temporary zoom change for specific
     40                                       // renderer, no scheme/host is specified.
     41   };
     42 
     43   // Structure used to notify about zoom changes. Host and/or scheme are empty
     44   // if not applicable to |mode|.
     45   struct ZoomLevelChange {
     46     ZoomLevelChangeMode mode;
     47     std::string host;
     48     std::string scheme;
     49     double zoom_level;
     50   };
     51 
     52   CONTENT_EXPORT static HostZoomMap* GetForBrowserContext(
     53       BrowserContext* browser_context);
     54 
     55   // Copy the zoom levels from the given map. Can only be called on the UI
     56   // thread.
     57   virtual void CopyFrom(HostZoomMap* copy) = 0;
     58 
     59   // Here |host| is the host portion of URL, or (in the absence of a host)
     60   // the complete spec of the URL.
     61   // Returns the zoom for the specified |scheme| and |host|. See class
     62   // description for details.
     63   //
     64   // This may be called on any thread.
     65   virtual double GetZoomLevelForHostAndScheme(
     66       const std::string& scheme,
     67       const std::string& host) const = 0;
     68 
     69   // Here |host| is the host portion of URL, or (in the absence of a host)
     70   // the complete spec of the URL.
     71   // Sets the zoom level for the |host| to |level|.  If the level matches the
     72   // current default zoom level, the host is erased from the saved preferences;
     73   // otherwise the new value is written out.
     74   // Zoom levels specified for both scheme and host are not affected.
     75   //
     76   // This should only be called on the UI thread.
     77   virtual void SetZoomLevelForHost(const std::string& host, double level) = 0;
     78 
     79   // Here |host| is the host portion of URL, or (in the absence of a host)
     80   // the complete spec of the URL.
     81   // Sets the zoom level for the |scheme|/|host| pair to |level|. No values
     82   // will be erased during this operation, and this value will not be stored in
     83   // the preferences.
     84   //
     85   // This should only be called on the UI thread.
     86   virtual void SetZoomLevelForHostAndScheme(const std::string& scheme,
     87                                             const std::string& host,
     88                                             double level) = 0;
     89 
     90   // Get/Set the default zoom level for pages that don't override it.
     91   virtual double GetDefaultZoomLevel() const = 0;
     92   virtual void SetDefaultZoomLevel(double level) = 0;;
     93 
     94   typedef base::Callback<void(const ZoomLevelChange&)> ZoomLevelChangedCallback;
     95   typedef base::CallbackList<void(const ZoomLevelChange&)>::Subscription
     96       Subscription;
     97   // Add and remove zoom level changed callbacks.
     98   virtual scoped_ptr<Subscription> AddZoomLevelChangedCallback(
     99       const ZoomLevelChangedCallback& callback) = 0;
    100 
    101  protected:
    102   virtual ~HostZoomMap() {}
    103 };
    104 
    105 }  // namespace content
    106 
    107 #endif  // CONTENT_PUBLIC_BROWSER_HOST_ZOOM_MAP_H_
    108