1 // Copyright 2014 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.
5 #ifndef APPS_APP_WINDOW_REGISTRY_H_
6 #define APPS_APP_WINDOW_REGISTRY_H_
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/singleton.h"
15 #include "base/observer_list.h"
16 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
17 #include "components/keyed_service/core/keyed_service.h"
18 #include "ui/gfx/native_widget_types.h"
22 class DevToolsAgentHost
;
30 // The AppWindowRegistry tracks the AppWindows for all platform apps for a
31 // particular browser context.
32 class AppWindowRegistry
: public KeyedService
{
36 // Called just after a app window was added.
37 virtual void OnAppWindowAdded(apps::AppWindow
* app_window
);
38 // Called when the window icon changes.
39 virtual void OnAppWindowIconChanged(apps::AppWindow
* app_window
);
40 // Called just after a app window was removed.
41 virtual void OnAppWindowRemoved(apps::AppWindow
* app_window
);
42 // Called just after a app window was hidden. This is different from
43 // window visibility as a minimize does not hide a window, but does make
45 virtual void OnAppWindowHidden(apps::AppWindow
* app_window
);
46 // Called just after a app window was shown.
47 virtual void OnAppWindowShown(apps::AppWindow
* app_window
);
53 typedef std::list
<apps::AppWindow
*> AppWindowList
;
54 typedef AppWindowList::const_iterator const_iterator
;
55 typedef std::set
<std::string
> InspectedWindowSet
;
57 explicit AppWindowRegistry(content::BrowserContext
* context
);
58 virtual ~AppWindowRegistry();
60 // Returns the instance for the given browser context, or NULL if none. This
61 // is a convenience wrapper around
62 // AppWindowRegistry::Factory::GetForBrowserContext().
63 static AppWindowRegistry
* Get(content::BrowserContext
* context
);
65 void AddAppWindow(apps::AppWindow
* app_window
);
66 void AppWindowIconChanged(apps::AppWindow
* app_window
);
67 // Called by |app_window| when it is activated.
68 void AppWindowActivated(apps::AppWindow
* app_window
);
69 void AppWindowHidden(apps::AppWindow
* app_window
);
70 void AppWindowShown(apps::AppWindow
* app_window
);
71 void RemoveAppWindow(apps::AppWindow
* app_window
);
73 void AddObserver(Observer
* observer
);
74 void RemoveObserver(Observer
* observer
);
76 // Returns a set of windows owned by the application identified by app_id.
77 AppWindowList
GetAppWindowsForApp(const std::string
& app_id
) const;
78 const AppWindowList
& app_windows() const { return app_windows_
; }
80 // Close all app windows associated with an app.
81 void CloseAllAppWindowsForApp(const std::string
& app_id
);
83 // Helper functions to find app windows with particular attributes.
84 apps::AppWindow
* GetAppWindowForRenderViewHost(
85 content::RenderViewHost
* render_view_host
) const;
86 apps::AppWindow
* GetAppWindowForNativeWindow(gfx::NativeWindow window
) const;
87 // Returns an app window for the given app, or NULL if no app windows are
88 // open. If there is a window for the given app that is active, that one will
89 // be returned, otherwise an arbitrary window will be returned.
90 apps::AppWindow
* GetCurrentAppWindowForApp(const std::string
& app_id
) const;
91 // Returns an app window for the given app and window key, or NULL if no app
92 // window with the key are open. If there is a window for the given app and
93 // key that is active, that one will be returned, otherwise an arbitrary
94 // window will be returned.
95 apps::AppWindow
* GetAppWindowForAppAndKey(const std::string
& app_id
,
96 const std::string
& window_key
)
99 // Returns whether a AppWindow's ID was last known to have a DevToolsAgent
100 // attached to it, which should be restored during a reload of a corresponding
101 // newly created |render_view_host|.
102 bool HadDevToolsAttached(content::RenderViewHost
* render_view_host
) const;
104 // Returns the app window for |window|, looking in all browser contexts.
105 static apps::AppWindow
* GetAppWindowForNativeWindowAnyProfile(
106 gfx::NativeWindow window
);
108 // Returns true if the number of app windows registered across all browser
109 // contexts is non-zero. |window_type_mask| is a bitwise OR filter of
110 // AppWindow::WindowType, or 0 for any window type.
111 static bool IsAppWindowRegisteredInAnyProfile(int window_type_mask
);
113 // Close all app windows in all profiles.
114 static void CloseAllAppWindows();
116 class Factory
: public BrowserContextKeyedServiceFactory
{
118 static AppWindowRegistry
* GetForBrowserContext(
119 content::BrowserContext
* context
,
122 static Factory
* GetInstance();
125 friend struct DefaultSingletonTraits
<Factory
>;
130 // BrowserContextKeyedServiceFactory
131 virtual KeyedService
* BuildServiceInstanceFor(
132 content::BrowserContext
* context
) const OVERRIDE
;
133 virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE
;
134 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE
;
135 virtual content::BrowserContext
* GetBrowserContextToUse(
136 content::BrowserContext
* context
) const OVERRIDE
;
140 void OnDevToolsStateChanged(content::DevToolsAgentHost
*, bool attached
);
143 // Ensures the specified |app_window| is included in |app_windows_|.
144 // Otherwise adds |app_window| to the back of |app_windows_|.
145 void AddAppWindowToList(apps::AppWindow
* app_window
);
147 // Bring |app_window| to the front of |app_windows_|. If it is not in the
148 // list, add it first.
149 void BringToFront(apps::AppWindow
* app_window
);
151 content::BrowserContext
* context_
;
152 AppWindowList app_windows_
;
153 InspectedWindowSet inspected_windows_
;
154 ObserverList
<Observer
> observers_
;
155 base::Callback
<void(content::DevToolsAgentHost
*, bool)> devtools_callback_
;
160 #endif // APPS_APP_WINDOW_REGISTRY_H_