Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/shell_window_registry.h" | 5 #include "chrome/browser/extensions/shell_window_registry.h" |
| 6 #include "chrome/browser/profiles/profile_dependency_manager.h" | 6 #include "chrome/browser/profiles/profile_dependency_manager.h" |
| 7 #include "chrome/browser/ui/extensions/shell_window.h" | 7 #include "chrome/browser/ui/extensions/shell_window.h" |
| 8 #include "chrome/common/extensions/extension.h" | 8 #include "chrome/common/extensions/extension.h" |
| 9 #include "content/public/browser/notification_types.h" | |
| 9 #include "content/public/browser/render_view_host.h" | 10 #include "content/public/browser/render_view_host.h" |
| 10 #include "content/public/browser/web_contents.h" | 11 #include "content/public/browser/web_contents.h" |
| 11 | 12 |
| 13 namespace { | |
|
benwells
2012/10/23 06:18:48
Nit: blank line after namespace
tapted
2012/10/23 07:04:23
Done.
| |
| 14 std::string GetWindowKeyForRenderViewHost( | |
| 15 const extensions::ShellWindowRegistry* registry, | |
| 16 content::RenderViewHost* render_view_host) { | |
| 17 ShellWindow* shell_window = | |
| 18 registry->GetShellWindowForRenderViewHost(render_view_host); | |
| 19 if (!shell_window) | |
| 20 return std::string(); // Not a ShellWindow. | |
|
benwells
2012/10/23 06:18:48
Blank line after early return.
tapted
2012/10/23 07:04:23
Done.
| |
| 21 if (shell_window->window_key().empty()) | |
| 22 return std::string(); // Not created with an |id| in CreateParams. | |
| 23 | |
| 24 std::string key = shell_window->extension()->id(); | |
|
benwells
2012/10/23 06:18:48
Please put a comment in describing the key format.
tapted
2012/10/23 07:04:23
Done.
| |
| 25 key += shell_window->window_key(); | |
| 26 return key; | |
| 27 } | |
|
benwells
2012/10/23 06:18:48
Nit: blank line between }'s
tapted
2012/10/23 07:04:23
Done.
| |
| 28 } | |
| 29 | |
| 12 namespace extensions { | 30 namespace extensions { |
| 13 | 31 |
| 14 ShellWindowRegistry::ShellWindowRegistry() {} | 32 ShellWindowRegistry::ShellWindowRegistry(Profile* profile) { |
| 33 registrar_.Add(this, content::NOTIFICATION_DEVTOOLS_AGENT_ATTACHED, | |
| 34 content::Source<content::BrowserContext>(profile)); | |
| 35 registrar_.Add(this, content::NOTIFICATION_DEVTOOLS_AGENT_DETACHED, | |
| 36 content::Source<content::BrowserContext>(profile)); | |
| 37 } | |
| 15 | 38 |
| 16 ShellWindowRegistry::~ShellWindowRegistry() {} | 39 ShellWindowRegistry::~ShellWindowRegistry() {} |
| 17 | 40 |
| 18 // static | 41 // static |
| 19 ShellWindowRegistry* ShellWindowRegistry::Get(Profile* profile) { | 42 ShellWindowRegistry* ShellWindowRegistry::Get(Profile* profile) { |
| 20 return Factory::GetForProfile(profile); | 43 return Factory::GetForProfile(profile); |
| 21 } | 44 } |
| 22 | 45 |
| 23 void ShellWindowRegistry::AddShellWindow(ShellWindow* shell_window) { | 46 void ShellWindowRegistry::AddShellWindow(ShellWindow* shell_window) { |
| 24 shell_windows_.insert(shell_window); | 47 shell_windows_.insert(shell_window); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 78 i != shell_windows_.end(); ++i) { | 101 i != shell_windows_.end(); ++i) { |
| 79 if ((*i)->extension()->id() == app_id) | 102 if ((*i)->extension()->id() == app_id) |
| 80 result = *i; | 103 result = *i; |
| 81 if (result->GetBaseWindow()->IsActive()) | 104 if (result->GetBaseWindow()->IsActive()) |
| 82 return result; | 105 return result; |
| 83 } | 106 } |
| 84 | 107 |
| 85 return result; | 108 return result; |
| 86 } | 109 } |
| 87 | 110 |
| 111 bool ShellWindowRegistry::HadDevToolsAttached( | |
| 112 content::RenderViewHost* render_view_host) const { | |
| 113 std::string key = GetWindowKeyForRenderViewHost(this, render_view_host); | |
| 114 return key.empty() ? false : inspected_windows_.count(key); | |
| 115 } | |
| 116 | |
| 117 void ShellWindowRegistry::Observe(int type, | |
| 118 const content::NotificationSource& source, | |
| 119 const content::NotificationDetails& details) { | |
| 120 content::RenderViewHost* render_view_host = | |
| 121 content::Details<content::RenderViewHost>(details).ptr(); | |
| 122 std::string key = GetWindowKeyForRenderViewHost(this, render_view_host); | |
| 123 if (key.empty()) | |
| 124 return; | |
| 125 | |
| 126 if (type == content::NOTIFICATION_DEVTOOLS_AGENT_ATTACHED) { | |
|
benwells
2012/10/23 06:18:48
Use switch here and put NOT_REACHED for default ca
tapted
2012/10/23 07:04:23
Done.
| |
| 127 inspected_windows_.insert(key); | |
| 128 } else if (type == content::NOTIFICATION_DEVTOOLS_AGENT_DETACHED) { | |
| 129 inspected_windows_.erase(key); | |
| 130 } | |
| 131 } | |
| 132 | |
| 88 /////////////////////////////////////////////////////////////////////////////// | 133 /////////////////////////////////////////////////////////////////////////////// |
| 89 // Factory boilerplate | 134 // Factory boilerplate |
| 90 | 135 |
| 91 // static | 136 // static |
| 92 ShellWindowRegistry* ShellWindowRegistry::Factory::GetForProfile( | 137 ShellWindowRegistry* ShellWindowRegistry::Factory::GetForProfile( |
| 93 Profile* profile) { | 138 Profile* profile) { |
| 94 return static_cast<ShellWindowRegistry*>( | 139 return static_cast<ShellWindowRegistry*>( |
| 95 GetInstance()->GetServiceForProfile(profile, true)); | 140 GetInstance()->GetServiceForProfile(profile, true)); |
| 96 } | 141 } |
| 97 | 142 |
| 98 ShellWindowRegistry::Factory* ShellWindowRegistry::Factory::GetInstance() { | 143 ShellWindowRegistry::Factory* ShellWindowRegistry::Factory::GetInstance() { |
| 99 return Singleton<ShellWindowRegistry::Factory>::get(); | 144 return Singleton<ShellWindowRegistry::Factory>::get(); |
| 100 } | 145 } |
| 101 | 146 |
| 102 ShellWindowRegistry::Factory::Factory() | 147 ShellWindowRegistry::Factory::Factory() |
| 103 : ProfileKeyedServiceFactory("ShellWindowRegistry", | 148 : ProfileKeyedServiceFactory("ShellWindowRegistry", |
| 104 ProfileDependencyManager::GetInstance()) { | 149 ProfileDependencyManager::GetInstance()) { |
| 105 } | 150 } |
| 106 | 151 |
| 107 ShellWindowRegistry::Factory::~Factory() { | 152 ShellWindowRegistry::Factory::~Factory() { |
| 108 } | 153 } |
| 109 | 154 |
| 110 ProfileKeyedService* ShellWindowRegistry::Factory::BuildServiceInstanceFor( | 155 ProfileKeyedService* ShellWindowRegistry::Factory::BuildServiceInstanceFor( |
| 111 Profile* profile) const { | 156 Profile* profile) const { |
| 112 return new ShellWindowRegistry(); | 157 return new ShellWindowRegistry(profile); |
| 113 } | 158 } |
| 114 | 159 |
| 115 bool ShellWindowRegistry::Factory::ServiceHasOwnInstanceInIncognito() const { | 160 bool ShellWindowRegistry::Factory::ServiceHasOwnInstanceInIncognito() const { |
| 116 return true; | 161 return true; |
| 117 } | 162 } |
| 118 | 163 |
| 119 bool ShellWindowRegistry::Factory::ServiceIsCreatedWithProfile() const { | 164 bool ShellWindowRegistry::Factory::ServiceIsCreatedWithProfile() const { |
| 120 return true; | 165 return true; |
| 121 } | 166 } |
| 122 | 167 |
| 123 bool ShellWindowRegistry::Factory::ServiceIsNULLWhileTesting() const { | 168 bool ShellWindowRegistry::Factory::ServiceIsNULLWhileTesting() const { |
| 124 return false; | 169 return false; |
| 125 } | 170 } |
| 126 | 171 |
| 127 } // namespace extensions | 172 } // namespace extensions |
| OLD | NEW |