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_CHOOSER_CONTROLLER_H_ | |
6 #define CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DEVICE_CHOOSER_CONTROLLER_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/memory/weak_ptr.h" | |
11 #include "base/time/time.h" | |
12 #include "base/timer/timer.h" | |
13 #include "content/public/browser/bluetooth_chooser.h" | |
14 #include "third_party/WebKit/public/platform/modules/bluetooth/web_bluetooth.moj om.h" | |
15 | |
16 namespace device { | |
17 class BluetoothAdapter; | |
18 class BluetoothDevice; | |
19 class BluetoothDiscoveryFilter; | |
20 class BluetoothDiscoverySession; | |
21 } | |
22 | |
23 namespace content { | |
24 | |
25 class RenderFrameHost; | |
26 class WebContents; | |
27 class WebBluetoothServiceImpl; | |
28 | |
29 // Class that interacts with a chooser and starts a bluetooth discovery session. | |
30 // This class needs to be re-instantiated for each call to GetDevice(). Calling | |
31 // GetDevice() twice for the same instance will DCHECK. | |
32 class BluetoothDeviceChooserController final { | |
33 public: | |
34 typedef base::Callback<void(blink::mojom::WebBluetoothRequestDeviceOptionsPtr, | |
35 const std::string& device_address)> | |
36 SuccessCallback; | |
37 typedef base::Callback<void(blink::mojom::WebBluetoothError error)> | |
38 ErrorCallback; | |
39 | |
40 // |web_bluetooth_service_| service that owns this class. | |
41 // |render_frame_host| should be the RenderFrameHost that owns the | |
42 // |web_bluetooth_service_|. | |
43 // |adapter| should be the adapter used to scan for Bluetooth devices. | |
44 // |scan_duration| is how long will a discovery session be active. | |
45 BluetoothDeviceChooserController( | |
46 WebBluetoothServiceImpl* web_bluetooth_service_, | |
47 RenderFrameHost* render_frame_host, | |
48 device::BluetoothAdapter* adapter, | |
49 base::TimeDelta scan_duration); | |
50 ~BluetoothDeviceChooserController(); | |
51 | |
52 // This function performs the following checks before starting a discovery | |
53 // session: | |
54 // - Validates filters in |request_device_options|. | |
55 // - Removes any blacklisted UUIDs from | |
56 // |request_device_options.optinal_services|. | |
57 // - Checks if the request came from a cross-origin iframe. | |
58 // - Checks if the request came from a unique origin. | |
59 // - Checks if the adapter is present. | |
60 // - Checks if the Web Bluetooth API has been disabled. | |
61 // - Checks if we are allowed to ask for scanning permission. | |
62 // If any of the previous checks failed then this function runs | |
63 // |error_callback| with the corresponding error. Otherwise this function | |
64 // populates the embedder provided BluetoothChooser with existing devices and | |
65 // starts a new discovery session. | |
66 // This function should only be called once per | |
67 // BluetoothDeviceChooserController instance. Calling this function more than | |
68 // once will DCHECK. | |
69 void GetDevice( | |
70 blink::mojom::WebBluetoothRequestDeviceOptionsPtr request_device_options, | |
71 const SuccessCallback& success_callback, | |
72 const ErrorCallback& error_callback); | |
73 | |
74 // Adds a device to the chooser. Should only be called after GetDevice and | |
75 // before either of the callbacks are run. | |
76 void AddFilteredDevice(const device::BluetoothDevice& device); | |
77 | |
78 // Stops the current discovery session and notifies the chooser | |
Jeffrey Yasskin
2016/05/13 21:36:55
Mhmm. Another way to do it would be to just make t
ortuno
2016/05/13 22:14:38
Ack.
| |
79 // that the adapter changed states. | |
80 void AdapterPoweredChanged(bool powered); | |
81 | |
82 private: | |
83 // Notifies the chooser that discovery is starting and starts a discovery | |
84 // session. | |
85 void StartDeviceDiscovery(); | |
86 | |
87 // Stops the discovery session and notifies the chooser. | |
88 void StopDeviceDiscovery(); | |
89 | |
90 // StartDiscoverySessionWithFilter callbacks: | |
91 void OnStartDiscoverySessionSuccess( | |
92 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session); | |
93 void OnStartDiscoverySessionFailed(); | |
94 | |
95 // BluetoothChooser::EventHandler: | |
96 // Runs error_callback_ if the chooser was cancelled or if we weren't able | |
97 // to show the chooser. Otherwise runs success_callback_ with | |
98 // |device_address|. | |
99 void OnBluetoothChooserEvent(BluetoothChooser::Event event, | |
100 const std::string& device_address); | |
101 | |
102 // Helper function to asynchronously run success_callback_. | |
103 void PostSuccessCallback(const std::string& device_address); | |
104 // Helper function to asynchronously run error_callback_. | |
105 void PostErrorCallback(blink::mojom::WebBluetoothError error); | |
106 | |
107 // The adapter used to get existing devices and start a discovery session. | |
108 device::BluetoothAdapter* adapter_; | |
109 // The WebBluetoothServiceImpl that owns this instance. | |
110 WebBluetoothServiceImpl* web_bluetooth_service_; | |
111 // The RenderFrameHost that owns web_bluetooth_service_. | |
112 RenderFrameHost* render_frame_host_; | |
113 // The WebContents that owns render_frame_host_. | |
114 WebContents* web_contents_; | |
115 | |
116 // Contains the filters and optional services used when scanning. | |
117 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options_; | |
118 | |
119 // Callbacks to be called with the result of the chooser. | |
120 SuccessCallback success_callback_; | |
121 ErrorCallback error_callback_; | |
122 | |
123 // The currently opened BluetoothChooser. | |
124 std::unique_ptr<BluetoothChooser> chooser_; | |
125 | |
126 // Automatically stops Bluetooth discovery a set amount of time after it was | |
127 // started. | |
128 base::Timer discovery_session_timer_; | |
129 | |
130 // The last discovery session to be started. | |
Jeffrey Yasskin
2016/05/13 21:36:55
Looking at the implementation, you do actually nul
ortuno
2016/05/13 22:14:38
Done. Thanks for opening the issue!
| |
131 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session_; | |
132 | |
133 // Weak pointer factory for generating 'this' pointers that might live longer | |
134 // than we do. | |
135 // Note: This should remain the last member so it'll be destroyed and | |
136 // invalidate its weak pointers before any other members are destroyed. | |
137 base::WeakPtrFactory<BluetoothDeviceChooserController> weak_ptr_factory_; | |
138 }; | |
139 | |
140 } // namespace content | |
141 | |
142 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DEVICE_CHOOSER_CONTROLLER_H_ | |
OLD | NEW |