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 EXTENSIONS_BROWSER_APP_WINDOW_APP_WINDOW_REGISTRY_H_
6 #define EXTENSIONS_BROWSER_APP_WINDOW_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
;
26 namespace extensions
{
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(AppWindow
* app_window
);
38 // Called when the window icon changes.
39 virtual void OnAppWindowIconChanged(AppWindow
* app_window
);
40 // Called just after a app window was removed.
41 virtual void OnAppWindowRemoved(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(AppWindow
* app_window
);
46 // Called just after a app window was shown.
47 virtual void OnAppWindowShown(AppWindow
* app_window
, bool was_hidden
);
48 // Called just after a app window was activated.
49 virtual void OnAppWindowActivated(AppWindow
* app_window
);
55 typedef std::list
<AppWindow
*> AppWindowList
;
56 typedef AppWindowList::const_iterator const_iterator
;
57 typedef std::set
<std::string
> InspectedWindowSet
;
59 explicit AppWindowRegistry(content::BrowserContext
* context
);
60 ~AppWindowRegistry() override
;
62 // Returns the instance for the given browser context, or NULL if none. This
63 // is a convenience wrapper around
64 // AppWindowRegistry::Factory::GetForBrowserContext().
65 static AppWindowRegistry
* Get(content::BrowserContext
* context
);
67 void AddAppWindow(AppWindow
* app_window
);
68 void AppWindowIconChanged(AppWindow
* app_window
);
69 // Called by |app_window| when it is activated.
70 void AppWindowActivated(AppWindow
* app_window
);
71 void AppWindowHidden(AppWindow
* app_window
);
72 void AppWindowShown(AppWindow
* app_window
, bool was_hidden
);
73 void RemoveAppWindow(AppWindow
* app_window
);
75 void AddObserver(Observer
* observer
);
76 void RemoveObserver(Observer
* observer
);
78 // Returns a set of windows owned by the application identified by app_id.
79 AppWindowList
GetAppWindowsForApp(const std::string
& app_id
) const;
80 const AppWindowList
& app_windows() const { return app_windows_
; }
82 // Close all app windows associated with an app.
83 void CloseAllAppWindowsForApp(const std::string
& app_id
);
85 // Helper functions to find app windows with particular attributes.
86 AppWindow
* GetAppWindowForWebContents(
87 const content::WebContents
* web_contents
) const;
88 AppWindow
* GetAppWindowForNativeWindow(gfx::NativeWindow window
) const;
89 // Returns an app window for the given app, or NULL if no app windows are
90 // open. If there is a window for the given app that is active, that one will
91 // be returned, otherwise an arbitrary window will be returned.
92 AppWindow
* GetCurrentAppWindowForApp(const std::string
& app_id
) const;
93 // Returns an app window for the given app and window key, or NULL if no app
94 // window with the key are open. If there is a window for the given app and
95 // key that is active, that one will be returned, otherwise an arbitrary
96 // window will be returned.
97 AppWindow
* GetAppWindowForAppAndKey(const std::string
& app_id
,
98 const std::string
& window_key
) const;
100 // Returns whether a AppWindow's ID was last known to have a DevToolsAgent
101 // attached to it, which should be restored during a reload of a corresponding
102 // newly created |web_contents|.
103 bool HadDevToolsAttached(content::WebContents
* web_contents
) const;
105 class Factory
: public BrowserContextKeyedServiceFactory
{
107 static AppWindowRegistry
* GetForBrowserContext(
108 content::BrowserContext
* context
,
111 static Factory
* GetInstance();
114 friend struct DefaultSingletonTraits
<Factory
>;
119 // BrowserContextKeyedServiceFactory
120 KeyedService
* BuildServiceInstanceFor(
121 content::BrowserContext
* context
) const override
;
122 bool ServiceIsCreatedWithBrowserContext() const override
;
123 bool ServiceIsNULLWhileTesting() const override
;
124 content::BrowserContext
* GetBrowserContextToUse(
125 content::BrowserContext
* context
) const override
;
129 void OnDevToolsStateChanged(content::DevToolsAgentHost
*, bool attached
);
132 // Ensures the specified |app_window| is included in |app_windows_|.
133 // Otherwise adds |app_window| to the back of |app_windows_|.
134 void AddAppWindowToList(AppWindow
* app_window
);
136 // Bring |app_window| to the front of |app_windows_|. If it is not in the
137 // list, add it first.
138 void BringToFront(AppWindow
* app_window
);
140 // Create a key that identifies an AppWindow across App reloads. If the window
141 // was given an id in CreateParams, the key is the extension id, a colon
142 // separator, and the AppWindow's |id|. If there is no |id|, the
143 // chrome-extension://extension-id/page.html URL will be used. If the
144 // WebContents is not for a AppWindow, return an empty string.
145 std::string
GetWindowKeyForWebContents(
146 content::WebContents
* web_contents
) const;
148 content::BrowserContext
* context_
;
149 AppWindowList app_windows_
;
150 InspectedWindowSet inspected_windows_
;
151 base::ObserverList
<Observer
> observers_
;
152 base::Callback
<void(content::DevToolsAgentHost
*, bool)> devtools_callback_
;
155 } // namespace extensions
157 #endif // EXTENSIONS_BROWSER_APP_WINDOW_APP_WINDOW_REGISTRY_H_