blob: f32b39b4702fae31ba08159d85b6774aa0fb755e [file] [log] [blame]
[email protected]f6d9b282013-08-09 11:03:201// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]d72d3a62012-05-10 03:45:082// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]f6d9b282013-08-09 11:03:205#ifndef APPS_SHELL_WINDOW_REGISTRY_H_
6#define APPS_SHELL_WINDOW_REGISTRY_H_
[email protected]d72d3a62012-05-10 03:45:087
[email protected]76af173df2013-05-22 07:49:378#include <list>
[email protected]d72d3a62012-05-10 03:45:089
[email protected]a23f62e2013-04-26 13:13:0210#include "base/callback.h"
[email protected]d72d3a62012-05-10 03:45:0811#include "base/compiler_specific.h"
12#include "base/memory/singleton.h"
[email protected]259771102012-05-31 16:52:2013#include "base/observer_list.h"
[email protected]0dd6f2032013-05-20 23:33:4014#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]763cb9072012-06-25 21:39:5116#include "ui/gfx/native_widget_types.h"
[email protected]d72d3a62012-05-10 03:45:0817
[email protected]7bc8299d2012-06-13 09:18:2918namespace content {
[email protected]01996a592013-09-13 06:55:0419class BrowserContext;
[email protected]a23f62e2013-04-26 13:13:0220class DevToolsAgentHost;
[email protected]7bc8299d2012-06-13 09:18:2921class RenderViewHost;
22}
23
[email protected]f6d9b282013-08-09 11:03:2024namespace apps {
25
26class ShellWindow;
[email protected]d9ede582012-08-14 19:21:3827
[email protected]d72d3a62012-05-10 03:45:0828// The ShellWindowRegistry tracks the ShellWindows for all platform apps for a
[email protected]01996a592013-09-13 06:55:0429// particular browser context.
[email protected]f1484c52013-05-22 23:25:4430class ShellWindowRegistry : public BrowserContextKeyedService {
[email protected]d72d3a62012-05-10 03:45:0831 public:
[email protected]259771102012-05-31 16:52:2032 class Observer {
33 public:
34 // Called just after a shell window was added.
[email protected]d53417e2013-07-01 07:07:3935 virtual void OnShellWindowAdded(apps::ShellWindow* shell_window) = 0;
[email protected]424f7052012-12-05 05:25:2636 // Called when the window icon changes.
[email protected]d53417e2013-07-01 07:07:3937 virtual void OnShellWindowIconChanged(apps::ShellWindow* shell_window) = 0;
[email protected]259771102012-05-31 16:52:2038 // Called just after a shell window was removed.
[email protected]d53417e2013-07-01 07:07:3939 virtual void OnShellWindowRemoved(apps::ShellWindow* shell_window) = 0;
[email protected]259771102012-05-31 16:52:2040
41 protected:
42 virtual ~Observer() {}
43 };
44
[email protected]d53417e2013-07-01 07:07:3945 typedef std::list<apps::ShellWindow*> ShellWindowList;
[email protected]76af173df2013-05-22 07:49:3746 typedef ShellWindowList::const_iterator const_iterator;
[email protected]e85cc642012-10-24 06:14:2347 typedef std::set<std::string> InspectedWindowSet;
[email protected]d72d3a62012-05-10 03:45:0848
[email protected]01996a592013-09-13 06:55:0449 explicit ShellWindowRegistry(content::BrowserContext* context);
[email protected]d72d3a62012-05-10 03:45:0850 virtual ~ShellWindowRegistry();
51
[email protected]01996a592013-09-13 06:55:0452 // 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]d72d3a62012-05-10 03:45:0856
[email protected]d53417e2013-07-01 07:07:3957 void AddShellWindow(apps::ShellWindow* shell_window);
58 void ShellWindowIconChanged(apps::ShellWindow* shell_window);
[email protected]76af173df2013-05-22 07:49:3759 // Called by |shell_window| when it is activated.
[email protected]d53417e2013-07-01 07:07:3960 void ShellWindowActivated(apps::ShellWindow* shell_window);
61 void RemoveShellWindow(apps::ShellWindow* shell_window);
[email protected]d72d3a62012-05-10 03:45:0862
[email protected]259771102012-05-31 16:52:2063 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]76af173df2013-05-22 07:49:3767 ShellWindowList GetShellWindowsForApp(const std::string& app_id) const;
68 const ShellWindowList& shell_windows() const { return shell_windows_; }
[email protected]d72d3a62012-05-10 03:45:0869
[email protected]90a4b9d2013-07-31 12:14:4070 // Close all shell windows associated with an app.
71 void CloseAllShellWindowsForApp(const std::string& app_id);
72
[email protected]763cb9072012-06-25 21:39:5173 // Helper functions to find shell windows with particular attributes.
[email protected]d53417e2013-07-01 07:07:3974 apps::ShellWindow* GetShellWindowForRenderViewHost(
[email protected]7bc8299d2012-06-13 09:18:2975 content::RenderViewHost* render_view_host) const;
[email protected]d53417e2013-07-01 07:07:3976 apps::ShellWindow* GetShellWindowForNativeWindow(
77 gfx::NativeWindow window) const;
[email protected]6137ac22012-08-21 23:02:5278 // 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]d53417e2013-07-01 07:07:3981 apps::ShellWindow* GetCurrentShellWindowForApp(
82 const std::string& app_id) const;
[email protected]2ce9fec2012-12-06 22:48:1383 // 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]d53417e2013-07-01 07:07:3987 apps::ShellWindow* GetShellWindowForAppAndKey(
88 const std::string& app_id,
89 const std::string& window_key) const;
[email protected]7bc8299d2012-06-13 09:18:2990
[email protected]e85cc642012-10-24 06:14:2391 // 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]01996a592013-09-13 06:55:0496 // Returns the shell window for |window|, looking in all browser contexts.
[email protected]d53417e2013-07-01 07:07:3997 static apps::ShellWindow* GetShellWindowForNativeWindowAnyProfile(
[email protected]b9ab6f842013-02-05 07:31:2098 gfx::NativeWindow window);
99
[email protected]01996a592013-09-13 06:55:04100 // 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]24ced7dc02013-04-04 08:32:39102 // ShellWindow::WindowType, or 0 for any window type.
103 static bool IsShellWindowRegisteredInAnyProfile(int window_type_mask);
104
[email protected]f1484c52013-05-22 23:25:44105 class Factory : public BrowserContextKeyedServiceFactory {
[email protected]d72d3a62012-05-10 03:45:08106 public:
[email protected]01996a592013-09-13 06:55:04107 static ShellWindowRegistry* GetForBrowserContext(
108 content::BrowserContext* context, bool create);
[email protected]d72d3a62012-05-10 03:45:08109
110 static Factory* GetInstance();
111 private:
112 friend struct DefaultSingletonTraits<Factory>;
113
114 Factory();
115 virtual ~Factory();
116
[email protected]f1484c52013-05-22 23:25:44117 // BrowserContextKeyedServiceFactory
118 virtual BrowserContextKeyedService* BuildServiceInstanceFor(
[email protected]01996a592013-09-13 06:55:04119 content::BrowserContext* context) const OVERRIDE;
[email protected]f1484c52013-05-22 23:25:44120 virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE;
[email protected]bb05cae12012-09-06 00:37:52121 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE;
[email protected]d3aa61452013-05-03 23:29:04122 virtual content::BrowserContext* GetBrowserContextToUse(
123 content::BrowserContext* context) const OVERRIDE;
[email protected]d72d3a62012-05-10 03:45:08124 };
125
[email protected]771c8d272013-05-17 09:47:40126 protected:
127 void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
128
129 private:
[email protected]76af173df2013-05-22 07:49:37130 // Ensures the specified |shell_window| is included in |shell_windows_|.
131 // Otherwise adds |shell_window| to the back of |shell_windows_|.
[email protected]d53417e2013-07-01 07:07:39132 void AddShellWindowToList(apps::ShellWindow* shell_window);
[email protected]76af173df2013-05-22 07:49:37133
134 // Bring |shell_window| to the front of |shell_windows_|. If it is not in the
135 // list, add it first.
[email protected]d53417e2013-07-01 07:07:39136 void BringToFront(apps::ShellWindow* shell_window);
[email protected]76af173df2013-05-22 07:49:37137
[email protected]01996a592013-09-13 06:55:04138 content::BrowserContext* context_;
[email protected]76af173df2013-05-22 07:49:37139 ShellWindowList shell_windows_;
[email protected]e85cc642012-10-24 06:14:23140 InspectedWindowSet inspected_windows_;
[email protected]259771102012-05-31 16:52:20141 ObserverList<Observer> observers_;
[email protected]a23f62e2013-04-26 13:13:02142 base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
[email protected]d72d3a62012-05-10 03:45:08143};
144
[email protected]d9ede582012-08-14 19:21:38145} // namespace extensions
146
[email protected]f6d9b282013-08-09 11:03:20147#endif // APPS_SHELL_WINDOW_REGISTRY_H_