Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2010 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 CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_
      6 #define CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_
      7 #pragma once
      8 
      9 #include <map>
     10 #include <string>
     11 #include <vector>
     12 
     13 #include "base/basictypes.h"
     14 #include "third_party/skia/include/core/SkColor.h"
     15 
     16 namespace gfx {
     17 class Canvas;
     18 class Rect;
     19 }
     20 
     21 class GURL;
     22 class SkBitmap;
     23 
     24 // ExtensionAction encapsulates the state of a browser or page action.
     25 // Instances can have both global and per-tab state. If a property does not have
     26 // a per-tab value, the global value is used instead.
     27 class ExtensionAction {
     28  public:
     29   // Use this ID to indicate the default state for properties that take a tab_id
     30   // parameter.
     31   static const int kDefaultTabId;
     32 
     33   ExtensionAction();
     34   ~ExtensionAction();
     35 
     36   // extension id
     37   std::string extension_id() const { return extension_id_; }
     38   void set_extension_id(const std::string& extension_id) {
     39     extension_id_ = extension_id;
     40   }
     41 
     42   // action id -- only used with legacy page actions API
     43   std::string id() const { return id_; }
     44   void set_id(const std::string& id) { id_ = id; }
     45 
     46   // static icon paths from manifest -- only used with legacy page actions API.
     47   std::vector<std::string>* icon_paths() { return &icon_paths_; }
     48 
     49   // Set the url which the popup will load when the user clicks this action's
     50   // icon.  Setting an empty URL will disable the popup for a given tab.
     51   void SetPopupUrl(int tab_id, const GURL& url);
     52 
     53   // Use HasPopup() to see if a popup should be displayed.
     54   bool HasPopup(int tab_id);
     55 
     56   // Get the URL to display in a popup.
     57   GURL GetPopupUrl(int tab_id);
     58 
     59   // Set this action's title on a specific tab.
     60   void SetTitle(int tab_id, const std::string& title) {
     61     SetValue(&title_, tab_id, title);
     62   }
     63 
     64   // If tab |tab_id| has a set title, return it.  Otherwise, return
     65   // the default title.
     66   std::string GetTitle(int tab_id) { return GetValue(&title_, tab_id); }
     67 
     68   // Icons are a bit different because the default value can be set to either a
     69   // bitmap or a path. However, conceptually, there is only one default icon.
     70   // Setting the default icon using a path clears the bitmap and vice-versa.
     71   //
     72   // To get the default icon, first check for the bitmap. If it is null, check
     73   // for the path.
     74 
     75   // Set this action's icon bitmap on a specific tab.
     76   void SetIcon(int tab_id, const SkBitmap& bitmap);
     77 
     78   // Get the icon for a tab, or the default if no icon was set.
     79   SkBitmap GetIcon(int tab_id);
     80 
     81   // Set this action's icon index for a specific tab.  For use with
     82   // icon_paths(), only used in page actions.
     83   void SetIconIndex(int tab_id, int index);
     84 
     85   // Get this action's icon index for a tab, or the default if no icon index
     86   // was set.
     87   int GetIconIndex(int tab_id) {
     88     return GetValue(&icon_index_, tab_id);
     89   }
     90 
     91   // Non-tab-specific icon path. This is used to support the default_icon key of
     92   // page and browser actions.
     93   void set_default_icon_path(const std::string& path) {
     94     default_icon_path_ = path;
     95   }
     96   std::string default_icon_path() {
     97     return default_icon_path_;
     98   }
     99 
    100   // Set this action's badge text on a specific tab.
    101   void SetBadgeText(int tab_id, const std::string& text) {
    102     SetValue(&badge_text_, tab_id, text);
    103   }
    104   // Get the badge text for a tab, or the default if no badge text was set.
    105   std::string GetBadgeText(int tab_id) {
    106     return GetValue(&badge_text_, tab_id);
    107   }
    108 
    109   // Set this action's badge text color on a specific tab.
    110   void SetBadgeTextColor(int tab_id, const SkColor& text_color) {
    111     SetValue(&badge_text_color_, tab_id, text_color);
    112   }
    113   // Get the text color for a tab, or the default color if no text color
    114   // was set.
    115   SkColor GetBadgeTextColor(int tab_id) {
    116     return GetValue(&badge_text_color_, tab_id);
    117   }
    118 
    119   // Set this action's badge background color on a specific tab.
    120   void SetBadgeBackgroundColor(int tab_id, const SkColor& color) {
    121     SetValue(&badge_background_color_, tab_id, color);
    122   }
    123   // Get the badge background color for a tab, or the default if no color
    124   // was set.
    125   SkColor GetBadgeBackgroundColor(int tab_id) {
    126     return GetValue(&badge_background_color_, tab_id);
    127   }
    128 
    129   // Set this action's badge visibility on a specific tab.
    130   void SetIsVisible(int tab_id, bool value) {
    131     SetValue(&visible_, tab_id, value);
    132   }
    133   // Get the badge visibility for a tab, or the default badge visibility
    134   // if none was set.
    135   bool GetIsVisible(int tab_id) {
    136     return GetValue(&visible_, tab_id);
    137   }
    138 
    139   // Remove all tab-specific state.
    140   void ClearAllValuesForTab(int tab_id);
    141 
    142   // If the specified tab has a badge, paint it into the provided bounds.
    143   void PaintBadge(gfx::Canvas* canvas, const gfx::Rect& bounds, int tab_id);
    144 
    145  private:
    146   template <class T>
    147   struct ValueTraits {
    148     static T CreateEmpty() {
    149       return T();
    150     }
    151   };
    152 
    153   template<class T>
    154   void SetValue(std::map<int, T>* map, int tab_id, const T& val) {
    155     (*map)[tab_id] = val;
    156   }
    157 
    158   template<class T>
    159   T GetValue(std::map<int, T>* map, int tab_id) {
    160     typename std::map<int, T>::iterator iter = map->find(tab_id);
    161     if (iter != map->end()) {
    162       return iter->second;
    163     } else {
    164       iter = map->find(kDefaultTabId);
    165       return iter != map->end() ? iter->second : ValueTraits<T>::CreateEmpty();
    166     }
    167   }
    168 
    169   // The id for the extension this action belongs to (as defined in the
    170   // extension manifest).
    171   std::string extension_id_;
    172 
    173   // Each of these data items can have both a global state (stored with the key
    174   // kDefaultTabId), or tab-specific state (stored with the tab_id as the key).
    175   std::map<int, GURL> popup_url_;
    176   std::map<int, std::string> title_;
    177   std::map<int, SkBitmap> icon_;
    178   std::map<int, int> icon_index_;  // index into icon_paths_
    179   std::map<int, std::string> badge_text_;
    180   std::map<int, SkColor> badge_background_color_;
    181   std::map<int, SkColor> badge_text_color_;
    182   std::map<int, bool> visible_;
    183 
    184   std::string default_icon_path_;
    185 
    186   // The id for the ExtensionAction, for example: "RssPageAction". This is
    187   // needed for compat with an older version of the page actions API.
    188   std::string id_;
    189 
    190   // A list of paths to icons this action might show. This is needed to support
    191   // the legacy setIcon({iconIndex:...} method of the page actions API.
    192   std::vector<std::string> icon_paths_;
    193 };
    194 
    195 template<>
    196 struct ExtensionAction::ValueTraits<int> {
    197   static int CreateEmpty() {
    198     return -1;
    199   }
    200 };
    201 
    202 #endif  // CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_
    203