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 | |
25 namespace { | |
26 | |
27 bool IsVPNProvider(const extensions::Extension* extension) { | |
28 return extension->permissions_data()->HasAPIPermission( | |
29 extensions::APIPermission::kVpnProvider); | |
30 } | |
31 | |
32 Profile* GetProfileForPrimaryUser() { | |
33 const user_manager::User* const primary_user = | |
34 user_manager::UserManager::Get()->GetPrimaryUser(); | |
35 if (!primary_user) | |
36 return nullptr; | |
37 | |
38 return chromeos::ProfileHelper::Get()->GetProfileByUser(primary_user); | |
39 } | |
40 | |
41 } // namespace | |
42 | |
43 VPNDelegateChromeOS::VPNDelegateChromeOS() { | |
44 if (user_manager::UserManager::Get()->GetPrimaryUser()) { | |
45 // If a user is logged in, start observing the primary user's extension | |
46 // registry immediately. | |
47 AttachToPrimaryUserExtensionRegistry(); | |
48 } else { | |
49 // If no user is logged in, wait for the first user to log in and become the | |
50 // primary user. | |
51 registrar_.Add(this, | |
52 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, | |
53 content::NotificationService::AllSources()); | |
54 } | |
55 } | |
56 | |
57 VPNDelegateChromeOS::~VPNDelegateChromeOS() { | |
58 if (extension_registry_) | |
59 extension_registry_->RemoveObserver(this); | |
60 } | |
61 | |
62 bool VPNDelegateChromeOS::HaveThirdPartyVPNProviders() const { | |
63 return !third_party_vpn_providers_.empty(); | |
64 } | |
65 | |
66 const std::vector<ash::VPNProvider>& | |
67 VPNDelegateChromeOS::GetThirdPartyVPNProviders() const { | |
68 return third_party_vpn_providers_; | |
69 } | |
70 | |
71 void VPNDelegateChromeOS::ShowAddPage(const std::string& id) { | |
72 if (id.empty()) { | |
73 // If |id| is an empty string, show the built-in dialog for adding an | |
74 // OpenVPN or L2TP VPN. | |
75 ash::Shell::GetInstance()->system_tray_delegate()->ShowOtherNetworkDialog( | |
76 shill::kTypeVPN); | |
77 return; | |
78 } | |
79 | |
80 Profile* const profile = GetProfileForPrimaryUser(); | |
81 if (!profile) | |
82 return; | |
83 | |
84 // Request that the third-party VPN provider identified by |id| show its "add | |
85 // network" dialog. | |
86 chromeos::VpnServiceFactory::GetForBrowserContext(profile)->TriggerAdd(id); | |
87 } | |
88 | |
89 void VPNDelegateChromeOS::OnExtensionLoaded( | |
90 content::BrowserContext* browser_context, | |
91 const extensions::Extension* extension) { | |
92 if (IsVPNProvider(extension)) | |
93 UpdateThirdPartyVPNProviders(); | |
94 } | |
95 | |
96 void VPNDelegateChromeOS::OnExtensionUnloaded( | |
97 content::BrowserContext* browser_context, | |
98 const extensions::Extension* extension, | |
99 extensions::UnloadedExtensionInfo::Reason reason) { | |
100 if (IsVPNProvider(extension)) | |
101 UpdateThirdPartyVPNProviders(); | |
102 } | |
103 | |
104 void VPNDelegateChromeOS::OnShutdown(extensions::ExtensionRegistry* registry) { | |
105 DCHECK(extension_registry_); | |
106 extension_registry_->RemoveObserver(this); | |
107 extension_registry_ = nullptr; | |
108 } | |
109 | |
110 void VPNDelegateChromeOS::Observe( | |
111 int type, | |
112 const content::NotificationSource& source, | |
113 const content::NotificationDetails& details) { | |
114 // A user has logged in and become the primary user. Stop watching user logins | |
115 // and start observing the primary user's extension registry. | |
116 DCHECK_EQ(chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, type); | |
117 registrar_.RemoveAll(); | |
118 AttachToPrimaryUserExtensionRegistry(); | |
119 | |
120 } | |
121 | |
122 void VPNDelegateChromeOS::UpdateThirdPartyVPNProviders() { | |
123 DCHECK(extension_registry_); | |
124 | |
125 third_party_vpn_providers_.clear(); | |
126 const extensions::ExtensionSet& extensions = | |
127 extension_registry_->enabled_extensions(); | |
128 for (const auto& extension : extensions) { | |
129 if (IsVPNProvider(extension.get())) { | |
130 third_party_vpn_providers_.push_back(ash::VPNProvider(extension->name(), | |
131 extension->id())); | |
132 } | |
133 } | |
stevenjb
2015/03/10 18:06:33
nit: Even though loading / unloading extensions is
bartfab (slow)
2015/03/10 21:13:48
UpdateThirdPartyVPNProviders() will only be called
stevenjb
2015/03/10 21:25:13
Oh, I see, there is already an IsVPNProvider(exten
| |
134 | |
135 NotifyObservers(); | |
136 } | |
137 | |
138 void VPNDelegateChromeOS::AttachToPrimaryUserExtensionRegistry() { | |
139 DCHECK(!extension_registry_); | |
140 extension_registry_ = extensions::ExtensionRegistry::Get( | |
141 GetProfileForPrimaryUser()); | |
142 extension_registry_->AddObserver(this); | |
143 | |
144 UpdateThirdPartyVPNProviders(); | |
145 } | |
OLD | NEW |