| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/browser/api/serial/serial_event_dispatcher.h" | 5 #include "extensions/browser/api/serial/serial_event_dispatcher.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "extensions/browser/api/serial/serial_connection.h" | 10 #include "extensions/browser/api/serial/serial_connection.h" |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 const std::vector<char>& data, | 100 const std::vector<char>& data, |
| 101 serial::ReceiveError error) { | 101 serial::ReceiveError error) { |
| 102 DCHECK_CURRENTLY_ON(params.thread_id); | 102 DCHECK_CURRENTLY_ON(params.thread_id); |
| 103 | 103 |
| 104 // Note that an error (e.g. timeout) does not necessarily mean that no data | 104 // Note that an error (e.g. timeout) does not necessarily mean that no data |
| 105 // was read, so we may fire an onReceive regardless of any error code. | 105 // was read, so we may fire an onReceive regardless of any error code. |
| 106 if (data.size() > 0) { | 106 if (data.size() > 0) { |
| 107 serial::ReceiveInfo receive_info; | 107 serial::ReceiveInfo receive_info; |
| 108 receive_info.connection_id = params.connection_id; | 108 receive_info.connection_id = params.connection_id; |
| 109 receive_info.data = data; | 109 receive_info.data = data; |
| 110 scoped_ptr<base::ListValue> args = serial::OnReceive::Create(receive_info); | 110 std::unique_ptr<base::ListValue> args = |
| 111 scoped_ptr<extensions::Event> event( | 111 serial::OnReceive::Create(receive_info); |
| 112 std::unique_ptr<extensions::Event> event( |
| 112 new extensions::Event(extensions::events::SERIAL_ON_RECEIVE, | 113 new extensions::Event(extensions::events::SERIAL_ON_RECEIVE, |
| 113 serial::OnReceive::kEventName, std::move(args))); | 114 serial::OnReceive::kEventName, std::move(args))); |
| 114 PostEvent(params, std::move(event)); | 115 PostEvent(params, std::move(event)); |
| 115 } | 116 } |
| 116 | 117 |
| 117 if (error != serial::RECEIVE_ERROR_NONE) { | 118 if (error != serial::RECEIVE_ERROR_NONE) { |
| 118 serial::ReceiveErrorInfo error_info; | 119 serial::ReceiveErrorInfo error_info; |
| 119 error_info.connection_id = params.connection_id; | 120 error_info.connection_id = params.connection_id; |
| 120 error_info.error = error; | 121 error_info.error = error; |
| 121 scoped_ptr<base::ListValue> args = | 122 std::unique_ptr<base::ListValue> args = |
| 122 serial::OnReceiveError::Create(error_info); | 123 serial::OnReceiveError::Create(error_info); |
| 123 scoped_ptr<extensions::Event> event(new extensions::Event( | 124 std::unique_ptr<extensions::Event> event(new extensions::Event( |
| 124 extensions::events::SERIAL_ON_RECEIVE_ERROR, | 125 extensions::events::SERIAL_ON_RECEIVE_ERROR, |
| 125 serial::OnReceiveError::kEventName, std::move(args))); | 126 serial::OnReceiveError::kEventName, std::move(args))); |
| 126 PostEvent(params, std::move(event)); | 127 PostEvent(params, std::move(event)); |
| 127 if (ShouldPauseOnReceiveError(error)) { | 128 if (ShouldPauseOnReceiveError(error)) { |
| 128 SerialConnection* connection = | 129 SerialConnection* connection = |
| 129 params.connections->Get(params.extension_id, params.connection_id); | 130 params.connections->Get(params.extension_id, params.connection_id); |
| 130 if (connection) | 131 if (connection) |
| 131 connection->set_paused(true); | 132 connection->set_paused(true); |
| 132 } | 133 } |
| 133 } | 134 } |
| 134 | 135 |
| 135 // Queue up the next read operation. | 136 // Queue up the next read operation. |
| 136 BrowserThread::PostTask( | 137 BrowserThread::PostTask( |
| 137 params.thread_id, FROM_HERE, base::Bind(&StartReceive, params)); | 138 params.thread_id, FROM_HERE, base::Bind(&StartReceive, params)); |
| 138 } | 139 } |
| 139 | 140 |
| 140 // static | 141 // static |
| 141 void SerialEventDispatcher::PostEvent(const ReceiveParams& params, | 142 void SerialEventDispatcher::PostEvent( |
| 142 scoped_ptr<extensions::Event> event) { | 143 const ReceiveParams& params, |
| 144 std::unique_ptr<extensions::Event> event) { |
| 143 DCHECK_CURRENTLY_ON(params.thread_id); | 145 DCHECK_CURRENTLY_ON(params.thread_id); |
| 144 | 146 |
| 145 BrowserThread::PostTask( | 147 BrowserThread::PostTask( |
| 146 BrowserThread::UI, FROM_HERE, | 148 BrowserThread::UI, FROM_HERE, |
| 147 base::Bind(&DispatchEvent, params.browser_context_id, params.extension_id, | 149 base::Bind(&DispatchEvent, params.browser_context_id, params.extension_id, |
| 148 base::Passed(std::move(event)))); | 150 base::Passed(std::move(event)))); |
| 149 } | 151 } |
| 150 | 152 |
| 151 // static | 153 // static |
| 152 void SerialEventDispatcher::DispatchEvent(void* browser_context_id, | 154 void SerialEventDispatcher::DispatchEvent( |
| 153 const std::string& extension_id, | 155 void* browser_context_id, |
| 154 scoped_ptr<extensions::Event> event) { | 156 const std::string& extension_id, |
| 157 std::unique_ptr<extensions::Event> event) { |
| 155 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 158 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 156 | 159 |
| 157 content::BrowserContext* context = | 160 content::BrowserContext* context = |
| 158 reinterpret_cast<content::BrowserContext*>(browser_context_id); | 161 reinterpret_cast<content::BrowserContext*>(browser_context_id); |
| 159 if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context)) | 162 if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context)) |
| 160 return; | 163 return; |
| 161 | 164 |
| 162 EventRouter* router = EventRouter::Get(context); | 165 EventRouter* router = EventRouter::Get(context); |
| 163 if (router) | 166 if (router) |
| 164 router->DispatchEventToExtension(extension_id, std::move(event)); | 167 router->DispatchEventToExtension(extension_id, std::move(event)); |
| 165 } | 168 } |
| 166 | 169 |
| 167 } // namespace api | 170 } // namespace api |
| 168 | 171 |
| 169 } // namespace extensions | 172 } // namespace extensions |
| OLD | NEW |