Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: chrome/browser/extensions/api/serial/serial_api.cc

Issue 10919201: Make sure that a given app/extension requests only its own resources. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Improve tests; fix merge conflicts. Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/extension_system.h" 8 #include "chrome/browser/extensions/extension_system.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 27 matching lines...) Expand all
38 } 38 }
39 39
40 SerialAsyncApiFunction::~SerialAsyncApiFunction() { 40 SerialAsyncApiFunction::~SerialAsyncApiFunction() {
41 } 41 }
42 42
43 bool SerialAsyncApiFunction::PrePrepare() { 43 bool SerialAsyncApiFunction::PrePrepare() {
44 manager_ = ExtensionSystem::Get(profile())->serial_connection_manager(); 44 manager_ = ExtensionSystem::Get(profile())->serial_connection_manager();
45 return manager_ != NULL; 45 return manager_ != NULL;
46 } 46 }
47 47
48 SerialConnection* SerialAsyncApiFunction::GetSerialConnection(
49 int api_resource_id) {
50 return manager_->Get(extension_->id(), api_resource_id);
51 }
52
53 void SerialAsyncApiFunction::RemoveSerialConnection(int api_resource_id) {
54 manager_->Remove(extension_->id(), api_resource_id);
55 }
56
48 SerialGetPortsFunction::SerialGetPortsFunction() {} 57 SerialGetPortsFunction::SerialGetPortsFunction() {}
49 58
50 bool SerialGetPortsFunction::Prepare() { 59 bool SerialGetPortsFunction::Prepare() {
51 set_work_thread_id(BrowserThread::FILE); 60 set_work_thread_id(BrowserThread::FILE);
52 return true; 61 return true;
53 } 62 }
54 63
55 void SerialGetPortsFunction::Work() { 64 void SerialGetPortsFunction::Work() {
56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
57 66
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 } 118 }
110 119
111 void SerialOpenFunction::Work() { 120 void SerialOpenFunction::Work() {
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
113 const SerialPortEnumerator::StringSet name_set( 122 const SerialPortEnumerator::StringSet name_set(
114 SerialPortEnumerator::GenerateValidSerialPortNames()); 123 SerialPortEnumerator::GenerateValidSerialPortNames());
115 if (DoesPortExist(params_->port)) { 124 if (DoesPortExist(params_->port)) {
116 SerialConnection* serial_connection = CreateSerialConnection( 125 SerialConnection* serial_connection = CreateSerialConnection(
117 params_->port, 126 params_->port,
118 bitrate_, 127 bitrate_,
128 extension_->id(),
119 event_notifier_); 129 event_notifier_);
120 CHECK(serial_connection); 130 CHECK(serial_connection);
121 int id = manager_->Add(serial_connection); 131 int id = manager_->Add(serial_connection);
122 CHECK(id); 132 CHECK(id);
123 133
124 bool open_result = serial_connection->Open(); 134 bool open_result = serial_connection->Open();
125 if (!open_result) { 135 if (!open_result) {
126 serial_connection->Close(); 136 serial_connection->Close();
127 manager_->Remove(id); 137 RemoveSerialConnection(id);
128 id = -1; 138 id = -1;
129 } 139 }
130 140
131 DictionaryValue* result = new DictionaryValue(); 141 DictionaryValue* result = new DictionaryValue();
132 result->SetInteger(kConnectionIdKey, id); 142 result->SetInteger(kConnectionIdKey, id);
133 SetResult(result); 143 SetResult(result);
134 AsyncWorkCompleted(); 144 AsyncWorkCompleted();
135 } else { 145 } else {
136 DictionaryValue* result = new DictionaryValue(); 146 DictionaryValue* result = new DictionaryValue();
137 result->SetInteger(kConnectionIdKey, -1); 147 result->SetInteger(kConnectionIdKey, -1);
138 SetResult(result); 148 SetResult(result);
139 AsyncWorkCompleted(); 149 AsyncWorkCompleted();
140 } 150 }
141 } 151 }
142 152
143 SerialConnection* SerialOpenFunction::CreateSerialConnection( 153 SerialConnection* SerialOpenFunction::CreateSerialConnection(
144 const std::string& port, 154 const std::string& port,
145 int bitrate, 155 int bitrate,
156 const std::string& owner_extension_id,
146 ApiResourceEventNotifier* event_notifier) { 157 ApiResourceEventNotifier* event_notifier) {
147 return new SerialConnection(port, bitrate, event_notifier); 158 return new SerialConnection(port, bitrate, owner_extension_id,
159 event_notifier);
148 } 160 }
149 161
150 bool SerialOpenFunction::DoesPortExist(const std::string& port) { 162 bool SerialOpenFunction::DoesPortExist(const std::string& port) {
151 const SerialPortEnumerator::StringSet name_set( 163 const SerialPortEnumerator::StringSet name_set(
152 SerialPortEnumerator::GenerateValidSerialPortNames()); 164 SerialPortEnumerator::GenerateValidSerialPortNames());
153 return SerialPortEnumerator::DoesPortExist(name_set, params_->port); 165 return SerialPortEnumerator::DoesPortExist(name_set, params_->port);
154 } 166 }
155 167
156 bool SerialOpenFunction::Respond() { 168 bool SerialOpenFunction::Respond() {
157 return true; 169 return true;
158 } 170 }
159 171
160 SerialCloseFunction::SerialCloseFunction() { 172 SerialCloseFunction::SerialCloseFunction() {
161 } 173 }
162 174
163 SerialCloseFunction::~SerialCloseFunction() { 175 SerialCloseFunction::~SerialCloseFunction() {
164 } 176 }
165 177
166 bool SerialCloseFunction::Prepare() { 178 bool SerialCloseFunction::Prepare() {
167 set_work_thread_id(BrowserThread::FILE); 179 set_work_thread_id(BrowserThread::FILE);
168 180
169 params_ = api::serial::Close::Params::Create(*args_); 181 params_ = api::serial::Close::Params::Create(*args_);
170 EXTENSION_FUNCTION_VALIDATE(params_.get()); 182 EXTENSION_FUNCTION_VALIDATE(params_.get());
171 183
172 return true; 184 return true;
173 } 185 }
174 186
175 void SerialCloseFunction::Work() { 187 void SerialCloseFunction::Work() {
176 bool close_result = false; 188 bool close_result = false;
177 SerialConnection* serial_connection = manager_->Get(params_->connection_id); 189 SerialConnection* serial_connection = GetSerialConnection(
190 params_->connection_id);
178 if (serial_connection) { 191 if (serial_connection) {
179 serial_connection->Close(); 192 serial_connection->Close();
180 manager_->Remove(params_->connection_id); 193 RemoveSerialConnection(params_->connection_id);
181 close_result = true; 194 close_result = true;
182 } 195 }
183 196
184 SetResult(Value::CreateBooleanValue(close_result)); 197 SetResult(Value::CreateBooleanValue(close_result));
185 } 198 }
186 199
187 bool SerialCloseFunction::Respond() { 200 bool SerialCloseFunction::Respond() {
188 return true; 201 return true;
189 } 202 }
190 203
(...skipping 13 matching lines...) Expand all
204 return false; 217 return false;
205 } 218 }
206 219
207 return true; 220 return true;
208 } 221 }
209 222
210 void SerialReadFunction::Work() { 223 void SerialReadFunction::Work() {
211 int bytes_read = -1; 224 int bytes_read = -1;
212 scoped_refptr<net::IOBufferWithSize> io_buffer( 225 scoped_refptr<net::IOBufferWithSize> io_buffer(
213 new net::IOBufferWithSize(params_->bytes_to_read)); 226 new net::IOBufferWithSize(params_->bytes_to_read));
214 SerialConnection* serial_connection(manager_->Get(params_->connection_id)); 227 SerialConnection* serial_connection(GetSerialConnection(
228 params_->connection_id));
215 229
216 if (serial_connection) 230 if (serial_connection)
217 bytes_read = serial_connection->Read(io_buffer); 231 bytes_read = serial_connection->Read(io_buffer);
218 232
219 DictionaryValue* result = new DictionaryValue(); 233 DictionaryValue* result = new DictionaryValue();
220 234
221 // The API is defined to require a 'data' value, so we will always 235 // The API is defined to require a 'data' value, so we will always
222 // create a BinaryValue, even if it's zero-length. 236 // create a BinaryValue, even if it's zero-length.
223 if (bytes_read < 0) 237 if (bytes_read < 0)
224 bytes_read = 0; 238 bytes_read = 0;
(...skipping 21 matching lines...) Expand all
246 EXTENSION_FUNCTION_VALIDATE(params_.get()); 260 EXTENSION_FUNCTION_VALIDATE(params_.get());
247 261
248 io_buffer_size_ = params_->data.size(); 262 io_buffer_size_ = params_->data.size();
249 io_buffer_ = new net::WrappedIOBuffer(params_->data.data()); 263 io_buffer_ = new net::WrappedIOBuffer(params_->data.data());
250 264
251 return true; 265 return true;
252 } 266 }
253 267
254 void SerialWriteFunction::Work() { 268 void SerialWriteFunction::Work() {
255 int bytes_written = -1; 269 int bytes_written = -1;
256 SerialConnection* serial_connection = manager_->Get(params_->connection_id); 270 SerialConnection* serial_connection = GetSerialConnection(
271 params_->connection_id);
257 if (serial_connection) 272 if (serial_connection)
258 bytes_written = serial_connection->Write(io_buffer_, io_buffer_size_); 273 bytes_written = serial_connection->Write(io_buffer_, io_buffer_size_);
259 else 274 else
260 error_ = kSerialConnectionNotFoundError; 275 error_ = kSerialConnectionNotFoundError;
261 276
262 DictionaryValue* result = new DictionaryValue(); 277 DictionaryValue* result = new DictionaryValue();
263 result->SetInteger(kBytesWrittenKey, bytes_written); 278 result->SetInteger(kBytesWrittenKey, bytes_written);
264 SetResult(result); 279 SetResult(result);
265 } 280 }
266 281
(...skipping 10 matching lines...) Expand all
277 bool SerialFlushFunction::Prepare() { 292 bool SerialFlushFunction::Prepare() {
278 set_work_thread_id(BrowserThread::FILE); 293 set_work_thread_id(BrowserThread::FILE);
279 294
280 params_ = api::serial::Flush::Params::Create(*args_); 295 params_ = api::serial::Flush::Params::Create(*args_);
281 EXTENSION_FUNCTION_VALIDATE(params_.get()); 296 EXTENSION_FUNCTION_VALIDATE(params_.get());
282 return true; 297 return true;
283 } 298 }
284 299
285 void SerialFlushFunction::Work() { 300 void SerialFlushFunction::Work() {
286 bool flush_result = false; 301 bool flush_result = false;
287 SerialConnection* serial_connection = manager_->Get(params_->connection_id); 302 SerialConnection* serial_connection = GetSerialConnection(
303 params_->connection_id);
288 if (serial_connection) { 304 if (serial_connection) {
289 serial_connection->Flush(); 305 serial_connection->Flush();
290 flush_result = true; 306 flush_result = true;
291 } 307 }
292 308
293 SetResult(Value::CreateBooleanValue(flush_result)); 309 SetResult(Value::CreateBooleanValue(flush_result));
294 } 310 }
295 311
296 bool SerialFlushFunction::Respond() { 312 bool SerialFlushFunction::Respond() {
297 return true; 313 return true;
(...skipping 11 matching lines...) Expand all
309 325
310 params_ = api::serial::GetControlSignals::Params::Create( 326 params_ = api::serial::GetControlSignals::Params::Create(
311 *args_); 327 *args_);
312 EXTENSION_FUNCTION_VALIDATE(params_.get()); 328 EXTENSION_FUNCTION_VALIDATE(params_.get());
313 329
314 return true; 330 return true;
315 } 331 }
316 332
317 void SerialGetControlSignalsFunction::Work() { 333 void SerialGetControlSignalsFunction::Work() {
318 DictionaryValue *result = new DictionaryValue(); 334 DictionaryValue *result = new DictionaryValue();
319 SerialConnection* serial_connection = manager_->Get(params_->connection_id); 335 SerialConnection* serial_connection = GetSerialConnection(
336 params_->connection_id);
320 if (serial_connection) { 337 if (serial_connection) {
321 SerialConnection::ControlSignals control_signals = { 0 }; 338 SerialConnection::ControlSignals control_signals = { 0 };
322 if (serial_connection->GetControlSignals(control_signals)) { 339 if (serial_connection->GetControlSignals(control_signals)) {
323 api_response_ = true; 340 api_response_ = true;
324 result->SetBoolean(kDcdKey, control_signals.dcd); 341 result->SetBoolean(kDcdKey, control_signals.dcd);
325 result->SetBoolean(kCtsKey, control_signals.cts); 342 result->SetBoolean(kCtsKey, control_signals.cts);
326 } else { 343 } else {
327 error_ = kErrorGetControlSignalsFailed; 344 error_ = kErrorGetControlSignalsFailed;
328 } 345 }
329 } else { 346 } else {
(...skipping 18 matching lines...) Expand all
348 set_work_thread_id(BrowserThread::FILE); 365 set_work_thread_id(BrowserThread::FILE);
349 366
350 params_ = api::serial::SetControlSignals::Params::Create( 367 params_ = api::serial::SetControlSignals::Params::Create(
351 *args_); 368 *args_);
352 EXTENSION_FUNCTION_VALIDATE(params_.get()); 369 EXTENSION_FUNCTION_VALIDATE(params_.get());
353 370
354 return true; 371 return true;
355 } 372 }
356 373
357 void SerialSetControlSignalsFunction::Work() { 374 void SerialSetControlSignalsFunction::Work() {
358 SerialConnection* serial_connection = manager_->Get(params_->connection_id); 375 SerialConnection* serial_connection = GetSerialConnection(
376 params_->connection_id);
359 if (serial_connection) { 377 if (serial_connection) {
360 SerialConnection::ControlSignals control_signals = { 0 }; 378 SerialConnection::ControlSignals control_signals = { 0 };
361 control_signals.should_set_dtr = params_->options.dtr.get() != NULL; 379 control_signals.should_set_dtr = params_->options.dtr.get() != NULL;
362 if (control_signals.should_set_dtr) 380 if (control_signals.should_set_dtr)
363 control_signals.dtr = *(params_->options.dtr); 381 control_signals.dtr = *(params_->options.dtr);
364 control_signals.should_set_rts = params_->options.rts.get() != NULL; 382 control_signals.should_set_rts = params_->options.rts.get() != NULL;
365 if (control_signals.should_set_rts) 383 if (control_signals.should_set_rts)
366 control_signals.rts = *(params_->options.rts); 384 control_signals.rts = *(params_->options.rts);
367 if (serial_connection->SetControlSignals(control_signals)) { 385 if (serial_connection->SetControlSignals(control_signals)) {
368 SetResult(Value::CreateBooleanValue(true)); 386 SetResult(Value::CreateBooleanValue(true));
369 } else { 387 } else {
370 error_ = kErrorSetControlSignalsFailed; 388 error_ = kErrorSetControlSignalsFailed;
371 SetResult(Value::CreateBooleanValue(false)); 389 SetResult(Value::CreateBooleanValue(false));
372 } 390 }
373 } else { 391 } else {
374 error_ = kSerialConnectionNotFoundError; 392 error_ = kSerialConnectionNotFoundError;
375 SetResult(Value::CreateBooleanValue(false)); 393 SetResult(Value::CreateBooleanValue(false));
376 } 394 }
377 } 395 }
378 396
379 bool SerialSetControlSignalsFunction::Respond() { 397 bool SerialSetControlSignalsFunction::Respond() {
380 return true; 398 return true;
381 } 399 }
382 400
383 } // namespace extensions 401 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/serial/serial_api.h ('k') | chrome/browser/extensions/api/serial/serial_apitest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698