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