| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/ui/ash/vpn_delegate_chromeos.h" | 5 #include "chrome/browser/ui/ash/vpn_list_forwarder.h" |
| 6 | 6 |
| 7 #include "ash/public/interfaces/vpn_list.mojom.h" |
| 7 #include "base/bind.h" | 8 #include "base/bind.h" |
| 8 #include "base/location.h" | 9 #include "base/location.h" |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "chrome/browser/chrome_notification_types.h" | 12 #include "chrome/browser/chrome_notification_types.h" |
| 12 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 13 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/ui/ash/system_tray_client.h" | 15 #include "chrome/browser/ui/ash/ash_util.h" |
| 15 #include "components/user_manager/user.h" | 16 #include "components/user_manager/user.h" |
| 16 #include "components/user_manager/user_manager.h" | 17 #include "components/user_manager/user_manager.h" |
| 17 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
| 18 #include "content/public/browser/notification_source.h" | 19 #include "content/public/browser/notification_source.h" |
| 19 #include "extensions/browser/api/vpn_provider/vpn_service.h" | 20 #include "content/public/common/service_manager_connection.h" |
| 20 #include "extensions/browser/api/vpn_provider/vpn_service_factory.h" | |
| 21 #include "extensions/browser/extension_registry.h" | 21 #include "extensions/browser/extension_registry.h" |
| 22 #include "extensions/common/extension.h" | 22 #include "extensions/common/extension.h" |
| 23 #include "extensions/common/extension_set.h" | 23 #include "extensions/common/extension_set.h" |
| 24 #include "extensions/common/permissions/api_permission.h" | 24 #include "extensions/common/permissions/api_permission.h" |
| 25 #include "extensions/common/permissions/permissions_data.h" | 25 #include "extensions/common/permissions/permissions_data.h" |
| 26 #include "third_party/cros_system_api/dbus/shill/dbus-constants.h" | 26 #include "services/service_manager/public/cpp/connector.h" |
| 27 #include "ui/base/l10n/l10n_util.h" | |
| 28 | 27 |
| 29 namespace { | 28 namespace { |
| 30 | 29 |
| 31 bool IsVPNProvider(const extensions::Extension* extension) { | 30 bool IsVPNProvider(const extensions::Extension* extension) { |
| 32 return extension->permissions_data()->HasAPIPermission( | 31 return extension->permissions_data()->HasAPIPermission( |
| 33 extensions::APIPermission::kVpnProvider); | 32 extensions::APIPermission::kVpnProvider); |
| 34 } | 33 } |
| 35 | 34 |
| 36 Profile* GetProfileForPrimaryUser() { | 35 Profile* GetProfileForPrimaryUser() { |
| 37 const user_manager::User* const primary_user = | 36 const user_manager::User* const primary_user = |
| 38 user_manager::UserManager::Get()->GetPrimaryUser(); | 37 user_manager::UserManager::Get()->GetPrimaryUser(); |
| 39 if (!primary_user) | 38 if (!primary_user) |
| 40 return nullptr; | 39 return nullptr; |
| 41 | 40 |
| 42 return chromeos::ProfileHelper::Get()->GetProfileByUser(primary_user); | 41 return chromeos::ProfileHelper::Get()->GetProfileByUser(primary_user); |
| 43 } | 42 } |
| 44 | 43 |
| 44 // Connects to the VpnList mojo interface in ash. |
| 45 ash::mojom::VpnListPtr ConnectToVpnList() { |
| 46 ash::mojom::VpnListPtr vpn_list; |
| 47 service_manager::Connector* connector = |
| 48 content::ServiceManagerConnection::GetForProcess()->GetConnector(); |
| 49 // Under mash the VpnList interface is in the ash process. In classic ash |
| 50 // we provide it to ourself. |
| 51 if (chrome::IsRunningInMash()) |
| 52 connector->ConnectToInterface("ash", &vpn_list); |
| 53 else |
| 54 connector->ConnectToInterface("content_browser", &vpn_list); |
| 55 return vpn_list; |
| 56 } |
| 57 |
| 45 } // namespace | 58 } // namespace |
| 46 | 59 |
| 47 VPNDelegateChromeOS::VPNDelegateChromeOS() : weak_factory_(this) { | 60 VpnListForwarder::VpnListForwarder() : weak_factory_(this) { |
| 48 if (user_manager::UserManager::Get()->GetPrimaryUser()) { | 61 if (user_manager::UserManager::Get()->GetPrimaryUser()) { |
| 49 // If a user is logged in, start observing the primary user's extension | 62 // If a user is logged in, start observing the primary user's extension |
| 50 // registry immediately. | 63 // registry immediately. |
| 51 AttachToPrimaryUserExtensionRegistry(); | 64 AttachToPrimaryUserExtensionRegistry(); |
| 52 } else { | 65 } else { |
| 53 // If no user is logged in, wait until the first user logs in (thus becoming | 66 // 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. | 67 // the primary user) and a profile is created for that user. |
| 55 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CREATED, | 68 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CREATED, |
| 56 content::NotificationService::AllSources()); | 69 content::NotificationService::AllSources()); |
| 57 } | 70 } |
| 58 } | 71 } |
| 59 | 72 |
| 60 VPNDelegateChromeOS::~VPNDelegateChromeOS() { | 73 VpnListForwarder::~VpnListForwarder() { |
| 61 if (extension_registry_) | 74 if (extension_registry_) |
| 62 extension_registry_->RemoveObserver(this); | 75 extension_registry_->RemoveObserver(this); |
| 63 } | 76 } |
| 64 | 77 |
| 65 void VPNDelegateChromeOS::ShowAddPage(const std::string& extension_id) { | 78 void VpnListForwarder::OnExtensionLoaded( |
| 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, | 79 content::BrowserContext* browser_context, |
| 84 const extensions::Extension* extension) { | 80 const extensions::Extension* extension) { |
| 85 if (IsVPNProvider(extension)) | 81 if (IsVPNProvider(extension)) |
| 86 UpdateVPNProviders(); | 82 UpdateVPNProviders(); |
| 87 } | 83 } |
| 88 | 84 |
| 89 void VPNDelegateChromeOS::OnExtensionUnloaded( | 85 void VpnListForwarder::OnExtensionUnloaded( |
| 90 content::BrowserContext* browser_context, | 86 content::BrowserContext* browser_context, |
| 91 const extensions::Extension* extension, | 87 const extensions::Extension* extension, |
| 92 extensions::UnloadedExtensionInfo::Reason reason) { | 88 extensions::UnloadedExtensionInfo::Reason reason) { |
| 93 if (IsVPNProvider(extension)) | 89 if (IsVPNProvider(extension)) |
| 94 UpdateVPNProviders(); | 90 UpdateVPNProviders(); |
| 95 } | 91 } |
| 96 | 92 |
| 97 void VPNDelegateChromeOS::OnShutdown(extensions::ExtensionRegistry* registry) { | 93 void VpnListForwarder::OnShutdown(extensions::ExtensionRegistry* registry) { |
| 98 DCHECK(extension_registry_); | 94 DCHECK(extension_registry_); |
| 99 extension_registry_->RemoveObserver(this); | 95 extension_registry_->RemoveObserver(this); |
| 100 extension_registry_ = nullptr; | 96 extension_registry_ = nullptr; |
| 101 } | 97 } |
| 102 | 98 |
| 103 void VPNDelegateChromeOS::Observe(int type, | 99 void VpnListForwarder::Observe(int type, |
| 104 const content::NotificationSource& source, | 100 const content::NotificationSource& source, |
| 105 const content::NotificationDetails& details) { | 101 const content::NotificationDetails& details) { |
| 106 DCHECK_EQ(chrome::NOTIFICATION_PROFILE_CREATED, type); | 102 DCHECK_EQ(chrome::NOTIFICATION_PROFILE_CREATED, type); |
| 107 const Profile* const profile = content::Source<Profile>(source).ptr(); | 103 const Profile* const profile = content::Source<Profile>(source).ptr(); |
| 108 if (!chromeos::ProfileHelper::Get()->IsPrimaryProfile(profile)) { | 104 if (!chromeos::ProfileHelper::Get()->IsPrimaryProfile(profile)) { |
| 109 // If the profile that was just created does not belong to the primary user | 105 // If the profile that was just created does not belong to the primary user |
| 110 // (e.g. login profile), ignore it. | 106 // (e.g. login profile), ignore it. |
| 111 return; | 107 return; |
| 112 } | 108 } |
| 113 | 109 |
| 114 // The first user logged in (thus becoming the primary user) and a profile was | 110 // 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 | 111 // created for that user. Stop observing profile creation. Wait one message |
| 116 // loop cycle to allow other code which observes the | 112 // loop cycle to allow other code which observes the |
| 117 // chrome::NOTIFICATION_PROFILE_CREATED notification to finish initializing | 113 // chrome::NOTIFICATION_PROFILE_CREATED notification to finish initializing |
| 118 // the profile, then start observing the primary user's extension registry. | 114 // the profile, then start observing the primary user's extension registry. |
| 119 registrar_.RemoveAll(); | 115 registrar_.RemoveAll(); |
| 120 base::ThreadTaskRunnerHandle::Get()->PostTask( | 116 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 121 FROM_HERE, | 117 FROM_HERE, |
| 122 base::Bind(&VPNDelegateChromeOS::AttachToPrimaryUserExtensionRegistry, | 118 base::Bind(&VpnListForwarder::AttachToPrimaryUserExtensionRegistry, |
| 123 weak_factory_.GetWeakPtr())); | 119 weak_factory_.GetWeakPtr())); |
| 124 } | 120 } |
| 125 | 121 |
| 126 void VPNDelegateChromeOS::UpdateVPNProviders() { | 122 void VpnListForwarder::UpdateVPNProviders() { |
| 127 DCHECK(extension_registry_); | 123 DCHECK(extension_registry_); |
| 128 | 124 |
| 129 std::vector<ash::VPNProvider> third_party_providers; | 125 std::vector<ash::mojom::ThirdPartyVpnProviderPtr> third_party_providers; |
| 130 for (const auto& extension : extension_registry_->enabled_extensions()) { | 126 for (const auto& extension : extension_registry_->enabled_extensions()) { |
| 131 if (IsVPNProvider(extension.get())) { | 127 if (!IsVPNProvider(extension.get())) |
| 132 third_party_providers.push_back( | 128 continue; |
| 133 ash::VPNProvider(extension->id(), extension->name())); | 129 |
| 134 } | 130 ash::mojom::ThirdPartyVpnProviderPtr provider = |
| 131 ash::mojom::ThirdPartyVpnProvider::New(); |
| 132 provider->name = extension->name(); |
| 133 provider->extension_id = extension->id(); |
| 134 third_party_providers.push_back(std::move(provider)); |
| 135 } | 135 } |
| 136 | 136 |
| 137 // Ash adds the built-in OpenVPN/L2TP provider. | 137 // Ash starts without any third-party providers. If we've never sent one then |
| 138 SetThirdPartyVpnProviders(third_party_providers); | 138 // there's no need to send an empty list. This case commonly occurs on startup |
| 139 // when the user has no third-party VPN extensions installed. |
| 140 if (!sent_providers_ && third_party_providers.empty()) |
| 141 return; |
| 142 |
| 143 // It's rare to install or uninstall VPN provider extensions, so don't bother |
| 144 // caching the interface pointer between calls to this function. |
| 145 ash::mojom::VpnListPtr vpn_list = ConnectToVpnList(); |
| 146 vpn_list->SetThirdPartyVpnProviders(std::move(third_party_providers)); |
| 147 |
| 148 sent_providers_ = true; |
| 139 } | 149 } |
| 140 | 150 |
| 141 void VPNDelegateChromeOS::AttachToPrimaryUserExtensionRegistry() { | 151 void VpnListForwarder::AttachToPrimaryUserExtensionRegistry() { |
| 142 DCHECK(!extension_registry_); | 152 DCHECK(!extension_registry_); |
| 143 extension_registry_ = | 153 extension_registry_ = |
| 144 extensions::ExtensionRegistry::Get(GetProfileForPrimaryUser()); | 154 extensions::ExtensionRegistry::Get(GetProfileForPrimaryUser()); |
| 145 extension_registry_->AddObserver(this); | 155 extension_registry_->AddObserver(this); |
| 146 | 156 |
| 147 UpdateVPNProviders(); | 157 UpdateVPNProviders(); |
| 148 } | 158 } |
| OLD | NEW |