1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_DEVTOOLS_DEVICE_DEVTOOLS_ANDROID_BRIDGE_H_
#define CHROME_BROWSER_DEVTOOLS_DEVICE_DEVTOOLS_ANDROID_BRIDGE_H_
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/cancelable_callback.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/devtools/device/android_device_manager.h"
#include "chrome/browser/devtools/device/devtools_device_discovery.h"
#include "chrome/browser/profiles/profile_keyed_service_factory.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/prefs/pref_change_registrar.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/devtools_agent_host.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
namespace base {
template<typename T> struct DefaultSingletonTraits;
} // namespace base
namespace content {
class BrowserContext;
}
class PortForwardingController;
class Profile;
class TCPDeviceProvider;
class DevToolsAndroidBridge : public KeyedService {
public:
class Factory : public ProfileKeyedServiceFactory {
public:
// Returns singleton instance of DevToolsAndroidBridge.
static Factory* GetInstance();
// Returns DevToolsAndroidBridge associated with |profile|.
static DevToolsAndroidBridge* GetForProfile(Profile* profile);
Factory(const Factory&) = delete;
Factory& operator=(const Factory&) = delete;
private:
friend struct base::DefaultSingletonTraits<Factory>;
Factory();
~Factory() override;
// BrowserContextKeyedServiceFactory overrides:
std::unique_ptr<KeyedService> BuildServiceInstanceForBrowserContext(
content::BrowserContext* context) const override;
};
using RemotePage = DevToolsDeviceDiscovery::RemotePage;
using RemotePages = DevToolsDeviceDiscovery::RemotePages;
using RemoteBrowser = DevToolsDeviceDiscovery::RemoteBrowser;
using RemoteBrowsers = DevToolsDeviceDiscovery::RemoteBrowsers;
using RemoteDevice = DevToolsDeviceDiscovery::RemoteDevice;
using RemoteDevices = DevToolsDeviceDiscovery::RemoteDevices;
using CompleteDevice = DevToolsDeviceDiscovery::CompleteDevice;
using CompleteDevices = DevToolsDeviceDiscovery::CompleteDevices;
using JsonRequestCallback = base::OnceCallback<void(int, const std::string&)>;
class DeviceListListener {
public:
virtual void DeviceListChanged(const RemoteDevices& devices) = 0;
protected:
virtual ~DeviceListListener() = default;
};
explicit DevToolsAndroidBridge(Profile* profile);
~DevToolsAndroidBridge() override;
DevToolsAndroidBridge(const DevToolsAndroidBridge&) = delete;
DevToolsAndroidBridge& operator=(const DevToolsAndroidBridge&) = delete;
void AddDeviceListListener(DeviceListListener* listener);
void RemoveDeviceListListener(DeviceListListener* listener);
class DeviceCountListener {
public:
virtual void DeviceCountChanged(int count) = 0;
protected:
virtual ~DeviceCountListener() = default;
};
void AddDeviceCountListener(DeviceCountListener* listener);
void RemoveDeviceCountListener(DeviceCountListener* listener);
using PortStatus = int;
using PortStatusMap = std::map<int, PortStatus>;
using BrowserStatus = std::pair<scoped_refptr<RemoteBrowser>, PortStatusMap>;
using ForwardingStatus = std::vector<BrowserStatus>;
class PortForwardingListener {
public:
using PortStatusMap = DevToolsAndroidBridge::PortStatusMap;
using BrowserStatus = DevToolsAndroidBridge::BrowserStatus;
using ForwardingStatus = DevToolsAndroidBridge::ForwardingStatus;
virtual void PortStatusChanged(const ForwardingStatus&) = 0;
protected:
virtual ~PortForwardingListener() = default;
};
void AddPortForwardingListener(PortForwardingListener* listener);
void RemovePortForwardingListener(PortForwardingListener* listener);
void set_device_providers_for_test(
const AndroidDeviceManager::DeviceProviders& device_providers) {
device_manager_->SetDeviceProviders(device_providers);
}
void set_task_scheduler_for_test(
base::RepeatingCallback<void(base::OnceClosure)> scheduler) {
task_scheduler_ = scheduler;
}
void OpenRemotePage(scoped_refptr<RemoteBrowser> browser,
const std::string& url);
scoped_refptr<content::DevToolsAgentHost> GetBrowserAgentHost(
scoped_refptr<RemoteBrowser> browser);
void SendJsonRequest(const std::string& browser_id_str,
const std::string& url,
JsonRequestCallback callback);
using TCPProviderCallback =
base::RepeatingCallback<void(scoped_refptr<TCPDeviceProvider>)>;
void set_tcp_provider_callback_for_test(TCPProviderCallback callback);
void set_usb_device_manager_for_test(
mojo::PendingRemote<device::mojom::UsbDeviceManager> fake_usb_manager);
void Shutdown() override;
private:
friend struct content::BrowserThread::DeleteOnThread<
content::BrowserThread::UI>;
friend class base::DeleteHelper<DevToolsAndroidBridge>;
void StartDeviceListPolling();
void StopDeviceListPolling();
bool NeedsDeviceListPolling();
void ReceivedDeviceList(const CompleteDevices& complete_devices);
void StartDeviceCountPolling();
void StopDeviceCountPolling();
void RequestDeviceCount(base::RepeatingCallback<void(int)> callback);
void ReceivedDeviceCount(int count);
void CreateDeviceProviders();
base::WeakPtr<DevToolsAndroidBridge> AsWeakPtr() {
return weak_factory_.GetWeakPtr();
}
const raw_ptr<Profile> profile_;
std::unique_ptr<AndroidDeviceManager> device_manager_;
using DeviceMap =
std::map<std::string, scoped_refptr<AndroidDeviceManager::Device> >;
DeviceMap device_map_;
using DeviceListListeners =
std::vector<raw_ptr<DeviceListListener, VectorExperimental>>;
DeviceListListeners device_list_listeners_;
using DeviceCountListeners =
std::vector<raw_ptr<DeviceCountListener, VectorExperimental>>;
DeviceCountListeners device_count_listeners_;
base::CancelableRepeatingCallback<void(int)> device_count_callback_;
base::RepeatingCallback<void(base::OnceClosure)> task_scheduler_;
using PortForwardingListeners =
std::vector<raw_ptr<PortForwardingListener, VectorExperimental>>;
PortForwardingListeners port_forwarding_listeners_;
std::unique_ptr<PortForwardingController> port_forwarding_controller_;
PrefChangeRegistrar pref_change_registrar_;
TCPProviderCallback tcp_provider_callback_;
std::unique_ptr<DevToolsDeviceDiscovery> device_discovery_;
base::WeakPtrFactory<DevToolsAndroidBridge> weak_factory_{this};
};
#endif // CHROME_BROWSER_DEVTOOLS_DEVICE_DEVTOOLS_ANDROID_BRIDGE_H_
|