Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_ACTION_MANAGER_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_EXTENSION_ACTION_MANAGER_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
| 10 | |
Ben Murdoch | a02191e | 2014-04-16 11:17:03 +0100 | [diff] [blame] | 11 | #include "base/scoped_observer.h" |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 12 | #include "components/keyed_service/core/keyed_service.h" |
Ben Murdoch | a02191e | 2014-04-16 11:17:03 +0100 | [diff] [blame] | 13 | #include "extensions/browser/extension_registry_observer.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 14 | |
| 15 | class ExtensionAction; |
| 16 | class Profile; |
| 17 | |
| 18 | namespace extensions { |
| 19 | |
| 20 | class Extension; |
Ben Murdoch | a02191e | 2014-04-16 11:17:03 +0100 | [diff] [blame] | 21 | class ExtensionRegistry; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 22 | |
| 23 | // Owns the ExtensionActions associated with each extension. These actions live |
| 24 | // while an extension is loaded and are destroyed on unload. |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 25 | class ExtensionActionManager : public KeyedService, |
Ben Murdoch | a02191e | 2014-04-16 11:17:03 +0100 | [diff] [blame] | 26 | public ExtensionRegistryObserver { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 27 | public: |
| 28 | explicit ExtensionActionManager(Profile* profile); |
| 29 | virtual ~ExtensionActionManager(); |
| 30 | |
| 31 | // Returns this profile's ExtensionActionManager. One instance is |
| 32 | // shared between a profile and its incognito version. |
| 33 | static ExtensionActionManager* Get(Profile* profile); |
| 34 | |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 35 | // Retrieves the page action, or browser action for |extension|. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 36 | // If the result is not NULL, it remains valid until the extension is |
| 37 | // unloaded. |
| 38 | ExtensionAction* GetPageAction(const extensions::Extension& extension) const; |
| 39 | ExtensionAction* GetBrowserAction( |
| 40 | const extensions::Extension& extension) const; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 41 | ExtensionAction* GetSystemIndicator( |
| 42 | const extensions::Extension& extension) const; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 43 | |
| 44 | private: |
Ben Murdoch | a02191e | 2014-04-16 11:17:03 +0100 | [diff] [blame] | 45 | // Implement ExtensionRegistryObserver. |
| 46 | virtual void OnExtensionUnloaded(content::BrowserContext* browser_context, |
Ben Murdoch | 0529e5d | 2014-04-24 10:50:13 +0100 | [diff] [blame^] | 47 | const Extension* extension, |
| 48 | UnloadedExtensionInfo::Reason reason) |
| 49 | OVERRIDE; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 50 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 51 | Profile* profile_; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 52 | |
Ben Murdoch | a02191e | 2014-04-16 11:17:03 +0100 | [diff] [blame] | 53 | // Listen to extension unloaded notifications. |
Ben Murdoch | 0529e5d | 2014-04-24 10:50:13 +0100 | [diff] [blame^] | 54 | ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
| 55 | extension_registry_observer_; |
Ben Murdoch | a02191e | 2014-04-16 11:17:03 +0100 | [diff] [blame] | 56 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 57 | // Keyed by Extension ID. These maps are populated lazily when their |
| 58 | // ExtensionAction is first requested, and the entries are removed when the |
| 59 | // extension is unloaded. Not every extension has a page action or browser |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 60 | // action. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 61 | typedef std::map<std::string, linked_ptr<ExtensionAction> > ExtIdToActionMap; |
| 62 | mutable ExtIdToActionMap page_actions_; |
| 63 | mutable ExtIdToActionMap browser_actions_; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 64 | mutable ExtIdToActionMap system_indicators_; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 67 | } // namespace extensions |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 68 | |
| 69 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_ACTION_MANAGER_H_ |