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

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

Issue 2522613004: Revert of chromeos: Convert ash VPNDelegate interface to mojo (Closed)
Patch Set: Created 4 years, 1 month 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
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_list_forwarder.h"
6
7 #include "ash/public/interfaces/vpn_list.mojom.h"
8 #include "base/bind.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/threading/thread_task_runner_handle.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 "chrome/browser/ui/ash/ash_util.h"
16 #include "components/user_manager/user.h"
17 #include "components/user_manager/user_manager.h"
18 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/notification_source.h"
20 #include "content/public/common/service_manager_connection.h"
21 #include "extensions/browser/extension_registry.h"
22 #include "extensions/common/extension.h"
23 #include "extensions/common/extension_set.h"
24 #include "extensions/common/permissions/api_permission.h"
25 #include "extensions/common/permissions/permissions_data.h"
26 #include "services/service_manager/public/cpp/connector.h"
27
28 namespace {
29
30 bool IsVPNProvider(const extensions::Extension* extension) {
31 return extension->permissions_data()->HasAPIPermission(
32 extensions::APIPermission::kVpnProvider);
33 }
34
35 Profile* GetProfileForPrimaryUser() {
36 const user_manager::User* const primary_user =
37 user_manager::UserManager::Get()->GetPrimaryUser();
38 if (!primary_user)
39 return nullptr;
40
41 return chromeos::ProfileHelper::Get()->GetProfileByUser(primary_user);
42 }
43
44 // Connects to the VpnList mojo interface in ash.
45 ash::mojom::VpnListPtr ConnectToVpnList() {
46 ash::mojom::VpnListPtr vpn_list;
47 service_manager::Connector* connector =
48 content::ServiceManagerConnection::GetForProcess()->GetConnector();
49 // Under mash the VpnList interface is in the ash process. In classic ash
50 // we provide it to ourself.
51 if (chrome::IsRunningInMash())
52 connector->ConnectToInterface("ash", &vpn_list);
53 else
54 connector->ConnectToInterface("content_browser", &vpn_list);
55 return vpn_list;
56 }
57
58 } // namespace
59
60 VpnListForwarder::VpnListForwarder() : weak_factory_(this) {
61 if (user_manager::UserManager::Get()->GetPrimaryUser()) {
62 // If a user is logged in, start observing the primary user's extension
63 // registry immediately.
64 AttachToPrimaryUserExtensionRegistry();
65 } else {
66 // If no user is logged in, wait until the first user logs in (thus becoming
67 // the primary user) and a profile is created for that user.
68 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CREATED,
69 content::NotificationService::AllSources());
70 }
71 }
72
73 VpnListForwarder::~VpnListForwarder() {
74 if (extension_registry_)
75 extension_registry_->RemoveObserver(this);
76 }
77
78 void VpnListForwarder::OnExtensionLoaded(
79 content::BrowserContext* browser_context,
80 const extensions::Extension* extension) {
81 if (IsVPNProvider(extension))
82 UpdateVPNProviders();
83 }
84
85 void VpnListForwarder::OnExtensionUnloaded(
86 content::BrowserContext* browser_context,
87 const extensions::Extension* extension,
88 extensions::UnloadedExtensionInfo::Reason reason) {
89 if (IsVPNProvider(extension))
90 UpdateVPNProviders();
91 }
92
93 void VpnListForwarder::OnShutdown(extensions::ExtensionRegistry* registry) {
94 DCHECK(extension_registry_);
95 extension_registry_->RemoveObserver(this);
96 extension_registry_ = nullptr;
97 }
98
99 void VpnListForwarder::Observe(int type,
100 const content::NotificationSource& source,
101 const content::NotificationDetails& details) {
102 DCHECK_EQ(chrome::NOTIFICATION_PROFILE_CREATED, type);
103 const Profile* const profile = content::Source<Profile>(source).ptr();
104 if (!chromeos::ProfileHelper::Get()->IsPrimaryProfile(profile)) {
105 // If the profile that was just created does not belong to the primary user
106 // (e.g. login profile), ignore it.
107 return;
108 }
109
110 // The first user logged in (thus becoming the primary user) and a profile was
111 // created for that user. Stop observing profile creation. Wait one message
112 // loop cycle to allow other code which observes the
113 // chrome::NOTIFICATION_PROFILE_CREATED notification to finish initializing
114 // the profile, then start observing the primary user's extension registry.
115 registrar_.RemoveAll();
116 base::ThreadTaskRunnerHandle::Get()->PostTask(
117 FROM_HERE,
118 base::Bind(&VpnListForwarder::AttachToPrimaryUserExtensionRegistry,
119 weak_factory_.GetWeakPtr()));
120 }
121
122 void VpnListForwarder::UpdateVPNProviders() {
123 DCHECK(extension_registry_);
124
125 std::vector<ash::mojom::ThirdPartyVpnProviderPtr> third_party_providers;
126 for (const auto& extension : extension_registry_->enabled_extensions()) {
127 if (!IsVPNProvider(extension.get()))
128 continue;
129
130 ash::mojom::ThirdPartyVpnProviderPtr provider =
131 ash::mojom::ThirdPartyVpnProvider::New();
132 provider->name = extension->name();
133 provider->extension_id = extension->id();
134 third_party_providers.push_back(std::move(provider));
135 }
136
137 // Ash starts without any third-party providers. If we've never sent one then
138 // there's no need to send an empty list. This case commonly occurs on startup
139 // when the user has no third-party VPN extensions installed.
140 if (!sent_providers_ && third_party_providers.empty())
141 return;
142
143 // It's rare to install or uninstall VPN provider extensions, so don't bother
144 // caching the interface pointer between calls to this function.
145 ash::mojom::VpnListPtr vpn_list = ConnectToVpnList();
146 vpn_list->SetThirdPartyVpnProviders(std::move(third_party_providers));
147
148 sent_providers_ = true;
149 }
150
151 void VpnListForwarder::AttachToPrimaryUserExtensionRegistry() {
152 DCHECK(!extension_registry_);
153 extension_registry_ =
154 extensions::ExtensionRegistry::Get(GetProfileForPrimaryUser());
155 extension_registry_->AddObserver(this);
156
157 UpdateVPNProviders();
158 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/ash/vpn_list_forwarder.h ('k') | chrome/browser/ui/views/ash/chrome_browser_main_extra_parts_ash.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698