1 // Copyright 2014 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/policy/chrome_browser_policy_connector.h"
9 #include "base/callback.h"
10 #include "base/command_line.h"
11 #include "base/files/file_path.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/path_service.h"
15 #include "base/strings/sys_string_conversions.h"
16 #include "chrome/browser/policy/configuration_policy_handler_list_factory.h"
17 #include "chrome/browser/policy/device_management_service_configuration.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "components/policy/core/common/async_policy_provider.h"
20 #include "components/policy/core/common/cloud/device_management_service.h"
21 #include "components/policy/core/common/configuration_policy_provider.h"
22 #include "components/policy/core/common/policy_map.h"
23 #include "components/policy/core/common/policy_namespace.h"
24 #include "components/policy/core/common/policy_service.h"
25 #include "components/policy/core/common/policy_types.h"
26 #include "content/public/browser/browser_thread.h"
27 #include "content/public/common/content_switches.h"
28 #include "net/url_request/url_request_context_getter.h"
29 #include "policy/policy_constants.h"
32 #include "components/policy/core/common/policy_loader_win.h"
33 #elif defined(OS_MACOSX)
34 #include <CoreFoundation/CoreFoundation.h>
35 #include "components/policy/core/common/policy_loader_mac.h"
36 #include "components/policy/core/common/preferences_mac.h"
37 #elif defined(OS_POSIX) && !defined(OS_ANDROID)
38 #include "components/policy/core/common/config_dir_policy_loader.h"
39 #elif defined(OS_ANDROID)
40 #include "components/policy/core/common/policy_provider_android.h"
43 using content::BrowserThread
;
49 #if defined(OS_MACOSX)
50 base::FilePath
GetManagedPolicyPath() {
51 CFBundleRef
bundle(CFBundleGetMainBundle());
53 return base::FilePath();
55 CFStringRef bundle_id
= CFBundleGetIdentifier(bundle
);
57 return base::FilePath();
59 return policy::PolicyLoaderMac::GetManagedPolicyPath(bundle_id
);
61 #endif // defined(OS_MACOSX)
65 ChromeBrowserPolicyConnector::ChromeBrowserPolicyConnector()
66 : BrowserPolicyConnector(base::Bind(&BuildHandlerList
)) {
67 ConfigurationPolicyProvider
* platform_provider
= CreatePlatformProvider();
68 if (platform_provider
)
69 SetPlatformPolicyProvider(make_scoped_ptr(platform_provider
));
72 ChromeBrowserPolicyConnector::~ChromeBrowserPolicyConnector() {}
74 void ChromeBrowserPolicyConnector::Init(
75 PrefService
* local_state
,
76 scoped_refptr
<net::URLRequestContextGetter
> request_context
) {
77 // Initialization of some of the providers requires the FILE thread; make
78 // sure that threading is ready at this point.
79 DCHECK(BrowserThread::IsThreadInitialized(BrowserThread::FILE));
81 scoped_ptr
<DeviceManagementService::Configuration
> configuration(
82 new DeviceManagementServiceConfiguration(
83 BrowserPolicyConnector::GetDeviceManagementUrl()));
84 scoped_ptr
<DeviceManagementService
> device_management_service(
85 new DeviceManagementService(configuration
.Pass()));
86 device_management_service
->ScheduleInitialization(
87 kServiceInitializationStartupDelay
);
89 BrowserPolicyConnector::Init(
90 local_state
, request_context
, device_management_service
.Pass());
92 AppendExtraFlagsPerPolicy();
95 ConfigurationPolicyProvider
*
96 ChromeBrowserPolicyConnector::CreatePlatformProvider() {
98 scoped_ptr
<AsyncPolicyLoader
> loader(PolicyLoaderWin::Create(
99 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
100 kRegistryChromePolicyKey
));
101 return new AsyncPolicyProvider(GetSchemaRegistry(), loader
.Pass());
102 #elif defined(OS_MACOSX)
103 scoped_ptr
<AsyncPolicyLoader
> loader(new PolicyLoaderMac(
104 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
105 GetManagedPolicyPath(),
106 new MacPreferences()));
107 return new AsyncPolicyProvider(GetSchemaRegistry(), loader
.Pass());
108 #elif defined(OS_POSIX) && !defined(OS_ANDROID)
109 base::FilePath config_dir_path
;
110 if (PathService::Get(chrome::DIR_POLICY_FILES
, &config_dir_path
)) {
111 scoped_ptr
<AsyncPolicyLoader
> loader(new ConfigDirPolicyLoader(
112 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
114 POLICY_SCOPE_MACHINE
));
115 return new AsyncPolicyProvider(GetSchemaRegistry(), loader
.Pass());
119 #elif defined(OS_ANDROID)
120 return new PolicyProviderAndroid();
126 void ChromeBrowserPolicyConnector::AppendExtraFlagsPerPolicy() {
127 PolicyService
* policy_service
= GetPolicyService();
128 PolicyNamespace chrome_ns
= PolicyNamespace(POLICY_DOMAIN_CHROME
, "");
129 const PolicyMap
& chrome_policy
= policy_service
->GetPolicies(chrome_ns
);
130 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
132 if (command_line
->HasSwitch(switches::kEnableNpapi
))
135 // The list of Plugin related policies that re-enable NPAPI. Remove once NPAPI
137 const std::string plugin_policies
[] = { key::kEnabledPlugins
,
138 key::kPluginsAllowedForUrls
,
139 key::kPluginsBlockedForUrls
,
140 key::kDisabledPluginsExceptions
,
141 key::kDisabledPlugins
};
142 for (auto policy
: plugin_policies
) {
143 if (chrome_policy
.GetValue(policy
)) {
144 command_line
->AppendSwitch(switches::kEnableNpapi
);
150 } // namespace policy