Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / bluetooth / bluetooth_dispatcher_host.h
blob7e3e7af45cb7d59206db6e04237ee11c962828d7
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.
5 #ifndef CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DISPATCHER_HOST_H_
6 #define CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DISPATCHER_HOST_H_
8 #include "base/basictypes.h"
9 #include "base/id_map.h"
10 #include "base/memory/weak_ptr.h"
11 #include "content/public/browser/bluetooth_chooser.h"
12 #include "content/public/browser/browser_message_filter.h"
13 #include "device/bluetooth/bluetooth_adapter.h"
14 #include "device/bluetooth/bluetooth_gatt_connection.h"
15 #include "device/bluetooth/bluetooth_gatt_service.h"
17 namespace device {
18 class BluetoothUUID;
21 namespace content {
23 struct BluetoothScanFilter;
25 // Dispatches and sends bluetooth related messages sent to/from a child
26 // process BluetoothDispatcher from/to the main browser process.
28 // Intended to be instantiated by the RenderProcessHost and installed as
29 // a filter on the channel. BrowserMessageFilter is refcounted and typically
30 // lives as long as it is installed on a channel.
32 // UI Thread Note:
33 // BluetoothDispatcherHost is constructed, operates, and destroyed on the UI
34 // thread because BluetoothAdapter and related objects live there.
35 class CONTENT_EXPORT BluetoothDispatcherHost final
36 : public BrowserMessageFilter,
37 public device::BluetoothAdapter::Observer {
38 public:
39 BluetoothDispatcherHost(int render_process_id);
40 // BrowserMessageFilter:
41 void OnDestruct() const override;
42 void OverrideThreadForMessage(const IPC::Message& message,
43 BrowserThread::ID* thread) override;
44 bool OnMessageReceived(const IPC::Message& message) override;
46 void SetBluetoothAdapterForTesting(
47 scoped_refptr<device::BluetoothAdapter> mock_adapter);
49 protected:
50 ~BluetoothDispatcherHost() override;
52 private:
53 friend class base::DeleteHelper<BluetoothDispatcherHost>;
54 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>;
56 struct RequestDeviceSession;
58 // Set |adapter_| to a BluetoothAdapter instance and register observers,
59 // releasing references to previous |adapter_|.
60 void set_adapter(scoped_refptr<device::BluetoothAdapter> adapter);
62 // Stops all BluetoothDiscoverySessions being run for requestDevice()
63 // choosers.
64 void StopDeviceDiscovery();
66 // BluetoothAdapter::Observer:
67 void AdapterPoweredChanged(device::BluetoothAdapter* adapter,
68 bool powered) override;
69 void DeviceAdded(device::BluetoothAdapter* adapter,
70 device::BluetoothDevice* device) override;
71 void DeviceRemoved(device::BluetoothAdapter* adapter,
72 device::BluetoothDevice* device) override;
74 // IPC Handlers, see definitions in bluetooth_messages.h.
75 void OnRequestDevice(
76 int thread_id,
77 int request_id,
78 int frame_routing_id,
79 const std::vector<content::BluetoothScanFilter>& filters,
80 const std::vector<device::BluetoothUUID>& optional_services);
81 void OnConnectGATT(int thread_id, int request_id,
82 const std::string& device_instance_id);
83 void OnGetPrimaryService(int thread_id,
84 int request_id,
85 const std::string& device_instance_id,
86 const std::string& service_uuid);
87 void OnGetCharacteristic(int thread_id,
88 int request_id,
89 const std::string& service_instance_id,
90 const std::string& characteristic_uuid);
91 void OnReadValue(int thread_id,
92 int request_id,
93 const std::string& characteristic_instance_id);
94 void OnWriteValue(int thread_id,
95 int request_id,
96 const std::string& characteristic_instance_id,
97 const std::vector<uint8_t>& value);
99 // Callbacks for BluetoothAdapter::StartDiscoverySession.
100 void OnDiscoverySessionStarted(
101 int chooser_id,
102 scoped_ptr<device::BluetoothDiscoverySession> discovery_session);
103 void OnDiscoverySessionStartedError(int chooser_id);
105 // BluetoothChooser::EventHandler:
106 void OnBluetoothChooserEvent(int chooser_id,
107 BluetoothChooser::Event event,
108 const std::string& device_id);
110 // The chooser implementation yields to the event loop to avoid re-entering
111 // code that's still using the RequestDeviceSession, and continues with this
112 // function.
113 void FinishClosingChooser(int chooser_id,
114 BluetoothChooser::Event event,
115 const std::string& device_id);
117 // Callbacks for BluetoothDevice::CreateGattConnection.
118 void OnGATTConnectionCreated(
119 int thread_id,
120 int request_id,
121 const std::string& device_instance_id,
122 base::TimeTicks start_time,
123 scoped_ptr<device::BluetoothGattConnection> connection);
124 void OnCreateGATTConnectionError(
125 int thread_id,
126 int request_id,
127 const std::string& device_instance_id,
128 base::TimeTicks start_time,
129 device::BluetoothDevice::ConnectErrorCode error_code);
131 // Callback for future BluetoothAdapter::ServicesDiscovered callback:
132 // For now we just post a delayed task.
133 // See: https://crbug.com/484504
134 void OnServicesDiscovered(int thread_id,
135 int request_id,
136 const std::string& device_instance_id,
137 const std::string& service_uuid);
139 // Callbacks for BluetoothGattCharacteristic::ReadRemoteCharacteristic.
140 void OnCharacteristicValueRead(int thread_id,
141 int request_id,
142 const std::vector<uint8>& value);
143 void OnCharacteristicReadValueError(
144 int thread_id,
145 int request_id,
146 device::BluetoothGattService::GattErrorCode);
148 // Callbacks for BluetoothGattCharacteristic::WriteRemoteCharacteristic.
149 void OnWriteValueSuccess(int thread_id, int request_id);
150 void OnWriteValueFailed(int thread_id,
151 int request_id,
152 device::BluetoothGattService::GattErrorCode);
154 // Show help pages from the chooser dialog.
155 void ShowBluetoothOverviewLink();
156 void ShowBluetoothPairingLink();
157 void ShowBluetoothAdapterOffLink();
159 int render_process_id_;
161 // Maps a (thread_id,request_id) to information about its requestDevice call,
162 // including the chooser dialog.
163 // An entry is added to this map in OnRequestDevice, and should be removed
164 // again everywhere a requestDevice() reply is sent.
165 IDMap<RequestDeviceSession, IDMapOwnPointer> request_device_sessions_;
167 // Maps to get the object's parent based on it's instanceID
168 // Map of service_instance_id to device_instance_id.
169 std::map<std::string, std::string> service_to_device_;
170 // Map of characteristic_instance_id to service_instance_id.
171 std::map<std::string, std::string> characteristic_to_service_;
173 // Defines how long to scan for and how long to discover services for.
174 int current_delay_time_;
176 // A BluetoothAdapter instance representing an adapter of the system.
177 scoped_refptr<device::BluetoothAdapter> adapter_;
179 // Automatically stops Bluetooth discovery a set amount of time after it was
180 // started. We have a single timer for all of Web Bluetooth because it's
181 // simpler than tracking timeouts for each RequestDeviceSession individually,
182 // and because there's no harm in extending the length of a few discovery
183 // sessions when other sessions are active.
184 base::Timer discovery_session_timer_;
186 // Must be last member, see base/memory/weak_ptr.h documentation
187 base::WeakPtrFactory<BluetoothDispatcherHost> weak_ptr_factory_;
189 DISALLOW_COPY_AND_ASSIGN(BluetoothDispatcherHost);
192 } // namespace content
194 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DISPATCHER_HOST_H_