[email protected] | f6d9b28 | 2013-08-09 11:03:20 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | f6d9b28 | 2013-08-09 11:03:20 | [diff] [blame] | 5 | #ifndef APPS_SHELL_WINDOW_REGISTRY_H_ |
| 6 | #define APPS_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 | |
[email protected] | 7bc8299d | 2012-06-13 09:18:29 | [diff] [blame] | 18 | namespace content { |
[email protected] | 01996a59 | 2013-09-13 06:55:04 | [diff] [blame] | 19 | class BrowserContext; |
[email protected] | a23f62e | 2013-04-26 13:13:02 | [diff] [blame] | 20 | class DevToolsAgentHost; |
[email protected] | 7bc8299d | 2012-06-13 09:18:29 | [diff] [blame] | 21 | class RenderViewHost; |
| 22 | } |
| 23 | |
[email protected] | f6d9b28 | 2013-08-09 11:03:20 | [diff] [blame] | 24 | namespace apps { |
| 25 | |
| 26 | class ShellWindow; |
[email protected] | d9ede58 | 2012-08-14 19:21:38 | [diff] [blame] | 27 | |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 28 | // The ShellWindowRegistry tracks the ShellWindows for all platform apps for a |
[email protected] | 01996a59 | 2013-09-13 06:55:04 | [diff] [blame] | 29 | // particular browser context. |
[email protected] | f1484c5 | 2013-05-22 23:25:44 | [diff] [blame] | 30 | class ShellWindowRegistry : public BrowserContextKeyedService { |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 31 | public: |
[email protected] | 25977110 | 2012-05-31 16:52:20 | [diff] [blame] | 32 | class Observer { |
| 33 | public: |
| 34 | // Called just after a shell window was added. |
[email protected] | d53417e | 2013-07-01 07:07:39 | [diff] [blame] | 35 | virtual void OnShellWindowAdded(apps::ShellWindow* shell_window) = 0; |
[email protected] | 424f705 | 2012-12-05 05:25:26 | [diff] [blame] | 36 | // Called when the window icon changes. |
[email protected] | d53417e | 2013-07-01 07:07:39 | [diff] [blame] | 37 | virtual void OnShellWindowIconChanged(apps::ShellWindow* shell_window) = 0; |
[email protected] | 25977110 | 2012-05-31 16:52:20 | [diff] [blame] | 38 | // Called just after a shell window was removed. |
[email protected] | d53417e | 2013-07-01 07:07:39 | [diff] [blame] | 39 | virtual void OnShellWindowRemoved(apps::ShellWindow* shell_window) = 0; |
[email protected] | 25977110 | 2012-05-31 16:52:20 | [diff] [blame] | 40 | |
| 41 | protected: |
| 42 | virtual ~Observer() {} |
| 43 | }; |
| 44 | |
[email protected] | d53417e | 2013-07-01 07:07:39 | [diff] [blame] | 45 | typedef std::list<apps::ShellWindow*> ShellWindowList; |
[email protected] | 76af173df | 2013-05-22 07:49:37 | [diff] [blame] | 46 | typedef ShellWindowList::const_iterator const_iterator; |
[email protected] | e85cc64 | 2012-10-24 06:14:23 | [diff] [blame] | 47 | typedef std::set<std::string> InspectedWindowSet; |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 48 | |
[email protected] | 01996a59 | 2013-09-13 06:55:04 | [diff] [blame] | 49 | explicit ShellWindowRegistry(content::BrowserContext* context); |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 50 | virtual ~ShellWindowRegistry(); |
| 51 | |
[email protected] | 01996a59 | 2013-09-13 06:55:04 | [diff] [blame] | 52 | // Returns the instance for the given browser context, or NULL if none. This |
| 53 | // is a convenience wrapper around |
| 54 | // ShellWindowRegistry::Factory::GetForBrowserContext(). |
| 55 | static ShellWindowRegistry* Get(content::BrowserContext* context); |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 56 | |
[email protected] | d53417e | 2013-07-01 07:07:39 | [diff] [blame] | 57 | void AddShellWindow(apps::ShellWindow* shell_window); |
| 58 | void ShellWindowIconChanged(apps::ShellWindow* shell_window); |
[email protected] | 76af173df | 2013-05-22 07:49:37 | [diff] [blame] | 59 | // Called by |shell_window| when it is activated. |
[email protected] | d53417e | 2013-07-01 07:07:39 | [diff] [blame] | 60 | void ShellWindowActivated(apps::ShellWindow* shell_window); |
| 61 | void RemoveShellWindow(apps::ShellWindow* shell_window); |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 62 | |
[email protected] | 25977110 | 2012-05-31 16:52:20 | [diff] [blame] | 63 | void AddObserver(Observer* observer); |
| 64 | void RemoveObserver(Observer* observer); |
| 65 | |
| 66 | // Returns a set of windows owned by the application identified by app_id. |
[email protected] | 76af173df | 2013-05-22 07:49:37 | [diff] [blame] | 67 | ShellWindowList GetShellWindowsForApp(const std::string& app_id) const; |
| 68 | const ShellWindowList& shell_windows() const { return shell_windows_; } |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 69 | |
[email protected] | 90a4b9d | 2013-07-31 12:14:40 | [diff] [blame] | 70 | // Close all shell windows associated with an app. |
| 71 | void CloseAllShellWindowsForApp(const std::string& app_id); |
| 72 | |
[email protected] | 763cb907 | 2012-06-25 21:39:51 | [diff] [blame] | 73 | // Helper functions to find shell windows with particular attributes. |
[email protected] | d53417e | 2013-07-01 07:07:39 | [diff] [blame] | 74 | apps::ShellWindow* GetShellWindowForRenderViewHost( |
[email protected] | 7bc8299d | 2012-06-13 09:18:29 | [diff] [blame] | 75 | content::RenderViewHost* render_view_host) const; |
[email protected] | d53417e | 2013-07-01 07:07:39 | [diff] [blame] | 76 | apps::ShellWindow* GetShellWindowForNativeWindow( |
| 77 | 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. |
[email protected] | d53417e | 2013-07-01 07:07:39 | [diff] [blame] | 81 | apps::ShellWindow* GetCurrentShellWindowForApp( |
| 82 | const std::string& app_id) const; |
[email protected] | 2ce9fec | 2012-12-06 22:48:13 | [diff] [blame] | 83 | // Returns an app window for the given app and window key, or NULL if no shell |
| 84 | // window with the key are open. If there is a window for the given app and |
| 85 | // key that is active, that one will be returned, otherwise an arbitrary |
| 86 | // window will be returned. |
[email protected] | d53417e | 2013-07-01 07:07:39 | [diff] [blame] | 87 | apps::ShellWindow* GetShellWindowForAppAndKey( |
| 88 | const std::string& app_id, |
| 89 | const std::string& window_key) const; |
[email protected] | 7bc8299d | 2012-06-13 09:18:29 | [diff] [blame] | 90 | |
[email protected] | e85cc64 | 2012-10-24 06:14:23 | [diff] [blame] | 91 | // Returns whether a ShellWindow's ID was last known to have a DevToolsAgent |
| 92 | // attached to it, which should be restored during a reload of a corresponding |
| 93 | // newly created |render_view_host|. |
| 94 | bool HadDevToolsAttached(content::RenderViewHost* render_view_host) const; |
| 95 | |
[email protected] | 01996a59 | 2013-09-13 06:55:04 | [diff] [blame] | 96 | // Returns the shell window for |window|, looking in all browser contexts. |
[email protected] | d53417e | 2013-07-01 07:07:39 | [diff] [blame] | 97 | static apps::ShellWindow* GetShellWindowForNativeWindowAnyProfile( |
[email protected] | b9ab6f84 | 2013-02-05 07:31:20 | [diff] [blame] | 98 | gfx::NativeWindow window); |
| 99 | |
[email protected] | 01996a59 | 2013-09-13 06:55:04 | [diff] [blame] | 100 | // Returns true if the number of shell windows registered across all browser |
| 101 | // contexts is non-zero. |window_type_mask| is a bitwise OR filter of |
[email protected] | 24ced7dc0 | 2013-04-04 08:32:39 | [diff] [blame] | 102 | // ShellWindow::WindowType, or 0 for any window type. |
| 103 | static bool IsShellWindowRegisteredInAnyProfile(int window_type_mask); |
| 104 | |
[email protected] | f1484c5 | 2013-05-22 23:25:44 | [diff] [blame] | 105 | class Factory : public BrowserContextKeyedServiceFactory { |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 106 | public: |
[email protected] | 01996a59 | 2013-09-13 06:55:04 | [diff] [blame] | 107 | static ShellWindowRegistry* GetForBrowserContext( |
| 108 | content::BrowserContext* context, bool create); |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 109 | |
| 110 | static Factory* GetInstance(); |
| 111 | private: |
| 112 | friend struct DefaultSingletonTraits<Factory>; |
| 113 | |
| 114 | Factory(); |
| 115 | virtual ~Factory(); |
| 116 | |
[email protected] | f1484c5 | 2013-05-22 23:25:44 | [diff] [blame] | 117 | // BrowserContextKeyedServiceFactory |
| 118 | virtual BrowserContextKeyedService* BuildServiceInstanceFor( |
[email protected] | 01996a59 | 2013-09-13 06:55:04 | [diff] [blame] | 119 | content::BrowserContext* context) const OVERRIDE; |
[email protected] | f1484c5 | 2013-05-22 23:25:44 | [diff] [blame] | 120 | virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE; |
[email protected] | bb05cae1 | 2012-09-06 00:37:52 | [diff] [blame] | 121 | virtual bool ServiceIsNULLWhileTesting() const OVERRIDE; |
[email protected] | d3aa6145 | 2013-05-03 23:29:04 | [diff] [blame] | 122 | virtual content::BrowserContext* GetBrowserContextToUse( |
| 123 | content::BrowserContext* context) const OVERRIDE; |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 124 | }; |
| 125 | |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 126 | protected: |
| 127 | void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached); |
| 128 | |
| 129 | private: |
[email protected] | 76af173df | 2013-05-22 07:49:37 | [diff] [blame] | 130 | // Ensures the specified |shell_window| is included in |shell_windows_|. |
| 131 | // Otherwise adds |shell_window| to the back of |shell_windows_|. |
[email protected] | d53417e | 2013-07-01 07:07:39 | [diff] [blame] | 132 | void AddShellWindowToList(apps::ShellWindow* shell_window); |
[email protected] | 76af173df | 2013-05-22 07:49:37 | [diff] [blame] | 133 | |
| 134 | // Bring |shell_window| to the front of |shell_windows_|. If it is not in the |
| 135 | // list, add it first. |
[email protected] | d53417e | 2013-07-01 07:07:39 | [diff] [blame] | 136 | void BringToFront(apps::ShellWindow* shell_window); |
[email protected] | 76af173df | 2013-05-22 07:49:37 | [diff] [blame] | 137 | |
[email protected] | 01996a59 | 2013-09-13 06:55:04 | [diff] [blame] | 138 | content::BrowserContext* context_; |
[email protected] | 76af173df | 2013-05-22 07:49:37 | [diff] [blame] | 139 | ShellWindowList shell_windows_; |
[email protected] | e85cc64 | 2012-10-24 06:14:23 | [diff] [blame] | 140 | InspectedWindowSet inspected_windows_; |
[email protected] | 25977110 | 2012-05-31 16:52:20 | [diff] [blame] | 141 | ObserverList<Observer> observers_; |
[email protected] | a23f62e | 2013-04-26 13:13:02 | [diff] [blame] | 142 | base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_; |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 143 | }; |
| 144 | |
[email protected] | d9ede58 | 2012-08-14 19:21:38 | [diff] [blame] | 145 | } // namespace extensions |
| 146 | |
[email protected] | f6d9b28 | 2013-08-09 11:03:20 | [diff] [blame] | 147 | #endif // APPS_SHELL_WINDOW_REGISTRY_H_ |