diff options
author | Rahul Chaturvedi <rkc@chromium.org> | 2014-12-04 14:06:24 -0500 |
---|---|---|
committer | Rahul Chaturvedi <rkc@chromium.org> | 2014-12-04 19:07:39 +0000 |
commit | 19de704d2740b353ea43024b7270ddf39ce98ee6 (patch) | |
tree | 7a461161a2912d349060c2a5ce95cca2a88133ea /components/copresence_endpoints/public | |
parent | c0c568c37bd1cfe742902318f597a58f49092651 (diff) | |
download | chromium_src-19de704d2740b353ea43024b7270ddf39ce98ee6.zip chromium_src-19de704d2740b353ea43024b7270ddf39ce98ee6.tar.gz chromium_src-19de704d2740b353ea43024b7270ddf39ce98ee6.tar.bz2 |
Rewrite the CopresenceSocket API.
We've changed the concept of the sockets API to be more of an *endpoints* API
on Android. This CL matches the change. We now only have endpoints that the
API can work with. To make a connection between devices, an app needs to create
a local endpoint, and then connect it to a remote endpoint. The connection can
happen via a 'connect' call (not implemented yet) or via accepting connections.
Currently we only support one local endpoint and have matched the wire protocol
to match Android. Keep in mind this is still completely experimental code with
exactly one user, so things will probably change again quite drastically. This
is why I have *only* implemented the functions we currently need and absolutely
nothing else.
Reviews requested,
/* - xiyuan@
extensions/* - kalman@
components/* - erikwright@ (we're mostly just renaming an existing component)
extension_function_histogram_value.h - isherman@ (deleting old enums)
R=armansito@chromium.org, erikwright@chromium.org, isherman@chromium.org, jar@chromium.org, kalman@chromium.org, mark@chromium.org, xiyuan@chromium.org
BUG=418617
Review URL: https://codereview.chromium.org/771683004
Cr-Commit-Position: refs/heads/master@{#306863}
Diffstat (limited to 'components/copresence_endpoints/public')
-rw-r--r-- | components/copresence_endpoints/public/copresence_endpoint.h | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/components/copresence_endpoints/public/copresence_endpoint.h b/components/copresence_endpoints/public/copresence_endpoint.h new file mode 100644 index 0000000..c0b7bc5 --- /dev/null +++ b/components/copresence_endpoints/public/copresence_endpoint.h @@ -0,0 +1,89 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef COMPONENTS_COPRESENCE_ENDPOINTS_COPRESENCE_ENDPOINT_H_ +#define COMPONENTS_COPRESENCE_ENDPOINTS_COPRESENCE_ENDPOINT_H_ + +#include <string> + +#include "base/callback.h" +#include "base/macros.h" +#include "base/memory/ref_counted.h" +#include "base/memory/scoped_ptr.h" +#include "base/memory/weak_ptr.h" +#include "components/copresence_endpoints/copresence_socket.h" +#include "device/bluetooth/bluetooth_device.h" +#include "device/bluetooth/bluetooth_uuid.h" + +namespace device { +class BluetoothAdapter; +class BluetoothSocket; +} + +namespace copresence_endpoints { + +// A CopresenceEndpoint is an object that can be used for communication with +// remote endpoints. Creating this object will create a server that will listen +// on Bluetooth (currently) and accept connections. Once a connection is +// accepted, the endpoint continuously listens for data on the accepted +// connection(s). +class CopresenceEndpoint { + public: + // Callback with the locator data for the created peer. On a failed create, + // the locator string will be empty. + typedef base::Callback<void(const std::string&)> CreateEndpointCallback; + + // Create a CopresenceEndpoint and start listening for connections. Once the + // endpoint object is created, the locator data for the object is returned via + // create_callback. This locator data can be used to connect to this peer by + // a remote endpoint. + // endpoint_id is the id that this endpoint will use to identify itself. + // create_callback is called when the endpoint creation is complete. + // accept_callback is called when we receive an incoming connection. + // receive_callback is called when we receive data on this endpoint. + CopresenceEndpoint(int endpoint_id, + const CreateEndpointCallback& create_callback, + const base::Closure& accept_callback, + const CopresenceSocket::ReceiveCallback& receive_callback); + + virtual ~CopresenceEndpoint(); + + // Send's data to the remote device connected to this endpoint. + bool Send(const scoped_refptr<net::IOBuffer>& buffer, int buffer_size); + + // This will return a string containing the data needed for a remote endpoint + // to connect to this endpoint. + std::string GetLocator(); + + private: + void OnGetAdapter(scoped_refptr<device::BluetoothAdapter> adapter); + void OnCreateService(scoped_refptr<device::BluetoothSocket> socket); + void OnCreateServiceError(const std::string& message); + + void OnAccept(const device::BluetoothDevice* device, + scoped_refptr<device::BluetoothSocket> socket); + void OnAcceptError(const std::string& message); + + scoped_refptr<device::BluetoothAdapter> adapter_; + scoped_refptr<device::BluetoothSocket> server_socket_; + // TODO(rkc): Eventually an endpoint will be able to accept multiple + // connections. Whenever the API supports one-to-many connections, fix this + // code to do so too. + scoped_ptr<CopresenceSocket> client_socket_; + + int endpoint_id_; + CreateEndpointCallback create_callback_; + base::Closure accept_callback_; + CopresenceSocket::ReceiveCallback receive_callback_; + + scoped_ptr<device::BluetoothDevice::PairingDelegate> delegate_; + + base::WeakPtrFactory<CopresenceEndpoint> weak_ptr_factory_; + + DISALLOW_COPY_AND_ASSIGN(CopresenceEndpoint); +}; + +} // namespace copresence_endpoints + +#endif // COMPONENTS_COPRESENCE_ENDPOINTS_COPRESENCE_ENDPOINT_H_ |