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 "ash/shell.h" | |
8 #include "ash/system/tray/system_tray_delegate.h" | |
9 #include "base/logging.h" | |
10 #include "chrome/browser/chrome_notification_types.h" | |
11 #include "chrome/browser/chromeos/profiles/profile_helper.h" | |
12 #include "chrome/browser/profiles/profile.h" | |
13 #include "components/user_manager/user.h" | |
14 #include "components/user_manager/user_manager.h" | |
15 #include "content/public/browser/notification_service.h" | |
16 #include "extensions/browser/api/vpn_provider/vpn_service.h" | |
17 #include "extensions/browser/api/vpn_provider/vpn_service_factory.h" | |
18 #include "extensions/browser/extension_registry.h" | |
19 #include "extensions/common/extension.h" | |
20 #include "extensions/common/extension_set.h" | |
21 #include "extensions/common/permissions/api_permission.h" | |
22 #include "extensions/common/permissions/permissions_data.h" | |
23 #include "third_party/cros_system_api/dbus/service_constants.h" | |
24 #include "ui/base/l10n/l10n_util.h" | |
25 #include "ui/chromeos/strings/grit/ui_chromeos_strings.h" | |
26 | |
27 namespace { | |
28 | |
29 bool IsVPNProvider(const extensions::Extension* extension) { | |
30 return extension->permissions_data()->HasAPIPermission( | |
31 extensions::APIPermission::kVpnProvider); | |
32 } | |
33 | |
34 Profile* GetProfileForPrimaryUser() { | |
35 const user_manager::User* const primary_user = | |
36 user_manager::UserManager::Get()->GetPrimaryUser(); | |
37 if (!primary_user) | |
38 return nullptr; | |
39 | |
40 return chromeos::ProfileHelper::Get()->GetProfileByUser(primary_user); | |
41 } | |
42 | |
43 } // namespace | |
44 | |
45 VPNDelegateChromeOS::VPNDelegateChromeOS() { | |
46 if (user_manager::UserManager::Get()->GetPrimaryUser()) { | |
47 // If a user is logged in, start observing the primary user's extension | |
48 // registry immediately. | |
49 AttachToPrimaryUserExtensionRegistry(); | |
50 } else { | |
51 // If no user is logged in, wait for the first user to log in and become the | |
52 // primary user. | |
53 registrar_.Add(this, | |
54 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, | |
55 content::NotificationService::AllSources()); | |
56 } | |
57 } | |
58 | |
59 VPNDelegateChromeOS::~VPNDelegateChromeOS() { | |
60 if (extension_registry_) | |
61 extension_registry_->RemoveObserver(this); | |
62 } | |
63 | |
64 bool VPNDelegateChromeOS::HaveThirdPartyVPNProviders() const { | |
65 // If there are third-party VPN providers, the list will contain the built-in | |
66 // OpenVPN/L2TP provider and at least one more entry. | |
67 return vpn_providers_.size() > 1; | |
stevenjb
2015/03/12 17:40:45
I don't like relying on this, it seems fragile. I
bartfab (slow)
2015/03/17 12:38:21
Done.
| |
68 } | |
69 | |
70 const std::vector<ash::VPNProvider>& | |
71 VPNDelegateChromeOS::GetVPNProviders() const { | |
72 return vpn_providers_; | |
73 } | |
74 | |
75 void VPNDelegateChromeOS::ShowAddPage(const ash::VPNProvider::Key& key) { | |
76 if (!key.third_party) { | |
77 // Show the "add network" dialog for the built-in OpenVPN/L2TP provider. | |
78 ash::Shell::GetInstance()->system_tray_delegate()->ShowOtherNetworkDialog( | |
79 shill::kTypeVPN); | |
80 return; | |
81 } | |
82 | |
83 Profile* const profile = GetProfileForPrimaryUser(); | |
84 if (!profile) | |
85 return; | |
86 | |
87 // Request that the third-party VPN provider identified by |key.extension_id| | |
88 // show its "add network" dialog. | |
89 chromeos::VpnServiceFactory::GetForBrowserContext(profile)->TriggerAdd( | |
90 key.extension_id); | |
91 } | |
92 | |
93 void VPNDelegateChromeOS::OnExtensionLoaded( | |
94 content::BrowserContext* browser_context, | |
95 const extensions::Extension* extension) { | |
96 if (IsVPNProvider(extension)) | |
97 UpdateVPNProviders(); | |
98 } | |
99 | |
100 void VPNDelegateChromeOS::OnExtensionUnloaded( | |
101 content::BrowserContext* browser_context, | |
102 const extensions::Extension* extension, | |
103 extensions::UnloadedExtensionInfo::Reason reason) { | |
104 if (IsVPNProvider(extension)) | |
105 UpdateVPNProviders(); | |
106 } | |
107 | |
108 void VPNDelegateChromeOS::OnShutdown(extensions::ExtensionRegistry* registry) { | |
109 DCHECK(extension_registry_); | |
110 extension_registry_->RemoveObserver(this); | |
111 extension_registry_ = nullptr; | |
112 } | |
113 | |
114 void VPNDelegateChromeOS::Observe( | |
115 int type, | |
116 const content::NotificationSource& source, | |
117 const content::NotificationDetails& details) { | |
118 // A user has logged in and become the primary user. Stop watching user logins | |
119 // and start observing the primary user's extension registry. | |
120 DCHECK_EQ(chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, type); | |
121 registrar_.RemoveAll(); | |
122 AttachToPrimaryUserExtensionRegistry(); | |
123 | |
124 } | |
125 | |
126 void VPNDelegateChromeOS::UpdateVPNProviders() { | |
127 DCHECK(extension_registry_); | |
128 | |
129 vpn_providers_.clear(); | |
130 const extensions::ExtensionSet& extensions = | |
131 extension_registry_->enabled_extensions(); | |
stevenjb
2015/03/12 17:40:45
nit: Don't really need this intermediate.
bartfab (slow)
2015/03/17 12:38:21
Done.
| |
132 for (const auto& extension : extensions) { | |
133 if (IsVPNProvider(extension.get())) { | |
134 vpn_providers_.push_back( | |
135 ash::VPNProvider(ash::VPNProvider::Key(extension->id()), | |
136 extension->name())); | |
137 } | |
138 } | |
139 // Add the built-in OpenVPN/L2TP provider. | |
140 vpn_providers_.push_back(ash::VPNProvider( | |
141 ash::VPNProvider::Key(), | |
stevenjb
2015/03/12 17:40:45
nit: Either comment 'Use an empty key for built-in
bartfab (slow)
2015/03/17 12:38:21
Done.
| |
142 l10n_util::GetStringUTF8(IDS_NETWORK_VPN_BUILT_IN_PROVIDER))); | |
143 | |
144 NotifyObservers(); | |
145 } | |
146 | |
147 void VPNDelegateChromeOS::AttachToPrimaryUserExtensionRegistry() { | |
148 DCHECK(!extension_registry_); | |
149 extension_registry_ = extensions::ExtensionRegistry::Get( | |
150 GetProfileForPrimaryUser()); | |
151 extension_registry_->AddObserver(this); | |
152 | |
153 UpdateVPNProviders(); | |
154 } | |
OLD | NEW |