Skip to content

Commit

Permalink
[TaskScheduler] Update terminal_private_api to use TaskScheduler.
Browse files Browse the repository at this point in the history
FILE BrowserThread is deprecrated. Make terminal_private_api
post tasks to TaskScheduler.

Bug: 689520
Change-Id: Idd71fde7c5f3744816016d82e8d73535f0ceeb6c
Reviewed-on: https://chromium-review.googlesource.com/578712
Commit-Queue: Istiaque Ahmed <lazyboy@chromium.org>
Reviewed-by: Gabriel Charette <gab@chromium.org>
Reviewed-by: Toni Barzic <tbarzic@chromium.org>
Cr-Commit-Position: refs/heads/master@{#489433}
  • Loading branch information
Istiaque Ahmed authored and Commit Bot committed Jul 25, 2017
1 parent b99830c commit 0372159
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 57 deletions.
66 changes: 35 additions & 31 deletions chrome/browser/extensions/api/terminal/terminal_private_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "base/json/json_writer.h"
#include "base/memory/ptr_util.h"
#include "base/sys_info.h"
#include "base/task_scheduler/post_task.h"
#include "base/values.h"
#include "chrome/browser/extensions/api/terminal/terminal_extension_helper.h"
#include "chrome/browser/extensions/extension_service.h"
Expand Down Expand Up @@ -140,11 +141,12 @@ TerminalPrivateOpenTerminalProcessFunction::Run() {
if (tab_id < 0)
return RespondNow(Error("Not called from a tab or app window"));

// Registry lives on FILE thread.
content::BrowserThread::PostTask(
content::BrowserThread::FILE, FROM_HERE,
// Registry lives on its own task runner.
chromeos::ProcessProxyRegistry::GetTaskRunner()->PostTask(
FROM_HERE,
base::BindOnce(
&TerminalPrivateOpenTerminalProcessFunction::OpenOnFileThread, this,
&TerminalPrivateOpenTerminalProcessFunction::OpenOnRegistryTaskRunner,
this,
base::Bind(&NotifyProcessOutput, browser_context(), extension_id(),
tab_id),
base::Bind(
Expand All @@ -153,7 +155,7 @@ TerminalPrivateOpenTerminalProcessFunction::Run() {
return RespondLater();
}

void TerminalPrivateOpenTerminalProcessFunction::OpenOnFileThread(
void TerminalPrivateOpenTerminalProcessFunction::OpenOnRegistryTaskRunner(
const ProcessOutputCallback& output_callback,
const OpenProcessCallback& callback) {
DCHECK(!command_.empty());
Expand Down Expand Up @@ -182,15 +184,16 @@ ExtensionFunction::ResponseAction TerminalPrivateSendInputFunction::Run() {
std::unique_ptr<SendInput::Params> params(SendInput::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());

// Registry lives on the FILE thread.
content::BrowserThread::PostTask(
content::BrowserThread::FILE, FROM_HERE,
base::BindOnce(&TerminalPrivateSendInputFunction::SendInputOnFileThread,
this, params->pid, params->input));
// Registry lives on its own task runner.
chromeos::ProcessProxyRegistry::GetTaskRunner()->PostTask(
FROM_HERE,
base::BindOnce(
&TerminalPrivateSendInputFunction::SendInputOnRegistryTaskRunner,
this, params->pid, params->input));
return RespondLater();
}

void TerminalPrivateSendInputFunction::SendInputOnFileThread(
void TerminalPrivateSendInputFunction::SendInputOnRegistryTaskRunner(
int terminal_id,
const std::string& text) {
bool success =
Expand All @@ -215,17 +218,16 @@ TerminalPrivateCloseTerminalProcessFunction::Run() {
CloseTerminalProcess::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());

// Registry lives on the FILE thread.
content::BrowserThread::PostTask(
content::BrowserThread::FILE, FROM_HERE,
base::BindOnce(
&TerminalPrivateCloseTerminalProcessFunction::CloseOnFileThread, this,
params->pid));
// Registry lives on its own task runner.
chromeos::ProcessProxyRegistry::GetTaskRunner()->PostTask(
FROM_HERE, base::BindOnce(&TerminalPrivateCloseTerminalProcessFunction::
CloseOnRegistryTaskRunner,
this, params->pid));

return RespondLater();
}

void TerminalPrivateCloseTerminalProcessFunction::CloseOnFileThread(
void TerminalPrivateCloseTerminalProcessFunction::CloseOnRegistryTaskRunner(
int terminal_id) {
bool success =
chromeos::ProcessProxyRegistry::Get()->CloseProcess(terminal_id);
Expand All @@ -251,17 +253,17 @@ TerminalPrivateOnTerminalResizeFunction::Run() {
OnTerminalResize::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());

// Registry lives on the FILE thread.
content::BrowserThread::PostTask(
content::BrowserThread::FILE, FROM_HERE,
base::BindOnce(
&TerminalPrivateOnTerminalResizeFunction::OnResizeOnFileThread, this,
params->pid, params->width, params->height));
// Registry lives on its own task runner.
chromeos::ProcessProxyRegistry::GetTaskRunner()->PostTask(
FROM_HERE,
base::BindOnce(&TerminalPrivateOnTerminalResizeFunction::
OnResizeOnRegistryTaskRunner,
this, params->pid, params->width, params->height));

return RespondLater();
}

void TerminalPrivateOnTerminalResizeFunction::OnResizeOnFileThread(
void TerminalPrivateOnTerminalResizeFunction::OnResizeOnRegistryTaskRunner(
int terminal_id,
int width,
int height) {
Expand Down Expand Up @@ -296,16 +298,18 @@ ExtensionFunction::ResponseAction TerminalPrivateAckOutputFunction::Run() {
if (tab_id != params->tab_id)
return RespondNow(NoArguments());

// Registry lives on the FILE thread.
content::BrowserThread::PostTask(
content::BrowserThread::FILE, FROM_HERE,
base::BindOnce(&TerminalPrivateAckOutputFunction::AckOutputOnFileThread,
this, params->pid));
// Registry lives on its own task runner.
chromeos::ProcessProxyRegistry::GetTaskRunner()->PostTask(
FROM_HERE,
base::BindOnce(
&TerminalPrivateAckOutputFunction::AckOutputOnRegistryTaskRunner,
this, params->pid));

return RespondNow(NoArguments());
}

void TerminalPrivateAckOutputFunction::AckOutputOnFileThread(int terminal_id) {
void TerminalPrivateAckOutputFunction::AckOutputOnRegistryTaskRunner(
int terminal_id) {
chromeos::ProcessProxyRegistry::Get()->AckOutput(terminal_id);
}

Expand Down
12 changes: 6 additions & 6 deletions chrome/browser/extensions/api/terminal/terminal_private_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class TerminalPrivateOpenTerminalProcessFunction
const std::string& output)>;
using OpenProcessCallback = base::Callback<void(int terminal_id)>;

void OpenOnFileThread(const ProcessOutputCallback& output_callback,
const OpenProcessCallback& callback);
void OpenOnRegistryTaskRunner(const ProcessOutputCallback& output_callback,
const OpenProcessCallback& callback);
void RespondOnUIThread(int terminal_id);

std::string command_;
Expand All @@ -52,7 +52,7 @@ class TerminalPrivateSendInputFunction : public UIThreadExtensionFunction {
ExtensionFunction::ResponseAction Run() override;

private:
void SendInputOnFileThread(int terminal_id, const std::string& input);
void SendInputOnRegistryTaskRunner(int terminal_id, const std::string& input);
void RespondOnUIThread(bool success);
};

Expand All @@ -69,7 +69,7 @@ class TerminalPrivateCloseTerminalProcessFunction
ExtensionFunction::ResponseAction Run() override;

private:
void CloseOnFileThread(int terminal_id);
void CloseOnRegistryTaskRunner(int terminal_id);
void RespondOnUIThread(bool success);
};

Expand All @@ -86,7 +86,7 @@ class TerminalPrivateOnTerminalResizeFunction
ExtensionFunction::ResponseAction Run() override;

private:
void OnResizeOnFileThread(int terminal_id, int width, int height);
void OnResizeOnRegistryTaskRunner(int terminal_id, int width, int height);
void RespondOnUIThread(bool success);
};

Expand All @@ -101,7 +101,7 @@ class TerminalPrivateAckOutputFunction : public UIThreadExtensionFunction {
ExtensionFunction::ResponseAction Run() override;

private:
void AckOutputOnFileThread(int terminal_id);
void AckOutputOnRegistryTaskRunner(int terminal_id);
};

} // namespace extensions
Expand Down
3 changes: 2 additions & 1 deletion chromeos/process_proxy/process_proxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ int ProcessProxy::Open(const std::string& command) {

bool ProcessProxy::StartWatchingOutput(
const scoped_refptr<base::SingleThreadTaskRunner>& watcher_runner,
const scoped_refptr<base::SequencedTaskRunner>& callback_runner,
const OutputCallback& callback) {
DCHECK(process_launched_);
CHECK(!output_watcher_.get());
Expand All @@ -78,7 +79,7 @@ bool ProcessProxy::StartWatchingOutput(

callback_set_ = true;
callback_ = callback;
callback_runner_ = base::ThreadTaskRunnerHandle::Get();
callback_runner_ = callback_runner;
watcher_runner_ = watcher_runner;

// This object will delete itself once watching is stopped.
Expand Down
1 change: 1 addition & 0 deletions chromeos/process_proxy/process_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class ProcessProxy : public base::RefCountedThreadSafe<ProcessProxy> {

bool StartWatchingOutput(
const scoped_refptr<base::SingleThreadTaskRunner>& watcher_runner,
const scoped_refptr<base::SequencedTaskRunner>& callback_runner,
const OutputCallback& callback);

// Sends some data to the process.
Expand Down
13 changes: 12 additions & 1 deletion chromeos/process_proxy/process_proxy_registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/sequenced_task_runner.h"
#include "base/task_scheduler/lazy_task_runner.h"

namespace chromeos {

Expand Down Expand Up @@ -68,9 +70,18 @@ void ProcessProxyRegistry::ShutDown() {

// static
ProcessProxyRegistry* ProcessProxyRegistry::Get() {
DCHECK(ProcessProxyRegistry::GetTaskRunner()->RunsTasksInCurrentSequence());
return g_process_proxy_registry.Pointer();
}

// static
scoped_refptr<base::SequencedTaskRunner> ProcessProxyRegistry::GetTaskRunner() {
static base::LazySequencedTaskRunner task_runner =
LAZY_SEQUENCED_TASK_RUNNER_INITIALIZER(
base::TaskTraits({base::MayBlock(), base::TaskPriority::BACKGROUND}));
return task_runner.Get();
}

int ProcessProxyRegistry::OpenProcess(const std::string& command,
const OutputCallback& output_callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
Expand All @@ -89,7 +100,7 @@ int ProcessProxyRegistry::OpenProcess(const std::string& command,
// We can use Unretained because proxy will stop calling callback after it is
// closed, which is done before this object goes away.
if (!proxy->StartWatchingOutput(
watcher_thread_->task_runner(),
watcher_thread_->task_runner(), GetTaskRunner(),
base::Bind(&ProcessProxyRegistry::OnProcessOutput,
base::Unretained(this), terminal_id))) {
proxy->Close();
Expand Down
4 changes: 4 additions & 0 deletions chromeos/process_proxy/process_proxy_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class CHROMEOS_EXPORT ProcessProxyRegistry {

static ProcessProxyRegistry* Get();

// Returns a SequencedTaskRunner where the singleton instance of
// ProcessProxyRegistry lives.
static scoped_refptr<base::SequencedTaskRunner> GetTaskRunner();

// Starts new ProcessProxy (which starts new process).
// Returns ID used for the created process. Returns -1 on failure.
int OpenProcess(const std::string& command, const OutputCallback& callback);
Expand Down
Loading

0 comments on commit 0372159

Please sign in to comment.