| Index: chrome/browser/extensions/api/socket/socket_api.cc
|
| diff --git a/chrome/browser/extensions/api/socket/socket_api.cc b/chrome/browser/extensions/api/socket/socket_api.cc
|
| index 4d18ff918c703433b7dba2076144aa71e44ced6c..f2cf86c0b1422d008d0171d5563817e7fcf40b5c 100644
|
| --- a/chrome/browser/extensions/api/socket/socket_api.cc
|
| +++ b/chrome/browser/extensions/api/socket/socket_api.cc
|
| @@ -7,6 +7,8 @@
|
| #include "base/bind.h"
|
| #include "base/values.h"
|
| #include "chrome/browser/extensions/api/socket/socket_api_controller.h"
|
| +#include "chrome/browser/extensions/extension_service.h"
|
| +#include "chrome/browser/profiles/profile.h"
|
| #include "content/public/browser/browser_thread.h"
|
| #include "content/public/browser/browser_thread.h"
|
|
|
| @@ -18,13 +20,36 @@ const char kBytesWrittenKey[] = "bytesWritten";
|
| const char kSocketIdKey[] = "socketId";
|
| const char kUDPSocketType[] = "udp";
|
|
|
| -SocketCreateFunction::SocketCreateFunction() {
|
| +SocketController* SocketApiFunction::controller() {
|
| + return profile()->GetExtensionService()->socket_controller();
|
| }
|
|
|
| -SocketCreateFunction::~SocketCreateFunction() {
|
| +bool SocketApiFunction::RunImpl() {
|
| + if (!Prepare()) {
|
| + return false;
|
| + }
|
| + bool rv = BrowserThread::PostTask(
|
| + BrowserThread::IO, FROM_HERE,
|
| + base::Bind(&SocketApiFunction::WorkOnIOThread, this));
|
| + DCHECK(rv);
|
| + return true;
|
| +}
|
| +
|
| +void SocketApiFunction::WorkOnIOThread() {
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
| + Work();
|
| + bool rv = BrowserThread::PostTask(
|
| + BrowserThread::UI, FROM_HERE,
|
| + base::Bind(&SocketApiFunction::RespondOnUIThread, this));
|
| + DCHECK(rv);
|
| +}
|
| +
|
| +void SocketApiFunction::RespondOnUIThread() {
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
|
| + SendResponse(Respond());
|
| }
|
|
|
| -bool SocketCreateFunction::RunImpl() {
|
| +bool SocketCreateFunction::Prepare() {
|
| std::string socket_type;
|
| EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &socket_type));
|
|
|
| @@ -36,172 +61,81 @@ bool SocketCreateFunction::RunImpl() {
|
| if (socket_type != kUDPSocketType) {
|
| return false;
|
| }
|
| -
|
| - bool rv = BrowserThread::PostTask(
|
| - BrowserThread::IO, FROM_HERE,
|
| - base::Bind(&SocketCreateFunction::WorkOnIOThread, this));
|
| - DCHECK(rv);
|
| return true;
|
| }
|
|
|
| -void SocketCreateFunction::WorkOnIOThread() {
|
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
| +void SocketCreateFunction::Work() {
|
| DictionaryValue* result = new DictionaryValue();
|
| - SocketController* controller = SocketController::GetInstance();
|
| -
|
| - int socket_id = controller->CreateUdp(profile(), extension_id(),
|
| - source_url());
|
| + int socket_id = controller()->CreateUdp(profile(), extension_id(),
|
| + source_url());
|
| result->SetInteger(kSocketIdKey, socket_id);
|
| result_.reset(result);
|
|
|
| - bool rv = BrowserThread::PostTask(
|
| - BrowserThread::UI, FROM_HERE,
|
| - base::Bind(&SocketCreateFunction::RespondOnUIThread, this));
|
| - DCHECK(rv);
|
| -}
|
| -
|
| -void SocketCreateFunction::RespondOnUIThread() {
|
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
|
| - SendResponse(true);
|
| }
|
|
|
| -SocketDestroyFunction::SocketDestroyFunction() {
|
| -}
|
| -
|
| -SocketDestroyFunction::~SocketDestroyFunction() {
|
| +bool SocketCreateFunction::Respond() {
|
| + return true;
|
| }
|
|
|
| -bool SocketDestroyFunction::RunImpl() {
|
| +bool SocketDestroyFunction::Prepare() {
|
| EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
|
| -
|
| - bool rv = BrowserThread::PostTask(
|
| - BrowserThread::IO, FROM_HERE,
|
| - base::Bind(&SocketDestroyFunction::WorkOnIOThread, this));
|
| - DCHECK(rv);
|
| return true;
|
| }
|
|
|
| -void SocketDestroyFunction::WorkOnIOThread() {
|
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
| - SocketController* controller = SocketController::GetInstance();
|
| - controller->DestroyUdp(socket_id_);
|
| -
|
| - bool rv = BrowserThread::PostTask(
|
| - BrowserThread::UI, FROM_HERE,
|
| - base::Bind(&SocketDestroyFunction::RespondOnUIThread, this));
|
| - DCHECK(rv);
|
| -}
|
| -
|
| -void SocketDestroyFunction::RespondOnUIThread() {
|
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
|
| - SendResponse(true);
|
| -}
|
| -
|
| -SocketConnectFunction::SocketConnectFunction() {
|
| +void SocketDestroyFunction::Work() {
|
| + controller()->DestroyUdp(socket_id_);
|
| }
|
|
|
| -SocketConnectFunction::~SocketConnectFunction() {
|
| +bool SocketDestroyFunction::Respond() {
|
| + return true;
|
| }
|
|
|
| -bool SocketConnectFunction::RunImpl() {
|
| +bool SocketConnectFunction::Prepare() {
|
| EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
|
| EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &address_));
|
| EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_));
|
| -
|
| - bool rv = BrowserThread::PostTask(
|
| - BrowserThread::IO, FROM_HERE,
|
| - base::Bind(&SocketConnectFunction::WorkOnIOThread, this));
|
| - DCHECK(rv);
|
| return true;
|
| }
|
|
|
| -void SocketConnectFunction::WorkOnIOThread() {
|
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
| -
|
| - SocketController* controller = SocketController::GetInstance();
|
| - bool result = controller->ConnectUdp(socket_id_, address_, port_);
|
| +void SocketConnectFunction::Work() {
|
| + bool result = controller()->ConnectUdp(socket_id_, address_, port_);
|
| result_.reset(Value::CreateBooleanValue(result));
|
| -
|
| - bool rv = BrowserThread::PostTask(
|
| - BrowserThread::UI, FROM_HERE,
|
| - base::Bind(&SocketConnectFunction::RespondOnUIThread, this));
|
| - DCHECK(rv);
|
| }
|
|
|
| -void SocketConnectFunction::RespondOnUIThread() {
|
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
|
| - SendResponse(true);
|
| -}
|
| -
|
| -SocketCloseFunction::SocketCloseFunction() {
|
| -}
|
| -
|
| -SocketCloseFunction::~SocketCloseFunction() {
|
| +bool SocketConnectFunction::Respond() {
|
| + return true;
|
| }
|
|
|
| -bool SocketCloseFunction::RunImpl() {
|
| +bool SocketCloseFunction::Prepare() {
|
| EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
|
| -
|
| - bool rv = BrowserThread::PostTask(
|
| - BrowserThread::IO, FROM_HERE,
|
| - base::Bind(&SocketCloseFunction::WorkOnIOThread, this));
|
| - DCHECK(rv);
|
| return true;
|
| }
|
|
|
| -void SocketCloseFunction::WorkOnIOThread() {
|
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
| -
|
| - SocketController* controller = SocketController::GetInstance();
|
| - controller->CloseUdp(socket_id_);
|
| +void SocketCloseFunction::Work() {
|
| + controller()->CloseUdp(socket_id_);
|
| result_.reset(Value::CreateNullValue());
|
| -
|
| - bool rv = BrowserThread::PostTask(
|
| - BrowserThread::UI, FROM_HERE,
|
| - base::Bind(&SocketCloseFunction::RespondOnUIThread, this));
|
| - DCHECK(rv);
|
| }
|
|
|
| -void SocketCloseFunction::RespondOnUIThread() {
|
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
|
| - SendResponse(true);
|
| -}
|
| -
|
| -SocketWriteFunction::SocketWriteFunction() {
|
| -}
|
| -
|
| -SocketWriteFunction::~SocketWriteFunction() {
|
| +bool SocketCloseFunction::Respond() {
|
| + return true;
|
| }
|
|
|
| -bool SocketWriteFunction::RunImpl() {
|
| +bool SocketWriteFunction::Prepare() {
|
| EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
|
| EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &message_));
|
| -
|
| - bool rv = BrowserThread::PostTask(
|
| - BrowserThread::IO, FROM_HERE,
|
| - base::Bind(&SocketWriteFunction::WorkOnIOThread, this));
|
| - DCHECK(rv);
|
| return true;
|
| }
|
|
|
| -void SocketWriteFunction::WorkOnIOThread() {
|
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
| -
|
| - SocketController* controller = SocketController::GetInstance();
|
| - int bytesWritten = controller->WriteUdp(socket_id_, message_);
|
| +void SocketWriteFunction::Work() {
|
| + int bytesWritten = controller()->WriteUdp(socket_id_, message_);
|
|
|
| DictionaryValue* result = new DictionaryValue();
|
| result->SetInteger(kBytesWrittenKey, bytesWritten);
|
| result_.reset(result);
|
| - bool rv = BrowserThread::PostTask(
|
| - BrowserThread::UI, FROM_HERE,
|
| - base::Bind(&SocketWriteFunction::RespondOnUIThread, this));
|
| - DCHECK(rv);
|
| }
|
|
|
| -void SocketWriteFunction::RespondOnUIThread() {
|
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
|
| - SendResponse(true);
|
| +bool SocketWriteFunction::Respond() {
|
| + return true;
|
| }
|
|
|
| } // namespace extensions
|
|
|