1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/api/serial/serial_event_dispatcher.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/api/serial/serial_connection.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "extensions/browser/event_router.h"
namespace extensions {
namespace api {
namespace {
bool ShouldPauseOnReceiveError(serial::ReceiveError error) {
return error == serial::RECEIVE_ERROR_DEVICE_LOST ||
error == serial::RECEIVE_ERROR_SYSTEM_ERROR ||
error == serial::RECEIVE_ERROR_DISCONNECTED;
}
} // namespace
static base::LazyInstance<BrowserContextKeyedAPIFactory<SerialEventDispatcher> >
g_factory = LAZY_INSTANCE_INITIALIZER;
// static
BrowserContextKeyedAPIFactory<SerialEventDispatcher>*
SerialEventDispatcher::GetFactoryInstance() {
return g_factory.Pointer();
}
// static
SerialEventDispatcher* SerialEventDispatcher::Get(
content::BrowserContext* context) {
return BrowserContextKeyedAPIFactory<SerialEventDispatcher>::Get(context);
}
SerialEventDispatcher::SerialEventDispatcher(content::BrowserContext* context)
: thread_id_(SerialConnection::kThreadId),
profile_(Profile::FromBrowserContext(context)) {
ApiResourceManager<SerialConnection>* manager =
ApiResourceManager<SerialConnection>::Get(profile_);
DCHECK(manager) << "No serial connection manager.";
connections_ = manager->data_;
}
SerialEventDispatcher::~SerialEventDispatcher() {}
SerialEventDispatcher::ReceiveParams::ReceiveParams() {}
SerialEventDispatcher::ReceiveParams::~ReceiveParams() {}
void SerialEventDispatcher::PollConnection(const std::string& extension_id,
int connection_id) {
DCHECK_CURRENTLY_ON(thread_id_);
ReceiveParams params;
params.thread_id = thread_id_;
params.profile_id = profile_;
params.extension_id = extension_id;
params.connections = connections_;
params.connection_id = connection_id;
StartReceive(params);
}
// static
void SerialEventDispatcher::StartReceive(const ReceiveParams& params) {
DCHECK_CURRENTLY_ON(params.thread_id);
SerialConnection* connection =
params.connections->Get(params.extension_id, params.connection_id);
if (!connection)
return;
DCHECK(params.extension_id == connection->owner_extension_id());
if (connection->paused())
return;
connection->Receive(base::Bind(&ReceiveCallback, params));
}
// static
void SerialEventDispatcher::ReceiveCallback(const ReceiveParams& params,
const std::string& data,
serial::ReceiveError error) {
DCHECK_CURRENTLY_ON(params.thread_id);
// Note that an error (e.g. timeout) does not necessarily mean that no data
// was read, so we may fire an onReceive regardless of any error code.
if (data.length() > 0) {
serial::ReceiveInfo receive_info;
receive_info.connection_id = params.connection_id;
receive_info.data = data;
scoped_ptr<base::ListValue> args = serial::OnReceive::Create(receive_info);
scoped_ptr<extensions::Event> event(
new extensions::Event(serial::OnReceive::kEventName, args.Pass()));
PostEvent(params, event.Pass());
}
if (error != serial::RECEIVE_ERROR_NONE) {
serial::ReceiveErrorInfo error_info;
error_info.connection_id = params.connection_id;
error_info.error = error;
scoped_ptr<base::ListValue> args =
serial::OnReceiveError::Create(error_info);
scoped_ptr<extensions::Event> event(
new extensions::Event(serial::OnReceiveError::kEventName, args.Pass()));
PostEvent(params, event.Pass());
if (ShouldPauseOnReceiveError(error)) {
SerialConnection* connection =
params.connections->Get(params.extension_id, params.connection_id);
if (connection)
connection->set_paused(true);
}
}
// Queue up the next read operation.
BrowserThread::PostTask(params.thread_id,
FROM_HERE,
base::Bind(&StartReceive, params));
}
// static
void SerialEventDispatcher::PostEvent(const ReceiveParams& params,
scoped_ptr<extensions::Event> event) {
DCHECK_CURRENTLY_ON(params.thread_id);
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&DispatchEvent,
params.profile_id,
params.extension_id,
base::Passed(event.Pass())));
}
// static
void SerialEventDispatcher::DispatchEvent(void* profile_id,
const std::string& extension_id,
scoped_ptr<extensions::Event> event) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
Profile* profile = reinterpret_cast<Profile*>(profile_id);
if (!g_browser_process->profile_manager()->IsValidProfile(profile))
return;
EventRouter* router = EventRouter::Get(profile);
if (router)
router->DispatchEventToExtension(extension_id, event.Pass());
}
} // namespace api
} // namespace extensions
|