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

Side by Side Diff: chrome/browser/extensions/api/usb/usb_api.cc

Issue 10824298: Adding tests for USB extension API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 4 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/usb/usb_api.h" 5 #include "chrome/browser/extensions/api/usb/usb_api.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "chrome/browser/extensions/api/usb/usb_device_resource.h" 10 #include "chrome/browser/extensions/api/usb/usb_device_resource.h"
(...skipping 21 matching lines...) Expand all
32 } 32 }
33 33
34 UsbAsyncApiFunction::~UsbAsyncApiFunction() { 34 UsbAsyncApiFunction::~UsbAsyncApiFunction() {
35 } 35 }
36 36
37 bool UsbAsyncApiFunction::PrePrepare() { 37 bool UsbAsyncApiFunction::PrePrepare() {
38 manager_ = ExtensionSystem::Get(profile())->usb_device_resource_manager(); 38 manager_ = ExtensionSystem::Get(profile())->usb_device_resource_manager();
39 return manager_ != NULL; 39 return manager_ != NULL;
40 } 40 }
41 41
42 UsbDevice* UsbFindDeviceFunction::device_for_test_ = NULL;
43
42 UsbFindDeviceFunction::UsbFindDeviceFunction() : event_notifier_(NULL) {} 44 UsbFindDeviceFunction::UsbFindDeviceFunction() : event_notifier_(NULL) {}
43 45
44 UsbFindDeviceFunction::~UsbFindDeviceFunction() {} 46 UsbFindDeviceFunction::~UsbFindDeviceFunction() {}
45 47
48 void UsbFindDeviceFunction::SetDeviceForTest(UsbDevice* device) {
49 device_for_test_ = device;
50 }
51
46 bool UsbFindDeviceFunction::Prepare() { 52 bool UsbFindDeviceFunction::Prepare() {
47 parameters_ = FindDevice::Params::Create(*args_); 53 parameters_ = FindDevice::Params::Create(*args_);
48 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); 54 EXTENSION_FUNCTION_VALIDATE(parameters_.get());
49 event_notifier_ = CreateEventNotifier(DeprecatedExtractSrcId(2)); 55 event_notifier_ = CreateEventNotifier(DeprecatedExtractSrcId(2));
50 return true; 56 return true;
51 } 57 }
52 58
53 void UsbFindDeviceFunction::Work() { 59 void UsbFindDeviceFunction::Work() {
54 UsbService* const service = 60 UsbDevice* device = NULL;
55 UsbServiceFactory::GetInstance()->GetForProfile(profile()); 61 if (device_for_test_) {
56 DCHECK(service) << "No UsbService associated with profile."; 62 device = device_for_test_;
63 } else {
64 UsbService* const service = UsbServiceFactory::GetInstance()->GetForProfile(
65 profile());
66 DCHECK(service) << "No UsbService associated with profile.";
asargent_no_longer_on_chrome 2012/08/14 22:31:06 nit: Since we'll crash on the next line anyway if
57 67
58 UsbDevice* const device = service->FindDevice(parameters_->vendor_id, 68 device = service->FindDevice(parameters_->vendor_id,
59 parameters_->product_id); 69 parameters_->product_id);
70 }
71
60 if (!device) { 72 if (!device) {
61 SetResult(base::Value::CreateNullValue()); 73 SetResult(base::Value::CreateNullValue());
62 return; 74 return;
63 } 75 }
64 76
65 UsbDeviceResource* const resource = new UsbDeviceResource(event_notifier_, 77 UsbDeviceResource* const resource = new UsbDeviceResource(event_notifier_,
66 device); 78 device);
67 79
68 Device result; 80 Device result;
69 result.handle = manager_->Add(resource); 81 result.handle = manager_->Add(resource);
(...skipping 10 matching lines...) Expand all
80 92
81 UsbCloseDeviceFunction::~UsbCloseDeviceFunction() {} 93 UsbCloseDeviceFunction::~UsbCloseDeviceFunction() {}
82 94
83 bool UsbCloseDeviceFunction::Prepare() { 95 bool UsbCloseDeviceFunction::Prepare() {
84 parameters_ = CloseDevice::Params::Create(*args_); 96 parameters_ = CloseDevice::Params::Create(*args_);
85 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); 97 EXTENSION_FUNCTION_VALIDATE(parameters_.get());
86 return true; 98 return true;
87 } 99 }
88 100
89 void UsbCloseDeviceFunction::Work() { 101 void UsbCloseDeviceFunction::Work() {
102 UsbDeviceResource* const device = manager_->Get(parameters_->device.handle);
103 if (device)
104 device->Close();
105
90 manager_->Remove(parameters_->device.handle); 106 manager_->Remove(parameters_->device.handle);
91 } 107 }
92 108
93 bool UsbCloseDeviceFunction::Respond() { 109 bool UsbCloseDeviceFunction::Respond() {
94 return true; 110 return true;
95 } 111 }
96 112
97 UsbControlTransferFunction::UsbControlTransferFunction() {} 113 UsbControlTransferFunction::UsbControlTransferFunction() {}
98 114
99 UsbControlTransferFunction::~UsbControlTransferFunction() {} 115 UsbControlTransferFunction::~UsbControlTransferFunction() {}
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 if (device) { 188 if (device) {
173 device->IsochronousTransfer(parameters_->transfer_info); 189 device->IsochronousTransfer(parameters_->transfer_info);
174 } 190 }
175 } 191 }
176 192
177 bool UsbIsochronousTransferFunction::Respond() { 193 bool UsbIsochronousTransferFunction::Respond() {
178 return true; 194 return true;
179 } 195 }
180 196
181 } // namespace extensions 197 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698