Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Side by Side Diff: chrome/browser/extensions/extension_action.h

Issue 11547033: Implement declarativeContent API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Sync Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 overridden
224 // by an appearance set directly on the tab.
225 void DeclarativeShow(int tab_id);
226 void UndoDeclarativeShow(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 GetAppearance(tab_id) != INVISIBLE;
227 } 232 }
228 233
229 // True if the tab's action wants the user's attention. 234 // True if the tab's action wants the user's attention.
230 bool WantsAttention(int tab_id) const { 235 bool WantsAttention(int tab_id) const {
231 return GetValue(&appearance_, tab_id) == WANTS_ATTENTION; 236 return GetAppearance(tab_id) == WANTS_ATTENTION;
232 } 237 }
233 238
234 // Remove all tab-specific state. 239 // Remove all tab-specific state.
235 void ClearAllValuesForTab(int tab_id); 240 void ClearAllValuesForTab(int tab_id);
236 241
237 // If the specified tab has a badge, paint it into the provided bounds. 242 // 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); 243 void PaintBadge(gfx::Canvas* canvas, const gfx::Rect& bounds, int tab_id);
239 244
240 // Returns icon image with badge for specified tab. 245 // Returns icon image with badge for specified tab.
241 gfx::ImageSkia GetIconWithBadge(const gfx::ImageSkia& icon, 246 gfx::ImageSkia GetIconWithBadge(const gfx::ImageSkia& icon,
(...skipping 23 matching lines...) Expand all
265 static T CreateEmpty() { 270 static T CreateEmpty() {
266 return T(); 271 return T();
267 } 272 }
268 }; 273 };
269 274
270 template<class T> 275 template<class T>
271 void SetValue(std::map<int, T>* map, int tab_id, const T& val) { 276 void SetValue(std::map<int, T>* map, int tab_id, const T& val) {
272 (*map)[tab_id] = val; 277 (*map)[tab_id] = val;
273 } 278 }
274 279
280 template<class Map>
281 static const typename Map::mapped_type* FindOrNull(
282 const Map* map,
283 const typename Map::key_type& key) {
284 typename Map::const_iterator iter = map->find(key);
285 if (iter == map->end())
286 return NULL;
287 return &iter->second;
288 }
289
275 template<class T> 290 template<class T>
276 T GetValue(const std::map<int, T>* map, int tab_id) const { 291 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); 292 if (const T* tab_value = FindOrNull(map, tab_id)) {
278 if (iter != map->end()) { 293 return *tab_value;
279 return iter->second; 294 } else if (const T* default_value = FindOrNull(map, kDefaultTabId)) {
295 return *default_value;
280 } else { 296 } else {
281 iter = map->find(kDefaultTabId); 297 return ValueTraits<T>::CreateEmpty();
282 return iter != map->end() ? iter->second : ValueTraits<T>::CreateEmpty();
283 } 298 }
284 } 299 }
285 300
301 // Gets the appearance of |tab_id|. Returns the first of: a specific
302 // appearance set on the tab; a declarative appearance set on the tab; the
303 // default appearance set for all tabs; or INVISIBLE. Don't return this
304 // result to an extension's background page because the declarative state can
305 // leak information about hosts the extension doesn't have permission to
306 // access.
307 Appearance GetAppearance(int tab_id) const {
308 if (const Appearance* tab_appearance = FindOrNull(&appearance_, tab_id))
309 return *tab_appearance;
310
311 if (ContainsKey(declarative_show_count_, tab_id))
312 return ACTIVE;
313
314 if (const Appearance* default_appearance =
315 FindOrNull(&appearance_, kDefaultTabId))
316 return *default_appearance;
317
318 return INVISIBLE;
319 }
320
286 // The id for the extension this action belongs to (as defined in the 321 // The id for the extension this action belongs to (as defined in the
287 // extension manifest). 322 // extension manifest).
288 const std::string extension_id_; 323 const std::string extension_id_;
289 324
290 const extensions::ActionInfo::Type action_type_; 325 const extensions::ActionInfo::Type action_type_;
291 326
292 // Each of these data items can have both a global state (stored with the key 327 // 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). 328 // kDefaultTabId), or tab-specific state (stored with the tab_id as the key).
294 std::map<int, GURL> popup_url_; 329 std::map<int, GURL> popup_url_;
295 std::map<int, std::string> title_; 330 std::map<int, std::string> title_;
296 std::map<int, gfx::ImageSkia> icon_; 331 std::map<int, gfx::ImageSkia> icon_;
297 std::map<int, std::string> badge_text_; 332 std::map<int, std::string> badge_text_;
298 std::map<int, SkColor> badge_background_color_; 333 std::map<int, SkColor> badge_background_color_;
299 std::map<int, SkColor> badge_text_color_; 334 std::map<int, SkColor> badge_text_color_;
300 std::map<int, Appearance> appearance_; 335 std::map<int, Appearance> appearance_;
301 336
337 // Declarative state exists for two reasons: First, we need to hide it from
338 // the extension's background/event page to avoid leaking data from hosts the
339 // extension doesn't have permission to access. Second, the action's state
340 // gets both reset and given its declarative values in response to a
341 // WebContentsObserver::DidNavigateMainFrame event, and there's no way to set
342 // those up to be called in the right order.
343
344 // Maps tab_id to the number of active (applied-but-not-reverted)
345 // declarativeContent.ShowPageAction actions.
346 std::map<int, int> declarative_show_count_;
347
302 // IconAnimations are destroyed by a delayed task on the UI message loop so 348 // 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 349 // 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 350 // 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 351 // causes the WeakPtr in this map to become NULL. GetIconAnimation() removes
306 // NULLs to prevent the map from growing without bound. 352 // NULLs to prevent the map from growing without bound.
307 mutable std::map<int, base::WeakPtr<IconAnimation> > icon_animation_; 353 mutable std::map<int, base::WeakPtr<IconAnimation> > icon_animation_;
308 354
309 // ExtensionIconSet containing paths to bitmaps from which default icon's 355 // ExtensionIconSet containing paths to bitmaps from which default icon's
310 // image representations will be selected. 356 // image representations will be selected.
311 scoped_ptr<const ExtensionIconSet> default_icon_; 357 scoped_ptr<const ExtensionIconSet> default_icon_;
(...skipping 10 matching lines...) Expand all
322 }; 368 };
323 369
324 template<> 370 template<>
325 struct ExtensionAction::ValueTraits<int> { 371 struct ExtensionAction::ValueTraits<int> {
326 static int CreateEmpty() { 372 static int CreateEmpty() {
327 return -1; 373 return -1;
328 } 374 }
329 }; 375 };
330 376
331 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_ACTION_H_ 377 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_ACTION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698