Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(447)

Side by Side Diff: chrome/browser/ui/ash/vpn_delegate_chromeos.cc

Issue 984863005: Add ash::VPNDelegate and Chrome OS implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments. Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/ui/ash/vpn_delegate_chromeos.h ('k') | chrome/chrome_browser_ui.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 }
stevenjb 2015/03/09 23:20:03 I think we should cache these. Since this is going
bartfab (slow) 2015/03/10 17:54:54 Done. I explicitly re-fetch the entire vector in O
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 if (IsVPNProvider(extension))
131 NotifyObservers();
132 }
133
134 void VPNDelegateChromeOS::OnExtensionUnloaded(
135 content::BrowserContext* browser_context,
136 const extensions::Extension* extension,
137 extensions::UnloadedExtensionInfo::Reason reason) {
138 if (IsVPNProvider(extension))
139 NotifyObservers();
140 }
141
142 void VPNDelegateChromeOS::OnShutdown(extensions::ExtensionRegistry* registry) {
143 DCHECK(extension_registry_);
144 extension_registry_->RemoveObserver(this);
145 extension_registry_ = nullptr;
146 }
147
148 void VPNDelegateChromeOS::Observe(
149 int type,
150 const content::NotificationSource& source,
151 const content::NotificationDetails& details) {
152 // A user has logged in and become the primary user. Stop watching user logins
153 // and start observing the primary user's extension registry.
154 DCHECK_EQ(chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, type);
155 registrar_.RemoveAll();
156 AttachToPrimaryUserExtensionRegistry();
157
158 }
159
160 void VPNDelegateChromeOS::AttachToPrimaryUserExtensionRegistry() {
161 DCHECK(!extension_registry_);
162 extension_registry_ = extensions::ExtensionRegistry::Get(
163 chromeos::ProfileHelper::Get()->GetProfileByUser(
164 user_manager::UserManager::Get()->GetPrimaryUser()));
165 extension_registry_->AddObserver(this);
166 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/ash/vpn_delegate_chromeos.h ('k') | chrome/chrome_browser_ui.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698