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

Side by Side Diff: chrome/browser/extensions/api/bluetooth/bluetooth_api.cc

Issue 10915148: Change getDevices to use a DeviceCallback. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix test breakage 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/bluetooth/bluetooth_api.h" 5 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api.h"
6 6
7 #if defined(OS_CHROMEOS) 7 #if defined(OS_CHROMEOS)
8 #include <errno.h> 8 #include <errno.h>
9 #endif 9 #endif
10 10
11 #include <string> 11 #include <string>
12 12
13 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" 13 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h"
14 #include "chrome/browser/extensions/event_names.h"
14 #include "chrome/browser/extensions/extension_service.h" 15 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/common/extensions/api/experimental_bluetooth.h" 17 #include "chrome/common/extensions/api/experimental_bluetooth.h"
17 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
18 19
19 #if defined(OS_CHROMEOS) 20 #if defined(OS_CHROMEOS)
20 #include "base/memory/ref_counted.h" 21 #include "base/memory/ref_counted.h"
21 #include "base/safe_strerror_posix.h" 22 #include "base/safe_strerror_posix.h"
22 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" 23 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h"
23 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" 24 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h"
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 } 93 }
93 94
94 bool BluetoothGetNameFunction::RunImpl() { 95 bool BluetoothGetNameFunction::RunImpl() {
95 SetResult(Value::CreateStringValue(GetAdapter(profile()).name())); 96 SetResult(Value::CreateStringValue(GetAdapter(profile()).name()));
96 return true; 97 return true;
97 } 98 }
98 99
99 BluetoothGetDevicesFunction::BluetoothGetDevicesFunction() 100 BluetoothGetDevicesFunction::BluetoothGetDevicesFunction()
100 : callbacks_pending_(0) {} 101 : callbacks_pending_(0) {}
101 102
102 void BluetoothGetDevicesFunction::AddDeviceIfTrueCallback( 103 void BluetoothGetDevicesFunction::DispatchDeviceSearchResult(
103 ListValue* list, 104 const chromeos::BluetoothDevice& device) {
105 experimental_bluetooth::Device extension_device;
106 experimental_bluetooth::BluetoothDeviceToApiDevice(device, &extension_device);
107 GetEventRouter(profile())->DispatchDeviceEvent(
108 extensions::event_names::kBluetoothOnDeviceSearchResult,
109 extension_device);
110 }
111
112 void BluetoothGetDevicesFunction::ProvidesServiceCallback(
104 const chromeos::BluetoothDevice* device, 113 const chromeos::BluetoothDevice* device,
105 bool shouldAdd) { 114 bool providesService) {
106 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 115 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
107 116
108 if (shouldAdd) 117 CHECK(device);
109 list->Append(experimental_bluetooth::BluetoothDeviceToValue(*device)); 118 if (providesService)
119 DispatchDeviceSearchResult(*device);
110 120
111 callbacks_pending_--; 121 callbacks_pending_--;
112 if (callbacks_pending_ == -1) 122 if (callbacks_pending_ == -1)
113 SendResponse(true); 123 SendResponse(true);
114 } 124 }
115 125
116 bool BluetoothGetDevicesFunction::RunImpl() { 126 bool BluetoothGetDevicesFunction::RunImpl() {
117 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 127 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
118 128
119 scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_)); 129 scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_));
120 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); 130 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
121 const experimental_bluetooth::GetDevicesOptions& options = params->options; 131 const experimental_bluetooth::GetDevicesOptions& options = params->options;
122 132
123 std::string uuid; 133 std::string uuid;
124 if (options.uuid.get() != NULL) { 134 if (options.uuid.get() != NULL) {
125 uuid = chromeos::bluetooth_utils::CanonicalUuid(*options.uuid.get()); 135 uuid = chromeos::bluetooth_utils::CanonicalUuid(*options.uuid.get());
126 if (uuid.empty()) { 136 if (uuid.empty()) {
127 SetError(kInvalidUuid); 137 SetError(kInvalidUuid);
128 return false; 138 return false;
129 } 139 }
130 } 140 }
131 141
132 ListValue* matches = new ListValue;
133 SetResult(matches);
134
135 CHECK_EQ(0, callbacks_pending_); 142 CHECK_EQ(0, callbacks_pending_);
136 143
137 chromeos::BluetoothAdapter::DeviceList devices = 144 chromeos::BluetoothAdapter::DeviceList devices =
138 GetMutableAdapter(profile())->GetDevices(); 145 GetMutableAdapter(profile())->GetDevices();
139 for (chromeos::BluetoothAdapter::DeviceList::iterator i = devices.begin(); 146 for (chromeos::BluetoothAdapter::DeviceList::iterator i = devices.begin();
140 i != devices.end(); ++i) { 147 i != devices.end(); ++i) {
141 chromeos::BluetoothDevice* device = *i; 148 chromeos::BluetoothDevice* device = *i;
149 CHECK(device);
142 150
143 if (!uuid.empty() && !(device->ProvidesServiceWithUUID(uuid))) 151 if (!uuid.empty() && !(device->ProvidesServiceWithUUID(uuid)))
144 continue; 152 continue;
145 153
146 if (options.name.get() == NULL) { 154 if (options.name.get() == NULL) {
147 matches->Append(experimental_bluetooth::BluetoothDeviceToValue(*device)); 155 DispatchDeviceSearchResult(*device);
148 continue; 156 continue;
149 } 157 }
150 158
151 callbacks_pending_++; 159 callbacks_pending_++;
152 device->ProvidesServiceWithName( 160 device->ProvidesServiceWithName(
153 *(options.name), 161 *(options.name),
154 base::Bind(&BluetoothGetDevicesFunction::AddDeviceIfTrueCallback, 162 base::Bind(&BluetoothGetDevicesFunction::ProvidesServiceCallback,
155 this, 163 this,
156 matches,
157 device)); 164 device));
158 } 165 }
159 callbacks_pending_--; 166 callbacks_pending_--;
160 167
161 // The count is checked for -1 because of the extra decrement after the 168 // The count is checked for -1 because of the extra decrement after the
162 // for-loop, which ensures that all requests have been made before 169 // for-loop, which ensures that all requests have been made before
163 // SendResponse happens. 170 // SendResponse happens.
164 if (callbacks_pending_ == -1) 171 if (callbacks_pending_ == -1)
165 SendResponse(true); 172 SendResponse(true);
166 173
(...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after
601 #endif 608 #endif
602 609
603 BluetoothReadFunction::BluetoothReadFunction() {} 610 BluetoothReadFunction::BluetoothReadFunction() {}
604 BluetoothReadFunction::~BluetoothReadFunction() {} 611 BluetoothReadFunction::~BluetoothReadFunction() {}
605 612
606 BluetoothWriteFunction::BluetoothWriteFunction() {} 613 BluetoothWriteFunction::BluetoothWriteFunction() {}
607 BluetoothWriteFunction::~BluetoothWriteFunction() {} 614 BluetoothWriteFunction::~BluetoothWriteFunction() {}
608 615
609 } // namespace api 616 } // namespace api
610 } // namespace extensions 617 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698