refactor: rename the atom directory to shell
This commit is contained in:
parent
4575a4aae3
commit
d7f07e8a80
631 changed files with 0 additions and 0 deletions
128
shell/browser/lib/bluetooth_chooser.cc
Normal file
128
shell/browser/lib/bluetooth_chooser.cc
Normal file
|
@ -0,0 +1,128 @@
|
|||
// 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/once_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) {}
|
||||
|
||||
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_map_.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", GetDeviceList(),
|
||||
base::BindOnce(&OnDeviceChosen, event_handler_));
|
||||
if (!prevent_default) {
|
||||
auto it = device_map_.begin();
|
||||
auto device_id = it->first;
|
||||
event_handler_.Run(Event::SELECTED, device_id);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case DiscoveryState::DISCOVERING:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void BluetoothChooser::AddOrUpdateDevice(const std::string& device_id,
|
||||
bool should_update_name,
|
||||
const base::string16& device_name,
|
||||
bool is_gatt_connected,
|
||||
bool is_paired,
|
||||
int signal_strength_level) {
|
||||
bool changed = false;
|
||||
auto entry = device_map_.find(device_id);
|
||||
if (entry == device_map_.end()) {
|
||||
device_map_[device_id] = device_name;
|
||||
changed = true;
|
||||
} else if (should_update_name) {
|
||||
entry->second = device_name;
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
// Emit a select-bluetooth-device handler to allow for user to listen for
|
||||
// bluetooth device found.
|
||||
bool prevent_default = api_web_contents_->Emit(
|
||||
"select-bluetooth-device", GetDeviceList(),
|
||||
base::BindOnce(&OnDeviceChosen, event_handler_));
|
||||
|
||||
// If emit not implimented select first device that matches the filters
|
||||
// provided.
|
||||
if (!prevent_default) {
|
||||
event_handler_.Run(Event::SELECTED, device_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<atom::BluetoothChooser::DeviceInfo>
|
||||
BluetoothChooser::GetDeviceList() {
|
||||
std::vector<atom::BluetoothChooser::DeviceInfo> vec;
|
||||
for (const auto& it : device_map_) {
|
||||
DeviceInfo info = {it.first, it.second};
|
||||
vec.push_back(info);
|
||||
}
|
||||
|
||||
return vec;
|
||||
}
|
||||
|
||||
} // namespace atom
|
50
shell/browser/lib/bluetooth_chooser.h
Normal file
50
shell/browser/lib/bluetooth_chooser.h
Normal file
|
@ -0,0 +1,50 @@
|
|||
// 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 <map>
|
||||
#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 AddOrUpdateDevice(const std::string& device_id,
|
||||
bool should_update_name,
|
||||
const base::string16& device_name,
|
||||
bool is_gatt_connected,
|
||||
bool is_paired,
|
||||
int signal_strength_level) override;
|
||||
std::vector<DeviceInfo> GetDeviceList();
|
||||
|
||||
private:
|
||||
std::map<std::string, base::string16> device_map_;
|
||||
api::WebContents* api_web_contents_;
|
||||
EventHandler event_handler_;
|
||||
int num_retries_ = 0;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(BluetoothChooser);
|
||||
};
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_BROWSER_LIB_BLUETOOTH_CHOOSER_H_
|
26
shell/browser/lib/power_observer.h
Normal file
26
shell/browser/lib/power_observer.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
// Copyright (c) 2017 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_POWER_OBSERVER_H_
|
||||
#define ATOM_BROWSER_LIB_POWER_OBSERVER_H_
|
||||
|
||||
#include "base/macros.h"
|
||||
|
||||
#if defined(OS_LINUX)
|
||||
#include "atom/browser/lib/power_observer_linux.h"
|
||||
#else
|
||||
#include "base/power_monitor/power_observer.h"
|
||||
#endif // defined(OS_LINUX)
|
||||
|
||||
namespace atom {
|
||||
|
||||
#if defined(OS_LINUX)
|
||||
typedef PowerObserverLinux PowerObserver;
|
||||
#else
|
||||
typedef base::PowerObserver PowerObserver;
|
||||
#endif // defined(OS_LINUX)
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_BROWSER_LIB_POWER_OBSERVER_H_
|
166
shell/browser/lib/power_observer_linux.cc
Normal file
166
shell/browser/lib/power_observer_linux.cc
Normal file
|
@ -0,0 +1,166 @@
|
|||
// Copyright (c) 2017 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/power_observer_linux.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <uv.h>
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
|
||||
#include "base/bind.h"
|
||||
#include "device/bluetooth/dbus/bluez_dbus_thread_manager.h"
|
||||
|
||||
namespace {
|
||||
|
||||
const char kLogindServiceName[] = "org.freedesktop.login1";
|
||||
const char kLogindObjectPath[] = "/org/freedesktop/login1";
|
||||
const char kLogindManagerInterface[] = "org.freedesktop.login1.Manager";
|
||||
|
||||
std::string get_executable_basename() {
|
||||
char buf[4096];
|
||||
size_t buf_size = sizeof(buf);
|
||||
std::string rv("electron");
|
||||
if (!uv_exepath(buf, &buf_size)) {
|
||||
rv = strrchr(static_cast<const char*>(buf), '/') + 1;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace atom {
|
||||
|
||||
PowerObserverLinux::PowerObserverLinux()
|
||||
: lock_owner_name_(get_executable_basename()), weak_ptr_factory_(this) {
|
||||
auto* bus = bluez::BluezDBusThreadManager::Get()->GetSystemBus();
|
||||
if (!bus) {
|
||||
LOG(WARNING) << "Failed to get system bus connection";
|
||||
return;
|
||||
}
|
||||
|
||||
// set up the logind proxy
|
||||
|
||||
const auto weakThis = weak_ptr_factory_.GetWeakPtr();
|
||||
|
||||
logind_ = bus->GetObjectProxy(kLogindServiceName,
|
||||
dbus::ObjectPath(kLogindObjectPath));
|
||||
logind_->ConnectToSignal(
|
||||
kLogindManagerInterface, "PrepareForShutdown",
|
||||
base::BindRepeating(&PowerObserverLinux::OnPrepareForShutdown, weakThis),
|
||||
base::BindRepeating(&PowerObserverLinux::OnSignalConnected, weakThis));
|
||||
logind_->ConnectToSignal(
|
||||
kLogindManagerInterface, "PrepareForSleep",
|
||||
base::BindRepeating(&PowerObserverLinux::OnPrepareForSleep, weakThis),
|
||||
base::BindRepeating(&PowerObserverLinux::OnSignalConnected, weakThis));
|
||||
logind_->WaitForServiceToBeAvailable(base::BindRepeating(
|
||||
&PowerObserverLinux::OnLoginServiceAvailable, weakThis));
|
||||
}
|
||||
|
||||
PowerObserverLinux::~PowerObserverLinux() = default;
|
||||
|
||||
void PowerObserverLinux::OnLoginServiceAvailable(bool service_available) {
|
||||
if (!service_available) {
|
||||
LOG(WARNING) << kLogindServiceName << " not available";
|
||||
return;
|
||||
}
|
||||
// Take sleep inhibit lock
|
||||
BlockSleep();
|
||||
}
|
||||
|
||||
void PowerObserverLinux::BlockSleep() {
|
||||
dbus::MethodCall sleep_inhibit_call(kLogindManagerInterface, "Inhibit");
|
||||
dbus::MessageWriter inhibit_writer(&sleep_inhibit_call);
|
||||
inhibit_writer.AppendString("sleep"); // what
|
||||
// Use the executable name as the lock owner, which will list rebrands of the
|
||||
// electron executable as separate entities.
|
||||
inhibit_writer.AppendString(lock_owner_name_); // who
|
||||
inhibit_writer.AppendString("Application cleanup before suspend"); // why
|
||||
inhibit_writer.AppendString("delay"); // mode
|
||||
logind_->CallMethod(
|
||||
&sleep_inhibit_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
|
||||
base::BindOnce(&PowerObserverLinux::OnInhibitResponse,
|
||||
weak_ptr_factory_.GetWeakPtr(), &sleep_lock_));
|
||||
}
|
||||
|
||||
void PowerObserverLinux::UnblockSleep() {
|
||||
sleep_lock_.reset();
|
||||
}
|
||||
|
||||
void PowerObserverLinux::BlockShutdown() {
|
||||
if (shutdown_lock_.is_valid()) {
|
||||
LOG(WARNING) << "Trying to subscribe to shutdown multiple times";
|
||||
return;
|
||||
}
|
||||
dbus::MethodCall shutdown_inhibit_call(kLogindManagerInterface, "Inhibit");
|
||||
dbus::MessageWriter inhibit_writer(&shutdown_inhibit_call);
|
||||
inhibit_writer.AppendString("shutdown"); // what
|
||||
inhibit_writer.AppendString(lock_owner_name_); // who
|
||||
inhibit_writer.AppendString("Ensure a clean shutdown"); // why
|
||||
inhibit_writer.AppendString("delay"); // mode
|
||||
logind_->CallMethod(
|
||||
&shutdown_inhibit_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
|
||||
base::BindOnce(&PowerObserverLinux::OnInhibitResponse,
|
||||
weak_ptr_factory_.GetWeakPtr(), &shutdown_lock_));
|
||||
}
|
||||
|
||||
void PowerObserverLinux::UnblockShutdown() {
|
||||
if (!shutdown_lock_.is_valid()) {
|
||||
LOG(WARNING)
|
||||
<< "Trying to unsubscribe to shutdown without being subscribed";
|
||||
return;
|
||||
}
|
||||
shutdown_lock_.reset();
|
||||
}
|
||||
|
||||
void PowerObserverLinux::SetShutdownHandler(base::Callback<bool()> handler) {
|
||||
should_shutdown_ = std::move(handler);
|
||||
}
|
||||
|
||||
void PowerObserverLinux::OnInhibitResponse(base::ScopedFD* scoped_fd,
|
||||
dbus::Response* response) {
|
||||
if (response != nullptr) {
|
||||
dbus::MessageReader reader(response);
|
||||
reader.PopFileDescriptor(scoped_fd);
|
||||
}
|
||||
}
|
||||
|
||||
void PowerObserverLinux::OnPrepareForSleep(dbus::Signal* signal) {
|
||||
dbus::MessageReader reader(signal);
|
||||
bool suspending;
|
||||
if (!reader.PopBool(&suspending)) {
|
||||
LOG(ERROR) << "Invalid signal: " << signal->ToString();
|
||||
return;
|
||||
}
|
||||
if (suspending) {
|
||||
OnSuspend();
|
||||
UnblockSleep();
|
||||
} else {
|
||||
BlockSleep();
|
||||
OnResume();
|
||||
}
|
||||
}
|
||||
|
||||
void PowerObserverLinux::OnPrepareForShutdown(dbus::Signal* signal) {
|
||||
dbus::MessageReader reader(signal);
|
||||
bool shutting_down;
|
||||
if (!reader.PopBool(&shutting_down)) {
|
||||
LOG(ERROR) << "Invalid signal: " << signal->ToString();
|
||||
return;
|
||||
}
|
||||
if (shutting_down) {
|
||||
if (!should_shutdown_ || should_shutdown_.Run()) {
|
||||
// The user didn't try to prevent shutdown. Release the lock and allow the
|
||||
// shutdown to continue normally.
|
||||
shutdown_lock_.reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PowerObserverLinux::OnSignalConnected(const std::string& /*interface*/,
|
||||
const std::string& signal,
|
||||
bool success) {
|
||||
LOG_IF(WARNING, !success) << "Failed to connect to " << signal;
|
||||
}
|
||||
|
||||
} // namespace atom
|
54
shell/browser/lib/power_observer_linux.h
Normal file
54
shell/browser/lib/power_observer_linux.h
Normal file
|
@ -0,0 +1,54 @@
|
|||
// Copyright (c) 2017 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_POWER_OBSERVER_LINUX_H_
|
||||
#define ATOM_BROWSER_LIB_POWER_OBSERVER_LINUX_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "base/macros.h"
|
||||
#include "base/memory/weak_ptr.h"
|
||||
#include "base/power_monitor/power_observer.h"
|
||||
#include "dbus/bus.h"
|
||||
#include "dbus/message.h"
|
||||
#include "dbus/object_proxy.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
class PowerObserverLinux : public base::PowerObserver {
|
||||
public:
|
||||
PowerObserverLinux();
|
||||
~PowerObserverLinux() override;
|
||||
|
||||
protected:
|
||||
void BlockSleep();
|
||||
void UnblockSleep();
|
||||
void BlockShutdown();
|
||||
void UnblockShutdown();
|
||||
|
||||
void SetShutdownHandler(base::Callback<bool()> should_shutdown);
|
||||
|
||||
private:
|
||||
void OnLoginServiceAvailable(bool available);
|
||||
void OnInhibitResponse(base::ScopedFD* scoped_fd, dbus::Response* response);
|
||||
void OnPrepareForSleep(dbus::Signal* signal);
|
||||
void OnPrepareForShutdown(dbus::Signal* signal);
|
||||
void OnSignalConnected(const std::string& interface,
|
||||
const std::string& signal,
|
||||
bool success);
|
||||
|
||||
base::Callback<bool()> should_shutdown_;
|
||||
|
||||
scoped_refptr<dbus::ObjectProxy> logind_;
|
||||
std::string lock_owner_name_;
|
||||
base::ScopedFD sleep_lock_;
|
||||
base::ScopedFD shutdown_lock_;
|
||||
base::WeakPtrFactory<PowerObserverLinux> weak_ptr_factory_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(PowerObserverLinux);
|
||||
};
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_BROWSER_LIB_POWER_OBSERVER_LINUX_H_
|
Loading…
Add table
Add a link
Reference in a new issue