| 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 "chrome/browser/ui/ash/vpn_delegate_chromeos.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/location.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/threading/thread_task_runner_handle.h" | |
| 11 #include "chrome/browser/chrome_notification_types.h" | |
| 12 #include "chrome/browser/chromeos/profiles/profile_helper.h" | |
| 13 #include "chrome/browser/profiles/profile.h" | |
| 14 #include "chrome/browser/ui/ash/system_tray_client.h" | |
| 15 #include "components/user_manager/user.h" | |
| 16 #include "components/user_manager/user_manager.h" | |
| 17 #include "content/public/browser/notification_service.h" | |
| 18 #include "content/public/browser/notification_source.h" | |
| 19 #include "extensions/browser/api/vpn_provider/vpn_service.h" | |
| 20 #include "extensions/browser/api/vpn_provider/vpn_service_factory.h" | |
| 21 #include "extensions/browser/extension_registry.h" | |
| 22 #include "extensions/common/extension.h" | |
| 23 #include "extensions/common/extension_set.h" | |
| 24 #include "extensions/common/permissions/api_permission.h" | |
| 25 #include "extensions/common/permissions/permissions_data.h" | |
| 26 #include "third_party/cros_system_api/dbus/shill/dbus-constants.h" | |
| 27 #include "ui/base/l10n/l10n_util.h" | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 bool IsVPNProvider(const extensions::Extension* extension) { | |
| 32 return extension->permissions_data()->HasAPIPermission( | |
| 33 extensions::APIPermission::kVpnProvider); | |
| 34 } | |
| 35 | |
| 36 Profile* GetProfileForPrimaryUser() { | |
| 37 const user_manager::User* const primary_user = | |
| 38 user_manager::UserManager::Get()->GetPrimaryUser(); | |
| 39 if (!primary_user) | |
| 40 return nullptr; | |
| 41 | |
| 42 return chromeos::ProfileHelper::Get()->GetProfileByUser(primary_user); | |
| 43 } | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 VPNDelegateChromeOS::VPNDelegateChromeOS() : weak_factory_(this) { | |
| 48 if (user_manager::UserManager::Get()->GetPrimaryUser()) { | |
| 49 // If a user is logged in, start observing the primary user's extension | |
| 50 // registry immediately. | |
| 51 AttachToPrimaryUserExtensionRegistry(); | |
| 52 } else { | |
| 53 // If no user is logged in, wait until the first user logs in (thus becoming | |
| 54 // the primary user) and a profile is created for that user. | |
| 55 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CREATED, | |
| 56 content::NotificationService::AllSources()); | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 VPNDelegateChromeOS::~VPNDelegateChromeOS() { | |
| 61 if (extension_registry_) | |
| 62 extension_registry_->RemoveObserver(this); | |
| 63 } | |
| 64 | |
| 65 void VPNDelegateChromeOS::ShowAddPage(const std::string& extension_id) { | |
| 66 if (extension_id.empty()) { | |
| 67 // Show the "add network" dialog for the built-in OpenVPN/L2TP provider. | |
| 68 SystemTrayClient::Get()->ShowNetworkCreate(shill::kTypeVPN); | |
| 69 return; | |
| 70 } | |
| 71 | |
| 72 Profile* const profile = GetProfileForPrimaryUser(); | |
| 73 if (!profile) | |
| 74 return; | |
| 75 | |
| 76 // Request that the third-party VPN provider identified by |key.extension_id| | |
| 77 // show its "add network" dialog. | |
| 78 chromeos::VpnServiceFactory::GetForBrowserContext(profile) | |
| 79 ->SendShowAddDialogToExtension(extension_id); | |
| 80 } | |
| 81 | |
| 82 void VPNDelegateChromeOS::OnExtensionLoaded( | |
| 83 content::BrowserContext* browser_context, | |
| 84 const extensions::Extension* extension) { | |
| 85 if (IsVPNProvider(extension)) | |
| 86 UpdateVPNProviders(); | |
| 87 } | |
| 88 | |
| 89 void VPNDelegateChromeOS::OnExtensionUnloaded( | |
| 90 content::BrowserContext* browser_context, | |
| 91 const extensions::Extension* extension, | |
| 92 extensions::UnloadedExtensionInfo::Reason reason) { | |
| 93 if (IsVPNProvider(extension)) | |
| 94 UpdateVPNProviders(); | |
| 95 } | |
| 96 | |
| 97 void VPNDelegateChromeOS::OnShutdown(extensions::ExtensionRegistry* registry) { | |
| 98 DCHECK(extension_registry_); | |
| 99 extension_registry_->RemoveObserver(this); | |
| 100 extension_registry_ = nullptr; | |
| 101 } | |
| 102 | |
| 103 void VPNDelegateChromeOS::Observe(int type, | |
| 104 const content::NotificationSource& source, | |
| 105 const content::NotificationDetails& details) { | |
| 106 DCHECK_EQ(chrome::NOTIFICATION_PROFILE_CREATED, type); | |
| 107 const Profile* const profile = content::Source<Profile>(source).ptr(); | |
| 108 if (!chromeos::ProfileHelper::Get()->IsPrimaryProfile(profile)) { | |
| 109 // If the profile that was just created does not belong to the primary user | |
| 110 // (e.g. login profile), ignore it. | |
| 111 return; | |
| 112 } | |
| 113 | |
| 114 // The first user logged in (thus becoming the primary user) and a profile was | |
| 115 // created for that user. Stop observing profile creation. Wait one message | |
| 116 // loop cycle to allow other code which observes the | |
| 117 // chrome::NOTIFICATION_PROFILE_CREATED notification to finish initializing | |
| 118 // the profile, then start observing the primary user's extension registry. | |
| 119 registrar_.RemoveAll(); | |
| 120 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 121 FROM_HERE, | |
| 122 base::Bind(&VPNDelegateChromeOS::AttachToPrimaryUserExtensionRegistry, | |
| 123 weak_factory_.GetWeakPtr())); | |
| 124 } | |
| 125 | |
| 126 void VPNDelegateChromeOS::UpdateVPNProviders() { | |
| 127 DCHECK(extension_registry_); | |
| 128 | |
| 129 std::vector<ash::VPNProvider> third_party_providers; | |
| 130 for (const auto& extension : extension_registry_->enabled_extensions()) { | |
| 131 if (IsVPNProvider(extension.get())) { | |
| 132 third_party_providers.push_back( | |
| 133 ash::VPNProvider(extension->id(), extension->name())); | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 // Ash adds the built-in OpenVPN/L2TP provider. | |
| 138 SetThirdPartyVpnProviders(third_party_providers); | |
| 139 } | |
| 140 | |
| 141 void VPNDelegateChromeOS::AttachToPrimaryUserExtensionRegistry() { | |
| 142 DCHECK(!extension_registry_); | |
| 143 extension_registry_ = | |
| 144 extensions::ExtensionRegistry::Get(GetProfileForPrimaryUser()); | |
| 145 extension_registry_->AddObserver(this); | |
| 146 | |
| 147 UpdateVPNProviders(); | |
| 148 } | |
| OLD | NEW |