1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
// Copyright 2012 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_process_launcher.h"
#include <stdint.h>
#include <memory>
#include <utility>
#include "base/functional/callback.h"
#include "base/task/thread_pool.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/api/messaging/launch_context.h"
#include "net/base/file_stream.h"
#include "url/gurl.h"
#if BUILDFLAG(IS_WIN)
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"
#include "extensions/common/extension_features.h"
#include "ui/views/win/hwnd_util.h"
#endif
namespace extensions {
namespace {
// Default implementation on NativeProcessLauncher interface.
class NativeProcessLauncherImpl : public NativeProcessLauncher {
public:
NativeProcessLauncherImpl(bool allow_user_level_hosts,
bool require_native_initiated_connections,
bool native_hosts_executables_launch_directly,
intptr_t window_handle,
const base::FilePath& profile_directory,
const std::string& connect_id,
const std::string& error_arg);
NativeProcessLauncherImpl(const NativeProcessLauncherImpl&) = delete;
NativeProcessLauncherImpl& operator=(const NativeProcessLauncherImpl&) =
delete;
void Launch(const GURL& origin,
const std::string& native_host_name,
LaunchedCallback callback) const override;
private:
void OnComplete(LaunchedCallback callback,
LaunchResult result,
base::Process process,
base::PlatformFile read_file,
std::unique_ptr<net::FileStream> read_stream,
std::unique_ptr<net::FileStream> write_stream) const;
const bool allow_user_level_hosts_;
const bool require_native_initiated_connections_;
const bool native_hosts_executables_launch_directly_;
// Handle of the native window corresponding to the extension.
const intptr_t window_handle_;
const base::FilePath profile_directory_;
const std::string connect_id_;
const std::string error_arg_;
// An in-progress launch.
mutable std::unique_ptr<LaunchContext> context_;
};
NativeProcessLauncherImpl::NativeProcessLauncherImpl(
bool allow_user_level_hosts,
bool require_native_initiated_connections,
bool native_hosts_executables_launch_directly,
intptr_t window_handle,
const base::FilePath& profile_directory,
const std::string& connect_id,
const std::string& error_arg)
: allow_user_level_hosts_(allow_user_level_hosts),
require_native_initiated_connections_(
require_native_initiated_connections),
native_hosts_executables_launch_directly_(
native_hosts_executables_launch_directly),
window_handle_(window_handle),
profile_directory_(profile_directory),
connect_id_(connect_id),
error_arg_(error_arg) {}
void NativeProcessLauncherImpl::Launch(const GURL& origin,
const std::string& native_host_name,
LaunchedCallback callback) const {
CHECK(!context_); // Parallel launches are not supported.
context_ = LaunchContext::Start(
allow_user_level_hosts_, require_native_initiated_connections_,
native_hosts_executables_launch_directly_, window_handle_,
profile_directory_, connect_id_, error_arg_, origin, native_host_name,
base::ThreadPool::CreateTaskRunner(
{base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN, base::MayBlock()}),
// `Unretained` is safe here, as `LaunchContext` guarantees that it will
// never run this callback after the context is destroyed.
base::BindOnce(&NativeProcessLauncherImpl::OnComplete,
base::Unretained(this), std::move(callback)));
}
void NativeProcessLauncherImpl::OnComplete(
LaunchedCallback callback,
LaunchResult result,
base::Process process,
base::PlatformFile read_file,
std::unique_ptr<net::FileStream> read_stream,
std::unique_ptr<net::FileStream> write_stream) const {
context_.reset();
std::move(callback).Run(result, std::move(process), read_file,
std::move(read_stream), std::move(write_stream));
}
} // namespace
// static
std::unique_ptr<NativeProcessLauncher> NativeProcessLauncher::CreateDefault(
bool allow_user_level_hosts,
gfx::NativeView native_view,
const base::FilePath& profile_directory,
bool require_native_initiated_connections,
const std::string& connect_id,
const std::string& error_arg,
Profile* profile) {
intptr_t window_handle = 0;
bool native_hosts_executables_launch_directly = false;
#if BUILDFLAG(IS_WIN)
window_handle = reinterpret_cast<intptr_t>(
views::HWNDForNativeView(native_view));
if (profile && profile->GetPrefs()->IsManagedPreference(
prefs::kNativeHostsExecutablesLaunchDirectly)) {
native_hosts_executables_launch_directly = profile->GetPrefs()->GetBoolean(
prefs::kNativeHostsExecutablesLaunchDirectly);
} else {
native_hosts_executables_launch_directly = base::FeatureList::IsEnabled(
extensions_features::kLaunchWindowsNativeHostsDirectly);
}
#endif // BUILDFLAG(IS_WIN)
return std::make_unique<NativeProcessLauncherImpl>(
allow_user_level_hosts, require_native_initiated_connections,
native_hosts_executables_launch_directly, window_handle,
profile_directory, connect_id, error_arg);
}
} // namespace extensions
|