blob: 07a0ccca13debabe77b7710732bff3505479564a [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
8#include <set>
9
[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]d72d3a62012-05-10 03:45:0850 typedef std::set<ShellWindow*> ShellWindowSet;
51 typedef ShellWindowSet::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]d72d3a62012-05-10 03:45:0863 void RemoveShellWindow(ShellWindow* shell_window);
64
[email protected]259771102012-05-31 16:52:2065 void AddObserver(Observer* observer);
66 void RemoveObserver(Observer* observer);
67
68 // Returns a set of windows owned by the application identified by app_id.
[email protected]6574d79e2012-07-25 22:48:2769 ShellWindowSet GetShellWindowsForApp(const std::string& app_id) const;
[email protected]d72d3a62012-05-10 03:45:0870 const ShellWindowSet& shell_windows() const { return shell_windows_; }
71
[email protected]763cb9072012-06-25 21:39:5172 // Helper functions to find shell windows with particular attributes.
[email protected]7bc8299d2012-06-13 09:18:2973 ShellWindow* GetShellWindowForRenderViewHost(
74 content::RenderViewHost* render_view_host) const;
[email protected]763cb9072012-06-25 21:39:5175 ShellWindow* GetShellWindowForNativeWindow(gfx::NativeWindow window) const;
[email protected]6137ac22012-08-21 23:02:5276 // Returns an app window for the given app, or NULL if no shell windows are
77 // open. If there is a window for the given app that is active, that one will
78 // be returned, otherwise an arbitrary window will be returned.
79 ShellWindow* GetCurrentShellWindowForApp(const std::string& app_id) const;
[email protected]2ce9fec2012-12-06 22:48:1380 // Returns an app window for the given app and window key, or NULL if no shell
81 // window with the key are open. If there is a window for the given app and
82 // key that is active, that one will be returned, otherwise an arbitrary
83 // window will be returned.
84 ShellWindow* GetShellWindowForAppAndKey(const std::string& app_id,
85 const std::string& window_key) const;
[email protected]7bc8299d2012-06-13 09:18:2986
[email protected]e85cc642012-10-24 06:14:2387 // Returns whether a ShellWindow's ID was last known to have a DevToolsAgent
88 // attached to it, which should be restored during a reload of a corresponding
89 // newly created |render_view_host|.
90 bool HadDevToolsAttached(content::RenderViewHost* render_view_host) const;
91
[email protected]b9ab6f842013-02-05 07:31:2092 // Returns the shell window for |window|, looking in all profiles.
93 static ShellWindow* GetShellWindowForNativeWindowAnyProfile(
94 gfx::NativeWindow window);
95
[email protected]24ced7dc02013-04-04 08:32:3996 // Returns true if the number of shell windows registered across all profiles
97 // is non-zero. |window_type_mask| is a bitwise OR filter of
98 // ShellWindow::WindowType, or 0 for any window type.
99 static bool IsShellWindowRegisteredInAnyProfile(int window_type_mask);
100
[email protected]d72d3a62012-05-10 03:45:08101 class Factory : public ProfileKeyedServiceFactory {
102 public:
[email protected]24ced7dc02013-04-04 08:32:39103 static ShellWindowRegistry* GetForProfile(Profile* profile, bool create);
[email protected]d72d3a62012-05-10 03:45:08104
105 static Factory* GetInstance();
106 private:
107 friend struct DefaultSingletonTraits<Factory>;
108
109 Factory();
110 virtual ~Factory();
111
112 // ProfileKeyedServiceFactory
113 virtual ProfileKeyedService* BuildServiceInstanceFor(
[email protected]c7fa4362013-04-26 18:09:02114 content::BrowserContext* profile) const OVERRIDE;
[email protected]bb05cae12012-09-06 00:37:52115 virtual bool ServiceIsCreatedWithProfile() const OVERRIDE;
116 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE;
[email protected]d3aa61452013-05-03 23:29:04117 virtual content::BrowserContext* GetBrowserContextToUse(
118 content::BrowserContext* context) const OVERRIDE;
[email protected]d72d3a62012-05-10 03:45:08119 };
120
[email protected]771c8d272013-05-17 09:47:40121 protected:
122 void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
123
124 private:
[email protected]a23f62e2013-04-26 13:13:02125 Profile* profile_;
[email protected]d72d3a62012-05-10 03:45:08126 ShellWindowSet shell_windows_;
[email protected]e85cc642012-10-24 06:14:23127 InspectedWindowSet inspected_windows_;
[email protected]259771102012-05-31 16:52:20128 ObserverList<Observer> observers_;
[email protected]a23f62e2013-04-26 13:13:02129 base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
[email protected]d72d3a62012-05-10 03:45:08130};
131
[email protected]d9ede582012-08-14 19:21:38132} // namespace extensions
133
[email protected]d72d3a62012-05-10 03:45:08134#endif // CHROME_BROWSER_EXTENSIONS_SHELL_WINDOW_REGISTRY_H_