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