OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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_ACTION2_H_ |
| 6 #define CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION2_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/basictypes.h" |
| 13 #include "base/scoped_ptr.h" |
| 14 #include "googleurl/src/gurl.h" |
| 15 #include "third_party/skia/include/core/SkBitmap.h" |
| 16 #include "third_party/skia/include/core/SkColor.h" |
| 17 |
| 18 // ExtensionAction2 encapsulates the state of a browser or page action. |
| 19 // Instances can have both global and per-tab state. If a property does not have |
| 20 // a per-tab value, the global value is used instead. |
| 21 // |
| 22 // TODO(aa): This should replace ExtensionAction and ExtensionActionState. |
| 23 class ExtensionAction2 { |
| 24 public: |
| 25 // Use this ID to indicate the default state for properties that take a tab_id |
| 26 // parameter. |
| 27 static const int kDefaultTabId; |
| 28 |
| 29 // extension id |
| 30 std::string extension_id() const { return extension_id_; } |
| 31 void set_extension_id(const std::string& extension_id) { |
| 32 extension_id_ = extension_id; |
| 33 } |
| 34 |
| 35 // popup details |
| 36 const GURL& popup_url() const { return popup_url_; } |
| 37 void set_popup_url(const GURL& url) { popup_url_ = url; } |
| 38 bool has_popup() const { return !popup_url_.is_empty(); } |
| 39 |
| 40 // title |
| 41 void SetTitle(int tab_id, const std::string& title) { |
| 42 SetValue(&title_, tab_id, title); |
| 43 } |
| 44 std::string GetTitle(int tab_id) { return GetValue(&title_, tab_id); } |
| 45 |
| 46 // Icons are a bit different because the default value can be set to either a |
| 47 // bitmap or a path. However, conceptually, there is only one default icon. |
| 48 // Setting the default icon using a path clears the bitmap and vice-versa. |
| 49 // |
| 50 // To get the default icon, first check for the bitmap. If it is null, check |
| 51 // for the path. |
| 52 |
| 53 // icon bitmap |
| 54 void SetIcon(int tab_id, const SkBitmap& bitmap) { |
| 55 SetValue(&icon_, tab_id, bitmap); |
| 56 if (tab_id == kDefaultTabId) |
| 57 default_icon_path_.clear(); |
| 58 } |
| 59 SkBitmap GetIcon(int tab_id) { return GetValue(&icon_, tab_id); } |
| 60 |
| 61 // icon path (relative to extension_id()'s root) |
| 62 // For legacy code, we also support setting the path as an index into |
| 63 // icon_paths(). |
| 64 void SetDefaultIcon(const std::string& path); |
| 65 void SetDefaultIcon(int icon_index); |
| 66 std::string GetDefaultIconPath() { |
| 67 return default_icon_path_; |
| 68 } |
| 69 |
| 70 // badge text |
| 71 void SetBadgeText(int tab_id, const std::string& text) { |
| 72 SetValue(&badge_text_, tab_id, text); |
| 73 } |
| 74 std::string GetBadgeText(int tab_id) { return GetValue(&badge_text_, tab_id);
} |
| 75 |
| 76 // badge text color |
| 77 void SetBadgeTextColor(int tab_id, const SkColor& text_color) { |
| 78 SetValue(&badge_text_color_, tab_id, text_color); |
| 79 } |
| 80 SkColor GetBadgeTextColor(int tab_id) { |
| 81 return GetValue(&badge_text_color_, tab_id); |
| 82 } |
| 83 |
| 84 // badge background color |
| 85 void SetBadgeBackgroundColor(int tab_id, const SkColor& color) { |
| 86 SetValue(&badge_background_color_, tab_id, color); |
| 87 } |
| 88 SkColor GetBadgeBackgroundColor(int tab_id) { |
| 89 return GetValue(&badge_background_color_, tab_id); |
| 90 } |
| 91 |
| 92 // Remove all tab-specific state. |
| 93 void ClearAllValuesForTab(int tab_id); |
| 94 |
| 95 //--------------------------------------------------------------------------- |
| 96 // Legacy support |
| 97 |
| 98 std::string id() const { return id_; } |
| 99 void set_id(const std::string& id) { id_ = id; } |
| 100 |
| 101 std::vector<std::string>* icon_paths() { return &icon_paths_; } |
| 102 |
| 103 private: |
| 104 template <class T> |
| 105 struct ValueTraits { |
| 106 static T CreateEmpty() { |
| 107 return T(); |
| 108 } |
| 109 }; |
| 110 |
| 111 template<class T> |
| 112 void SetValue(std::map<int, T>* map, int tab_id, T val) { |
| 113 (*map)[tab_id] = val; |
| 114 } |
| 115 |
| 116 template<class T> |
| 117 T GetValue(std::map<int, T>* map, int tab_id) { |
| 118 typename std::map<int, T>::iterator iter = map->find(tab_id); |
| 119 if (iter != map->end()) { |
| 120 return iter->second; |
| 121 } else { |
| 122 iter = map->find(kDefaultTabId); |
| 123 return iter != map->end() ? iter->second : ValueTraits<T>::CreateEmpty(); |
| 124 } |
| 125 } |
| 126 |
| 127 // The id for the extension this action belongs to (as defined in the |
| 128 // extension manifest). |
| 129 std::string extension_id_; |
| 130 |
| 131 // Each of these data items can have both a global state (stored with the key |
| 132 // kDefaultTabId), or tab-specific state (stored with the tab_id as the key). |
| 133 std::map<int, std::string> title_; |
| 134 std::map<int, SkBitmap> icon_; |
| 135 std::map<int, std::string> badge_text_; |
| 136 std::map<int, SkColor> badge_background_color_; |
| 137 std::map<int, SkColor> badge_text_color_; |
| 138 |
| 139 std::string default_icon_path_; |
| 140 |
| 141 // If the action has a popup, it has a URL and a height. |
| 142 GURL popup_url_; |
| 143 |
| 144 //--------------------------------------------------------------------------- |
| 145 // Legacy support |
| 146 |
| 147 // The id for the ExtensionAction2, for example: "RssPageAction". This is |
| 148 // needed for compat with an older version of the page actions API. |
| 149 std::string id_; |
| 150 |
| 151 // A list of paths to icons this action might show. This is needed to support |
| 152 // the setIcon({iconIndex:...} method. |
| 153 std::vector<std::string> icon_paths_; |
| 154 }; |
| 155 |
| 156 template <> |
| 157 struct ExtensionAction2::ValueTraits<SkColor> { |
| 158 static SkColor CreateEmpty() { |
| 159 return 0x00000000; |
| 160 } |
| 161 }; |
| 162 |
| 163 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION2_H_ |
OLD | NEW |