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

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

Issue 180163009: chrome.bluetooth API improvements. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address code review feedback. Created 6 years, 9 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_socket.h"
6
7 #include "device/bluetooth/bluetooth_socket.h"
8 #include "net/base/io_buffer.h"
9
10 namespace extensions {
11
12 // static
13 static base::LazyInstance<
14 BrowserContextKeyedAPIFactory<ApiResourceManager<BluetoothApiSocket> > >
15 g_server_factory = LAZY_INSTANCE_INITIALIZER;
16
17 // static
18 template <>
19 BrowserContextKeyedAPIFactory<ApiResourceManager<BluetoothApiSocket> >*
20 ApiResourceManager<BluetoothApiSocket>::GetFactoryInstance() {
21 return g_server_factory.Pointer();
22 }
23
24 BluetoothApiSocket::BluetoothApiSocket(
25 const std::string& owner_extension_id,
26 scoped_refptr<device::BluetoothSocket> socket,
27 const std::string& device_address,
28 const std::string& profile_uuid)
29 : ApiResource(owner_extension_id),
30 socket_(socket),
31 device_address_(device_address),
32 profile_uuid_(profile_uuid),
33 persistent_(false),
34 buffer_size_(0),
35 paused_(true) {
36 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
37 }
38
39 BluetoothApiSocket::~BluetoothApiSocket() {
40 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
41 }
42
43 void BluetoothApiSocket::Disconnect(const base::Closure& success_callback) {
44 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
45 socket_->Disconnect(success_callback);
46 }
47
48 bool BluetoothApiSocket::IsPersistent() const {
49 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
50 return persistent_;
51 }
52
53 void BluetoothApiSocket::Receive(
54 int count,
55 const ReceiveCompletionCallback& success_callback,
56 const ReceiveErrorCompletionCallback& error_callback) {
57 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
58 socket_->Receive(count,
59 success_callback,
60 base::Bind(&OnSocketReceiveError, error_callback));
61 }
62
63 // static
64 void BluetoothApiSocket::OnSocketReceiveError(
65 const ReceiveErrorCompletionCallback& error_callback,
66 device::BluetoothSocket::ErrorReason reason,
67 const std::string& message) {
68 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
69 BluetoothApiSocket::ErrorReason error_reason;
70 switch (reason) {
71 case device::BluetoothSocket::kIOPending:
72 error_reason = BluetoothApiSocket::kIOPending;
73 break;
74 case device::BluetoothSocket::kDisconnected:
75 error_reason = BluetoothApiSocket::kDisconnected;
76 break;
77 case device::BluetoothSocket::kSystemError:
78 error_reason = BluetoothApiSocket::kSystemError;
79 break;
80 default:
81 NOTREACHED();
82 }
83 error_callback.Run(error_reason, message);
84 }
85
86 void BluetoothApiSocket::Send(scoped_refptr<net::IOBuffer> buffer,
87 int buffer_size,
88 const SendCompletionCallback& success_callback,
89 const ErrorCompletionCallback& error_callback) {
90 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
91 socket_->Send(buffer, buffer_size, success_callback, error_callback);
92 }
93
94 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698