1 // Copyright (c) 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.
5 #include "chrome/browser/extensions/api/enterprise_device_attributes/enterprise_device_attributes_api.h"
7 #include "base/values.h"
8 #include "chrome/browser/app_mode/app_mode_utils.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
11 #include "chrome/browser/chromeos/profiles/profile_helper.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/common/extensions/api/enterprise_device_attributes.h"
14 #include "components/user_manager/user_manager.h"
16 namespace extensions
{
20 // Checks for the current browser context if the user is affiliated and the
21 // device is enterprise managed.
22 bool IsPermittedToGetDeviceId(content::BrowserContext
* context
) {
23 policy::BrowserPolicyConnectorChromeOS
* connector
=
24 g_browser_process
->platform_part()->browser_policy_connector_chromeos();
26 const user_manager::User
* user
=
27 chromeos::ProfileHelper::Get()->GetUserByProfile(
28 Profile::FromBrowserContext(context
));
30 return connector
->IsEnterpriseManaged() &&
31 connector
->GetUserAffiliation(user
->email()) ==
32 policy::USER_AFFILIATION_MANAGED
;
35 // Returns the directory device id for the permitted extensions or an empty
37 std::string
GetDirectoryDeviceId(content::BrowserContext
* context
) {
38 return IsPermittedToGetDeviceId(context
)
39 ? g_browser_process
->platform_part()
40 ->browser_policy_connector_chromeos()
47 EnterpriseDeviceAttributesGetDirectoryDeviceIdFunction::
48 EnterpriseDeviceAttributesGetDirectoryDeviceIdFunction() {}
50 EnterpriseDeviceAttributesGetDirectoryDeviceIdFunction::
51 ~EnterpriseDeviceAttributesGetDirectoryDeviceIdFunction() {}
53 ExtensionFunction::ResponseAction
54 EnterpriseDeviceAttributesGetDirectoryDeviceIdFunction::Run() {
55 const std::string device_id
= GetDirectoryDeviceId(browser_context());
56 return RespondNow(ArgumentList(
57 api::enterprise_device_attributes::GetDirectoryDeviceId::Results::Create(
61 } // namespace extensions