Merge pull request #5781 from deepak1556/bluetooth_patch

webContents: add event to select bluetooth device
This commit is contained in:
Cheng Zhao 2016-06-01 06:40:08 +00:00
commit 8f49d43b69
6 changed files with 202 additions and 0 deletions

View file

@ -14,6 +14,7 @@
#include "atom/browser/atom_browser_context.h" #include "atom/browser/atom_browser_context.h"
#include "atom/browser/atom_browser_main_parts.h" #include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/atom_security_state_model_client.h" #include "atom/browser/atom_security_state_model_client.h"
#include "atom/browser/lib/bluetooth_chooser.h"
#include "atom/browser/native_window.h" #include "atom/browser/native_window.h"
#include "atom/browser/net/atom_network_delegate.h" #include "atom/browser/net/atom_network_delegate.h"
#include "atom/browser/web_contents_permission_helper.h" #include "atom/browser/web_contents_permission_helper.h"
@ -504,6 +505,14 @@ void WebContents::RequestToLockMouse(
permission_helper->RequestPointerLockPermission(user_gesture); permission_helper->RequestPointerLockPermission(user_gesture);
} }
std::unique_ptr<content::BluetoothChooser> WebContents::RunBluetoothChooser(
content::RenderFrameHost* frame,
const content::BluetoothChooser::EventHandler& event_handler) {
std::unique_ptr<BluetoothChooser> bluetooth_chooser(
new BluetoothChooser(this, event_handler));
return std::move(bluetooth_chooser);
}
void WebContents::BeforeUnloadFired(const base::TimeTicks& proceed_time) { void WebContents::BeforeUnloadFired(const base::TimeTicks& proceed_time) {
// Do nothing, we override this method just to avoid compilation error since // Do nothing, we override this method just to avoid compilation error since
// there are two virtual functions named BeforeUnloadFired. // there are two virtual functions named BeforeUnloadFired.

View file

@ -211,6 +211,9 @@ class WebContents : public mate::TrackableObject<WebContents>,
content::WebContents* web_contents, content::WebContents* web_contents,
bool user_gesture, bool user_gesture,
bool last_unlocked_by_target) override; bool last_unlocked_by_target) override;
std::unique_ptr<content::BluetoothChooser> RunBluetoothChooser(
content::RenderFrameHost* frame,
const content::BluetoothChooser::EventHandler& handler) override;
// content::WebContentsObserver: // content::WebContentsObserver:
void BeforeUnloadFired(const base::TimeTicks& proceed_time) override; void BeforeUnloadFired(const base::TimeTicks& proceed_time) override;

View file

@ -0,0 +1,107 @@
// Copyright (c) 2016 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/lib/bluetooth_chooser.h"
#include "atom/common/native_mate_converters/callback.h"
#include "atom/common/native_mate_converters/string16_converter.h"
#include "native_mate/dictionary.h"
namespace mate {
template<>
struct Converter<atom::BluetoothChooser::DeviceInfo> {
static v8::Local<v8::Value> ToV8(
v8::Isolate* isolate, const atom::BluetoothChooser::DeviceInfo& val) {
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
dict.Set("deviceName", val.device_name);
dict.Set("deviceId", val.device_id);
return mate::ConvertToV8(isolate, dict);
}
};
} // namespace mate
namespace atom {
namespace {
const int kMaxScanRetries = 5;
void OnDeviceChosen(
const content::BluetoothChooser::EventHandler& handler,
const std::string& device_id) {
if (device_id.empty()) {
handler.Run(content::BluetoothChooser::Event::CANCELLED, device_id);
} else {
handler.Run(content::BluetoothChooser::Event::SELECTED, device_id);
}
}
} // namespace
BluetoothChooser::BluetoothChooser(
api::WebContents* contents,
const EventHandler& event_handler)
: api_web_contents_(contents),
event_handler_(event_handler),
num_retries_(0) {
}
BluetoothChooser::~BluetoothChooser() {
}
void BluetoothChooser::SetAdapterPresence(AdapterPresence presence) {
switch (presence) {
case AdapterPresence::ABSENT:
case AdapterPresence::POWERED_OFF:
event_handler_.Run(Event::CANCELLED, "");
break;
case AdapterPresence::POWERED_ON:
break;
}
}
void BluetoothChooser::ShowDiscoveryState(DiscoveryState state) {
switch (state) {
case DiscoveryState::FAILED_TO_START:
event_handler_.Run(Event::CANCELLED, "");
break;
case DiscoveryState::IDLE:
if (device_list_.empty()) {
auto event = ++num_retries_ > kMaxScanRetries ? Event::CANCELLED
: Event::RESCAN;
event_handler_.Run(event, "");
} else {
bool prevent_default =
api_web_contents_->Emit("select-bluetooth-device",
device_list_,
base::Bind(&OnDeviceChosen,
event_handler_));
if (!prevent_default) {
auto device_id = device_list_[0].device_id;
event_handler_.Run(Event::SELECTED, device_id);
}
}
break;
case DiscoveryState::DISCOVERING:
break;
}
}
void BluetoothChooser::AddDevice(const std::string& device_id,
const base::string16& device_name) {
DeviceInfo info = {device_id, device_name};
device_list_.push_back(info);
}
void BluetoothChooser::RemoveDevice(const std::string& device_id) {
for (auto it = device_list_.begin(); it != device_list_.end(); ++it) {
if (it->device_id == device_id) {
device_list_.erase(it);
return;
}
}
}
} // namespace atom

