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. 4 5 #include "chrome/browser/extensions/api/autotest_private/autotest_private_api.h" 6 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 "chrome/common/extensions/manifest_url_handler.h" 16 #include "extensions/browser/extension_function_registry.h" 17 #include "extensions/browser/extension_registry.h" 18 #include "extensions/browser/extension_system.h" 19 #include "extensions/common/manifest_handlers/background_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" 23 24 #if defined(OS_CHROMEOS) 25 #include "chrome/browser/chromeos/login/lock/screen_locker.h" 26 #include "chrome/browser/chromeos/login/users/user_manager.h" 27 #include "chromeos/dbus/dbus_thread_manager.h" 28 #include "chromeos/dbus/session_manager_client.h" 29 #endif 30 31 namespace extensions { 32 namespace { 33 34 base::ListValue* GetHostPermissions(const Extension* ext, bool effective_perm) { 35 const PermissionsData* permissions_data = ext->permissions_data(); 36 const URLPatternSet& pattern_set = 37 effective_perm ? permissions_data->GetEffectiveHostPermissions() 38 : permissions_data->active_permissions()->explicit_hosts(); 39 40 base::ListValue* permissions = new base::ListValue; 41 for (URLPatternSet::const_iterator perm = pattern_set.begin(); 42 perm != pattern_set.end(); 43 ++perm) { 44 permissions->Append(new base::StringValue(perm->GetAsString())); 45 } 46 47 return permissions; 48 } 49 50 base::ListValue* GetAPIPermissions(const Extension* ext) { 51 base::ListValue* permissions = new base::ListValue; 52 std::set<std::string> perm_list = 53 ext->permissions_data()->active_permissions()->GetAPIsAsStrings(); 54 for (std::set<std::string>::const_iterator perm = perm_list.begin(); 55 perm != perm_list.end(); ++perm) { 56 permissions->Append(new base::StringValue(perm->c_str())); 57 } 58 return permissions; 59 } 60 61 bool IsTestMode(Profile* profile) { 62 return AutotestPrivateAPI::GetFactoryInstance()->Get(profile)->test_mode(); 63 } 64 65 } // namespace 66 67 bool AutotestPrivateLogoutFunction::RunSync() { 68 DVLOG(1) << "AutotestPrivateLogoutFunction"; 69 if (!IsTestMode(GetProfile())) 70 chrome::AttemptUserExit(); 71 return true; 72 } 73 74 bool AutotestPrivateRestartFunction::RunSync() { 75 DVLOG(1) << "AutotestPrivateRestartFunction"; 76 if (!IsTestMode(GetProfile())) 77 chrome::AttemptRestart(); 78 return true; 79 } 80 81 bool AutotestPrivateShutdownFunction::RunSync() { 82 scoped_ptr<api::autotest_private::Shutdown::Params> params( 83 api::autotest_private::Shutdown::Params::Create(*args_)); 84 EXTENSION_FUNCTION_VALIDATE(params.get()); 85 86 DVLOG(1) << "AutotestPrivateShutdownFunction " << params->force; 87 88 if (!IsTestMode(GetProfile())) 89 chrome::AttemptExit(); 90 return true; 91 } 92 93 bool AutotestPrivateLoginStatusFunction::RunSync() { 94 DVLOG(1) << "AutotestPrivateLoginStatusFunction"; 95 96 base::DictionaryValue* result(new base::DictionaryValue); 97 #if defined(OS_CHROMEOS) 98 const chromeos::UserManager* user_manager = chromeos::UserManager::Get(); 99 const bool is_screen_locked = 100 !!chromeos::ScreenLocker::default_screen_locker(); 101 102 if (user_manager) { 103 result->SetBoolean("isLoggedIn", user_manager->IsUserLoggedIn()); 104 result->SetBoolean("isOwner", user_manager->IsCurrentUserOwner()); 105 result->SetBoolean("isScreenLocked", is_screen_locked); 106 if (user_manager->IsUserLoggedIn()) { 107 result->SetBoolean("isRegularUser", 108 user_manager->IsLoggedInAsRegularUser()); 109 result->SetBoolean("isGuest", user_manager->IsLoggedInAsGuest()); 110 result->SetBoolean("isKiosk", user_manager->IsLoggedInAsKioskApp()); 111 112 const chromeos::User* user = user_manager->GetLoggedInUser(); 113 result->SetString("email", user->email()); 114 result->SetString("displayEmail", user->display_email()); 115 116 std::string user_image; 117 switch (user->image_index()) { 118 case chromeos::User::kExternalImageIndex: 119 user_image = "file"; 120 break; 121 122 case chromeos::User::kProfileImageIndex: 123 user_image = "profile"; 124 break; 125 126 default: 127 user_image = base::IntToString(user->image_index()); 128 break; 129 } 130 result->SetString("userImage", user_image); 131 } 132 } 133 #endif 134 135 SetResult(result); 136 return true; 137 } 138 139 bool AutotestPrivateLockScreenFunction::RunSync() { 140 DVLOG(1) << "AutotestPrivateLockScreenFunction"; 141 #if defined(OS_CHROMEOS) 142 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()-> 143 RequestLockScreen(); 144 #endif 145 return true; 146 } 147 148 bool AutotestPrivateGetExtensionsInfoFunction::RunSync() { 149 DVLOG(1) << "AutotestPrivateGetExtensionsInfoFunction"; 150 151 ExtensionService* service = 152 ExtensionSystem::Get(GetProfile())->extension_service(); 153 ExtensionRegistry* registry = ExtensionRegistry::Get(GetProfile()); 154 const ExtensionSet& extensions = registry->enabled_extensions(); 155 const ExtensionSet& disabled_extensions = registry->disabled_extensions(); 156 ExtensionActionManager* extension_action_manager = 157 ExtensionActionManager::Get(GetProfile()); 158 159 base::ListValue* extensions_values = new base::ListValue; 160 ExtensionList all; 161 all.insert(all.end(), extensions.begin(), extensions.end()); 162 all.insert(all.end(), disabled_extensions.begin(), disabled_extensions.end()); 163 for (ExtensionList::const_iterator it = all.begin(); 164 it != all.end(); ++it) { 165 const Extension* extension = it->get(); 166 std::string id = extension->id(); 167 base::DictionaryValue* extension_value = new base::DictionaryValue; 168 extension_value->SetString("id", id); 169 extension_value->SetString("version", extension->VersionString()); 170 extension_value->SetString("name", extension->name()); 171 extension_value->SetString("publicKey", extension->public_key()); 172 extension_value->SetString("description", extension->description()); 173 extension_value->SetString( 174 "backgroundUrl", BackgroundInfo::GetBackgroundURL(extension).spec()); 175 extension_value->SetString("optionsUrl", 176 ManifestURL::GetOptionsPage(extension).spec()); 177 178 extension_value->Set("hostPermissions", 179 GetHostPermissions(extension, false)); 180 extension_value->Set("effectiveHostPermissions", 181 GetHostPermissions(extension, true)); 182 extension_value->Set("apiPermissions", GetAPIPermissions(extension)); 183 184 Manifest::Location location = extension->location(); 185 extension_value->SetBoolean("isComponent", 186 location == Manifest::COMPONENT); 187 extension_value->SetBoolean("isInternal", 188 location == Manifest::INTERNAL); 189 extension_value->SetBoolean("isUserInstalled", 190 location == Manifest::INTERNAL || 191 Manifest::IsUnpackedLocation(location)); 192 extension_value->SetBoolean("isEnabled", service->IsExtensionEnabled(id)); 193 extension_value->SetBoolean("allowedInIncognito", 194 util::IsIncognitoEnabled(id, GetProfile())); 195 extension_value->SetBoolean( 196 "hasPageAction", 197 extension_action_manager->GetPageAction(*extension) != NULL); 198 199 extensions_values->Append(extension_value); 200 } 201 202 base::DictionaryValue* return_value(new base::DictionaryValue); 203 return_value->Set("extensions", extensions_values); 204 SetResult(return_value); 205 return true; 206 } 207 208 static int AccessArray(const volatile int arr[], const volatile int *index) { 209 return arr[*index]; 210 } 211 212 bool AutotestPrivateSimulateAsanMemoryBugFunction::RunSync() { 213 DVLOG(1) << "AutotestPrivateSimulateAsanMemoryBugFunction"; 214 if (!IsTestMode(GetProfile())) { 215 // This array is volatile not to let compiler optimize us out. 216 volatile int testarray[3] = {0, 0, 0}; 217 218 // Cause Address Sanitizer to abort this process. 219 volatile int index = 5; 220 AccessArray(testarray, &index); 221 } 222 return true; 223 } 224 225 static base::LazyInstance<BrowserContextKeyedAPIFactory<AutotestPrivateAPI> > 226 g_factory = LAZY_INSTANCE_INITIALIZER; 227 228 // static 229 BrowserContextKeyedAPIFactory<AutotestPrivateAPI>* 230 AutotestPrivateAPI::GetFactoryInstance() { 231 return g_factory.Pointer(); 232 } 233 234 template <> 235 KeyedService* 236 BrowserContextKeyedAPIFactory<AutotestPrivateAPI>::BuildServiceInstanceFor( 237 content::BrowserContext* context) const { 238 return new AutotestPrivateAPI(); 239 } 240 241 AutotestPrivateAPI::AutotestPrivateAPI() : test_mode_(false) { 242 } 243 244 AutotestPrivateAPI::~AutotestPrivateAPI() { 245 } 246 247 } // namespace extensions 248