| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/browser/app_window/app_window_registry.h" | 5 #include "extensions/browser/app_window/app_window_registry.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 bool was_shown) { | 38 bool was_shown) { |
| 39 } | 39 } |
| 40 | 40 |
| 41 void AppWindowRegistry::Observer::OnAppWindowActivated(AppWindow* app_window) { | 41 void AppWindowRegistry::Observer::OnAppWindowActivated(AppWindow* app_window) { |
| 42 } | 42 } |
| 43 | 43 |
| 44 AppWindowRegistry::Observer::~Observer() { | 44 AppWindowRegistry::Observer::~Observer() { |
| 45 } | 45 } |
| 46 | 46 |
| 47 AppWindowRegistry::AppWindowRegistry(content::BrowserContext* context) | 47 AppWindowRegistry::AppWindowRegistry(content::BrowserContext* context) |
| 48 : context_(context), | 48 : context_(context) { |
| 49 devtools_callback_(base::Bind(&AppWindowRegistry::OnDevToolsStateChanged, | 49 content::DevToolsAgentHost::AddObserver(this); |
| 50 base::Unretained(this))) { | |
| 51 content::DevToolsAgentHost::AddAgentStateCallback(devtools_callback_); | |
| 52 } | 50 } |
| 53 | 51 |
| 54 AppWindowRegistry::~AppWindowRegistry() { | 52 AppWindowRegistry::~AppWindowRegistry() { |
| 55 content::DevToolsAgentHost::RemoveAgentStateCallback(devtools_callback_); | 53 content::DevToolsAgentHost::RemoveObserver(this); |
| 56 } | 54 } |
| 57 | 55 |
| 58 // static | 56 // static |
| 59 AppWindowRegistry* AppWindowRegistry::Get(content::BrowserContext* context) { | 57 AppWindowRegistry* AppWindowRegistry::Get(content::BrowserContext* context) { |
| 60 return Factory::GetForBrowserContext(context, true /* create */); | 58 return Factory::GetForBrowserContext(context, true /* create */); |
| 61 } | 59 } |
| 62 | 60 |
| 63 void AppWindowRegistry::AddAppWindow(AppWindow* app_window) { | 61 void AppWindowRegistry::AddAppWindow(AppWindow* app_window) { |
| 64 BringToFront(app_window); | 62 BringToFront(app_window); |
| 65 FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowAdded(app_window)); | 63 FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowAdded(app_window)); |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 } | 171 } |
| 174 return result; | 172 return result; |
| 175 } | 173 } |
| 176 | 174 |
| 177 bool AppWindowRegistry::HadDevToolsAttached( | 175 bool AppWindowRegistry::HadDevToolsAttached( |
| 178 content::WebContents* web_contents) const { | 176 content::WebContents* web_contents) const { |
| 179 std::string key = GetWindowKeyForWebContents(web_contents); | 177 std::string key = GetWindowKeyForWebContents(web_contents); |
| 180 return key.empty() ? false : inspected_windows_.count(key) != 0; | 178 return key.empty() ? false : inspected_windows_.count(key) != 0; |
| 181 } | 179 } |
| 182 | 180 |
| 183 void AppWindowRegistry::OnDevToolsStateChanged( | 181 void AppWindowRegistry::DevToolsAgentHostAttached( |
| 184 content::DevToolsAgentHost* agent_host, | 182 content::DevToolsAgentHost* agent_host) { |
| 185 bool attached) { | 183 std::string key = GetWindowKeyForAgentHost(agent_host); |
| 186 content::WebContents* web_contents = agent_host->GetWebContents(); | 184 if (!key.empty()) |
| 187 // Ignore unrelated notifications. | 185 inspected_windows_.insert(key); |
| 188 if (!web_contents || web_contents->GetBrowserContext() != context_) | 186 } |
| 189 return; | |
| 190 | 187 |
| 191 std::string key = GetWindowKeyForWebContents(web_contents); | 188 void AppWindowRegistry::DevToolsAgentHostDetached( |
| 192 if (key.empty()) | 189 content::DevToolsAgentHost* agent_host) { |
| 193 return; | 190 std::string key = GetWindowKeyForAgentHost(agent_host); |
| 194 | 191 if (!key.empty()) |
| 195 if (attached) | |
| 196 inspected_windows_.insert(key); | |
| 197 else | |
| 198 inspected_windows_.erase(key); | 192 inspected_windows_.erase(key); |
| 199 } | 193 } |
| 200 | 194 |
| 201 void AppWindowRegistry::AddAppWindowToList(AppWindow* app_window) { | 195 void AppWindowRegistry::AddAppWindowToList(AppWindow* app_window) { |
| 202 const AppWindowList::iterator it = | 196 const AppWindowList::iterator it = |
| 203 std::find(app_windows_.begin(), app_windows_.end(), app_window); | 197 std::find(app_windows_.begin(), app_windows_.end(), app_window); |
| 204 if (it != app_windows_.end()) | 198 if (it != app_windows_.end()) |
| 205 return; | 199 return; |
| 206 app_windows_.push_back(app_window); | 200 app_windows_.push_back(app_window); |
| 207 } | 201 } |
| 208 | 202 |
| 209 void AppWindowRegistry::BringToFront(AppWindow* app_window) { | 203 void AppWindowRegistry::BringToFront(AppWindow* app_window) { |
| 210 const AppWindowList::iterator it = | 204 const AppWindowList::iterator it = |
| 211 std::find(app_windows_.begin(), app_windows_.end(), app_window); | 205 std::find(app_windows_.begin(), app_windows_.end(), app_window); |
| 212 if (it != app_windows_.end()) | 206 if (it != app_windows_.end()) |
| 213 app_windows_.erase(it); | 207 app_windows_.erase(it); |
| 214 app_windows_.push_front(app_window); | 208 app_windows_.push_front(app_window); |
| 215 } | 209 } |
| 216 | 210 |
| 211 std::string AppWindowRegistry::GetWindowKeyForAgentHost( |
| 212 content::DevToolsAgentHost* agent_host) const { |
| 213 content::WebContents* web_contents = agent_host->GetWebContents(); |
| 214 if (!web_contents || web_contents->GetBrowserContext() != context_) |
| 215 return std::string(); |
| 216 return GetWindowKeyForWebContents(web_contents); |
| 217 } |
| 218 |
| 217 std::string AppWindowRegistry::GetWindowKeyForWebContents( | 219 std::string AppWindowRegistry::GetWindowKeyForWebContents( |
| 218 content::WebContents* web_contents) const { | 220 content::WebContents* web_contents) const { |
| 219 AppWindow* app_window = GetAppWindowForWebContents(web_contents); | 221 AppWindow* app_window = GetAppWindowForWebContents(web_contents); |
| 220 if (!app_window) | 222 if (!app_window) |
| 221 return std::string(); // Not an AppWindow. | 223 return std::string(); // Not an AppWindow. |
| 222 | 224 |
| 223 if (app_window->window_key().empty()) | 225 if (app_window->window_key().empty()) |
| 224 return web_contents->GetURL().possibly_invalid_spec(); | 226 return web_contents->GetURL().possibly_invalid_spec(); |
| 225 | 227 |
| 226 return base::StringPrintf("%s:%s", app_window->extension_id().c_str(), | 228 return base::StringPrintf("%s:%s", app_window->extension_id().c_str(), |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 bool AppWindowRegistry::Factory::ServiceIsNULLWhileTesting() const { | 263 bool AppWindowRegistry::Factory::ServiceIsNULLWhileTesting() const { |
| 262 return false; | 264 return false; |
| 263 } | 265 } |
| 264 | 266 |
| 265 content::BrowserContext* AppWindowRegistry::Factory::GetBrowserContextToUse( | 267 content::BrowserContext* AppWindowRegistry::Factory::GetBrowserContextToUse( |
| 266 content::BrowserContext* context) const { | 268 content::BrowserContext* context) const { |
| 267 return ExtensionsBrowserClient::Get()->GetOriginalContext(context); | 269 return ExtensionsBrowserClient::Get()->GetOriginalContext(context); |
| 268 } | 270 } |
| 269 | 271 |
| 270 } // namespace extensions | 272 } // namespace extensions |
| OLD | NEW |