Index: chrome/browser/ui/ash/vpn_delegate_chromeos.cc |
diff --git a/chrome/browser/ui/ash/vpn_delegate_chromeos.cc b/chrome/browser/ui/ash/vpn_delegate_chromeos.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1f29f746e0888d9ac83bbe88b70f3ebaf1b66a05 |
--- /dev/null |
+++ b/chrome/browser/ui/ash/vpn_delegate_chromeos.cc |
@@ -0,0 +1,164 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/ui/ash/vpn_delegate_chromeos.h" |
+ |
+#include <vector> |
+ |
+#include "ash/shell.h" |
+#include "ash/system/tray/system_tray_delegate.h" |
+#include "base/logging.h" |
+#include "chrome/browser/chrome_notification_types.h" |
+#include "chrome/browser/chromeos/profiles/profile_helper.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "components/user_manager/user.h" |
+#include "components/user_manager/user_manager.h" |
+#include "content/public/browser/notification_service.h" |
+#include "extensions/browser/api/vpn_provider/vpn_service.h" |
+#include "extensions/browser/api/vpn_provider/vpn_service_factory.h" |
+#include "extensions/browser/extension_registry.h" |
+#include "extensions/common/extension.h" |
+#include "extensions/common/extension_set.h" |
+#include "extensions/common/permissions/api_permission.h" |
+#include "extensions/common/permissions/permissions_data.h" |
+#include "third_party/cros_system_api/dbus/service_constants.h" |
+ |
+namespace { |
+ |
+bool IsVPNProvider(const extensions::Extension* extension) { |
+ return extension->permissions_data()->HasAPIPermission( |
+ extensions::APIPermission::kVpnProvider); |
+} |
+ |
+Profile* GetProfileForPrimaryUser() { |
+ const user_manager::User* const primary_user = |
+ user_manager::UserManager::Get()->GetPrimaryUser(); |
+ if (!primary_user) |
+ return nullptr; |
+ |
+ return chromeos::ProfileHelper::Get()->GetProfileByUser(primary_user); |
+} |
+ |
+const extensions::ExtensionSet* GetExtensionsForPrimaryUser() { |
+ Profile* const profile = GetProfileForPrimaryUser(); |
+ if (!profile) |
+ return nullptr; |
+ |
+ const extensions::ExtensionRegistry* const extension_registry = |
+ extensions::ExtensionRegistry::Get(profile); |
+ if (!extension_registry) |
+ return nullptr; |
+ |
+ return &extension_registry->enabled_extensions(); |
+} |
+ |
+} // namespace |
+ |
+VPNDelegateChromeOS::VPNDelegateChromeOS() { |
+ if (user_manager::UserManager::Get()->GetPrimaryUser()) { |
+ // If a user is logged in, start observing the primary user's extension |
+ // registry immediately. |
+ AttachToPrimaryUserExtensionRegistry(); |
+ } else { |
+ // If no user is logged in, wait for the first user to log in and become the |
+ // primary user. |
+ registrar_.Add(this, |
+ chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, |
+ content::NotificationService::AllSources()); |
+ } |
+} |
+ |
+VPNDelegateChromeOS::~VPNDelegateChromeOS() { |
+ if (extension_registry_) |
+ extension_registry_->RemoveObserver(this); |
+} |
+ |
+bool VPNDelegateChromeOS::HaveThirdPartyVPNProviders() const { |
+ const extensions::ExtensionSet* const extensions = |
+ GetExtensionsForPrimaryUser(); |
+ if (!extensions) |
+ return false; |
+ |
+ for (const auto& extension : *extensions) { |
+ if (IsVPNProvider(extension.get())) |
+ return true; |
+ } |
+ |
+ return false; |
+} |
+ |
+std::vector<ash::VPNProvider> |
+VPNDelegateChromeOS::GetThirdPartyVPNProviders() const { |
+ const extensions::ExtensionSet* const extensions = |
+ GetExtensionsForPrimaryUser(); |
+ if (!extensions) |
+ return std::vector<ash::VPNProvider>(); |
+ |
+ std::vector<ash::VPNProvider> third_party_vpn_providers; |
+ for (const auto& extension : *extensions) { |
+ if (IsVPNProvider(extension.get())) { |
+ third_party_vpn_providers.push_back(ash::VPNProvider(extension->name(), |
+ extension->id())); |
+ } |
+ } |
+ |
+ return third_party_vpn_providers; |
+} |
+ |
+void VPNDelegateChromeOS::ShowAddPage(const std::string& id) { |
+ if (id.empty()) { |
+ // If |id| is an empty string, show the built-in dialog for adding an |
+ // OpenVPN or L2TP VPN. |
+ ash::Shell::GetInstance()->system_tray_delegate()->ShowOtherNetworkDialog( |
+ shill::kTypeVPN); |
+ return; |
+ } |
+ |
+ Profile* const profile = GetProfileForPrimaryUser(); |
+ if (!profile) |
+ return; |
+ |
+ // Request that the third-party VPN provider identified by |id| show its "add |
+ // network" dialog. |
+ chromeos::VpnServiceFactory::GetForBrowserContext(profile)->TriggerAdd(id); |
+} |
+ |
+void VPNDelegateChromeOS::OnExtensionLoaded( |
+ content::BrowserContext* browser_context, |
+ const extensions::Extension* extension) { |
+ NotifyObservers(); |
Daniel Erat
2015/03/09 19:46:19
do you want to call IsVPNProvider() here to avoid
bartfab (slow)
2015/03/09 21:04:23
Excellent idea. Done.
|
+} |
+ |
+void VPNDelegateChromeOS::OnExtensionUnloaded( |
+ content::BrowserContext* browser_context, |
+ const extensions::Extension* extension, |
+ extensions::UnloadedExtensionInfo::Reason reason) { |
+ NotifyObservers(); |
Daniel Erat
2015/03/09 19:46:19
same here
bartfab (slow)
2015/03/09 21:04:23
Done.
|
+} |
+ |
+void VPNDelegateChromeOS::OnShutdown(extensions::ExtensionRegistry* registry) { |
+ DCHECK(extension_registry_); |
+ extension_registry_->RemoveObserver(this); |
+ extension_registry_ = nullptr; |
+} |
+ |
+void VPNDelegateChromeOS::Observe( |
+ int type, |
+ const content::NotificationSource& source, |
+ const content::NotificationDetails& details) { |
+ // A user has logged in and become the primary user. Stop watching user logins |
+ // and start observing the primary user's extension registry. |
+ DCHECK_EQ(chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, type); |
+ registrar_.RemoveAll(); |
+ AttachToPrimaryUserExtensionRegistry(); |
+ |
+} |
+ |
+void VPNDelegateChromeOS::AttachToPrimaryUserExtensionRegistry() { |
+ DCHECK(!extension_registry_); |
+ extension_registry_ = extensions::ExtensionRegistry::Get( |
+ chromeos::ProfileHelper::Get()->GetProfileByUser( |
+ user_manager::UserManager::Get()->GetPrimaryUser())); |
+ extension_registry_->AddObserver(this); |
+} |