OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_ACTION_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_ACTION_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_ACTION_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_ACTION_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 // Get the badge background color for a tab, or the default if no color | 213 // Get the badge background color for a tab, or the default if no color |
214 // was set. | 214 // was set. |
215 SkColor GetBadgeBackgroundColor(int tab_id) const { | 215 SkColor GetBadgeBackgroundColor(int tab_id) const { |
216 return GetValue(&badge_background_color_, tab_id); | 216 return GetValue(&badge_background_color_, tab_id); |
217 } | 217 } |
218 | 218 |
219 // Set this action's badge visibility on a specific tab. This takes | 219 // Set this action's badge visibility on a specific tab. This takes |
220 // care of any appropriate transition animations. Returns true if | 220 // care of any appropriate transition animations. Returns true if |
221 // the appearance has changed. | 221 // the appearance has changed. |
222 bool SetAppearance(int tab_id, Appearance value); | 222 bool SetAppearance(int tab_id, Appearance value); |
| 223 // The declarative appearance overrides a default appearance but is overrided |
| 224 // by an appearance set directly on the tab. |
| 225 void SetDeclarativeAppearance(int tab_id, Appearance value); |
| 226 void ClearDeclarativeAppearance(int tab_id); |
| 227 |
223 // Get the badge visibility for a tab, or the default badge visibility | 228 // Get the badge visibility for a tab, or the default badge visibility |
224 // if none was set. | 229 // if none was set. |
225 bool GetIsVisible(int tab_id) const { | 230 bool GetIsVisible(int tab_id) const { |
226 return GetValue(&appearance_, tab_id) != INVISIBLE; | 231 return GetDeclarativeValue(&appearance_, &declarative_appearance_, tab_id) |
| 232 != INVISIBLE; |
227 } | 233 } |
228 | 234 |
229 // True if the tab's action wants the user's attention. | 235 // True if the tab's action wants the user's attention. |
230 bool WantsAttention(int tab_id) const { | 236 bool WantsAttention(int tab_id) const { |
231 return GetValue(&appearance_, tab_id) == WANTS_ATTENTION; | 237 return GetDeclarativeValue(&appearance_, &declarative_appearance_, tab_id) |
| 238 == WANTS_ATTENTION; |
232 } | 239 } |
233 | 240 |
234 // Remove all tab-specific state. | 241 // Remove all tab-specific state. |
235 void ClearAllValuesForTab(int tab_id); | 242 void ClearAllValuesForTab(int tab_id); |
236 | 243 |
237 // If the specified tab has a badge, paint it into the provided bounds. | 244 // If the specified tab has a badge, paint it into the provided bounds. |
238 void PaintBadge(gfx::Canvas* canvas, const gfx::Rect& bounds, int tab_id); | 245 void PaintBadge(gfx::Canvas* canvas, const gfx::Rect& bounds, int tab_id); |
239 | 246 |
240 // Returns icon image with badge for specified tab. | 247 // Returns icon image with badge for specified tab. |
241 gfx::ImageSkia GetIconWithBadge(const gfx::ImageSkia& icon, | 248 gfx::ImageSkia GetIconWithBadge(const gfx::ImageSkia& icon, |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 T GetValue(const std::map<int, T>* map, int tab_id) const { | 283 T GetValue(const std::map<int, T>* map, int tab_id) const { |
277 typename std::map<int, T>::const_iterator iter = map->find(tab_id); | 284 typename std::map<int, T>::const_iterator iter = map->find(tab_id); |
278 if (iter != map->end()) { | 285 if (iter != map->end()) { |
279 return iter->second; | 286 return iter->second; |
280 } else { | 287 } else { |
281 iter = map->find(kDefaultTabId); | 288 iter = map->find(kDefaultTabId); |
282 return iter != map->end() ? iter->second : ValueTraits<T>::CreateEmpty(); | 289 return iter != map->end() ? iter->second : ValueTraits<T>::CreateEmpty(); |
283 } | 290 } |
284 } | 291 } |
285 | 292 |
| 293 // Returns the first of map[tab_id], declarative_map[tab_id], |
| 294 // map[kDefaultTabId], or ValueTraits<T>::CreateEmpty() that's defined. Don't |
| 295 // return this result to an extension's background page because the |
| 296 // declarative state can leak information about hosts the extension doesn't |
| 297 // have permission to access. |
| 298 template<class T> |
| 299 T GetDeclarativeValue(const std::map<int, T>* map, |
| 300 const std::map<int, T>* declarative_map, |
| 301 int tab_id) const { |
| 302 typename std::map<int, T>::const_iterator iter = map->find(tab_id); |
| 303 if (iter != map->end()) |
| 304 return iter->second; |
| 305 |
| 306 iter = declarative_map->find(tab_id); |
| 307 if (iter != declarative_map->end()) |
| 308 return iter->second; |
| 309 |
| 310 iter = map->find(kDefaultTabId); |
| 311 if (iter != map->end()) |
| 312 return iter->second; |
| 313 |
| 314 return ValueTraits<T>::CreateEmpty(); |
| 315 } |
| 316 |
286 // The id for the extension this action belongs to (as defined in the | 317 // The id for the extension this action belongs to (as defined in the |
287 // extension manifest). | 318 // extension manifest). |
288 const std::string extension_id_; | 319 const std::string extension_id_; |
289 | 320 |
290 const extensions::Extension::ActionInfo::Type action_type_; | 321 const extensions::Extension::ActionInfo::Type action_type_; |
291 | 322 |
292 // Each of these data items can have both a global state (stored with the key | 323 // Each of these data items can have both a global state (stored with the key |
293 // kDefaultTabId), or tab-specific state (stored with the tab_id as the key). | 324 // kDefaultTabId), or tab-specific state (stored with the tab_id as the key). |
294 std::map<int, GURL> popup_url_; | 325 std::map<int, GURL> popup_url_; |
295 std::map<int, std::string> title_; | 326 std::map<int, std::string> title_; |
296 std::map<int, gfx::ImageSkia> icon_; | 327 std::map<int, gfx::ImageSkia> icon_; |
297 std::map<int, std::string> badge_text_; | 328 std::map<int, std::string> badge_text_; |
298 std::map<int, SkColor> badge_background_color_; | 329 std::map<int, SkColor> badge_background_color_; |
299 std::map<int, SkColor> badge_text_color_; | 330 std::map<int, SkColor> badge_text_color_; |
300 std::map<int, Appearance> appearance_; | 331 std::map<int, Appearance> appearance_; |
| 332 // Declarative state exists for two reasons: First, we need to hide it from |
| 333 // the extension's background/event page to avoid leaking data from hosts the |
| 334 // extension doesn't have permission to access. Second, the action's state |
| 335 // gets both reset and given its declarative values in response to a |
| 336 // WebContentsObserver::DidNavigateMainFrame event, and there's no way to set |
| 337 // those up to be called in the right order. |
| 338 std::map<int, Appearance> declarative_appearance_; |
301 | 339 |
302 // IconAnimations are destroyed by a delayed task on the UI message loop so | 340 // IconAnimations are destroyed by a delayed task on the UI message loop so |
303 // that even if the Extension and ExtensionAction are destroyed on a non-UI | 341 // that even if the Extension and ExtensionAction are destroyed on a non-UI |
304 // thread, the animation will still only be touched from the UI thread. This | 342 // thread, the animation will still only be touched from the UI thread. This |
305 // causes the WeakPtr in this map to become NULL. GetIconAnimation() removes | 343 // causes the WeakPtr in this map to become NULL. GetIconAnimation() removes |
306 // NULLs to prevent the map from growing without bound. | 344 // NULLs to prevent the map from growing without bound. |
307 mutable std::map<int, base::WeakPtr<IconAnimation> > icon_animation_; | 345 mutable std::map<int, base::WeakPtr<IconAnimation> > icon_animation_; |
308 | 346 |
309 // ExtensionIconSet containing paths to bitmaps from which default icon's | 347 // ExtensionIconSet containing paths to bitmaps from which default icon's |
310 // image representations will be selected. | 348 // image representations will be selected. |
(...skipping 11 matching lines...) Expand all Loading... |
322 }; | 360 }; |
323 | 361 |
324 template<> | 362 template<> |
325 struct ExtensionAction::ValueTraits<int> { | 363 struct ExtensionAction::ValueTraits<int> { |
326 static int CreateEmpty() { | 364 static int CreateEmpty() { |
327 return -1; | 365 return -1; |
328 } | 366 } |
329 }; | 367 }; |
330 | 368 |
331 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_ACTION_H_ | 369 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_ACTION_H_ |
OLD | NEW |