[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [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_SHELL_WINDOW_REGISTRY_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_SHELL_WINDOW_REGISTRY_H_ |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 7 | |
[email protected] | 76af173df | 2013-05-22 07:49:37 | [diff] [blame^] | 8 | #include <list> |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 9 | |
[email protected] | a23f62e | 2013-04-26 13:13:02 | [diff] [blame] | 10 | #include "base/callback.h" |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
| 12 | #include "base/memory/singleton.h" |
[email protected] | 25977110 | 2012-05-31 16:52:20 | [diff] [blame] | 13 | #include "base/observer_list.h" |
[email protected] | 0dd6f203 | 2013-05-20 23:33:40 | [diff] [blame] | 14 | #include "components/browser_context_keyed_service/browser_context_keyed_service.h" |
| 15 | #include "components/browser_context_keyed_service/browser_context_keyed_service_factory.h" |
[email protected] | 763cb907 | 2012-06-25 21:39:51 | [diff] [blame] | 16 | #include "ui/gfx/native_widget_types.h" |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 17 | |
| 18 | class Profile; |
| 19 | class ShellWindow; |
| 20 | |
[email protected] | 7bc8299d | 2012-06-13 09:18:29 | [diff] [blame] | 21 | namespace content { |
[email protected] | a23f62e | 2013-04-26 13:13:02 | [diff] [blame] | 22 | class DevToolsAgentHost; |
[email protected] | 7bc8299d | 2012-06-13 09:18:29 | [diff] [blame] | 23 | class RenderViewHost; |
| 24 | } |
| 25 | |
[email protected] | d9ede58 | 2012-08-14 19:21:38 | [diff] [blame] | 26 | namespace extensions { |
| 27 | |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 28 | // The ShellWindowRegistry tracks the ShellWindows for all platform apps for a |
| 29 | // particular profile. |
| 30 | // This class is planned to evolve into tracking all PlatformApps for a |
| 31 | // particular profile, with a PlatformApp encapsulating all views (background |
| 32 | // page, shell windows, tray view, panels etc.) and other app level behaviour |
| 33 | // (e.g. notifications the app is interested in, lifetime of the background |
| 34 | // page). |
[email protected] | a23f62e | 2013-04-26 13:13:02 | [diff] [blame] | 35 | class ShellWindowRegistry : public ProfileKeyedService { |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 36 | public: |
[email protected] | 25977110 | 2012-05-31 16:52:20 | [diff] [blame] | 37 | class Observer { |
| 38 | public: |
| 39 | // Called just after a shell window was added. |
| 40 | virtual void OnShellWindowAdded(ShellWindow* shell_window) = 0; |
[email protected] | 424f705 | 2012-12-05 05:25:26 | [diff] [blame] | 41 | // Called when the window icon changes. |
| 42 | virtual void OnShellWindowIconChanged(ShellWindow* shell_window) = 0; |
[email protected] | 25977110 | 2012-05-31 16:52:20 | [diff] [blame] | 43 | // Called just after a shell window was removed. |
| 44 | virtual void OnShellWindowRemoved(ShellWindow* shell_window) = 0; |
| 45 | |
| 46 | protected: |
| 47 | virtual ~Observer() {} |
| 48 | }; |
| 49 | |
[email protected] | 76af173df | 2013-05-22 07:49:37 | [diff] [blame^] | 50 | typedef std::list<ShellWindow*> ShellWindowList; |
| 51 | typedef ShellWindowList::const_iterator const_iterator; |
[email protected] | e85cc64 | 2012-10-24 06:14:23 | [diff] [blame] | 52 | typedef std::set<std::string> InspectedWindowSet; |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 53 | |
[email protected] | e85cc64 | 2012-10-24 06:14:23 | [diff] [blame] | 54 | explicit ShellWindowRegistry(Profile* profile); |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 55 | virtual ~ShellWindowRegistry(); |
| 56 | |
| 57 | // Returns the instance for the given profile, or NULL if none. This is |
[email protected] | a6db612 | 2012-09-03 06:00:23 | [diff] [blame] | 58 | // a convenience wrapper around ShellWindowRegistry::Factory::GetForProfile. |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 59 | static ShellWindowRegistry* Get(Profile* profile); |
| 60 | |
| 61 | void AddShellWindow(ShellWindow* shell_window); |
[email protected] | 424f705 | 2012-12-05 05:25:26 | [diff] [blame] | 62 | void ShellWindowIconChanged(ShellWindow* shell_window); |
[email protected] | 76af173df | 2013-05-22 07:49:37 | [diff] [blame^] | 63 | // Called by |shell_window| when it is activated. |
| 64 | void ShellWindowActivated(ShellWindow* shell_window); |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 65 | void RemoveShellWindow(ShellWindow* shell_window); |
| 66 | |
[email protected] | 25977110 | 2012-05-31 16:52:20 | [diff] [blame] | 67 | void AddObserver(Observer* observer); |
| 68 | void RemoveObserver(Observer* observer); |
| 69 | |
| 70 | // Returns a set of windows owned by the application identified by app_id. |
[email protected] | 76af173df | 2013-05-22 07:49:37 | [diff] [blame^] | 71 | ShellWindowList GetShellWindowsForApp(const std::string& app_id) const; |
| 72 | const ShellWindowList& shell_windows() const { return shell_windows_; } |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 73 | |
[email protected] | 763cb907 | 2012-06-25 21:39:51 | [diff] [blame] | 74 | // Helper functions to find shell windows with particular attributes. |
[email protected] | 7bc8299d | 2012-06-13 09:18:29 | [diff] [blame] | 75 | ShellWindow* GetShellWindowForRenderViewHost( |
| 76 | content::RenderViewHost* render_view_host) const; |
[email protected] | 763cb907 | 2012-06-25 21:39:51 | [diff] [blame] | 77 | ShellWindow* GetShellWindowForNativeWindow(gfx::NativeWindow window) const; |
[email protected] | 6137ac2 | 2012-08-21 23:02:52 | [diff] [blame] | 78 | // Returns an app window for the given app, or NULL if no shell windows are |
| 79 | // open. If there is a window for the given app that is active, that one will |
| 80 | // be returned, otherwise an arbitrary window will be returned. |
| 81 | ShellWindow* GetCurrentShellWindowForApp(const std::string& app_id) const; |
[email protected] | 2ce9fec | 2012-12-06 22:48:13 | [diff] [blame] | 82 | // Returns an app window for the given app and window key, or NULL if no shell |
| 83 | // window with the key are open. If there is a window for the given app and |
| 84 | // key that is active, that one will be returned, otherwise an arbitrary |
| 85 | // window will be returned. |
| 86 | ShellWindow* GetShellWindowForAppAndKey(const std::string& app_id, |
| 87 | const std::string& window_key) const; |
[email protected] | 7bc8299d | 2012-06-13 09:18:29 | [diff] [blame] | 88 | |
[email protected] | e85cc64 | 2012-10-24 06:14:23 | [diff] [blame] | 89 | // Returns whether a ShellWindow's ID was last known to have a DevToolsAgent |
| 90 | // attached to it, which should be restored during a reload of a corresponding |
| 91 | // newly created |render_view_host|. |
| 92 | bool HadDevToolsAttached(content::RenderViewHost* render_view_host) const; |
| 93 | |
[email protected] | b9ab6f84 | 2013-02-05 07:31:20 | [diff] [blame] | 94 | // Returns the shell window for |window|, looking in all profiles. |
| 95 | static ShellWindow* GetShellWindowForNativeWindowAnyProfile( |
| 96 | gfx::NativeWindow window); |
| 97 | |
[email protected] | 24ced7dc0 | 2013-04-04 08:32:39 | [diff] [blame] | 98 | // Returns true if the number of shell windows registered across all profiles |
| 99 | // is non-zero. |window_type_mask| is a bitwise OR filter of |
| 100 | // ShellWindow::WindowType, or 0 for any window type. |
| 101 | static bool IsShellWindowRegisteredInAnyProfile(int window_type_mask); |
| 102 | |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 103 | class Factory : public ProfileKeyedServiceFactory { |
| 104 | public: |
[email protected] | 24ced7dc0 | 2013-04-04 08:32:39 | [diff] [blame] | 105 | static ShellWindowRegistry* GetForProfile(Profile* profile, bool create); |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 106 | |
| 107 | static Factory* GetInstance(); |
| 108 | private: |
| 109 | friend struct DefaultSingletonTraits<Factory>; |
| 110 | |
| 111 | Factory(); |
| 112 | virtual ~Factory(); |
| 113 | |
| 114 | // ProfileKeyedServiceFactory |
| 115 | virtual ProfileKeyedService* BuildServiceInstanceFor( |
[email protected] | c7fa436 | 2013-04-26 18:09:02 | [diff] [blame] | 116 | content::BrowserContext* profile) const OVERRIDE; |
[email protected] | bb05cae1 | 2012-09-06 00:37:52 | [diff] [blame] | 117 | virtual bool ServiceIsCreatedWithProfile() const OVERRIDE; |
| 118 | virtual bool ServiceIsNULLWhileTesting() const OVERRIDE; |
[email protected] | d3aa6145 | 2013-05-03 23:29:04 | [diff] [blame] | 119 | virtual content::BrowserContext* GetBrowserContextToUse( |
| 120 | content::BrowserContext* context) const OVERRIDE; |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 121 | }; |
| 122 | |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 123 | protected: |
| 124 | void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached); |
| 125 | |
| 126 | private: |
[email protected] | 76af173df | 2013-05-22 07:49:37 | [diff] [blame^] | 127 | // Ensures the specified |shell_window| is included in |shell_windows_|. |
| 128 | // Otherwise adds |shell_window| to the back of |shell_windows_|. |
| 129 | void AddShellWindowToList(ShellWindow* shell_window); |
| 130 | |
| 131 | // Bring |shell_window| to the front of |shell_windows_|. If it is not in the |
| 132 | // list, add it first. |
| 133 | void BringToFront(ShellWindow* shell_window); |
| 134 | |
[email protected] | a23f62e | 2013-04-26 13:13:02 | [diff] [blame] | 135 | Profile* profile_; |
[email protected] | 76af173df | 2013-05-22 07:49:37 | [diff] [blame^] | 136 | ShellWindowList shell_windows_; |
[email protected] | e85cc64 | 2012-10-24 06:14:23 | [diff] [blame] | 137 | InspectedWindowSet inspected_windows_; |
[email protected] | 25977110 | 2012-05-31 16:52:20 | [diff] [blame] | 138 | ObserverList<Observer> observers_; |
[email protected] | a23f62e | 2013-04-26 13:13:02 | [diff] [blame] | 139 | base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_; |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 140 | }; |
| 141 | |
[email protected] | d9ede58 | 2012-08-14 19:21:38 | [diff] [blame] | 142 | } // namespace extensions |
| 143 | |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 144 | #endif // CHROME_BROWSER_EXTENSIONS_SHELL_WINDOW_REGISTRY_H_ |