Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / apps / app_window_registry.cc
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.
4
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"
19
20 namespace {
21
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.
34
35   if (app_window->window_key().empty())
36     return app_window->web_contents()->GetURL().possibly_invalid_spec();
37
38   std::string key = app_window->extension()->id();
39   key += ':';
40   key += app_window->window_key();
41   return key;
42 }
43
44 }  // namespace
45
46 namespace apps {
47
48 #if defined(OS_CHROMEOS)
49 void AppWindowRegistry::Observer::OnAppWindowHidden(AppWindow* app_window) {
50 }
51
52 void AppWindowRegistry::Observer::OnAppWindowShown(AppWindow* app_window) {
53 }
54 #endif
55
56 AppWindowRegistry::Observer::~Observer() {
57 }
58
59 AppWindowRegistry::AppWindowRegistry(content::BrowserContext* context)
60     : context_(context),
61       devtools_callback_(base::Bind(&AppWindowRegistry::OnDevToolsStateChanged,
62                                     base::Unretained(this))) {
63   content::DevToolsManager::GetInstance()->AddAgentStateCallback(
64       devtools_callback_);
65 }
66
67 AppWindowRegistry::~AppWindowRegistry() {
68   content::DevToolsManager::GetInstance()->RemoveAgentStateCallback(
69       devtools_callback_);
70 }
71
72 // static
73 AppWindowRegistry* AppWindowRegistry::Get(content::BrowserContext* context) {
74   return Factory::GetForBrowserContext(context, true /* create */);
75 }
76
77 void AppWindowRegistry::AddAppWindow(AppWindow* app_window) {
78   BringToFront(app_window);
79   FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowAdded(app_window));
80 }
81
82 void AppWindowRegistry::AppWindowIconChanged(AppWindow* app_window) {
83   AddAppWindowToList(app_window);
84   FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowIconChanged(app_window));
85 }
86
87 void AppWindowRegistry::AppWindowActivated(AppWindow* app_window) {
88   BringToFront(app_window);
89 }
90
91 #if defined(OS_CHROMEOS)
92 void AppWindowRegistry::AppWindowHidden(AppWindow* app_window) {
93   FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowHidden(app_window));
94 }
95
96 void AppWindowRegistry::AppWindowShown(AppWindow* app_window) {
97   FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowShown(app_window));
98 }
99 #endif
100
101 void AppWindowRegistry::RemoveAppWindow(AppWindow* app_window) {
102   const AppWindowList::iterator it =
103       std::find(app_windows_.begin(), app_windows_.end(), app_window);
104   if (it != app_windows_.end())
105     app_windows_.erase(it);
106   FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowRemoved(app_window));
107 }
108
109 void AppWindowRegistry::AddObserver(Observer* observer) {
110   observers_.AddObserver(observer);
111 }
112
113 void AppWindowRegistry::RemoveObserver(Observer* observer) {
114   observers_.RemoveObserver(observer);
115 }
116
117 AppWindowRegistry::AppWindowList AppWindowRegistry::GetAppWindowsForApp(
118     const std::string& app_id) const {
119   AppWindowList app_windows;
120   for (AppWindowList::const_iterator i = app_windows_.begin();
121        i != app_windows_.end();
122        ++i) {
123     if ((*i)->extension_id() == app_id)
124       app_windows.push_back(*i);
125   }
126   return app_windows;
127 }
128
129 void AppWindowRegistry::CloseAllAppWindowsForApp(const std::string& app_id) {
130   for (AppWindowList::const_iterator i = app_windows_.begin();
131        i != app_windows_.end();) {
132     AppWindow* app_window = *(i++);
133     if (app_window->extension_id() == app_id)
134       app_window->GetBaseWindow()->Close();
135   }
136 }
137
138 AppWindow* AppWindowRegistry::GetAppWindowForRenderViewHost(
139     content::RenderViewHost* render_view_host) const {
140   for (AppWindowList::const_iterator i = app_windows_.begin();
141        i != app_windows_.end();
142        ++i) {
143     if ((*i)->web_contents()->GetRenderViewHost() == render_view_host)
144       return *i;
145   }
146
147   return NULL;
148 }
149
150 AppWindow* AppWindowRegistry::GetAppWindowForNativeWindow(
151     gfx::NativeWindow window) const {
152   for (AppWindowList::const_iterator i = app_windows_.begin();
153        i != app_windows_.end();
154        ++i) {
155     if ((*i)->GetNativeWindow() == window)
156       return *i;
157   }
158
159   return NULL;
160 }
161
162 AppWindow* AppWindowRegistry::GetCurrentAppWindowForApp(
163     const std::string& app_id) const {
164   AppWindow* result = NULL;
165   for (AppWindowList::const_iterator i = app_windows_.begin();
166        i != app_windows_.end();
167        ++i) {
168     if ((*i)->extension()->id() == app_id) {
169       result = *i;
170       if (result->GetBaseWindow()->IsActive())
171         return result;
172     }
173   }
174
175   return result;
176 }
177
178 AppWindow* AppWindowRegistry::GetAppWindowForAppAndKey(
179     const std::string& app_id,
180     const std::string& window_key) const {
181   AppWindow* result = NULL;
182   for (AppWindowList::const_iterator i = app_windows_.begin();
183        i != app_windows_.end();
184        ++i) {
185     if ((*i)->extension()->id() == app_id && (*i)->window_key() == window_key) {
186       result = *i;
187       if (result->GetBaseWindow()->IsActive())
188         return result;
189     }
190   }
191   return result;
192 }
193
194 bool AppWindowRegistry::HadDevToolsAttached(
195     content::RenderViewHost* render_view_host) const {
196   std::string key = GetWindowKeyForRenderViewHost(this, render_view_host);
197   return key.empty() ? false : inspected_windows_.count(key) != 0;
198 }
199
200 // static
201 AppWindow* AppWindowRegistry::GetAppWindowForNativeWindowAnyProfile(
202     gfx::NativeWindow window) {
203   std::vector<content::BrowserContext*> contexts =
204       AppsClient::Get()->GetLoadedBrowserContexts();
205   for (std::vector<content::BrowserContext*>::const_iterator i =
206            contexts.begin();
207        i != contexts.end();
208        ++i) {
209     AppWindowRegistry* registry =
210         Factory::GetForBrowserContext(*i, false /* create */);
211     if (!registry)
212       continue;
213
214     AppWindow* app_window = registry->GetAppWindowForNativeWindow(window);
215     if (app_window)
216       return app_window;
217   }
218
219   return NULL;
220 }
221
222 // static
223 bool AppWindowRegistry::IsAppWindowRegisteredInAnyProfile(
224     int window_type_mask) {
225   std::vector<content::BrowserContext*> contexts =
226       AppsClient::Get()->GetLoadedBrowserContexts();
227   for (std::vector<content::BrowserContext*>::const_iterator i =
228            contexts.begin();
229        i != contexts.end();
230        ++i) {
231     AppWindowRegistry* registry =
232         Factory::GetForBrowserContext(*i, false /* create */);
233     if (!registry)
234       continue;
235
236     const AppWindowList& app_windows = registry->app_windows();
237     if (app_windows.empty())
238       continue;
239
240     if (window_type_mask == 0)
241       return true;
242
243     for (const_iterator j = app_windows.begin(); j != app_windows.end(); ++j) {
244       if ((*j)->window_type() & window_type_mask)
245         return true;
246     }
247   }
248
249   return false;
250 }
251
252 void AppWindowRegistry::OnDevToolsStateChanged(
253     content::DevToolsAgentHost* agent_host,
254     bool attached) {
255   content::RenderViewHost* rvh = agent_host->GetRenderViewHost();
256   // Ignore unrelated notifications.
257   if (!rvh ||
258       rvh->GetSiteInstance()->GetProcess()->GetBrowserContext() != context_)
259     return;
260
261   std::string key = GetWindowKeyForRenderViewHost(this, rvh);
262   if (key.empty())
263     return;
264
265   if (attached)
266     inspected_windows_.insert(key);
267   else
268     inspected_windows_.erase(key);
269 }
270
271 void AppWindowRegistry::AddAppWindowToList(AppWindow* app_window) {
272   const AppWindowList::iterator it =
273       std::find(app_windows_.begin(), app_windows_.end(), app_window);
274   if (it != app_windows_.end())
275     return;
276   app_windows_.push_back(app_window);
277 }
278
279 void AppWindowRegistry::BringToFront(AppWindow* app_window) {
280   const AppWindowList::iterator it =
281       std::find(app_windows_.begin(), app_windows_.end(), app_window);
282   if (it != app_windows_.end())
283     app_windows_.erase(it);
284   app_windows_.push_front(app_window);
285 }
286
287 ///////////////////////////////////////////////////////////////////////////////
288 // Factory boilerplate
289
290 // static
291 AppWindowRegistry* AppWindowRegistry::Factory::GetForBrowserContext(
292     content::BrowserContext* context,
293     bool create) {
294   return static_cast<AppWindowRegistry*>(
295       GetInstance()->GetServiceForBrowserContext(context, create));
296 }
297
298 AppWindowRegistry::Factory* AppWindowRegistry::Factory::GetInstance() {
299   return Singleton<AppWindowRegistry::Factory>::get();
300 }
301
302 AppWindowRegistry::Factory::Factory()
303     : BrowserContextKeyedServiceFactory(
304           "AppWindowRegistry",
305           BrowserContextDependencyManager::GetInstance()) {}
306
307 AppWindowRegistry::Factory::~Factory() {}
308
309 KeyedService* AppWindowRegistry::Factory::BuildServiceInstanceFor(
310     content::BrowserContext* context) const {
311   return new AppWindowRegistry(context);
312 }
313
314 bool AppWindowRegistry::Factory::ServiceIsCreatedWithBrowserContext() const {
315   return true;
316 }
317
318 bool AppWindowRegistry::Factory::ServiceIsNULLWhileTesting() const {
319   return false;
320 }
321
322 content::BrowserContext* AppWindowRegistry::Factory::GetBrowserContextToUse(
323     content::BrowserContext* context) const {
324   return extensions::ExtensionsBrowserClient::Get()->GetOriginalContext(
325       context);
326 }
327
328 }  // namespace extensions