blob: ad255cf883e5d6b1c1758f59e2587134040494cb [file] [log] [blame]
[email protected]d72d3a62012-05-10 03:45:081// 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]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
18class Profile;
19class ShellWindow;
20
[email protected]7bc8299d2012-06-13 09:18:2921namespace content {
[email protected]a23f62e2013-04-26 13:13:0222class DevToolsAgentHost;
[email protected]7bc8299d2012-06-13 09:18:2923class RenderViewHost;
24}
25
[email protected]d9ede582012-08-14 19:21:3826namespace extensions {
27
[email protected]d72d3a62012-05-10 03:45:0828// 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]a23f62e2013-04-26 13:13:0235class ShellWindowRegistry : public ProfileKeyedService {
[email protected]d72d3a62012-05-10 03:45:0836 public:
[email protected]259771102012-05-31 16:52:2037 class Observer {
38 public:
39 // Called just after a shell window was added.
40 virtual void OnShellWindowAdded(ShellWindow* shell_window) = 0;
[email protected]424f7052012-12-05 05:25:2641 // Called when the window icon changes.
42 virtual void OnShellWindowIconChanged(ShellWindow* shell_window) = 0;
[email protected]259771102012-05-31 16:52:2043 // 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]76af173df2013-05-22 07:49:3750 typedef std::list<ShellWindow*> ShellWindowList;
51 typedef ShellWindowList::const_iterator const_iterator;
[email protected]e85cc642012-10-24 06:14:2352 typedef std::set<std::string> InspectedWindowSet;
[email protected]d72d3a62012-05-10 03:45:0853
[email protected]e85cc642012-10-24 06:14:2354 explicit ShellWindowRegistry(Profile* profile);
[email protected]d72d3a62012-05-10 03:45:0855 virtual ~ShellWindowRegistry();
56
57 // Returns the instance for the given profile, or NULL if none. This is
[email protected]a6db6122012-09-03 06:00:2358 // a convenience wrapper around ShellWindowRegistry::Factory::GetForProfile.
[email protected]d72d3a62012-05-10 03:45:0859 static ShellWindowRegistry* Get(Profile* profile);
60
61 void AddShellWindow(ShellWindow* shell_window);
[email protected]424f7052012-12-05 05:25:2662 void ShellWindowIconChanged(ShellWindow* shell_window);
[email protected]76af173df2013-05-22 07:49:3763 // Called by |shell_window| when it is activated.
64 void ShellWindowActivated(ShellWindow* shell_window);
[email protected]d72d3a62012-05-10 03:45:0865 void RemoveShellWindow(ShellWindow* shell_window);
66
[email protected]259771102012-05-31 16:52:2067 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]76af173df2013-05-22 07:49:3771 ShellWindowList GetShellWindowsForApp(const std::string& app_id) const;
72 const ShellWindowList& shell_windows() const { return shell_windows_; }
[email protected]d72d3a62012-05-10 03:45:0873
[email protected]763cb9072012-06-25 21:39:5174 // Helper functions to find shell windows with particular attributes.
[email protected]7bc8299d2012-06-13 09:18:2975 ShellWindow* GetShellWindowForRenderViewHost(
76 content::RenderViewHost* render_view_host) const;
[email protected]763cb9072012-06-25 21:39:5177 ShellWindow* GetShellWindowForNativeWindow(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.
81 ShellWindow* GetCurrentShellWindowForApp(const std::string& app_id) const;
[email protected]2ce9fec2012-12-06 22:48:1382 // 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]7bc8299d2012-06-13 09:18:2988
[email protected]e85cc642012-10-24 06:14:2389 // 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]b9ab6f842013-02-05 07:31:2094 // Returns the shell window for |window|, looking in all profiles.
95 static ShellWindow* GetShellWindowForNativeWindowAnyProfile(
96 gfx::NativeWindow window);
97
[email protected]24ced7dc02013-04-04 08:32:3998 // 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]d72d3a62012-05-10 03:45:08103 class Factory : public ProfileKeyedServiceFactory {
104 public:
[email protected]24ced7dc02013-04-04 08:32:39105 static ShellWindowRegistry* GetForProfile(Profile* profile, bool create);
[email protected]d72d3a62012-05-10 03:45:08106
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]c7fa4362013-04-26 18:09:02116 content::BrowserContext* profile) const OVERRIDE;
[email protected]bb05cae12012-09-06 00:37:52117 virtual bool ServiceIsCreatedWithProfile() const OVERRIDE;
118 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE;
[email protected]d3aa61452013-05-03 23:29:04119 virtual content::BrowserContext* GetBrowserContextToUse(
120 content::BrowserContext* context) const OVERRIDE;
[email protected]d72d3a62012-05-10 03:45:08121 };
122
[email protected]771c8d272013-05-17 09:47:40123 protected:
124 void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
125
126 private:
[email protected]76af173df2013-05-22 07:49:37127 // 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]a23f62e2013-04-26 13:13:02135 Profile* profile_;
[email protected]76af173df2013-05-22 07:49:37136 ShellWindowList shell_windows_;
[email protected]e85cc642012-10-24 06:14:23137 InspectedWindowSet inspected_windows_;
[email protected]259771102012-05-31 16:52:20138 ObserverList<Observer> observers_;
[email protected]a23f62e2013-04-26 13:13:02139 base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
[email protected]d72d3a62012-05-10 03:45:08140};
141
[email protected]d9ede582012-08-14 19:21:38142} // namespace extensions
143
[email protected]d72d3a62012-05-10 03:45:08144#endif // CHROME_BROWSER_EXTENSIONS_SHELL_WINDOW_REGISTRY_H_