This source file includes following definitions.
- GetFactoryInstance
- Get
- browser_context_
- OnSocketConnect
- OnSocketResume
- StartSocketRead
- StartRead
- ReadCallback
- PostEvent
- DispatchEvent
#include "extensions/browser/api/sockets_tcp/tcp_socket_event_dispatcher.h"
#include "extensions/browser/api/socket/tcp_socket.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/extensions_browser_client.h"
#include "net/base/net_errors.h"
namespace {
int kDefaultBufferSize = 4096;
}
namespace extensions {
namespace core_api {
using content::BrowserThread;
static base::LazyInstance<
BrowserContextKeyedAPIFactory<TCPSocketEventDispatcher> > g_factory =
LAZY_INSTANCE_INITIALIZER;
BrowserContextKeyedAPIFactory<TCPSocketEventDispatcher>*
TCPSocketEventDispatcher::GetFactoryInstance() {
return g_factory.Pointer();
}
TCPSocketEventDispatcher* TCPSocketEventDispatcher::Get(
content::BrowserContext* context) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
return BrowserContextKeyedAPIFactory<TCPSocketEventDispatcher>::Get(context);
}
TCPSocketEventDispatcher::TCPSocketEventDispatcher(
content::BrowserContext* context)
: thread_id_(Socket::kThreadId), browser_context_(context) {
ApiResourceManager<ResumableTCPSocket>* manager =
ApiResourceManager<ResumableTCPSocket>::Get(browser_context_);
DCHECK(manager)
<< "There is no socket manager. "
"If this assertion is failing during a test, then it is likely that "
"TestExtensionSystem is failing to provide an instance of "
"ApiResourceManager<ResumableTCPSocket>.";
sockets_ = manager->data_;
}
TCPSocketEventDispatcher::~TCPSocketEventDispatcher() {}
TCPSocketEventDispatcher::ReadParams::ReadParams() {}
TCPSocketEventDispatcher::ReadParams::~ReadParams() {}
void TCPSocketEventDispatcher::OnSocketConnect(const std::string& extension_id,
int socket_id) {
DCHECK_CURRENTLY_ON(thread_id_);
StartSocketRead(extension_id, socket_id);
}
void TCPSocketEventDispatcher::OnSocketResume(const std::string& extension_id,
int socket_id) {
DCHECK_CURRENTLY_ON(thread_id_);
StartSocketRead(extension_id, socket_id);
}
void TCPSocketEventDispatcher::StartSocketRead(const std::string& extension_id,
int socket_id) {
DCHECK_CURRENTLY_ON(thread_id_);
ReadParams params;
params.thread_id = thread_id_;
params.browser_context_id = browser_context_;
params.extension_id = extension_id;
params.sockets = sockets_;
params.socket_id = socket_id;
StartRead(params);
}
void TCPSocketEventDispatcher::StartRead(const ReadParams& params) {
DCHECK_CURRENTLY_ON(params.thread_id);
ResumableTCPSocket* socket =
params.sockets->Get(params.extension_id, params.socket_id);
if (!socket) {
return;
}
DCHECK(params.extension_id == socket->owner_extension_id())
<< "Socket has wrong owner.";
if (socket->paused())
return;
int buffer_size = socket->buffer_size();
if (buffer_size <= 0)
buffer_size = kDefaultBufferSize;
socket->Read(buffer_size,
base::Bind(&TCPSocketEventDispatcher::ReadCallback, params));
}
void TCPSocketEventDispatcher::ReadCallback(
const ReadParams& params,
int bytes_read,
scoped_refptr<net::IOBuffer> io_buffer) {
DCHECK_CURRENTLY_ON(params.thread_id);
if (bytes_read == 0) {
bytes_read = net::ERR_CONNECTION_CLOSED;
}
if (bytes_read > 0) {
sockets_tcp::ReceiveInfo receive_info;
receive_info.socket_id = params.socket_id;
receive_info.data = std::string(io_buffer->data(), bytes_read);
scoped_ptr<base::ListValue> args =
sockets_tcp::OnReceive::Create(receive_info);
scoped_ptr<Event> event(
new Event(sockets_tcp::OnReceive::kEventName, args.Pass()));
PostEvent(params, event.Pass());
BrowserThread::PostTask(
params.thread_id,
FROM_HERE,
base::Bind(&TCPSocketEventDispatcher::StartRead, params));
} else if (bytes_read == net::ERR_IO_PENDING) {
} else {
sockets_tcp::ReceiveErrorInfo receive_error_info;
receive_error_info.socket_id = params.socket_id;
receive_error_info.result_code = bytes_read;
scoped_ptr<base::ListValue> args =
sockets_tcp::OnReceiveError::Create(receive_error_info);
scoped_ptr<Event> event(
new Event(sockets_tcp::OnReceiveError::kEventName, args.Pass()));
PostEvent(params, event.Pass());
ResumableTCPSocket* socket =
params.sockets->Get(params.extension_id, params.socket_id);
if (socket) {
socket->set_paused(true);
}
}
}
void TCPSocketEventDispatcher::PostEvent(const ReadParams& params,
scoped_ptr<Event> event) {
DCHECK_CURRENTLY_ON(params.thread_id);
BrowserThread::PostTask(BrowserThread::UI,
FROM_HERE,
base::Bind(&DispatchEvent,
params.browser_context_id,
params.extension_id,
base::Passed(event.Pass())));
}
void TCPSocketEventDispatcher::DispatchEvent(void* browser_context_id,
const std::string& extension_id,
scoped_ptr<Event> event) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
content::BrowserContext* context =
reinterpret_cast<content::BrowserContext*>(browser_context_id);
if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context))
return;
EventRouter* router = ExtensionSystem::Get(context)->event_router();
if (router)
router->DispatchEventToExtension(extension_id, event.Pass());
}
}
}