std::vector GetLoginItemSettingsHelper()

in shell/browser/browser_win.cc [185:272]


std::vector<Browser::LaunchItem> GetLoginItemSettingsHelper(
    base::win::RegistryValueIterator* it,
    boolean* executable_will_launch_at_login,
    std::wstring scope,
    const Browser::LoginItemSettings& options) {
  std::vector<Browser::LaunchItem> launch_items;

  base::FilePath lookup_exe_path;
  if (options.path.empty()) {
    std::wstring process_exe_path;
    GetProcessExecPath(&process_exe_path);
    lookup_exe_path =
        base::CommandLine::FromString(process_exe_path).GetProgram();
  } else {
    lookup_exe_path =
        base::CommandLine::FromString(base::as_wcstr(options.path))
            .GetProgram();
  }

  if (!lookup_exe_path.empty()) {
    while (it->Valid()) {
      base::CommandLine registry_launch_cmd =
          base::CommandLine::FromString(it->Value());
      base::FilePath registry_launch_path = registry_launch_cmd.GetProgram();
      bool exe_match = base::FilePath::CompareEqualIgnoreCase(
          lookup_exe_path.value(), registry_launch_path.value());

      // add launch item to vector if it has a matching path (case-insensitive)
      if (exe_match) {
        Browser::LaunchItem launch_item;
        launch_item.name = it->Name();
        launch_item.path = registry_launch_path.value();
        launch_item.args = registry_launch_cmd.GetArgs();
        launch_item.scope = scope;
        launch_item.enabled = true;

        // attempt to update launch_item.enabled if there is a matching key
        // value entry in the StartupApproved registry
        HKEY hkey;
        // StartupApproved registry path
        LPCTSTR path = TEXT(
            "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\StartupApp"
            "roved\\Run");
        LONG res;
        if (scope == L"user") {
          res =
              RegOpenKeyEx(HKEY_CURRENT_USER, path, 0, KEY_QUERY_VALUE, &hkey);
        } else {
          res =
              RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_QUERY_VALUE, &hkey);
        }
        if (res == ERROR_SUCCESS) {
          DWORD type, size;
          wchar_t startup_binary[12];
          LONG result =
              RegQueryValueEx(hkey, it->Name(), nullptr, &type,
                              reinterpret_cast<BYTE*>(&startup_binary),
                              &(size = sizeof(startup_binary)));
          if (result == ERROR_SUCCESS) {
            if (type == REG_BINARY) {
              // any other binary other than this indicates that the program is
              // not set to launch at login
              wchar_t binary_accepted[12] = {0x00, 0x00, 0x00, 0x00,
                                             0x00, 0x00, 0x00, 0x00,
                                             0x00, 0x00, 0x00, 0x00};
              wchar_t binary_accepted_alt[12] = {0x02, 0x00, 0x00, 0x00,
                                                 0x00, 0x00, 0x00, 0x00,
                                                 0x00, 0x00, 0x00, 0x00};
              std::string reg_binary(reinterpret_cast<char*>(binary_accepted));
              std::string reg_binary_alt(
                  reinterpret_cast<char*>(binary_accepted_alt));
              std::string reg_startup_binary(
                  reinterpret_cast<char*>(startup_binary));
              launch_item.enabled = (reg_startup_binary == reg_binary) ||
                                    (reg_startup_binary == reg_binary_alt);
            }
          }
        }

        *executable_will_launch_at_login =
            *executable_will_launch_at_login || launch_item.enabled;
        launch_items.push_back(launch_item);
      }
      it->operator++();
    }
  }
  return launch_items;
}