OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 #ifndef CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DEVICE_PROVIDER_H_ | |
6 #define CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DEVICE_PROVIDER_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/memory/weak_ptr.h" | |
11 #include "base/timer/timer.h" | |
12 #include "content/public/browser/bluetooth_chooser.h" | |
13 #include "third_party/WebKit/public/platform/modules/bluetooth/web_bluetooth.moj om.h" | |
14 | |
15 namespace device { | |
16 class BluetoothAdapter; | |
17 class BluetoothDevice; | |
18 class BluetoothDiscoveryFilter; | |
19 class BluetoothDiscoverySession; | |
20 } | |
21 | |
22 namespace content { | |
23 | |
24 class RenderFrameHost; | |
25 class WebContents; | |
26 | |
27 // Class that interacts with a chooser and starts a bluetooth discovery session. | |
Jeffrey Yasskin
2016/05/13 04:41:59
Can this class manage more than one successive Get
ortuno
2016/05/13 20:11:17
Added comment.
| |
28 class BluetoothDeviceProvider final { | |
Jeffrey Yasskin
2016/05/13 04:41:59
I'm not totally happy with this name, but I don't
ortuno
2016/05/13 20:11:17
Changed to BluetoothDeviceChooserController.
| |
29 public: | |
30 typedef base::Callback<void(const std::string& device_address)> | |
31 SuccessCallback; | |
32 typedef base::Callback<void(blink::mojom::WebBluetoothError error)> | |
33 ErrorCallback; | |
34 | |
35 // |render_frame_host| should be the RenderFrameHost that owns the | |
36 // WebBluetoothServiceImpl that owns this class. | |
37 // |adapter| should be the adapter used to scan for Bluetooth devices. | |
38 // |scan_duration| is how long will a discovery session be active for. | |
Jeffrey Yasskin
2016/05/13 04:41:59
Nit: "is how long a discovery session will be acti
ortuno
2016/05/13 20:11:17
Done.
| |
39 BluetoothDeviceProvider(RenderFrameHost* render_frame_host, | |
40 device::BluetoothAdapter* adapter, | |
41 int scan_duration); | |
Jeffrey Yasskin
2016/05/13 04:41:59
Lengths of time should be base::TimeDelta so that
ortuno
2016/05/13 20:11:18
Done.
| |
42 ~BluetoothDeviceProvider(); | |
43 | |
44 // This function performs the following checks before starting a discovery | |
Jeffrey Yasskin
2016/05/13 04:41:59
If this function is called again before the previo
ortuno
2016/05/13 20:11:17
It will DCHECK. Added comment.
| |
45 // session: | |
46 // - Validates filters in |request_device_options|. | |
47 // - Removes any blacklisted UUIDs from | |
48 // |request_device_options.optinal_services|. | |
49 // - Checks if the request came from a cross-origin iframe. | |
50 // - Checks if the request came from a unique origin. | |
51 // - Checks if the adapter is present. | |
52 // - Checks if the Web Bluetooth API has been disabled. | |
53 // - Checks if we are allowed to ask for scanning permission. | |
54 // If any of the previous checks failed then this function runs | |
55 // |error_callback| with the corresponding error. Otherwise this function | |
56 // populates the embedder provided BluetoothChooser with existing devices and | |
57 // starts a new discovery session. | |
58 void GetDevice( | |
59 blink::mojom::WebBluetoothRequestDeviceOptionsPtr request_device_options, | |
60 const SuccessCallback& success_callback, | |
61 const ErrorCallback& error_callback); | |
62 | |
63 // Adds a device to the chooser. | |
Jeffrey Yasskin
2016/05/13 04:41:59
Must this be called only while a GetDevice() is ac
ortuno
2016/05/13 20:11:18
Added comment.
| |
64 void AddFilteredDevice(const device::BluetoothDevice& device); | |
65 | |
66 // Stops the current discovery session and notifies the chooser | |
Jeffrey Yasskin
2016/05/13 04:41:59
It's odd that this class would need something exte
ortuno
2016/05/13 20:11:17
I agree it's a bit weird. The way I think about it
| |
67 // that the adapter changed states. | |
68 void AdapterPoweredChanged(bool powered); | |
69 | |
70 private: | |
71 // Notifies the chooser that discovery is starting and starts a discovery | |
72 // session. | |
73 void StartDeviceDiscovery(); | |
74 | |
75 // Stops the discovery session and notifies the chooser. | |
76 void StopDeviceDiscovery(); | |
77 | |
78 // StartDiscoverySessionWithFilter callbacks: | |
79 void OnStartDiscoverySessionSuccess( | |
80 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session); | |
81 void OnStartDiscoverySessionFailed(); | |
82 | |
83 // BluetoothChooser::EventHandler: | |
84 // Runs error_callback_ if the chooser was cancelled or if we weren't able | |
85 // to show the chooser. Otherwise runs success_callback_ with | |
86 // |device_address|. | |
87 void OnBluetoothChooserEvent(BluetoothChooser::Event event, | |
88 const std::string& device_address); | |
89 | |
90 // Helper function to asynchronously run success_callback_. | |
91 void PostSuccessCallback(const std::string& device_address); | |
92 // Helper function to asynchronously run error_callback_. | |
93 void PostErrorCallback(blink::mojom::WebBluetoothError error); | |
94 | |
95 // The adapter used to get existing devices and start a discovery session. | |
96 device::BluetoothAdapter* adapter_; | |
97 // The RenderFrameHost owning the WebBluetoothServiceImpl that owns this | |
98 // instance. | |
99 RenderFrameHost* render_frame_host_; | |
100 // The WebContents that owns render_frame_host_. | |
101 WebContents* web_contents_; | |
102 | |
103 // Contains the filters and optional services used when scanning. | |
104 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options_; | |
105 | |
106 // Callbacks to be called with the result of the chooser. | |
107 SuccessCallback success_callback_; | |
108 ErrorCallback error_callback_; | |
109 | |
110 // The currently opened BluetoothChooser. | |
111 std::unique_ptr<BluetoothChooser> chooser_; | |
112 | |
113 // Automatically stops Bluetooth discovery a set amount of time after it was | |
114 // started. | |
115 base::Timer discovery_session_timer_; | |
116 | |
117 // The last discovery session to be started. | |
Jeffrey Yasskin
2016/05/13 04:41:59
So this doesn't get nulled out when no discovery s
ortuno
2016/05/13 20:11:17
We could null it when we stop it but we would also
| |
118 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session_; | |
119 | |
120 // Weak pointer factory for generating 'this' pointers that might live longer | |
121 // than we do. | |
122 // Note: This should remain the last member so it'll be destroyed and | |
123 // invalidate its weak pointers before any other members are destroyed. | |
124 base::WeakPtrFactory<BluetoothDeviceProvider> weak_ptr_factory_; | |
125 }; | |
126 | |
127 } // namespace content | |
128 | |
129 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DEVICE_PROVIDER_H_ | |
OLD | NEW |