View file

@ -0,0 +1,45 @@
// Copyright (c) 2016 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_LIB_BLUETOOTH_CHOOSER_H_
#define ATOM_BROWSER_LIB_BLUETOOTH_CHOOSER_H_
#include <string>
#include <vector>
#include "atom/browser/api/atom_api_web_contents.h"
#include "content/public/browser/bluetooth_chooser.h"
namespace atom {
class BluetoothChooser : public content::BluetoothChooser {
public:
struct DeviceInfo {
std::string device_id;
base::string16 device_name;
};
explicit BluetoothChooser(api::WebContents* contents,
const EventHandler& handler);
~BluetoothChooser() override;
// content::BluetoothChooser:
void SetAdapterPresence(AdapterPresence presence) override;
void ShowDiscoveryState(DiscoveryState state) override;
void AddDevice(const std::string& device_id,
const base::string16& device_name) override;
void RemoveDevice(const std::string& device_id) override;
private:
std::vector<DeviceInfo> device_list_;
api::WebContents* api_web_contents_;
EventHandler event_handler_;
int num_retries_;
DISALLOW_COPY_AND_ASSIGN(BluetoothChooser);
};
} // namespace atom
#endif // ATOM_BROWSER_LIB_BLUETOOTH_CHOOSER_H_

View file

@ -373,6 +373,42 @@ The `editFlags` is an object with the following properties:
Emitted when there is a new context menu that needs to be handled. Emitted when there is a new context menu that needs to be handled.
### Event: 'select-bluetooth-device'
Returns:
* `event` Event
* `devices` [Objects]
* `deviceName` String
* `deviceId` String
* `callback` Function
* `deviceId` String
Emitted when bluetooth device needs to be selected on call to
`navigator.bluetooth.requestDevice`. To use `navigator.bluetooth` api
`webBluetooth` should be enabled. If `event.preventDefault` is not called,
first available device will be selected. `callback` should be called with
`deviceId` to be selected, passing empty string to `callback` will
cancel the request.
```javacript
app.commandLine.appendSwitch('enable-web-bluetooth')
app.on('ready', () => {
webContents.on('select-bluetooth-device', (event, deviceList, callback) => {
event.preventDefault()
let result = deviceList.find((device) => {
return device.deviceName === 'test'
})
if (!result) {
callback('')
} else {
callback(result.deviceId)
}
})
})
```
## Instance Methods ## Instance Methods
The `webContents` object has the following instance methods: The `webContents` object has the following instance methods:

View file

@ -181,6 +181,8 @@
'atom/browser/common_web_contents_delegate.h', 'atom/browser/common_web_contents_delegate.h',
'atom/browser/javascript_environment.cc', 'atom/browser/javascript_environment.cc',
'atom/browser/javascript_environment.h', 'atom/browser/javascript_environment.h',
'atom/browser/lib/bluetooth_chooser.cc',
'atom/browser/lib/bluetooth_chooser.h',
'atom/browser/login_handler.cc', 'atom/browser/login_handler.cc',
'atom/browser/login_handler.h', 'atom/browser/login_handler.h',
'atom/browser/mac/atom_application.h', 'atom/browser/mac/atom_application.h',