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_
10 #include "base/callback.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/singleton.h"
13 #include "base/observer_list.h"
14 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
15 #include "components/keyed_service/core/keyed_service.h"
16 #include "ui/gfx/native_widget_types.h"
20 class DevToolsAgentHost
;
28 // The AppWindowRegistry tracks the AppWindows for all platform apps for a
29 // particular browser context.
30 class AppWindowRegistry
: public KeyedService
{
34 // Called just after a app window was added.
35 virtual void OnAppWindowAdded(apps::AppWindow
* app_window
) = 0;
36 // Called when the window icon changes.
37 virtual void OnAppWindowIconChanged(apps::AppWindow
* app_window
) = 0;
38 // Called just after a app window was removed.
39 virtual void OnAppWindowRemoved(apps::AppWindow
* app_window
) = 0;
40 // Called just after a app window was hidden. This is different from
41 // window visibility as a minimize does not hide a window, but does make
43 virtual void OnAppWindowHidden(apps::AppWindow
* app_window
);
44 // Called just after a app window was shown.
45 virtual void OnAppWindowShown(apps::AppWindow
* app_window
);
51 typedef std::list
<apps::AppWindow
*> AppWindowList
;
52 typedef AppWindowList::const_iterator const_iterator
;
53 typedef std::set
<std::string
> InspectedWindowSet
;
55 explicit AppWindowRegistry(content::BrowserContext
* context
);
56 virtual ~AppWindowRegistry();
58 // Returns the instance for the given browser context, or NULL if none. This
59 // is a convenience wrapper around
60 // AppWindowRegistry::Factory::GetForBrowserContext().
61 static AppWindowRegistry
* Get(content::BrowserContext
* context
);
63 void AddAppWindow(apps::AppWindow
* app_window
);
64 void AppWindowIconChanged(apps::AppWindow
* app_window
);
65 // Called by |app_window| when it is activated.
66 void AppWindowActivated(apps::AppWindow
* app_window
);
67 void AppWindowHidden(apps::AppWindow
* app_window
);
68 void AppWindowShown(apps::AppWindow
* app_window
);
69 void RemoveAppWindow(apps::AppWindow
* app_window
);
71 void AddObserver(Observer
* observer
);
72 void RemoveObserver(Observer
* observer
);
74 // Returns a set of windows owned by the application identified by app_id.
75 AppWindowList
GetAppWindowsForApp(const std::string
& app_id
) const;
76 const AppWindowList
& app_windows() const { return app_windows_
; }
78 // Close all app windows associated with an app.
79 void CloseAllAppWindowsForApp(const std::string
& app_id
);
81 // Helper functions to find app windows with particular attributes.
82 apps::AppWindow
* GetAppWindowForRenderViewHost(
83 content::RenderViewHost
* render_view_host
) const;
84 apps::AppWindow
* GetAppWindowForNativeWindow(gfx::NativeWindow window
) const;
85 // Returns an app window for the given app, or NULL if no app windows are
86 // open. If there is a window for the given app that is active, that one will
87 // be returned, otherwise an arbitrary window will be returned.
88 apps::AppWindow
* GetCurrentAppWindowForApp(const std::string
& app_id
) const;
89 // Returns an app window for the given app and window key, or NULL if no app
90 // window with the key are open. If there is a window for the given app and
91 // key that is active, that one will be returned, otherwise an arbitrary
92 // window will be returned.
93 apps::AppWindow
* GetAppWindowForAppAndKey(const std::string
& app_id
,
94 const std::string
& window_key
)
97 // Returns whether a AppWindow's ID was last known to have a DevToolsAgent
98 // attached to it, which should be restored during a reload of a corresponding
99 // newly created |render_view_host|.
100 bool HadDevToolsAttached(content::RenderViewHost
* render_view_host
) const;
102 // Returns the app window for |window|, looking in all browser contexts.
103 static apps::AppWindow
* GetAppWindowForNativeWindowAnyProfile(
104 gfx::NativeWindow window
);
106 // Returns true if the number of app windows registered across all browser
107 // contexts is non-zero. |window_type_mask| is a bitwise OR filter of
108 // AppWindow::WindowType, or 0 for any window type.
109 static bool IsAppWindowRegisteredInAnyProfile(int window_type_mask
);
111 class Factory
: public BrowserContextKeyedServiceFactory
{
113 static AppWindowRegistry
* GetForBrowserContext(
114 content::BrowserContext
* context
,
117 static Factory
* GetInstance();
120 friend struct DefaultSingletonTraits
<Factory
>;
125 // BrowserContextKeyedServiceFactory
126 virtual KeyedService
* BuildServiceInstanceFor(
127 content::BrowserContext
* context
) const OVERRIDE
;
128 virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE
;
129 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE
;
130 virtual content::BrowserContext
* GetBrowserContextToUse(
131 content::BrowserContext
* context
) const OVERRIDE
;
135 void OnDevToolsStateChanged(content::DevToolsAgentHost
*, bool attached
);
138 // Ensures the specified |app_window| is included in |app_windows_|.
139 // Otherwise adds |app_window| to the back of |app_windows_|.
140 void AddAppWindowToList(apps::AppWindow
* app_window
);
142 // Bring |app_window| to the front of |app_windows_|. If it is not in the
143 // list, add it first.
144 void BringToFront(apps::AppWindow
* app_window
);
146 content::BrowserContext
* context_
;
147 AppWindowList app_windows_
;
148 InspectedWindowSet inspected_windows_
;
149 ObserverList
<Observer
> observers_
;
150 base::Callback
<void(content::DevToolsAgentHost
*, bool)> devtools_callback_
;
155 #endif // APPS_APP_WINDOW_REGISTRY_H_