Chromium Code Reviews| Index: chrome/browser/extensions/socket_api.cc |
| diff --git a/chrome/browser/extensions/socket_api.cc b/chrome/browser/extensions/socket_api.cc |
| index 27d3bb348e0fd03a1006300efdd11786673148ab..ef5f9906953b53844b62f3e8bf66e49e6047aa9c 100644 |
| --- a/chrome/browser/extensions/socket_api.cc |
| +++ b/chrome/browser/extensions/socket_api.cc |
| @@ -6,29 +6,27 @@ |
| #include "base/bind.h" |
| #include "base/values.h" |
| +#include "chrome/browser/extensions/socket_api_constants.h" |
|
Mihai Parparita -not on Chrome
2011/12/01 23:39:32
Per Aaron separate constants.h/.cc files are no lo
|
| +#include "chrome/browser/extensions/socket_api_controller.h" |
| +#include "content/public/browser/browser_thread.h" |
| #include "content/public/browser/browser_thread.h" |
| using content::BrowserThread; |
| namespace extensions { |
| -SocketCreateFunction::SocketCreateFunction() { |
| -} |
| - |
| -SocketCreateFunction::~SocketCreateFunction() { |
| -} |
| +namespace constants = socket_api_constants; |
| bool SocketCreateFunction::RunImpl() { |
| std::string socket_type; |
| EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &socket_type)); |
| - // TODO(miket): this constitutes a second form of truth as to the |
| - // enum validity. But our unit-test framework skips the enum |
| - // validation. So in order to get an invalid-enum test to pass, we |
| - // need duplicative value-checking. Too bad. Fix this if/when the |
| - // argument validation code is moved to C++ rather than its current |
| - // JavaScript form. |
| - if (socket_type != "udp") { |
| + // TODO(miket): this constitutes a second form of truth as to the enum |
| + // validity. But our unit-test framework skips the enum validation. So in |
| + // order to get an invalid-enum test to pass, we need duplicative |
| + // value-checking. Too bad. Fix this if/when the argument validation code is |
| + // moved to C++ rather than its current JavaScript form. |
| + if (socket_type != constants::kUdpSocketType) { |
|
Mihai Parparita -not on Chrome
2011/12/01 23:39:32
Per Aaron's feedback in http://codereview.chromium
miket_OOO
2011/12/02 21:06:36
OK. I must have picked the wrong examples in the s
|
| return false; |
| } |
| @@ -42,8 +40,13 @@ bool SocketCreateFunction::RunImpl() { |
| void SocketCreateFunction::WorkOnIOThread() { |
|
Mihai Parparita -not on Chrome
2011/12/01 23:39:32
Still too early to refactor this to avoid some of
miket_OOO
2011/12/02 21:06:36
Yes. But don't worry, it'll be done.
|
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| DictionaryValue* result = new DictionaryValue(); |
| - result->SetInteger("socketId", 42); |
| + SocketController* controller = SocketController::GetInstance(); |
| + |
| + int socket_id = controller->CreateUdp(profile(), extension_id(), |
| + source_url()); |
| + result->SetInteger(constants::kSocketIdKey, socket_id); |
| result_.reset(result); |
| + |
| bool rv = BrowserThread::PostTask( |
| BrowserThread::UI, FROM_HERE, |
| base::Bind(&SocketCreateFunction::RespondOnUIThread, this)); |
| @@ -55,15 +58,36 @@ void SocketCreateFunction::RespondOnUIThread() { |
| SendResponse(true); |
| } |
| -SocketConnectFunction::SocketConnectFunction() { |
| +bool SocketDestroyFunction::RunImpl() { |
| + 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; |
| } |
| -SocketConnectFunction::~SocketConnectFunction() { |
| +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); |
| } |
| bool SocketConnectFunction::RunImpl() { |
| - std::string socket_type; |
| - EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &socket_type)); |
| + 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, |
| @@ -74,7 +98,11 @@ bool SocketConnectFunction::RunImpl() { |
| void SocketConnectFunction::WorkOnIOThread() { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| - result_.reset(Value::CreateIntegerValue(4)); |
| + |
| + SocketController* controller = SocketController::GetInstance(); |
| + 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)); |
| @@ -86,64 +114,61 @@ void SocketConnectFunction::RespondOnUIThread() { |
| SendResponse(true); |
| } |
| -SocketDisconnectFunction::SocketDisconnectFunction() { |
| -} |
| - |
| -SocketDisconnectFunction::~SocketDisconnectFunction() { |
| -} |
| - |
| -bool SocketDisconnectFunction::RunImpl() { |
| - std::string socket_type; |
| - EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &socket_type)); |
| +bool SocketCloseFunction::RunImpl() { |
| + EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| bool rv = BrowserThread::PostTask( |
| BrowserThread::IO, FROM_HERE, |
| - base::Bind(&SocketDisconnectFunction::WorkOnIOThread, this)); |
| + base::Bind(&SocketCloseFunction::WorkOnIOThread, this)); |
| DCHECK(rv); |
| return true; |
| } |
| -void SocketDisconnectFunction::WorkOnIOThread() { |
| +void SocketCloseFunction::WorkOnIOThread() { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| - result_.reset(Value::CreateIntegerValue(4)); |
| + |
| + SocketController* controller = SocketController::GetInstance(); |
| + controller->CloseUdp(socket_id_); |
| + result_.reset(Value::CreateNullValue()); |
| + |
| bool rv = BrowserThread::PostTask( |
| BrowserThread::UI, FROM_HERE, |
| - base::Bind(&SocketDisconnectFunction::RespondOnUIThread, this)); |
| + base::Bind(&SocketCloseFunction::RespondOnUIThread, this)); |
| DCHECK(rv); |
| } |
| -void SocketDisconnectFunction::RespondOnUIThread() { |
| +void SocketCloseFunction::RespondOnUIThread() { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| SendResponse(true); |
| } |
| -SocketSendFunction::SocketSendFunction() { |
| -} |
| - |
| -SocketSendFunction::~SocketSendFunction() { |
| -} |
| - |
| -bool SocketSendFunction::RunImpl() { |
| - std::string socket_type; |
| - EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &socket_type)); |
| +bool SocketWriteFunction::RunImpl() { |
| + 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(&SocketSendFunction::WorkOnIOThread, this)); |
| + base::Bind(&SocketWriteFunction::WorkOnIOThread, this)); |
| DCHECK(rv); |
| return true; |
| } |
| -void SocketSendFunction::WorkOnIOThread() { |
| +void SocketWriteFunction::WorkOnIOThread() { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| - result_.reset(Value::CreateIntegerValue(4)); |
| + |
| + SocketController* controller = SocketController::GetInstance(); |
| + int bytesWritten = controller->WriteUdp(socket_id_, message_); |
| + |
| + DictionaryValue* result = new DictionaryValue(); |
| + result->SetInteger(constants::kBytesWrittenKey, bytesWritten); |
| + result_.reset(result); |
| bool rv = BrowserThread::PostTask( |
| BrowserThread::UI, FROM_HERE, |
| - base::Bind(&SocketSendFunction::RespondOnUIThread, this)); |
| + base::Bind(&SocketWriteFunction::RespondOnUIThread, this)); |
| DCHECK(rv); |
| } |
| -void SocketSendFunction::RespondOnUIThread() { |
| +void SocketWriteFunction::RespondOnUIThread() { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| SendResponse(true); |
| } |