1 // Copyright (c) 2012 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/autotest_private/autotest_private_api.h"
7 #include "base/lazy_instance.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "chrome/browser/extensions/extension_action_manager.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/extension_util.h"
12 #include "chrome/browser/lifetime/application_lifetime.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/extensions/api/autotest_private.h"
15 #include "extensions/browser/extension_function_registry.h"
16 #include "extensions/browser/extension_registry.h"
17 #include "extensions/browser/extension_system.h"
18 #include "extensions/common/manifest_handlers/background_info.h"
19 #include "extensions/common/manifest_handlers/options_page_info.h"
20 #include "extensions/common/permissions/api_permission_set.h"
21 #include "extensions/common/permissions/permission_set.h"
22 #include "extensions/common/permissions/permissions_data.h"
24 #if defined(OS_CHROMEOS)
25 #include "chrome/browser/chromeos/login/lock/screen_locker.h"
26 #include "chrome/browser/chromeos/system/input_device_settings.h"
27 #include "chromeos/dbus/dbus_thread_manager.h"
28 #include "chromeos/dbus/session_manager_client.h"
29 #include "components/user_manager/user.h"
30 #include "components/user_manager/user_manager.h"
33 namespace extensions
{
36 base::ListValue
* GetHostPermissions(const Extension
* ext
, bool effective_perm
) {
37 const PermissionsData
* permissions_data
= ext
->permissions_data();
38 const URLPatternSet
& pattern_set
=
39 effective_perm
? permissions_data
->GetEffectiveHostPermissions()
40 : permissions_data
->active_permissions()->explicit_hosts();
42 base::ListValue
* permissions
= new base::ListValue
;
43 for (URLPatternSet::const_iterator perm
= pattern_set
.begin();
44 perm
!= pattern_set
.end();
46 permissions
->Append(new base::StringValue(perm
->GetAsString()));
52 base::ListValue
* GetAPIPermissions(const Extension
* ext
) {
53 base::ListValue
* permissions
= new base::ListValue
;
54 std::set
<std::string
> perm_list
=
55 ext
->permissions_data()->active_permissions()->GetAPIsAsStrings();
56 for (std::set
<std::string
>::const_iterator perm
= perm_list
.begin();
57 perm
!= perm_list
.end(); ++perm
) {
58 permissions
->Append(new base::StringValue(perm
->c_str()));
63 bool IsTestMode(Profile
* profile
) {
64 return AutotestPrivateAPI::GetFactoryInstance()->Get(profile
)->test_mode();
69 bool AutotestPrivateLogoutFunction::RunSync() {
70 DVLOG(1) << "AutotestPrivateLogoutFunction";
71 if (!IsTestMode(GetProfile()))
72 chrome::AttemptUserExit();
76 bool AutotestPrivateRestartFunction::RunSync() {
77 DVLOG(1) << "AutotestPrivateRestartFunction";
78 if (!IsTestMode(GetProfile()))
79 chrome::AttemptRestart();
83 bool AutotestPrivateShutdownFunction::RunSync() {
84 scoped_ptr
<api::autotest_private::Shutdown::Params
> params(
85 api::autotest_private::Shutdown::Params::Create(*args_
));
86 EXTENSION_FUNCTION_VALIDATE(params
.get());
88 DVLOG(1) << "AutotestPrivateShutdownFunction " << params
->force
;
90 if (!IsTestMode(GetProfile()))
91 chrome::AttemptExit();
95 bool AutotestPrivateLoginStatusFunction::RunSync() {
96 DVLOG(1) << "AutotestPrivateLoginStatusFunction";
98 base::DictionaryValue
* result(new base::DictionaryValue
);
99 #if defined(OS_CHROMEOS)
100 const user_manager::UserManager
* user_manager
=
101 user_manager::UserManager::Get();
102 const bool is_screen_locked
=
103 !!chromeos::ScreenLocker::default_screen_locker();
106 result
->SetBoolean("isLoggedIn", user_manager
->IsUserLoggedIn());
107 result
->SetBoolean("isOwner", user_manager
->IsCurrentUserOwner());
108 result
->SetBoolean("isScreenLocked", is_screen_locked
);
109 if (user_manager
->IsUserLoggedIn()) {
110 result
->SetBoolean("isRegularUser",
111 user_manager
->IsLoggedInAsUserWithGaiaAccount());
112 result
->SetBoolean("isGuest", user_manager
->IsLoggedInAsGuest());
113 result
->SetBoolean("isKiosk", user_manager
->IsLoggedInAsKioskApp());
115 const user_manager::User
* user
= user_manager
->GetLoggedInUser();
116 result
->SetString("email", user
->email());
117 result
->SetString("displayEmail", user
->display_email());
119 std::string user_image
;
120 switch (user
->image_index()) {
121 case user_manager::User::USER_IMAGE_EXTERNAL
:
125 case user_manager::User::USER_IMAGE_PROFILE
:
126 user_image
= "profile";
130 user_image
= base::IntToString(user
->image_index());
133 result
->SetString("userImage", user_image
);
142 bool AutotestPrivateLockScreenFunction::RunSync() {
143 DVLOG(1) << "AutotestPrivateLockScreenFunction";
144 #if defined(OS_CHROMEOS)
145 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->
151 bool AutotestPrivateGetExtensionsInfoFunction::RunSync() {
152 DVLOG(1) << "AutotestPrivateGetExtensionsInfoFunction";
154 ExtensionService
* service
=
155 ExtensionSystem::Get(GetProfile())->extension_service();
156 ExtensionRegistry
* registry
= ExtensionRegistry::Get(GetProfile());
157 const ExtensionSet
& extensions
= registry
->enabled_extensions();
158 const ExtensionSet
& disabled_extensions
= registry
->disabled_extensions();
159 ExtensionActionManager
* extension_action_manager
=
160 ExtensionActionManager::Get(GetProfile());
162 base::ListValue
* extensions_values
= new base::ListValue
;
164 all
.insert(all
.end(), extensions
.begin(), extensions
.end());
165 all
.insert(all
.end(), disabled_extensions
.begin(), disabled_extensions
.end());
166 for (ExtensionList::const_iterator it
= all
.begin();
167 it
!= all
.end(); ++it
) {
168 const Extension
* extension
= it
->get();
169 std::string id
= extension
->id();
170 base::DictionaryValue
* extension_value
= new base::DictionaryValue
;
171 extension_value
->SetString("id", id
);
172 extension_value
->SetString("version", extension
->VersionString());
173 extension_value
->SetString("name", extension
->name());
174 extension_value
->SetString("publicKey", extension
->public_key());
175 extension_value
->SetString("description", extension
->description());
176 extension_value
->SetString(
177 "backgroundUrl", BackgroundInfo::GetBackgroundURL(extension
).spec());
178 extension_value
->SetString(
179 "optionsUrl", OptionsPageInfo::GetOptionsPage(extension
).spec());
181 extension_value
->Set("hostPermissions",
182 GetHostPermissions(extension
, false));
183 extension_value
->Set("effectiveHostPermissions",
184 GetHostPermissions(extension
, true));
185 extension_value
->Set("apiPermissions", GetAPIPermissions(extension
));
187 Manifest::Location location
= extension
->location();
188 extension_value
->SetBoolean("isComponent",
189 location
== Manifest::COMPONENT
);
190 extension_value
->SetBoolean("isInternal",
191 location
== Manifest::INTERNAL
);
192 extension_value
->SetBoolean("isUserInstalled",
193 location
== Manifest::INTERNAL
||
194 Manifest::IsUnpackedLocation(location
));
195 extension_value
->SetBoolean("isEnabled", service
->IsExtensionEnabled(id
));
196 extension_value
->SetBoolean("allowedInIncognito",
197 util::IsIncognitoEnabled(id
, GetProfile()));
198 extension_value
->SetBoolean(
200 extension_action_manager
->GetPageAction(*extension
) != NULL
);
202 extensions_values
->Append(extension_value
);
205 base::DictionaryValue
* return_value(new base::DictionaryValue
);
206 return_value
->Set("extensions", extensions_values
);
207 SetResult(return_value
);
211 static int AccessArray(const volatile int arr
[], const volatile int *index
) {
215 bool AutotestPrivateSimulateAsanMemoryBugFunction::RunSync() {
216 DVLOG(1) << "AutotestPrivateSimulateAsanMemoryBugFunction";
217 if (!IsTestMode(GetProfile())) {
218 // This array is volatile not to let compiler optimize us out.
219 volatile int testarray
[3] = {0, 0, 0};
221 // Cause Address Sanitizer to abort this process.
222 volatile int index
= 5;
223 AccessArray(testarray
, &index
);
228 bool AutotestPrivateSetTouchpadSensitivityFunction::RunSync() {
229 scoped_ptr
<api::autotest_private::SetTouchpadSensitivity::Params
> params(
230 api::autotest_private::SetTouchpadSensitivity::Params::Create(*args_
));
231 EXTENSION_FUNCTION_VALIDATE(params
.get());
233 DVLOG(1) << "AutotestPrivateSetTouchpadSensitivityFunction " << params
->value
;
235 #if defined(OS_CHROMEOS)
236 chromeos::system::InputDeviceSettings::Get()->SetTouchpadSensitivity(
242 bool AutotestPrivateSetTapToClickFunction::RunSync() {
243 scoped_ptr
<api::autotest_private::SetTapToClick::Params
> params(
244 api::autotest_private::SetTapToClick::Params::Create(*args_
));
245 EXTENSION_FUNCTION_VALIDATE(params
.get());
247 DVLOG(1) << "AutotestPrivateSetTapToClickFunction " << params
->enabled
;
249 #if defined(OS_CHROMEOS)
250 chromeos::system::InputDeviceSettings::Get()->SetTapToClick(params
->enabled
);
255 bool AutotestPrivateSetThreeFingerClickFunction::RunSync() {
256 scoped_ptr
<api::autotest_private::SetThreeFingerClick::Params
> params(
257 api::autotest_private::SetThreeFingerClick::Params::Create(*args_
));
258 EXTENSION_FUNCTION_VALIDATE(params
.get());
260 DVLOG(1) << "AutotestPrivateSetThreeFingerClickFunction " << params
->enabled
;
262 #if defined(OS_CHROMEOS)
263 chromeos::system::InputDeviceSettings::Get()->SetThreeFingerClick(
269 bool AutotestPrivateSetTapDraggingFunction::RunSync() {
270 scoped_ptr
<api::autotest_private::SetTapDragging::Params
> params(
271 api::autotest_private::SetTapDragging::Params::Create(*args_
));
272 EXTENSION_FUNCTION_VALIDATE(params
.get());
274 DVLOG(1) << "AutotestPrivateSetTapDraggingFunction " << params
->enabled
;
276 #if defined(OS_CHROMEOS)
277 chromeos::system::InputDeviceSettings::Get()->SetTapDragging(params
->enabled
);
282 bool AutotestPrivateSetNaturalScrollFunction::RunSync() {
283 scoped_ptr
<api::autotest_private::SetNaturalScroll::Params
> params(
284 api::autotest_private::SetNaturalScroll::Params::Create(*args_
));
285 EXTENSION_FUNCTION_VALIDATE(params
.get());
287 DVLOG(1) << "AutotestPrivateSetNaturalScrollFunction " << params
->enabled
;
289 #if defined(OS_CHROMEOS)
290 chromeos::system::InputDeviceSettings::Get()->SetNaturalScroll(
296 bool AutotestPrivateSetMouseSensitivityFunction::RunSync() {
297 scoped_ptr
<api::autotest_private::SetMouseSensitivity::Params
> params(
298 api::autotest_private::SetMouseSensitivity::Params::Create(*args_
));
299 EXTENSION_FUNCTION_VALIDATE(params
.get());
301 DVLOG(1) << "AutotestPrivateSetMouseSensitivityFunction " << params
->value
;
303 #if defined(OS_CHROMEOS)
304 chromeos::system::InputDeviceSettings::Get()->SetMouseSensitivity(
310 bool AutotestPrivateSetPrimaryButtonRightFunction::RunSync() {
311 scoped_ptr
<api::autotest_private::SetPrimaryButtonRight::Params
> params(
312 api::autotest_private::SetPrimaryButtonRight::Params::Create(*args_
));
313 EXTENSION_FUNCTION_VALIDATE(params
.get());
315 DVLOG(1) << "AutotestPrivateSetPrimaryButtonRightFunction " << params
->right
;
317 #if defined(OS_CHROMEOS)
318 chromeos::system::InputDeviceSettings::Get()->SetPrimaryButtonRight(
324 static base::LazyInstance
<BrowserContextKeyedAPIFactory
<AutotestPrivateAPI
> >
325 g_factory
= LAZY_INSTANCE_INITIALIZER
;
328 BrowserContextKeyedAPIFactory
<AutotestPrivateAPI
>*
329 AutotestPrivateAPI::GetFactoryInstance() {
330 return g_factory
.Pointer();
335 BrowserContextKeyedAPIFactory
<AutotestPrivateAPI
>::BuildServiceInstanceFor(
336 content::BrowserContext
* context
) const {
337 return new AutotestPrivateAPI();
340 AutotestPrivateAPI::AutotestPrivateAPI() : test_mode_(false) {
343 AutotestPrivateAPI::~AutotestPrivateAPI() {
346 } // namespace extensions