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 <string> | |
8 | |
9 #include "ash/shell.h" | |
10 #include "ash/system/tray/system_tray_delegate.h" | |
11 #include "base/logging.h" | |
12 #include "chrome/browser/chrome_notification_types.h" | |
13 #include "chrome/browser/chromeos/profiles/profile_helper.h" | |
14 #include "chrome/browser/profiles/profile.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 "extensions/browser/api/vpn_provider/vpn_service.h" | |
19 #include "extensions/browser/api/vpn_provider/vpn_service_factory.h" | |
20 #include "extensions/browser/extension_registry.h" | |
21 #include "extensions/common/extension.h" | |
22 #include "extensions/common/extension_set.h" | |
23 #include "extensions/common/permissions/api_permission.h" | |
24 #include "extensions/common/permissions/permissions_data.h" | |
25 #include "third_party/cros_system_api/dbus/service_constants.h" | |
26 #include "ui/base/l10n/l10n_util.h" | |
27 #include "ui/chromeos/strings/grit/ui_chromeos_strings.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() { | |
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 for the first user to log in and become the | |
54 // primary user. | |
55 registrar_.Add(this, | |
56 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, | |
57 content::NotificationService::AllSources()); | |
58 } | |
59 } | |
60 | |
61 VPNDelegateChromeOS::~VPNDelegateChromeOS() { | |
62 if (extension_registry_) | |
63 extension_registry_->RemoveObserver(this); | |
64 } | |
65 | |
66 bool VPNDelegateChromeOS::HaveThirdPartyVPNProviders() const { | |
67 // If there are third-party VPN providers, the list will contain the built-in | |
68 // OpenVPN/L2TP provider and at least one more entry. | |
69 return vpn_providers_.size() > 1; | |
70 } | |
71 | |
72 const std::vector<ash::VPNProvider>& | |
73 VPNDelegateChromeOS::GetVPNProviders() const { | |
74 return vpn_providers_; | |
75 } | |
76 | |
77 void VPNDelegateChromeOS::ShowAddPage(const std::string& id) { | |
78 if (id.empty()) { | |
79 // If |id| is an empty string, show the "add network" dialog for the | |
80 // built-in OpenVPN/L2TP provider. | |
81 ash::Shell::GetInstance()->system_tray_delegate()->ShowOtherNetworkDialog( | |
82 shill::kTypeVPN); | |
83 return; | |
84 } | |
85 | |
86 Profile* const profile = GetProfileForPrimaryUser(); | |
87 if (!profile) | |
88 return; | |
89 | |
90 // Request that the third-party VPN provider identified by |id| show its "add | |
91 // network" dialog. | |
92 chromeos::VpnServiceFactory::GetForBrowserContext(profile)->TriggerAdd(id); | |
93 } | |
94 | |
95 void VPNDelegateChromeOS::OnExtensionLoaded( | |
96 content::BrowserContext* browser_context, | |
97 const extensions::Extension* extension) { | |
98 if (IsVPNProvider(extension)) | |
99 UpdateVPNProviders(); | |
100 } | |
101 | |
102 void VPNDelegateChromeOS::OnExtensionUnloaded( | |
103 content::BrowserContext* browser_context, | |
104 const extensions::Extension* extension, | |
105 extensions::UnloadedExtensionInfo::Reason reason) { | |
106 if (IsVPNProvider(extension)) | |
107 UpdateVPNProviders(); | |
108 } | |
109 | |
110 void VPNDelegateChromeOS::OnShutdown(extensions::ExtensionRegistry* registry) { | |
111 DCHECK(extension_registry_); | |
112 extension_registry_->RemoveObserver(this); | |
113 extension_registry_ = nullptr; | |
114 } | |
115 | |
116 void VPNDelegateChromeOS::Observe( | |
117 int type, | |
118 const content::NotificationSource& source, | |
119 const content::NotificationDetails& details) { | |
120 // A user has logged in and become the primary user. Stop watching user logins | |
121 // and start observing the primary user's extension registry. | |
122 DCHECK_EQ(chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, type); | |
123 registrar_.RemoveAll(); | |
124 AttachToPrimaryUserExtensionRegistry(); | |
125 | |
126 } | |
127 | |
128 void VPNDelegateChromeOS::UpdateVPNProviders() { | |
129 DCHECK(extension_registry_); | |
130 | |
131 vpn_providers_.clear(); | |
132 const extensions::ExtensionSet& extensions = | |
133 extension_registry_->enabled_extensions(); | |
134 for (const auto& extension : extensions) { | |
135 if (IsVPNProvider(extension.get())) { | |
136 vpn_providers_.push_back(ash::VPNProvider(extension->name(), | |
137 extension->id())); | |
138 } | |
139 } | |
140 // Add the built-in OpenVPN/L2TP provider. Its ID is an empty string. | |
141 vpn_providers_.push_back(ash::VPNProvider( | |
142 l10n_util::GetStringUTF8(IDS_NETWORK_VPN_BUILT_IN_PROVIDER), | |
143 std::string())); | |
stevenjb
2015/03/11 16:52:52
If we are including OpenVPN/L2TP here, I think it
bartfab (slow)
2015/03/11 17:27:04
This is how I had implemented it at first but I ra
| |
144 | |
145 NotifyObservers(); | |
146 } | |
147 | |
148 void VPNDelegateChromeOS::AttachToPrimaryUserExtensionRegistry() { | |
149 DCHECK(!extension_registry_); | |
150 extension_registry_ = extensions::ExtensionRegistry::Get( | |
151 GetProfileForPrimaryUser()); | |
152 extension_registry_->AddObserver(this); | |
153 | |
154 UpdateVPNProviders(); | |
155 } | |
OLD | NEW |