This source file includes following definitions.
- Detach
- DoLaunchOnThreadPool
- CallCallbackOnIOThread
- PostErrorResult
- PostResult
- CreateDefault
#include "chrome/browser/extensions/api/messaging/native_process_launcher.h"
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/path_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/threading/sequenced_worker_pool.h"
#include "chrome/browser/extensions/api/messaging/native_messaging_host_manifest.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "content/public/browser/browser_thread.h"
#include "url/gurl.h"
#if defined(OS_WIN)
#include "ui/views/win/hwnd_util.h"
#endif
namespace extensions {
namespace {
#if defined(OS_WIN)
const char kParentWindowSwitchName[] = "parent-window";
#endif
class NativeProcessLauncherImpl : public NativeProcessLauncher {
public:
NativeProcessLauncherImpl(bool allow_user_level_hosts,
intptr_t native_window);
virtual ~NativeProcessLauncherImpl();
virtual void Launch(const GURL& origin,
const std::string& native_host_name,
LaunchedCallback callback) const OVERRIDE;
private:
class Core : public base::RefCountedThreadSafe<Core> {
public:
Core(bool allow_user_level_hosts, intptr_t native_window);
void Launch(const GURL& origin,
const std::string& native_host_name,
LaunchedCallback callback);
void Detach();
private:
friend class base::RefCountedThreadSafe<Core>;
virtual ~Core();
void DoLaunchOnThreadPool(const GURL& origin,
const std::string& native_host_name,
LaunchedCallback callback);
void PostErrorResult(const LaunchedCallback& callback, LaunchResult error);
void PostResult(const LaunchedCallback& callback,
base::ProcessHandle process_handle,
base::File read_file,
base::File write_file);
void CallCallbackOnIOThread(LaunchedCallback callback,
LaunchResult result,
base::ProcessHandle process_handle,
base::File read_file,
base::File write_file);
bool detached_;
bool allow_user_level_hosts_;
intptr_t window_handle_;
DISALLOW_COPY_AND_ASSIGN(Core);
};
scoped_refptr<Core> core_;
DISALLOW_COPY_AND_ASSIGN(NativeProcessLauncherImpl);
};
NativeProcessLauncherImpl::Core::Core(bool allow_user_level_hosts,
intptr_t window_handle)
: detached_(false),
allow_user_level_hosts_(allow_user_level_hosts),
window_handle_(window_handle) {
}
NativeProcessLauncherImpl::Core::~Core() {
DCHECK(detached_);
}
void NativeProcessLauncherImpl::Core::Detach() {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
detached_ = true;
}
void NativeProcessLauncherImpl::Core::Launch(
const GURL& origin,
const std::string& native_host_name,
LaunchedCallback callback) {
content::BrowserThread::PostBlockingPoolTask(
FROM_HERE, base::Bind(&Core::DoLaunchOnThreadPool, this,
origin, native_host_name, callback));
}
void NativeProcessLauncherImpl::Core::DoLaunchOnThreadPool(
const GURL& origin,
const std::string& native_host_name,
LaunchedCallback callback) {
DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
if (!NativeMessagingHostManifest::IsValidName(native_host_name)) {
PostErrorResult(callback, RESULT_INVALID_NAME);
return;
}
std::string error_message;
base::FilePath manifest_path =
FindManifest(native_host_name, allow_user_level_hosts_, &error_message);
if (manifest_path.empty()) {
LOG(ERROR) << "Can't find manifest for native messaging host "
<< native_host_name;
PostErrorResult(callback, RESULT_NOT_FOUND);
return;
}
scoped_ptr<NativeMessagingHostManifest> manifest =
NativeMessagingHostManifest::Load(manifest_path, &error_message);
if (!manifest) {
LOG(ERROR) << "Failed to load manifest for native messaging host "
<< native_host_name << ": " << error_message;
PostErrorResult(callback, RESULT_NOT_FOUND);
return;
}
if (manifest->name() != native_host_name) {
LOG(ERROR) << "Failed to load manifest for native messaging host "
<< native_host_name
<< ": Invalid name specified in the manifest.";
PostErrorResult(callback, RESULT_NOT_FOUND);
return;
}
if (!manifest->allowed_origins().MatchesSecurityOrigin(origin)) {
PostErrorResult(callback, RESULT_FORBIDDEN);
return;
}
base::FilePath host_path = manifest->path();
if (!host_path.IsAbsolute()) {
#if defined(OS_WIN)
host_path = manifest_path.DirName().Append(host_path);
#else
LOG(ERROR) << "Native messaging host path must be absolute for "
<< native_host_name;
PostErrorResult(callback, RESULT_NOT_FOUND);
return;
#endif
}
CommandLine command_line(host_path);
command_line.AppendArg(origin.spec());
#if defined(OS_WIN)
command_line.AppendSwitchASCII(kParentWindowSwitchName,
base::Int64ToString(window_handle_));
#endif
base::ProcessHandle process_handle;
base::File read_file;
base::File write_file;
if (NativeProcessLauncher::LaunchNativeProcess(
command_line, &process_handle, &read_file, &write_file)) {
PostResult(callback, process_handle, read_file.Pass(), write_file.Pass());
} else {
PostErrorResult(callback, RESULT_FAILED_TO_START);
}
}
void NativeProcessLauncherImpl::Core::CallCallbackOnIOThread(
LaunchedCallback callback,
LaunchResult result,
base::ProcessHandle process_handle,
base::File read_file,
base::File write_file) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (detached_)
return;
callback.Run(result, process_handle, read_file.Pass(), write_file.Pass());
}
void NativeProcessLauncherImpl::Core::PostErrorResult(
const LaunchedCallback& callback,
LaunchResult error) {
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::Bind(&NativeProcessLauncherImpl::Core::CallCallbackOnIOThread, this,
callback, error, base::kNullProcessHandle,
Passed(base::File()), Passed(base::File())));
}
void NativeProcessLauncherImpl::Core::PostResult(
const LaunchedCallback& callback,
base::ProcessHandle process_handle,
base::File read_file,
base::File write_file) {
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::Bind(&NativeProcessLauncherImpl::Core::CallCallbackOnIOThread, this,
callback, RESULT_SUCCESS, process_handle,
Passed(read_file.Pass()), Passed(write_file.Pass())));
}
NativeProcessLauncherImpl::NativeProcessLauncherImpl(
bool allow_user_level_hosts,
intptr_t window_handle)
: core_(new Core(allow_user_level_hosts, window_handle)) {
}
NativeProcessLauncherImpl::~NativeProcessLauncherImpl() {
core_->Detach();
}
void NativeProcessLauncherImpl::Launch(const GURL& origin,
const std::string& native_host_name,
LaunchedCallback callback) const {
core_->Launch(origin, native_host_name, callback);
}
}
scoped_ptr<NativeProcessLauncher> NativeProcessLauncher::CreateDefault(
bool allow_user_level_hosts,
gfx::NativeView native_view) {
intptr_t window_handle = 0;
#if defined(OS_WIN)
window_handle = reinterpret_cast<intptr_t>(
views::HWNDForNativeView(native_view));
#endif
return scoped_ptr<NativeProcessLauncher>(
new NativeProcessLauncherImpl(allow_user_level_hosts, window_handle));
}
}