OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/serial/serial_api.h" | 5 #include "chrome/browser/extensions/api/serial/serial_api.h" |
6 | 6 |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 #include "chrome/browser/extensions/api/api_resource_controller.h" | 8 #include "chrome/browser/extensions/api/api_resource_controller.h" |
9 #include "chrome/browser/extensions/api/serial/serial_connection.h" | 9 #include "chrome/browser/extensions/api/serial/serial_connection.h" |
10 #include "chrome/browser/extensions/api/serial/serial_port_enumerator.h" | 10 #include "chrome/browser/extensions/api/serial/serial_port_enumerator.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
32 | 32 |
33 ListValue* ports = new ListValue(); | 33 ListValue* ports = new ListValue(); |
34 SerialPortEnumerator::StringSet port_names = | 34 SerialPortEnumerator::StringSet port_names = |
35 SerialPortEnumerator::GenerateValidSerialPortNames(); | 35 SerialPortEnumerator::GenerateValidSerialPortNames(); |
36 SerialPortEnumerator::StringSet::const_iterator i = port_names.begin(); | 36 SerialPortEnumerator::StringSet::const_iterator i = port_names.begin(); |
37 while (i != port_names.end()) { | 37 while (i != port_names.end()) { |
38 ports->Append(Value::CreateStringValue(*i++)); | 38 ports->Append(Value::CreateStringValue(*i++)); |
39 } | 39 } |
40 | 40 |
41 result_.reset(ports); | 41 SetSingleResult(ports); |
42 } | 42 } |
43 | 43 |
44 bool SerialGetPortsFunction::Respond() { | 44 bool SerialGetPortsFunction::Respond() { |
45 return true; | 45 return true; |
46 } | 46 } |
47 | 47 |
48 SerialOpenFunction::SerialOpenFunction() | 48 SerialOpenFunction::SerialOpenFunction() |
49 : src_id_(-1), | 49 : src_id_(-1), |
50 event_notifier_(NULL) {} | 50 event_notifier_(NULL) {} |
51 | 51 |
(...skipping 26 matching lines...) Expand all Loading... |
78 | 78 |
79 bool open_result = serial_connection->Open(); | 79 bool open_result = serial_connection->Open(); |
80 if (!open_result) { | 80 if (!open_result) { |
81 serial_connection->Close(); | 81 serial_connection->Close(); |
82 controller()->RemoveSerialConnection(id); | 82 controller()->RemoveSerialConnection(id); |
83 id = -1; | 83 id = -1; |
84 } | 84 } |
85 | 85 |
86 DictionaryValue* result = new DictionaryValue(); | 86 DictionaryValue* result = new DictionaryValue(); |
87 result->SetInteger(kConnectionIdKey, id); | 87 result->SetInteger(kConnectionIdKey, id); |
88 result_.reset(result); | 88 SetSingleResult(result); |
89 AsyncWorkCompleted(); | 89 AsyncWorkCompleted(); |
90 } else { | 90 } else { |
91 DictionaryValue* result = new DictionaryValue(); | 91 DictionaryValue* result = new DictionaryValue(); |
92 result->SetInteger(kConnectionIdKey, -1); | 92 result->SetInteger(kConnectionIdKey, -1); |
93 result_.reset(result); | 93 SetSingleResult(result); |
94 AsyncWorkCompleted(); | 94 AsyncWorkCompleted(); |
95 } | 95 } |
96 } | 96 } |
97 | 97 |
98 bool SerialOpenFunction::Respond() { | 98 bool SerialOpenFunction::Respond() { |
99 return true; | 99 return true; |
100 } | 100 } |
101 | 101 |
102 bool SerialCloseFunction::Prepare() { | 102 bool SerialCloseFunction::Prepare() { |
103 set_work_thread_id(BrowserThread::FILE); | 103 set_work_thread_id(BrowserThread::FILE); |
104 | 104 |
105 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &connection_id_)); | 105 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &connection_id_)); |
106 return true; | 106 return true; |
107 } | 107 } |
108 | 108 |
109 void SerialCloseFunction::Work() { | 109 void SerialCloseFunction::Work() { |
110 bool close_result = false; | 110 bool close_result = false; |
111 SerialConnection* serial_connection = | 111 SerialConnection* serial_connection = |
112 controller()->GetSerialConnection(connection_id_); | 112 controller()->GetSerialConnection(connection_id_); |
113 if (serial_connection) { | 113 if (serial_connection) { |
114 serial_connection->Close(); | 114 serial_connection->Close(); |
115 controller()->RemoveSerialConnection(connection_id_); | 115 controller()->RemoveSerialConnection(connection_id_); |
116 close_result = true; | 116 close_result = true; |
117 } | 117 } |
118 | 118 |
119 result_.reset(Value::CreateBooleanValue(close_result)); | 119 SetSingleResult(Value::CreateBooleanValue(close_result)); |
120 } | 120 } |
121 | 121 |
122 bool SerialCloseFunction::Respond() { | 122 bool SerialCloseFunction::Respond() { |
123 return true; | 123 return true; |
124 } | 124 } |
125 | 125 |
126 bool SerialReadFunction::Prepare() { | 126 bool SerialReadFunction::Prepare() { |
127 set_work_thread_id(BrowserThread::FILE); | 127 set_work_thread_id(BrowserThread::FILE); |
128 | 128 |
129 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &connection_id_)); | 129 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &connection_id_)); |
(...skipping 10 matching lines...) Expand all Loading... |
140 | 140 |
141 DictionaryValue* result = new DictionaryValue(); | 141 DictionaryValue* result = new DictionaryValue(); |
142 | 142 |
143 // The API is defined to require a 'data' value, so we will always | 143 // The API is defined to require a 'data' value, so we will always |
144 // create a BinaryValue, even if it's zero-length. | 144 // create a BinaryValue, even if it's zero-length. |
145 if (bytes_read < 0) | 145 if (bytes_read < 0) |
146 bytes_read = 0; | 146 bytes_read = 0; |
147 result->SetInteger(kBytesReadKey, bytes_read); | 147 result->SetInteger(kBytesReadKey, bytes_read); |
148 result->Set(kDataKey, base::BinaryValue::CreateWithCopiedBuffer( | 148 result->Set(kDataKey, base::BinaryValue::CreateWithCopiedBuffer( |
149 reinterpret_cast<char*>(&byte), bytes_read)); | 149 reinterpret_cast<char*>(&byte), bytes_read)); |
150 result_.reset(result); | 150 SetSingleResult(result); |
151 } | 151 } |
152 | 152 |
153 bool SerialReadFunction::Respond() { | 153 bool SerialReadFunction::Respond() { |
154 return true; | 154 return true; |
155 } | 155 } |
156 | 156 |
157 SerialWriteFunction::SerialWriteFunction() | 157 SerialWriteFunction::SerialWriteFunction() |
158 : connection_id_(-1), io_buffer_(NULL), io_buffer_size_(0) { | 158 : connection_id_(-1), io_buffer_(NULL), io_buffer_size_(0) { |
159 } | 159 } |
160 | 160 |
(...skipping 16 matching lines...) Expand all Loading... |
177 int bytes_written = -1; | 177 int bytes_written = -1; |
178 SerialConnection* serial_connection = | 178 SerialConnection* serial_connection = |
179 controller()->GetSerialConnection(connection_id_); | 179 controller()->GetSerialConnection(connection_id_); |
180 if (serial_connection) | 180 if (serial_connection) |
181 bytes_written = serial_connection->Write(io_buffer_, io_buffer_size_); | 181 bytes_written = serial_connection->Write(io_buffer_, io_buffer_size_); |
182 else | 182 else |
183 error_ = kSerialConnectionNotFoundError; | 183 error_ = kSerialConnectionNotFoundError; |
184 | 184 |
185 DictionaryValue* result = new DictionaryValue(); | 185 DictionaryValue* result = new DictionaryValue(); |
186 result->SetInteger(kBytesWrittenKey, bytes_written); | 186 result->SetInteger(kBytesWrittenKey, bytes_written); |
187 result_.reset(result); | 187 SetSingleResult(result); |
188 } | 188 } |
189 | 189 |
190 bool SerialWriteFunction::Respond() { | 190 bool SerialWriteFunction::Respond() { |
191 return true; | 191 return true; |
192 } | 192 } |
193 | 193 |
194 bool SerialFlushFunction::Prepare() { | 194 bool SerialFlushFunction::Prepare() { |
195 set_work_thread_id(BrowserThread::FILE); | 195 set_work_thread_id(BrowserThread::FILE); |
196 | 196 |
197 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &connection_id_)); | 197 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &connection_id_)); |
198 return true; | 198 return true; |
199 } | 199 } |
200 | 200 |
201 void SerialFlushFunction::Work() { | 201 void SerialFlushFunction::Work() { |
202 bool flush_result = false; | 202 bool flush_result = false; |
203 SerialConnection* serial_connection = | 203 SerialConnection* serial_connection = |
204 controller()->GetSerialConnection(connection_id_); | 204 controller()->GetSerialConnection(connection_id_); |
205 if (serial_connection) { | 205 if (serial_connection) { |
206 serial_connection->Flush(); | 206 serial_connection->Flush(); |
207 flush_result = true; | 207 flush_result = true; |
208 } | 208 } |
209 | 209 |
210 result_.reset(Value::CreateBooleanValue(flush_result)); | 210 SetSingleResult(Value::CreateBooleanValue(flush_result)); |
211 } | 211 } |
212 | 212 |
213 bool SerialFlushFunction::Respond() { | 213 bool SerialFlushFunction::Respond() { |
214 return true; | 214 return true; |
215 } | 215 } |
216 | 216 |
217 } // namespace extensions | 217 } // namespace extensions |
OLD | NEW |