Make hidden app windows have no shelf presence [ChromeOS].
[chromium-blink-merge.git] / apps / app_window_registry.cc
blob4ed0b8bbd9a6258c45f216cb10f8f88f034bc595
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 #include "apps/app_window.h"
6 #include "apps/app_window_registry.h"
7 #include "apps/apps_client.h"
8 #include "apps/ui/native_app_window.h"
9 #include "components/keyed_service/content/browser_context_dependency_manager.h"
10 #include "content/public/browser/browser_context.h"
11 #include "content/public/browser/devtools_agent_host.h"
12 #include "content/public/browser/devtools_manager.h"
13 #include "content/public/browser/render_process_host.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/site_instance.h"
16 #include "content/public/browser/web_contents.h"
17 #include "extensions/browser/extensions_browser_client.h"
18 #include "extensions/common/extension.h"
20 namespace {
22 // Create a key that identifies a AppWindow in a RenderViewHost across App
23 // reloads. If the window was given an id in CreateParams, the key is the
24 // extension id, a colon separator, and the AppWindow's |id|. If there is no
25 // |id|, the chrome-extension://extension-id/page.html URL will be used. If the
26 // RenderViewHost is not for a AppWindow, return an empty string.
27 std::string GetWindowKeyForRenderViewHost(
28 const apps::AppWindowRegistry* registry,
29 content::RenderViewHost* render_view_host) {
30 apps::AppWindow* app_window =
31 registry->GetAppWindowForRenderViewHost(render_view_host);
32 if (!app_window)
33 return std::string(); // Not a AppWindow.
35 if (app_window->window_key().empty())
36 return app_window->web_contents()->GetURL().possibly_invalid_spec();
38 std::string key = app_window->extension()->id();
39 key += ':';
40 key += app_window->window_key();
41 return key;
44 } // namespace
46 namespace apps {
48 void AppWindowRegistry::Observer::OnAppWindowHidden(AppWindow* app_window) {}
50 void AppWindowRegistry::Observer::OnAppWindowShown(AppWindow* app_window) {}
52 AppWindowRegistry::Observer::~Observer() {}
54 AppWindowRegistry::AppWindowRegistry(content::BrowserContext* context)
55 : context_(context),
56 devtools_callback_(base::Bind(&AppWindowRegistry::OnDevToolsStateChanged,
57 base::Unretained(this))) {
58 content::DevToolsManager::GetInstance()->AddAgentStateCallback(
59 devtools_callback_);
62 AppWindowRegistry::~AppWindowRegistry() {
63 content::DevToolsManager::GetInstance()->RemoveAgentStateCallback(
64 devtools_callback_);
67 // static
68 AppWindowRegistry* AppWindowRegistry::Get(content::BrowserContext* context) {
69 return Factory::GetForBrowserContext(context, true /* create */);
72 void AppWindowRegistry::AddAppWindow(AppWindow* app_window) {
73 BringToFront(app_window);
74 FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowAdded(app_window));
77 void AppWindowRegistry::AppWindowIconChanged(AppWindow* app_window) {
78 AddAppWindowToList(app_window);
79 FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowIconChanged(app_window));
82 void AppWindowRegistry::AppWindowActivated(AppWindow* app_window) {
83 BringToFront(app_window);
86 void AppWindowRegistry::AppWindowHidden(AppWindow* app_window) {
87 FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowHidden(app_window));
90 void AppWindowRegistry::AppWindowShown(AppWindow* app_window) {
91 FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowShown(app_window));
94 void AppWindowRegistry::RemoveAppWindow(AppWindow* app_window) {
95 const AppWindowList::iterator it =
96 std::find(app_windows_.begin(), app_windows_.end(), app_window);
97 if (it != app_windows_.end())
98 app_windows_.erase(it);
99 FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowRemoved(app_window));
102 void AppWindowRegistry::AddObserver(Observer* observer) {
103 observers_.AddObserver(observer);
106 void AppWindowRegistry::RemoveObserver(Observer* observer) {
107 observers_.RemoveObserver(observer);
110 AppWindowRegistry::AppWindowList AppWindowRegistry::GetAppWindowsForApp(
111 const std::string& app_id) const {
112 AppWindowList app_windows;
113 for (AppWindowList::const_iterator i = app_windows_.begin();
114 i != app_windows_.end();
115 ++i) {
116 if ((*i)->extension_id() == app_id)
117 app_windows.push_back(*i);
119 return app_windows;
122 void AppWindowRegistry::CloseAllAppWindowsForApp(const std::string& app_id) {
123 const AppWindowList windows = GetAppWindowsForApp(app_id);
124 for (AppWindowRegistry::const_iterator it = windows.begin();
125 it != windows.end();
126 ++it) {
127 (*it)->GetBaseWindow()->Close();
131 AppWindow* AppWindowRegistry::GetAppWindowForRenderViewHost(
132 content::RenderViewHost* render_view_host) const {
133 for (AppWindowList::const_iterator i = app_windows_.begin();
134 i != app_windows_.end();
135 ++i) {
136 if ((*i)->web_contents()->GetRenderViewHost() == render_view_host)
137 return *i;
140 return NULL;
143 AppWindow* AppWindowRegistry::GetAppWindowForNativeWindow(
144 gfx::NativeWindow window) const {
145 for (AppWindowList::const_iterator i = app_windows_.begin();
146 i != app_windows_.end();
147 ++i) {
148 if ((*i)->GetNativeWindow() == window)
149 return *i;
152 return NULL;
155 AppWindow* AppWindowRegistry::GetCurrentAppWindowForApp(
156 const std::string& app_id) const {
157 AppWindow* result = NULL;
158 for (AppWindowList::const_iterator i = app_windows_.begin();
159 i != app_windows_.end();
160 ++i) {
161 if ((*i)->extension()->id() == app_id) {
162 result = *i;
163 if (result->GetBaseWindow()->IsActive())
164 return result;
168 return result;
171 AppWindow* AppWindowRegistry::GetAppWindowForAppAndKey(
172 const std::string& app_id,
173 const std::string& window_key) const {
174 AppWindow* result = NULL;
175 for (AppWindowList::const_iterator i = app_windows_.begin();
176 i != app_windows_.end();
177 ++i) {
178 if ((*i)->extension()->id() == app_id && (*i)->window_key() == window_key) {
179 result = *i;
180 if (result->GetBaseWindow()->IsActive())
181 return result;
184 return result;
187 bool AppWindowRegistry::HadDevToolsAttached(
188 content::RenderViewHost* render_view_host) const {
189 std::string key = GetWindowKeyForRenderViewHost(this, render_view_host);
190 return key.empty() ? false : inspected_windows_.count(key) != 0;
193 // static
194 AppWindow* AppWindowRegistry::GetAppWindowForNativeWindowAnyProfile(
195 gfx::NativeWindow window) {
196 std::vector<content::BrowserContext*> contexts =
197 AppsClient::Get()->GetLoadedBrowserContexts();
198 for (std::vector<content::BrowserContext*>::const_iterator i =
199 contexts.begin();
200 i != contexts.end();
201 ++i) {
202 AppWindowRegistry* registry =
203 Factory::GetForBrowserContext(*i, false /* create */);
204 if (!registry)
205 continue;
207 AppWindow* app_window = registry->GetAppWindowForNativeWindow(window);
208 if (app_window)
209 return app_window;
212 return NULL;
215 // static
216 bool AppWindowRegistry::IsAppWindowRegisteredInAnyProfile(
217 int window_type_mask) {
218 std::vector<content::BrowserContext*> contexts =
219 AppsClient::Get()->GetLoadedBrowserContexts();
220 for (std::vector<content::BrowserContext*>::const_iterator i =
221 contexts.begin();
222 i != contexts.end();
223 ++i) {
224 AppWindowRegistry* registry =
225 Factory::GetForBrowserContext(*i, false /* create */);
226 if (!registry)
227 continue;
229 const AppWindowList& app_windows = registry->app_windows();
230 if (app_windows.empty())
231 continue;
233 if (window_type_mask == 0)
234 return true;
236 for (const_iterator j = app_windows.begin(); j != app_windows.end(); ++j) {
237 if ((*j)->window_type() & window_type_mask)
238 return true;
242 return false;
245 // static
246 void AppWindowRegistry::CloseAllAppWindows() {
247 std::vector<content::BrowserContext*> contexts =
248 AppsClient::Get()->GetLoadedBrowserContexts();
249 for (std::vector<content::BrowserContext*>::const_iterator i =
250 contexts.begin();
251 i != contexts.end();
252 ++i) {
253 AppWindowRegistry* registry =
254 Factory::GetForBrowserContext(*i, false /* create */);
255 if (!registry)
256 continue;
258 while (!registry->app_windows().empty())
259 registry->app_windows().front()->GetBaseWindow()->Close();
263 void AppWindowRegistry::OnDevToolsStateChanged(
264 content::DevToolsAgentHost* agent_host,
265 bool attached) {
266 content::RenderViewHost* rvh = agent_host->GetRenderViewHost();
267 // Ignore unrelated notifications.
268 if (!rvh ||
269 rvh->GetSiteInstance()->GetProcess()->GetBrowserContext() != context_)
270 return;
272 std::string key = GetWindowKeyForRenderViewHost(this, rvh);
273 if (key.empty())
274 return;
276 if (attached)
277 inspected_windows_.insert(key);
278 else
279 inspected_windows_.erase(key);
282 void AppWindowRegistry::AddAppWindowToList(AppWindow* app_window) {
283 const AppWindowList::iterator it =
284 std::find(app_windows_.begin(), app_windows_.end(), app_window);
285 if (it != app_windows_.end())
286 return;
287 app_windows_.push_back(app_window);
290 void AppWindowRegistry::BringToFront(AppWindow* app_window) {
291 const AppWindowList::iterator it =
292 std::find(app_windows_.begin(), app_windows_.end(), app_window);
293 if (it != app_windows_.end())
294 app_windows_.erase(it);
295 app_windows_.push_front(app_window);
298 ///////////////////////////////////////////////////////////////////////////////
299 // Factory boilerplate
301 // static
302 AppWindowRegistry* AppWindowRegistry::Factory::GetForBrowserContext(
303 content::BrowserContext* context,
304 bool create) {
305 return static_cast<AppWindowRegistry*>(
306 GetInstance()->GetServiceForBrowserContext(context, create));
309 AppWindowRegistry::Factory* AppWindowRegistry::Factory::GetInstance() {
310 return Singleton<AppWindowRegistry::Factory>::get();
313 AppWindowRegistry::Factory::Factory()
314 : BrowserContextKeyedServiceFactory(
315 "AppWindowRegistry",
316 BrowserContextDependencyManager::GetInstance()) {}
318 AppWindowRegistry::Factory::~Factory() {}
320 KeyedService* AppWindowRegistry::Factory::BuildServiceInstanceFor(
321 content::BrowserContext* context) const {
322 return new AppWindowRegistry(context);
325 bool AppWindowRegistry::Factory::ServiceIsCreatedWithBrowserContext() const {
326 return true;
329 bool AppWindowRegistry::Factory::ServiceIsNULLWhileTesting() const {
330 return false;
333 content::BrowserContext* AppWindowRegistry::Factory::GetBrowserContextToUse(
334 content::BrowserContext* context) const {
335 return extensions::ExtensionsBrowserClient::Get()->GetOriginalContext(
336 context);
339 } // namespace apps