Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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/api/socket/app_firewall_hole_manager.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/stl_util.h" | |
| 11 #include "components/keyed_service/content/browser_context_dependency_manager.h" | |
| 12 #include "content/public/browser/browser_context.h" | |
| 13 #include "extensions/browser/app_window/app_window.h" | |
| 14 | |
| 15 using chromeos::FirewallHole; | |
| 16 using content::BrowserContext; | |
| 17 | |
| 18 namespace extensions { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 class AppFirewallHoleManagerFactory : public BrowserContextKeyedServiceFactory { | |
| 23 public: | |
| 24 static AppFirewallHoleManager* GetForBrowserContext(BrowserContext* context, | |
| 25 bool create) { | |
| 26 return static_cast<AppFirewallHoleManager*>( | |
| 27 GetInstance()->GetServiceForBrowserContext(context, create)); | |
| 28 } | |
| 29 | |
| 30 static AppFirewallHoleManagerFactory* GetInstance() { | |
| 31 return Singleton<AppFirewallHoleManagerFactory>::get(); | |
| 32 } | |
| 33 | |
| 34 AppFirewallHoleManagerFactory() | |
| 35 : BrowserContextKeyedServiceFactory( | |
| 36 "AppFirewallHoleManager", | |
| 37 BrowserContextDependencyManager::GetInstance()) { | |
| 38 DependsOn(AppWindowRegistry::Factory::GetInstance()); | |
| 39 } | |
| 40 | |
| 41 ~AppFirewallHoleManagerFactory() override {} | |
| 42 | |
| 43 private: | |
| 44 // BrowserContextKeyedServiceFactory | |
| 45 KeyedService* BuildServiceInstanceFor( | |
| 46 BrowserContext* context) const override { | |
| 47 return new AppFirewallHoleManager(context); | |
| 48 } | |
| 49 | |
| 50 BrowserContext* GetBrowserContextToUse( | |
| 51 BrowserContext* context) const override { | |
| 52 return context; | |
| 53 } | |
| 54 }; | |
| 55 | |
| 56 bool HasVisibleAppWindows(BrowserContext* context, | |
| 57 const std::string& extension_id) { | |
| 58 AppWindowRegistry* registry = AppWindowRegistry::Get(context); | |
| 59 | |
| 60 for (const AppWindow* window : registry->GetAppWindowsForApp(extension_id)) { | |
| 61 if (!window->is_hidden()) { | |
| 62 return true; | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 return false; | |
| 67 } | |
| 68 | |
| 69 } // namespace | |
| 70 | |
| 71 AppFirewallHole::~AppFirewallHole() { | |
|
rpaquay
2015/03/23 18:29:07
nit: Move destructor after constructor?
Reilly Grant (use Gerrit)
2015/03/23 20:09:31
This matches the order in the header file.
| |
| 72 manager_->Close(this); | |
| 73 } | |
| 74 | |
| 75 AppFirewallHole::AppFirewallHole(AppFirewallHoleManager* manager, | |
| 76 PortType type, | |
| 77 uint16_t port, | |
| 78 const std::string& extension_id) | |
| 79 : type_(type), | |
| 80 port_(port), | |
| 81 extension_id_(extension_id), | |
| 82 manager_(manager), | |
| 83 weak_factory_(this) { | |
| 84 } | |
| 85 | |
| 86 void AppFirewallHole::SetVisible(bool app_visible) { | |
| 87 app_visible_ = app_visible; | |
| 88 if (app_visible_) { | |
| 89 if (!firewall_hole_) { | |
| 90 FirewallHole::Open(type_, port_, "" /* all interfaces */, | |
| 91 base::Bind(&AppFirewallHole::OnFirewallHoleOpened, | |
| 92 weak_factory_.GetWeakPtr())); | |
| 93 } | |
| 94 } else { | |
| 95 firewall_hole_.reset(nullptr); | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 void AppFirewallHole::OnFirewallHoleOpened( | |
| 100 scoped_ptr<FirewallHole> firewall_hole) { | |
| 101 if (app_visible_) { | |
| 102 DCHECK(!firewall_hole_); | |
| 103 firewall_hole_ = firewall_hole.Pass(); | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 AppFirewallHoleManager::AppFirewallHoleManager(BrowserContext* context) | |
| 108 : context_(context), observer_(this) { | |
| 109 observer_.Add(AppWindowRegistry::Get(context)); | |
| 110 } | |
| 111 | |
| 112 AppFirewallHoleManager::~AppFirewallHoleManager() { | |
| 113 STLDeleteValues(&tracked_holes_); | |
| 114 } | |
| 115 | |
| 116 AppFirewallHoleManager* AppFirewallHoleManager::Get(BrowserContext* context) { | |
| 117 return AppFirewallHoleManagerFactory::GetForBrowserContext(context, true); | |
| 118 } | |
| 119 | |
| 120 AppFirewallHole* AppFirewallHoleManager::Open(AppFirewallHole::PortType type, | |
| 121 uint16_t port, | |
| 122 const std::string& extension_id) { | |
| 123 AppFirewallHole* hole = new AppFirewallHole(this, type, port, extension_id); | |
| 124 tracked_holes_.insert(std::make_pair(extension_id, hole)); | |
| 125 if (HasVisibleAppWindows(context_, extension_id)) { | |
| 126 hole->SetVisible(true); | |
| 127 } | |
| 128 return hole; | |
| 129 } | |
| 130 | |
| 131 void AppFirewallHoleManager::Close(AppFirewallHole* hole) { | |
| 132 auto range = tracked_holes_.equal_range(hole->extension_id()); | |
| 133 for (auto iter = range.first; iter != range.second; ++iter) { | |
| 134 if (iter->second == hole) { | |
| 135 tracked_holes_.erase(iter); | |
| 136 return; | |
| 137 } | |
| 138 } | |
| 139 NOTREACHED(); | |
| 140 } | |
| 141 | |
| 142 void AppFirewallHoleManager::OnAppWindowRemoved(AppWindow* app_window) { | |
| 143 OnAppWindowHidden(app_window); | |
| 144 } | |
| 145 | |
| 146 void AppFirewallHoleManager::OnAppWindowHidden(AppWindow* app_window) { | |
| 147 DCHECK(context_ == app_window->browser_context()); | |
| 148 if (!HasVisibleAppWindows(context_, app_window->extension_id())) { | |
| 149 const auto& range = tracked_holes_.equal_range(app_window->extension_id()); | |
| 150 for (auto iter = range.first; iter != range.second; ++iter) { | |
| 151 iter->second->SetVisible(false); | |
| 152 } | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 void AppFirewallHoleManager::OnAppWindowShown(AppWindow* app_window, | |
| 157 bool was_hidden) { | |
| 158 const auto& range = tracked_holes_.equal_range(app_window->extension_id()); | |
| 159 for (auto iter = range.first; iter != range.second; ++iter) { | |
| 160 iter->second->SetVisible(true); | |
| 161 } | |
| 162 } | |
| 163 | |
| 164 } // namespace extensions | |
| OLD | NEW |