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/browser_context_keyed_service/browser_context_keyed_service.h"
15 #include "components/browser_context_keyed_service/browser_context_keyed_service_factory.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 BrowserContextKeyedService
{
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;
42 virtual ~Observer() {}
45 typedef std::list
<apps::AppWindow
*> AppWindowList
;
46 typedef AppWindowList::const_iterator const_iterator
;
47 typedef std::set
<std::string
> InspectedWindowSet
;
49 explicit AppWindowRegistry(content::BrowserContext
* context
);
50 virtual ~AppWindowRegistry();
52 // Returns the instance for the given browser context, or NULL if none. This
53 // is a convenience wrapper around
54 // AppWindowRegistry::Factory::GetForBrowserContext().
55 static AppWindowRegistry
* Get(content::BrowserContext
* context
);
57 void AddAppWindow(apps::AppWindow
* app_window
);
58 void AppWindowIconChanged(apps::AppWindow
* app_window
);
59 // Called by |app_window| when it is activated.
60 void AppWindowActivated(apps::AppWindow
* app_window
);
61 void RemoveAppWindow(apps::AppWindow
* app_window
);
63 void AddObserver(Observer
* observer
);
64 void RemoveObserver(Observer
* observer
);
66 // Returns a set of windows owned by the application identified by app_id.
67 AppWindowList
GetAppWindowsForApp(const std::string
& app_id
) const;
68 const AppWindowList
& app_windows() const { return app_windows_
; }
70 // Close all app windows associated with an app.
71 void CloseAllAppWindowsForApp(const std::string
& app_id
);
73 // Helper functions to find app windows with particular attributes.
74 apps::AppWindow
* GetAppWindowForRenderViewHost(
75 content::RenderViewHost
* render_view_host
) const;
76 apps::AppWindow
* GetAppWindowForNativeWindow(gfx::NativeWindow window
) const;
77 // Returns an app window for the given app, or NULL if no app windows are
78 // open. If there is a window for the given app that is active, that one will
79 // be returned, otherwise an arbitrary window will be returned.
80 apps::AppWindow
* GetCurrentAppWindowForApp(const std::string
& app_id
) const;
81 // Returns an app window for the given app and window key, or NULL if no app
82 // window with the key are open. If there is a window for the given app and
83 // key that is active, that one will be returned, otherwise an arbitrary
84 // window will be returned.
85 apps::AppWindow
* GetAppWindowForAppAndKey(const std::string
& app_id
,
86 const std::string
& window_key
)
89 // Returns whether a AppWindow'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;
94 // Returns the app window for |window|, looking in all browser contexts.
95 static apps::AppWindow
* GetAppWindowForNativeWindowAnyProfile(
96 gfx::NativeWindow window
);
98 // Returns true if the number of app windows registered across all browser
99 // contexts is non-zero. |window_type_mask| is a bitwise OR filter of
100 // AppWindow::WindowType, or 0 for any window type.
101 static bool IsAppWindowRegisteredInAnyProfile(int window_type_mask
);
103 class Factory
: public BrowserContextKeyedServiceFactory
{
105 static AppWindowRegistry
* GetForBrowserContext(
106 content::BrowserContext
* context
,
109 static Factory
* GetInstance();
112 friend struct DefaultSingletonTraits
<Factory
>;
117 // BrowserContextKeyedServiceFactory
118 virtual BrowserContextKeyedService
* BuildServiceInstanceFor(
119 content::BrowserContext
* context
) const OVERRIDE
;
120 virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE
;
121 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE
;
122 virtual content::BrowserContext
* GetBrowserContextToUse(
123 content::BrowserContext
* context
) const OVERRIDE
;
127 void OnDevToolsStateChanged(content::DevToolsAgentHost
*, bool attached
);
130 // Ensures the specified |app_window| is included in |app_windows_|.
131 // Otherwise adds |app_window| to the back of |app_windows_|.
132 void AddAppWindowToList(apps::AppWindow
* app_window
);
134 // Bring |app_window| to the front of |app_windows_|. If it is not in the
135 // list, add it first.
136 void BringToFront(apps::AppWindow
* app_window
);
138 content::BrowserContext
* context_
;
139 AppWindowList app_windows_
;
140 InspectedWindowSet inspected_windows_
;
141 ObserverList
<Observer
> observers_
;
142 base::Callback
<void(content::DevToolsAgentHost
*, bool)> devtools_callback_
;
145 } // namespace extensions
147 #endif // APPS_APP_WINDOW_REGISTRY_H_