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