chromium/chrome/browser/extensions/api/messaging/native_messaging_test_util.cc

// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/api/messaging/native_messaging_test_util.h"

#include <memory>
#include <tuple>
#include <utility>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/json/json_file_value_serializer.h"
#include "base/path_service.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_restrictions.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/common/chrome_paths.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(IS_WIN)
#include "base/win/registry.h"
#endif

namespace extensions {

namespace {

void WriteTestNativeHostManifest(const base::FilePath& target_dir,
                                 const std::string& host_name,
                                 const base::FilePath& host_path,
                                 bool user_level,
                                 bool supports_native_initiated_connections) {}

}  // namespace

const char ScopedTestNativeMessagingHost::kHostName[] =;
const char ScopedTestNativeMessagingHost::kBinaryMissingHostName[] =;
const char ScopedTestNativeMessagingHost::
    kSupportsNativeInitiatedConnectionsHostName[] =;
const char ScopedTestNativeMessagingHost::kExtensionId[] =;
#if BUILDFLAG(IS_WIN)
const char ScopedTestNativeMessagingHost::kHostExeName[] =
    "com.google.chrome.test.exe.echo";
#endif

ScopedTestNativeMessagingHost::ScopedTestNativeMessagingHost() {}

void ScopedTestNativeMessagingHost::RegisterTestHost(bool user_level) {}

#if BUILDFLAG(IS_WIN)
// On Windows, a new codepath is used to directly launch .EXE-based Native
// Hosts.
void ScopedTestNativeMessagingHost::RegisterTestExeHost(
    std::string_view filename,
    bool user_level) {
  base::ScopedAllowBlockingForTesting allow_blocking;
  ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());

  base::FilePath binary_dir;
  ASSERT_TRUE(base::PathService::Get(base::DIR_EXE, &binary_dir));
  HKEY root_key = user_level ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE;
  ASSERT_NO_FATAL_FAILURE(registry_override_.OverrideRegistry(root_key));

  // Unlike in the |RegisterTestHost| case above, we must leave the Host
  // .exe where it was built, because the Host will fail to run from the
  // temp_dir_ if is_component_build is set for the build.
  base::FilePath host_path = binary_dir.AppendASCII(filename);
  ASSERT_NO_FATAL_FAILURE(WriteTestNativeHostManifest(
      temp_dir_.GetPath(), kHostExeName, host_path, user_level, false));
}
#endif

ScopedTestNativeMessagingHost::~ScopedTestNativeMessagingHost() {}

}  // namespace extensions