blob: b78e8a2bd8600e9674b291cd08ac3f9fa19ad13e [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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 Murdocha02191e2014-04-16 11:17:03 +010011#include "base/scoped_observer.h"
Torne (Richard Coles)a1401312014-03-18 10:20:56 +000012#include "components/keyed_service/core/keyed_service.h"
Ben Murdocha02191e2014-04-16 11:17:03 +010013#include "extensions/browser/extension_registry_observer.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000014
15class ExtensionAction;
16class Profile;
17
18namespace extensions {
19
20class Extension;
Ben Murdocha02191e2014-04-16 11:17:03 +010021class ExtensionRegistry;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000022
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)a1401312014-03-18 10:20:56 +000025class ExtensionActionManager : public KeyedService,
Ben Murdocha02191e2014-04-16 11:17:03 +010026 public ExtensionRegistryObserver {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000027 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)5d1f7b12014-02-21 12:16:55 +000035 // Retrieves the page action, or browser action for |extension|.
Torne (Richard Coles)58218062012-11-14 11:43:16 +000036 // 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)2a99a7e2013-03-28 15:31:22 +000041 ExtensionAction* GetSystemIndicator(
42 const extensions::Extension& extension) const;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000043
44 private:
Ben Murdocha02191e2014-04-16 11:17:03 +010045 // Implement ExtensionRegistryObserver.
46 virtual void OnExtensionUnloaded(content::BrowserContext* browser_context,
Ben Murdoch0529e5d2014-04-24 10:50:13 +010047 const Extension* extension,
48 UnloadedExtensionInfo::Reason reason)
49 OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000050
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000051 Profile* profile_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000052
Ben Murdocha02191e2014-04-16 11:17:03 +010053 // Listen to extension unloaded notifications.
Ben Murdoch0529e5d2014-04-24 10:50:13 +010054 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
55 extension_registry_observer_;
Ben Murdocha02191e2014-04-16 11:17:03 +010056
Torne (Richard Coles)58218062012-11-14 11:43:16 +000057 // 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)5d1f7b12014-02-21 12:16:55 +000060 // action.
Torne (Richard Coles)58218062012-11-14 11:43:16 +000061 typedef std::map<std::string, linked_ptr<ExtensionAction> > ExtIdToActionMap;
62 mutable ExtIdToActionMap page_actions_;
63 mutable ExtIdToActionMap browser_actions_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000064 mutable ExtIdToActionMap system_indicators_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000065};
66
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000067} // namespace extensions
Torne (Richard Coles)58218062012-11-14 11:43:16 +000068
69#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_ACTION_MANAGER_H_