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 <vector> | |
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 | |
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 const extensions::ExtensionSet* GetExtensionsForPrimaryUser() { | |
44 Profile* const profile = GetProfileForPrimaryUser(); | |
45 if (!profile) | |
46 return nullptr; | |
47 | |
48 const extensions::ExtensionRegistry* const extension_registry = | |
49 extensions::ExtensionRegistry::Get(profile); | |
50 if (!extension_registry) | |
51 return nullptr; | |
52 | |
53 return &extension_registry->enabled_extensions(); | |
54 } | |
55 | |
56 } // namespace | |
57 | |
58 VPNDelegateChromeOS::VPNDelegateChromeOS() { | |
59 if (user_manager::UserManager::Get()->GetPrimaryUser()) { | |
60 // If a user is logged in, start observing the primary user's extension | |
61 // registry immediately. | |
62 AttachToPrimaryUserExtensionRegistry(); | |
63 } else { | |
64 // If no user is logged in, wait for the first user to log in and become the | |
65 // primary user. | |
66 registrar_.Add(this, | |
67 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, | |
68 content::NotificationService::AllSources()); | |
69 } | |
70 } | |
71 | |
72 VPNDelegateChromeOS::~VPNDelegateChromeOS() { | |
73 if (extension_registry_) | |
74 extension_registry_->RemoveObserver(this); | |
75 } | |
76 | |
77 bool VPNDelegateChromeOS::HaveThirdPartyVPNProviders() const { | |
78 const extensions::ExtensionSet* const extensions = | |
79 GetExtensionsForPrimaryUser(); | |
80 if (!extensions) | |
81 return false; | |
82 | |
83 for (const auto& extension : *extensions) { | |
84 if (IsVPNProvider(extension.get())) | |
85 return true; | |
86 } | |
87 | |
88 return false; | |
89 } | |
90 | |
91 std::vector<ash::VPNProvider> | |
92 VPNDelegateChromeOS::GetThirdPartyVPNProviders() const { | |
93 const extensions::ExtensionSet* const extensions = | |
94 GetExtensionsForPrimaryUser(); | |
95 if (!extensions) | |
96 return std::vector<ash::VPNProvider>(); | |
97 | |
98 std::vector<ash::VPNProvider> third_party_vpn_providers; | |
99 for (const auto& extension : *extensions) { | |
100 if (IsVPNProvider(extension.get())) { | |
101 third_party_vpn_providers.push_back(ash::VPNProvider(extension->name(), | |
102 extension->id())); | |
103 } | |
104 } | |
105 | |
106 return third_party_vpn_providers; | |
107 } | |
108 | |
109 void VPNDelegateChromeOS::ShowAddPage(const std::string& id) { | |
110 if (id.empty()) { | |
111 // If |id| is an empty string, show the built-in dialog for adding an | |
112 // OpenVPN or L2TP VPN. | |
113 ash::Shell::GetInstance()->system_tray_delegate()->ShowOtherNetworkDialog( | |
114 shill::kTypeVPN); | |
115 return; | |
116 } | |
117 | |
118 Profile* const profile = GetProfileForPrimaryUser(); | |
119 if (!profile) | |
120 return; | |
121 | |
122 // Request that the third-party VPN provider identified by |id| show its "add | |
123 // network" dialog. | |
124 chromeos::VpnServiceFactory::GetForBrowserContext(profile)->TriggerAdd(id); | |
125 } | |
126 | |
127 void VPNDelegateChromeOS::OnExtensionLoaded( | |
128 content::BrowserContext* browser_context, | |
129 const extensions::Extension* extension) { | |
130 NotifyObservers(); | |
Daniel Erat
2015/03/09 19:46:19
do you want to call IsVPNProvider() here to avoid
bartfab (slow)
2015/03/09 21:04:23
Excellent idea. Done.
| |
131 } | |
132 | |
133 void VPNDelegateChromeOS::OnExtensionUnloaded( | |
134 content::BrowserContext* browser_context, | |
135 const extensions::Extension* extension, | |
136 extensions::UnloadedExtensionInfo::Reason reason) { | |
137 NotifyObservers(); | |
Daniel Erat
2015/03/09 19:46:19
same here
bartfab (slow)
2015/03/09 21:04:23
Done.
| |
138 } | |
139 | |
140 void VPNDelegateChromeOS::OnShutdown(extensions::ExtensionRegistry* registry) { | |
141 DCHECK(extension_registry_); | |
142 extension_registry_->RemoveObserver(this); | |
143 extension_registry_ = nullptr; | |
144 } | |
145 | |
146 void VPNDelegateChromeOS::Observe( | |
147 int type, | |
148 const content::NotificationSource& source, | |
149 const content::NotificationDetails& details) { | |
150 // A user has logged in and become the primary user. Stop watching user logins | |
151 // and start observing the primary user's extension registry. | |
152 DCHECK_EQ(chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, type); | |
153 registrar_.RemoveAll(); | |
154 AttachToPrimaryUserExtensionRegistry(); | |
155 | |
156 } | |
157 | |
158 void VPNDelegateChromeOS::AttachToPrimaryUserExtensionRegistry() { | |
159 DCHECK(!extension_registry_); | |
160 extension_registry_ = extensions::ExtensionRegistry::Get( | |
161 chromeos::ProfileHelper::Get()->GetProfileByUser( | |
162 user_manager::UserManager::Get()->GetPrimaryUser())); | |
163 extension_registry_->AddObserver(this); | |
164 } | |
OLD | NEW |