Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / extensions / browser / app_window / 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 "extensions/browser/app_window/app_window_registry.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "components/keyed_service/content/browser_context_dependency_manager.h"
11 #include "content/public/browser/browser_context.h"
12 #include "content/public/browser/devtools_agent_host.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/app_window/app_window.h"
18 #include "extensions/browser/app_window/native_app_window.h"
19 #include "extensions/browser/extensions_browser_client.h"
20 #include "extensions/common/extension.h"
21
22 namespace extensions {
23
24 namespace {
25
26 // Create a key that identifies a AppWindow in a RenderViewHost across App
27 // reloads. If the window was given an id in CreateParams, the key is the
28 // extension id, a colon separator, and the AppWindow's |id|. If there is no
29 // |id|, the chrome-extension://extension-id/page.html URL will be used. If the
30 // RenderViewHost is not for a AppWindow, return an empty string.
31 std::string GetWindowKeyForRenderViewHost(
32     const AppWindowRegistry* registry,
33     content::RenderViewHost* render_view_host) {
34   AppWindow* app_window =
35       registry->GetAppWindowForRenderViewHost(render_view_host);
36   if (!app_window)
37     return std::string();  // Not a AppWindow.
38
39   if (app_window->window_key().empty())
40     return app_window->web_contents()->GetURL().possibly_invalid_spec();
41
42   std::string key = app_window->extension_id();
43   key += ':';
44   key += app_window->window_key();
45   return key;
46 }
47
48 }  // namespace
49
50 void AppWindowRegistry::Observer::OnAppWindowAdded(AppWindow* app_window) {
51 }
52
53 void AppWindowRegistry::Observer::OnAppWindowIconChanged(
54     AppWindow* app_window) {
55 }
56
57 void AppWindowRegistry::Observer::OnAppWindowRemoved(AppWindow* app_window) {
58 }
59
60 void AppWindowRegistry::Observer::OnAppWindowHidden(AppWindow* app_window) {
61 }
62
63 void AppWindowRegistry::Observer::OnAppWindowShown(AppWindow* app_window,
64                                                    bool was_shown) {
65 }
66
67 AppWindowRegistry::Observer::~Observer() {
68 }
69
70 AppWindowRegistry::AppWindowRegistry(content::BrowserContext* context)
71     : context_(context),
72       devtools_callback_(base::Bind(&AppWindowRegistry::OnDevToolsStateChanged,
73                                     base::Unretained(this))) {
74   content::DevToolsAgentHost::AddAgentStateCallback(devtools_callback_);
75 }
76
77 AppWindowRegistry::~AppWindowRegistry() {
78   content::DevToolsAgentHost::RemoveAgentStateCallback(devtools_callback_);
79 }
80
81 // static
82 AppWindowRegistry* AppWindowRegistry::Get(content::BrowserContext* context) {
83   return Factory::GetForBrowserContext(context, true /* create */);
84 }
85
86 void AppWindowRegistry::AddAppWindow(AppWindow* app_window) {
87   BringToFront(app_window);
88   FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowAdded(app_window));
89 }
90
91 void AppWindowRegistry::AppWindowIconChanged(AppWindow* app_window) {
92   AddAppWindowToList(app_window);
93   FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowIconChanged(app_window));
94 }
95
96 void AppWindowRegistry::AppWindowActivated(AppWindow* app_window) {
97   BringToFront(app_window);
98 }
99
100 void AppWindowRegistry::AppWindowHidden(AppWindow* app_window) {
101   FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowHidden(app_window));
102 }
103
104 void AppWindowRegistry::AppWindowShown(AppWindow* app_window, bool was_hidden) {
105   FOR_EACH_OBSERVER(Observer, observers_,
106                     OnAppWindowShown(app_window, was_hidden));
107 }
108
109 void AppWindowRegistry::RemoveAppWindow(AppWindow* app_window) {
110   const AppWindowList::iterator it =
111       std::find(app_windows_.begin(), app_windows_.end(), app_window);
112   if (it != app_windows_.end())
113     app_windows_.erase(it);
114   FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowRemoved(app_window));
115 }
116
117 void AppWindowRegistry::AddObserver(Observer* observer) {
118   observers_.AddObserver(observer);
119 }
120
121 void AppWindowRegistry::RemoveObserver(Observer* observer) {
122   observers_.RemoveObserver(observer);
123 }
124
125 AppWindowRegistry::AppWindowList AppWindowRegistry::GetAppWindowsForApp(
126     const std::string& app_id) const {
127   AppWindowList app_windows;
128   for (AppWindowList::const_iterator i = app_windows_.begin();
129        i != app_windows_.end();
130        ++i) {
131     if ((*i)->extension_id() == app_id)
132       app_windows.push_back(*i);
133   }
134   return app_windows;
135 }
136
137 void AppWindowRegistry::CloseAllAppWindowsForApp(const std::string& app_id) {
138   const AppWindowList windows = GetAppWindowsForApp(app_id);
139   for (AppWindowRegistry::const_iterator it = windows.begin();
140        it != windows.end();
141        ++it) {
142     (*it)->GetBaseWindow()->Close();
143   }
144 }
145
146 AppWindow* AppWindowRegistry::GetAppWindowForRenderViewHost(
147     content::RenderViewHost* render_view_host) const {
148   for (AppWindowList::const_iterator i = app_windows_.begin();
149        i != app_windows_.end();
150        ++i) {
151     if ((*i)->web_contents()->GetRenderViewHost() == render_view_host)
152       return *i;
153   }
154
155   return NULL;
156 }
157
158 AppWindow* AppWindowRegistry::GetAppWindowForNativeWindow(
159     gfx::NativeWindow window) const {
160   for (AppWindowList::const_iterator i = app_windows_.begin();
161        i != app_windows_.end();
162        ++i) {
163     if ((*i)->GetNativeWindow() == window)
164       return *i;
165   }
166
167   return NULL;
168 }
169
170 AppWindow* AppWindowRegistry::GetCurrentAppWindowForApp(
171     const std::string& app_id) const {
172   AppWindow* result = NULL;
173   for (AppWindowList::const_iterator i = app_windows_.begin();
174        i != app_windows_.end();
175        ++i) {
176     if ((*i)->extension_id() == app_id) {
177       result = *i;
178       if (result->GetBaseWindow()->IsActive())
179         return result;
180     }
181   }
182
183   return result;
184 }
185
186 AppWindow* AppWindowRegistry::GetAppWindowForAppAndKey(
187     const std::string& app_id,
188     const std::string& window_key) const {
189   AppWindow* result = NULL;
190   for (AppWindowList::const_iterator i = app_windows_.begin();
191        i != app_windows_.end();
192        ++i) {
193     if ((*i)->extension_id() == app_id && (*i)->window_key() == window_key) {
194       result = *i;
195       if (result->GetBaseWindow()->IsActive())
196         return result;
197     }
198   }
199   return result;
200 }
201
202 bool AppWindowRegistry::HadDevToolsAttached(
203     content::RenderViewHost* render_view_host) const {
204   std::string key = GetWindowKeyForRenderViewHost(this, render_view_host);
205   return key.empty() ? false : inspected_windows_.count(key) != 0;
206 }
207
208 void AppWindowRegistry::OnDevToolsStateChanged(
209     content::DevToolsAgentHost* agent_host,
210     bool attached) {
211   content::WebContents* web_contents = agent_host->GetWebContents();
212   // Ignore unrelated notifications.
213   if (!web_contents || web_contents->GetBrowserContext() != context_)
214     return;
215
216   std::string key =
217       GetWindowKeyForRenderViewHost(this, web_contents->GetRenderViewHost());
218   if (key.empty())
219     return;
220
221   if (attached)
222     inspected_windows_.insert(key);
223   else
224     inspected_windows_.erase(key);
225 }
226
227 void AppWindowRegistry::AddAppWindowToList(AppWindow* app_window) {
228   const AppWindowList::iterator it =
229       std::find(app_windows_.begin(), app_windows_.end(), app_window);
230   if (it != app_windows_.end())
231     return;
232   app_windows_.push_back(app_window);
233 }
234
235 void AppWindowRegistry::BringToFront(AppWindow* app_window) {
236   const AppWindowList::iterator it =
237       std::find(app_windows_.begin(), app_windows_.end(), app_window);
238   if (it != app_windows_.end())
239     app_windows_.erase(it);
240   app_windows_.push_front(app_window);
241 }
242
243 ///////////////////////////////////////////////////////////////////////////////
244 // Factory boilerplate
245
246 // static
247 AppWindowRegistry* AppWindowRegistry::Factory::GetForBrowserContext(
248     content::BrowserContext* context,
249     bool create) {
250   return static_cast<AppWindowRegistry*>(
251       GetInstance()->GetServiceForBrowserContext(context, create));
252 }
253
254 AppWindowRegistry::Factory* AppWindowRegistry::Factory::GetInstance() {
255   return Singleton<AppWindowRegistry::Factory>::get();
256 }
257
258 AppWindowRegistry::Factory::Factory()
259     : BrowserContextKeyedServiceFactory(
260           "AppWindowRegistry",
261           BrowserContextDependencyManager::GetInstance()) {}
262
263 AppWindowRegistry::Factory::~Factory() {}
264
265 KeyedService* AppWindowRegistry::Factory::BuildServiceInstanceFor(
266     content::BrowserContext* context) const {
267   return new AppWindowRegistry(context);
268 }
269
270 bool AppWindowRegistry::Factory::ServiceIsCreatedWithBrowserContext() const {
271   return true;
272 }
273
274 bool AppWindowRegistry::Factory::ServiceIsNULLWhileTesting() const {
275   return false;
276 }
277
278 content::BrowserContext* AppWindowRegistry::Factory::GetBrowserContextToUse(
279     content::BrowserContext* context) const {
280   return ExtensionsBrowserClient::Get()->GetOriginalContext(context);
281 }
282
283 }  // namespace extensions