Commit e471fab8 authored by reillyg's avatar reillyg Committed by Commit bot

Remove BrowserThread dependency from usb_service.

Instead of explicitly depending on specific browser threads the USB
service can assume that it is instantiated on BrowserThread::FILE (or
equivalent) and save a TaskRunner reference from this instantiation for
later use when called from other threads.

To reach BrowserThread::UI (required for DBus on Chrome OS) a reference
to the appropriate TaskRunner must be provided when calling
UsbService::GetInstance().

BUG=

Review URL: https://codereview.chromium.org/507503002

Cr-Commit-Position: refs/heads/master@{#292546}
parent a859ef1c
...@@ -156,7 +156,6 @@ static_library("browser") { ...@@ -156,7 +156,6 @@ static_library("browser") {
"//components/storage_monitor", "//components/storage_monitor",
"//components/translate/content/browser", "//components/translate/content/browser",
"//components/url_matcher", "//components/url_matcher",
"//components/usb_service",
"//components/visitedlink/browser", "//components/visitedlink/browser",
"//components/visitedlink/common", "//components/visitedlink/common",
"//components/web_modal", "//components/web_modal",
...@@ -450,6 +449,10 @@ static_library("browser") { ...@@ -450,6 +449,10 @@ static_library("browser") {
# Non-mobile. # Non-mobile.
sources += rebase_path(gypi_values.chrome_browser_non_mobile_sources, sources += rebase_path(gypi_values.chrome_browser_non_mobile_sources,
".", "//chrome") ".", "//chrome")
deps += [
"//components/usb_service",
"//device/core",
]
} }
if (is_android) { if (is_android) {
...@@ -464,7 +467,6 @@ static_library("browser") { ...@@ -464,7 +467,6 @@ static_library("browser") {
"//third_party/libaddressinput", "//third_party/libaddressinput",
"//components/feedback", "//components/feedback",
"//components/storage_monitor", "//components/storage_monitor",
"//components/usb_service",
"//components/web_modal", "//components/web_modal",
] ]
} else { } else {
......
...@@ -70,6 +70,7 @@ include_rules = [ ...@@ -70,6 +70,7 @@ include_rules = [
"+components/translate/core/browser", "+components/translate/core/browser",
"+components/translate/core/common", "+components/translate/core/common",
"+components/url_matcher", "+components/url_matcher",
"+components/usb_service",
"+components/user_manager", "+components/user_manager",
"+components/user_prefs", "+components/user_prefs",
"+components/web_modal", "+components/web_modal",
...@@ -78,6 +79,7 @@ include_rules = [ ...@@ -78,6 +79,7 @@ include_rules = [
"+content/test/net", "+content/test/net",
"+courgette", "+courgette",
"+device/bluetooth", "+device/bluetooth",
"+device/core",
"+device/media_transfer_protocol", "+device/media_transfer_protocol",
"+extensions/browser", "+extensions/browser",
"+extensions/common", "+extensions/common",
......
...@@ -106,6 +106,7 @@ ...@@ -106,6 +106,7 @@
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
#include "components/gcm_driver/gcm_driver_android.h" #include "components/gcm_driver/gcm_driver_android.h"
#else #else
#include "chrome/browser/chrome_device_client.h"
#include "chrome/browser/services/gcm/gcm_desktop_utils.h" #include "chrome/browser/services/gcm/gcm_desktop_utils.h"
#include "components/gcm_driver/gcm_client_factory.h" #include "components/gcm_driver/gcm_client_factory.h"
#endif #endif
...@@ -194,6 +195,10 @@ BrowserProcessImpl::BrowserProcessImpl( ...@@ -194,6 +195,10 @@ BrowserProcessImpl::BrowserProcessImpl(
InitIdleMonitor(); InitIdleMonitor();
#endif #endif
#if !defined(OS_ANDROID)
device_client_.reset(new ChromeDeviceClient);
#endif
#if defined(ENABLE_EXTENSIONS) #if defined(ENABLE_EXTENSIONS)
apps::AppsClient::Set(ChromeAppsClient::GetInstance()); apps::AppsClient::Set(ChromeAppsClient::GetInstance());
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "base/timer/timer.h" #include "base/timer/timer.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
class ChromeDeviceClient;
class ChromeNetLog; class ChromeNetLog;
class ChromeResourceDispatcherHostDelegate; class ChromeResourceDispatcherHostDelegate;
class RemoteDebuggingServer; class RemoteDebuggingServer;
...@@ -300,6 +301,10 @@ class BrowserProcessImpl : public BrowserProcess, ...@@ -300,6 +301,10 @@ class BrowserProcessImpl : public BrowserProcess,
scoped_ptr<gcm::GCMDriver> gcm_driver_; scoped_ptr<gcm::GCMDriver> gcm_driver_;
#if !defined(OS_ANDROID)
scoped_ptr<ChromeDeviceClient> device_client_;
#endif
DISALLOW_COPY_AND_ASSIGN(BrowserProcessImpl); DISALLOW_COPY_AND_ASSIGN(BrowserProcessImpl);
}; };
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chrome_device_client.h"
#include "base/logging.h"
#include "components/usb_service/usb_service.h"
#include "content/public/browser/browser_thread.h"
ChromeDeviceClient::ChromeDeviceClient() {}
ChromeDeviceClient::~ChromeDeviceClient() {}
usb_service::UsbService* ChromeDeviceClient::GetUsbService() {
return usb_service::UsbService::GetInstance(
content::BrowserThread::GetMessageLoopProxyForThread(
content::BrowserThread::UI));
}
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_CHROME_DEVICE_CLIENT_H_
#define CHROME_BROWSER_CHROME_DEVICE_CLIENT_H_
#include "device/core/device_client.h"
#include "base/compiler_specific.h"
#include "base/macros.h"
// Implementation of device::DeviceClient that returns //device service
// singletons appropriate for use within the Chrome application.
class ChromeDeviceClient : device::DeviceClient {
public:
ChromeDeviceClient();
virtual ~ChromeDeviceClient();
// device::DeviceClient implementation
virtual usb_service::UsbService* GetUsbService() OVERRIDE;
private:
DISALLOW_COPY_AND_ASSIGN(ChromeDeviceClient);
};
#endif // CHROME_BROWSER_CHROME_DEVICE_CLIENT_H_
include_rules = [
"+components/usb_service"
]
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "components/usb_service/usb_service.h" #include "components/usb_service/usb_service.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "crypto/rsa_private_key.h" #include "crypto/rsa_private_key.h"
#include "device/core/device_client.h"
#include "net/base/ip_endpoint.h" #include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
#include "net/socket/stream_socket.h" #include "net/socket/stream_socket.h"
...@@ -224,7 +225,7 @@ static void OpenAndroidDeviceOnFileThread( ...@@ -224,7 +225,7 @@ static void OpenAndroidDeviceOnFileThread(
static int CountOnFileThread() { static int CountOnFileThread() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
UsbService* service = UsbService::GetInstance(); UsbService* service = device::DeviceClient::Get()->GetUsbService();
UsbDevices usb_devices; UsbDevices usb_devices;
if (service != NULL) if (service != NULL)
service->GetDevices(&usb_devices); service->GetDevices(&usb_devices);
...@@ -249,7 +250,7 @@ static void EnumerateOnFileThread( ...@@ -249,7 +250,7 @@ static void EnumerateOnFileThread(
scoped_refptr<base::MessageLoopProxy> caller_message_loop_proxy) { scoped_refptr<base::MessageLoopProxy> caller_message_loop_proxy) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
UsbService* service = UsbService::GetInstance(); UsbService* service = device::DeviceClient::Get()->GetUsbService();
UsbDevices usb_devices; UsbDevices usb_devices;
if (service != NULL) if (service != NULL)
service->GetDevices(&usb_devices); service->GetDevices(&usb_devices);
......
...@@ -2279,6 +2279,8 @@ ...@@ -2279,6 +2279,8 @@
'chrome_browser_non_mobile_sources': [ 'chrome_browser_non_mobile_sources': [
'browser/chrome_browser_field_trials_desktop.cc', 'browser/chrome_browser_field_trials_desktop.cc',
'browser/chrome_browser_field_trials_desktop.h', 'browser/chrome_browser_field_trials_desktop.h',
'browser/chrome_device_client.cc',
'browser/chrome_device_client.h',
], ],
'chrome_browser_supervised_user_sources': [ 'chrome_browser_supervised_user_sources': [
'browser/supervised_user/custodian_profile_downloader_service.cc', 'browser/supervised_user/custodian_profile_downloader_service.cc',
...@@ -2911,7 +2913,6 @@ ...@@ -2911,7 +2913,6 @@
'../components/components.gyp:storage_monitor', '../components/components.gyp:storage_monitor',
'../components/components.gyp:translate_content_browser', '../components/components.gyp:translate_content_browser',
'../components/components.gyp:url_matcher', '../components/components.gyp:url_matcher',
'../components/components.gyp:usb_service',
'../components/components.gyp:visitedlink_browser', '../components/components.gyp:visitedlink_browser',
'../components/components.gyp:visitedlink_common', '../components/components.gyp:visitedlink_common',
'../components/components.gyp:web_modal', '../components/components.gyp:web_modal',
...@@ -3236,6 +3237,10 @@ ...@@ -3236,6 +3237,10 @@
'sources': [ '<@(chrome_browser_mobile_sources)' ], 'sources': [ '<@(chrome_browser_mobile_sources)' ],
}, { # OS!="android" and OS!="ios" }, { # OS!="android" and OS!="ios"
'sources': [ '<@(chrome_browser_non_mobile_sources)' ], 'sources': [ '<@(chrome_browser_non_mobile_sources)' ],
'dependencies': [
'../components/components.gyp:usb_service',
'../device/core/core.gyp:device_core',
]
}], }],
['OS=="android"', { ['OS=="android"', {
'dependencies': [ 'dependencies': [
...@@ -3248,7 +3253,6 @@ ...@@ -3248,7 +3253,6 @@
'dependencies!': [ 'dependencies!': [
'../components/components.gyp:feedback_component', '../components/components.gyp:feedback_component',
'../components/components.gyp:storage_monitor', '../components/components.gyp:storage_monitor',
'../components/components.gyp:usb_service',
'../components/components.gyp:web_modal', '../components/components.gyp:web_modal',
'../third_party/libaddressinput/libaddressinput.gyp:libaddressinput', '../third_party/libaddressinput/libaddressinput.gyp:libaddressinput',
], ],
......
include_rules = [ include_rules = [
"+chromeos", "+chromeos",
"+content/public/browser",
"-net", "-net",
"+net/base", "+net/base",
......
...@@ -15,13 +15,10 @@ ...@@ -15,13 +15,10 @@
#include "components/usb_service/usb_device_handle.h" #include "components/usb_service/usb_device_handle.h"
#include "components/usb_service/usb_interface.h" #include "components/usb_service/usb_interface.h"
#include "net/base/io_buffer.h" #include "net/base/io_buffer.h"
#include "third_party/libusb/src/libusb/libusb.h"
struct libusb_device_handle;
struct libusb_iso_packet_descriptor;
struct libusb_transfer;
namespace base { namespace base {
class MessageLoopProxy; class SingleThreadTaskRunner;
} }
namespace usb_service { namespace usb_service {
...@@ -97,8 +94,6 @@ class UsbDeviceHandleImpl : public UsbDeviceHandle { ...@@ -97,8 +94,6 @@ class UsbDeviceHandleImpl : public UsbDeviceHandle {
virtual ~UsbDeviceHandleImpl(); virtual ~UsbDeviceHandleImpl();
private: private:
friend void HandleTransferCompletion(PlatformUsbTransferHandle handle);
class InterfaceClaimer; class InterfaceClaimer;
struct Transfer; struct Transfer;
...@@ -111,6 +106,16 @@ class UsbDeviceHandleImpl : public UsbDeviceHandle { ...@@ -111,6 +106,16 @@ class UsbDeviceHandleImpl : public UsbDeviceHandle {
scoped_refptr<InterfaceClaimer> GetClaimedInterfaceForEndpoint( scoped_refptr<InterfaceClaimer> GetClaimedInterfaceForEndpoint(
unsigned char endpoint); unsigned char endpoint);
// If the device's task runner is on the current thread then the transfer will
// be submitted directly, otherwise a task to do so it posted. The callback
// will be called on the current message loop of the thread where this
// function was called.
void PostOrSubmitTransfer(PlatformUsbTransferHandle handle,
UsbTransferType transfer_type,
net::IOBuffer* buffer,
size_t length,
const UsbTransferCallback& callback);
// Submits a transfer and starts tracking it. Retains the buffer and copies // Submits a transfer and starts tracking it. Retains the buffer and copies
// the completion callback until the transfer finishes, whereupon it invokes // the completion callback until the transfer finishes, whereupon it invokes
// the callback then releases the buffer. // the callback then releases the buffer.
...@@ -118,12 +123,15 @@ class UsbDeviceHandleImpl : public UsbDeviceHandle { ...@@ -118,12 +123,15 @@ class UsbDeviceHandleImpl : public UsbDeviceHandle {
UsbTransferType transfer_type, UsbTransferType transfer_type,
net::IOBuffer* buffer, net::IOBuffer* buffer,
const size_t length, const size_t length,
scoped_refptr<base::MessageLoopProxy> message_loop_proxy, scoped_refptr<base::SingleThreadTaskRunner> task_runner,
const UsbTransferCallback& callback); const UsbTransferCallback& callback);
static void LIBUSB_CALL
PlatformTransferCallback(PlatformUsbTransferHandle handle);
// Invokes the callbacks associated with a given transfer, and removes it from // Invokes the callbacks associated with a given transfer, and removes it from
// the in-flight transfer set. // the in-flight transfer set.
void TransferComplete(PlatformUsbTransferHandle transfer); void CompleteTransfer(PlatformUsbTransferHandle transfer);
bool GetSupportedLanguages(); bool GetSupportedLanguages();
bool GetStringDescriptor(uint8 string_id, base::string16* string); bool GetStringDescriptor(uint8 string_id, base::string16* string);
...@@ -154,6 +162,8 @@ class UsbDeviceHandleImpl : public UsbDeviceHandle { ...@@ -154,6 +162,8 @@ class UsbDeviceHandleImpl : public UsbDeviceHandle {
// before this handle. // before this handle.
scoped_refptr<UsbContext> context_; scoped_refptr<UsbContext> context_;
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
base::ThreadChecker thread_checker_; base::ThreadChecker thread_checker_;
DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl); DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl);
......
...@@ -6,12 +6,15 @@ ...@@ -6,12 +6,15 @@
#include <algorithm> #include <algorithm>
#include "base/bind.h"
#include "base/location.h"
#include "base/single_thread_task_runner.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/thread_task_runner_handle.h"
#include "components/usb_service/usb_context.h" #include "components/usb_service/usb_context.h"
#include "components/usb_service/usb_device_handle_impl.h" #include "components/usb_service/usb_device_handle_impl.h"
#include "components/usb_service/usb_error.h" #include "components/usb_service/usb_error.h"
#include "components/usb_service/usb_interface_impl.h" #include "components/usb_service/usb_interface_impl.h"
#include "content/public/browser/browser_thread.h"
#include "third_party/libusb/src/libusb/libusb.h" #include "third_party/libusb/src/libusb/libusb.h"
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
...@@ -20,16 +23,14 @@ ...@@ -20,16 +23,14 @@
#include "chromeos/dbus/permission_broker_client.h" #include "chromeos/dbus/permission_broker_client.h"
#endif // defined(OS_CHROMEOS) #endif // defined(OS_CHROMEOS)
using content::BrowserThread;
namespace { namespace {
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
void OnRequestUsbAccessReplied( void OnRequestUsbAccessReplied(
scoped_refptr<base::SingleThreadTaskRunner> task_runner,
const base::Callback<void(bool success)>& callback, const base::Callback<void(bool success)>& callback,
bool success) { bool success) {
BrowserThread::PostTask( task_runner->PostTask(FROM_HERE, base::Bind(callback, success));
BrowserThread::FILE, FROM_HERE, base::Bind(callback, success));
} }
#endif // defined(OS_CHROMEOS) #endif // defined(OS_CHROMEOS)
...@@ -37,14 +38,17 @@ void OnRequestUsbAccessReplied( ...@@ -37,14 +38,17 @@ void OnRequestUsbAccessReplied(
namespace usb_service { namespace usb_service {
UsbDeviceImpl::UsbDeviceImpl(scoped_refptr<UsbContext> context, UsbDeviceImpl::UsbDeviceImpl(
PlatformUsbDevice platform_device, scoped_refptr<UsbContext> context,
uint16 vendor_id, scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
uint16 product_id, PlatformUsbDevice platform_device,
uint32 unique_id) uint16 vendor_id,
uint16 product_id,
uint32 unique_id)
: UsbDevice(vendor_id, product_id, unique_id), : UsbDevice(vendor_id, product_id, unique_id),
platform_device_(platform_device), platform_device_(platform_device),
context_(context) { context_(context),
ui_task_runner_(ui_task_runner) {
CHECK(platform_device) << "platform_device cannot be NULL"; CHECK(platform_device) << "platform_device cannot be NULL";
libusb_ref_device(platform_device); libusb_ref_device(platform_device);
} }
...@@ -77,15 +81,16 @@ void UsbDeviceImpl::RequestUsbAccess( ...@@ -77,15 +81,16 @@ void UsbDeviceImpl::RequestUsbAccess(
return; return;
} }
BrowserThread::PostTask( ui_task_runner_->PostTask(
BrowserThread::UI,
FROM_HERE, FROM_HERE,
base::Bind(&chromeos::PermissionBrokerClient::RequestUsbAccess, base::Bind(&chromeos::PermissionBrokerClient::RequestUsbAccess,
base::Unretained(client), base::Unretained(client),
vendor_id(), vendor_id(),
product_id(), product_id(),
interface_id, interface_id,
base::Bind(&OnRequestUsbAccessReplied, callback))); base::Bind(&OnRequestUsbAccessReplied,
base::ThreadTaskRunnerHandle::Get(),
callback)));
} }
} }
......
...@@ -15,6 +15,10 @@ ...@@ -15,6 +15,10 @@
struct libusb_device; struct libusb_device;
struct libusb_config_descriptor; struct libusb_config_descriptor;
namespace base {
class SingleThreadTaskRunner;
}
namespace usb_service { namespace usb_service {
class UsbDeviceHandleImpl; class UsbDeviceHandleImpl;
...@@ -40,6 +44,7 @@ class UsbDeviceImpl : public UsbDevice { ...@@ -40,6 +44,7 @@ class UsbDeviceImpl : public UsbDevice {
// Called by UsbServiceImpl only; // Called by UsbServiceImpl only;
UsbDeviceImpl(scoped_refptr<UsbContext> context, UsbDeviceImpl(scoped_refptr<UsbContext> context,
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
PlatformUsbDevice platform_device, PlatformUsbDevice platform_device,
uint16 vendor_id, uint16 vendor_id,
uint16 product_id, uint16 product_id,
...@@ -61,6 +66,9 @@ class UsbDeviceImpl : public UsbDevice { ...@@ -61,6 +66,9 @@ class UsbDeviceImpl : public UsbDevice {
typedef std::vector<scoped_refptr<UsbDeviceHandleImpl> > HandlesVector; typedef std::vector<scoped_refptr<UsbDeviceHandleImpl> > HandlesVector;
HandlesVector handles_; HandlesVector handles_;
// Reference to the UI thread for permission-broker calls.
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
DISALLOW_COPY_AND_ASSIGN(UsbDeviceImpl); DISALLOW_COPY_AND_ASSIGN(UsbDeviceImpl);
}; };
......
...@@ -12,6 +12,10 @@ ...@@ -12,6 +12,10 @@
#include "base/threading/non_thread_safe.h" #include "base/threading/non_thread_safe.h"
#include "components/usb_service/usb_service_export.h" #include "components/usb_service/usb_service_export.h"
namespace base {
class SingleThreadTaskRunner;
}
namespace usb_service { namespace usb_service {
class UsbDevice; class UsbDevice;
...@@ -22,9 +26,12 @@ class UsbDevice; ...@@ -22,9 +26,12 @@ class UsbDevice;
// competition for the same USB device. // competition for the same USB device.
class USB_SERVICE_EXPORT UsbService : public base::NonThreadSafe { class USB_SERVICE_EXPORT UsbService : public base::NonThreadSafe {
public: public:
// Must be called on FILE thread. // Must be called on a thread with a MessageLoopForIO (for example
// Returns NULL when failed to initialized. // BrowserThread::FILE). The UI task runner reference is used to talk to the
static UsbService* GetInstance(); // PermissionBrokerClient on ChromeOS (UI thread). Returns NULL when
// initialization fails.
static UsbService* GetInstance(
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner);
static void SetInstanceForTest(UsbService* instance); static void SetInstanceForTest(UsbService* instance);
......
...@@ -9,11 +9,11 @@ ...@@ -9,11 +9,11 @@
#include "base/lazy_instance.h" #include "base/lazy_instance.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "components/usb_service/usb_context.h" #include "components/usb_service/usb_context.h"
#include "components/usb_service/usb_device_impl.h" #include "components/usb_service/usb_device_impl.h"
#include "components/usb_service/usb_error.h" #include "components/usb_service/usb_error.h"
#include "content/public/browser/browser_thread.h"
#include "third_party/libusb/src/libusb/libusb.h" #include "third_party/libusb/src/libusb/libusb.h"
namespace usb_service { namespace usb_service {
...@@ -32,7 +32,9 @@ class UsbServiceImpl ...@@ -32,7 +32,9 @@ class UsbServiceImpl
: public UsbService, : public UsbService,
private base::MessageLoop::DestructionObserver { private base::MessageLoop::DestructionObserver {
public: public:
explicit UsbServiceImpl(PlatformUsbContext context); explicit UsbServiceImpl(
PlatformUsbContext context,
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner);
virtual ~UsbServiceImpl(); virtual ~UsbServiceImpl();
private: private:
...@@ -48,6 +50,8 @@ class UsbServiceImpl ...@@ -48,6 +50,8 @@ class UsbServiceImpl
void RefreshDevices(); void RefreshDevices();
scoped_refptr<UsbContext> context_; scoped_refptr<UsbContext> context_;
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
// TODO(ikarienator): Figure out a better solution. // TODO(ikarienator): Figure out a better solution.
uint32 next_unique_id_; uint32 next_unique_id_;
...@@ -85,8 +89,12 @@ void UsbServiceImpl::WillDestroyCurrentMessageLoop() { ...@@ -85,8 +89,12 @@ void UsbServiceImpl::WillDestroyCurrentMessageLoop() {
g_usb_service_instance.Get().reset(NULL); g_usb_service_instance.Get().reset(NULL);
} }
UsbServiceImpl::UsbServiceImpl(PlatformUsbContext context) UsbServiceImpl::UsbServiceImpl(
: context_(new UsbContext(context)), next_unique_id_(0) { PlatformUsbContext context,
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner)
: context_(new UsbContext(context)),
ui_task_runner_(ui_task_runner),
next_unique_id_(0) {
base::MessageLoop::current()->AddDestructionObserver(this); base::MessageLoop::current()->AddDestructionObserver(this);
} }
...@@ -124,6 +132,7 @@ void UsbServiceImpl::RefreshDevices() { ...@@ -124,6 +132,7 @@ void UsbServiceImpl::RefreshDevices() {
continue; continue;
} }
UsbDeviceImpl* new_device = new UsbDeviceImpl(context_, UsbDeviceImpl* new_device = new UsbDeviceImpl(context_,
ui_task_runner_,
platform_devices[i], platform_devices[i],
descriptor.idVendor, descriptor.idVendor,
descriptor.idProduct, descriptor.idProduct,
...@@ -153,7 +162,8 @@ void UsbServiceImpl::RefreshDevices() { ...@@ -153,7 +162,8 @@ void UsbServiceImpl::RefreshDevices() {
} }
// static // static
UsbService* UsbService::GetInstance() { UsbService* UsbService::GetInstance(
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) {
UsbService* instance = g_usb_service_instance.Get().get(); UsbService* instance = g_usb_service_instance.Get().get();
if (!instance) { if (!instance) {
PlatformUsbContext context = NULL; PlatformUsbContext context = NULL;
...@@ -166,7 +176,7 @@ UsbService* UsbService::GetInstance() { ...@@ -166,7 +176,7 @@ UsbService* UsbService::GetInstance() {
if (!context) if (!context)
return NULL; return NULL;
instance = new UsbServiceImpl(context); instance = new UsbServiceImpl(context, ui_task_runner);
g_usb_service_instance.Get().reset(instance); g_usb_service_instance.Get().reset(instance);
} }
return instance; return instance;
......
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
source_set("core") {
sources = [
"device_client.cc",
"device_client.h",
]
}
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
{
'variables': {
'chromium_code': 1,
},
'targets': [
{
'target_name': 'device_core',
'type': 'static_library',
'include_dirs': [
'../..',
],
'sources': [
'device_client.cc',
'device_client.h',
],
},
],
}
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "device/core/device_client.h"
#include "base/logging.h"
namespace device {
namespace {
DeviceClient* g_instance = NULL;
} // namespace
DeviceClient::DeviceClient() {
DCHECK(!g_instance);
g_instance = this;
}
DeviceClient::~DeviceClient() {
g_instance = NULL;
}
/* static */
DeviceClient* DeviceClient::Get() {
DCHECK(g_instance);
return g_instance;
}
usb_service::UsbService* DeviceClient::GetUsbService() {
// This should never be called by clients which do not support the USB API.
NOTREACHED();
return NULL;
}
} // namespace device
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef DEVICE_CORE_DEVICE_CLIENT_H_
#define DEVICE_CORE_DEVICE_CLIENT_H_
#include "base/macros.h"
namespace usb_service {
class UsbService;
}
namespace device {
// Interface used by consumers of //device APIs to get pointers to the service
// singletons appropriate for a given embedding application. For an example see
// //chrome/browser/chrome_device_client.h.
class DeviceClient {
public:
// Construction sets the single instance.
DeviceClient();
// Destruction clears the single instance.
~DeviceClient();
// Returns the single instance of |this|.
static DeviceClient* Get();
// Returns the UsbService instance for this embedder.
virtual usb_service::UsbService* GetUsbService();
private:
DISALLOW_COPY_AND_ASSIGN(DeviceClient);
};
} // namespace device
#endif // DEVICE_CORE_DEVICE_CLIENT_H_
...@@ -155,7 +155,7 @@ UsbTestGadgetImpl::UsbTestGadgetImpl() { ...@@ -155,7 +155,7 @@ UsbTestGadgetImpl::UsbTestGadgetImpl() {
session_id_ = base::StringPrintf( session_id_ = base::StringPrintf(
"%s:%p", base::HexEncode(&process_id, sizeof(process_id)).c_str(), this); "%s:%p", base::HexEncode(&process_id, sizeof(process_id)).c_str(), this);
usb_service_ = UsbService::GetInstance(); usb_service_ = UsbService::GetInstance(NULL);
} }
UsbTestGadgetImpl::~UsbTestGadgetImpl() { UsbTestGadgetImpl::~UsbTestGadgetImpl() {
......
include_rules = [ include_rules = [
"+components/usb_service", "+components/usb_service",
"+device/core"
] ]
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "base/message_loop/message_loop_proxy.h" #include "base/message_loop/message_loop_proxy.h"
#include "components/usb_service/usb_device_handle.h" #include "components/usb_service/usb_device_handle.h"
#include "components/usb_service/usb_service.h" #include "components/usb_service/usb_service.h"
#include "device/core/device_client.h"
#include "extensions/browser/api/usb/usb_device_resource.h" #include "extensions/browser/api/usb/usb_device_resource.h"
#include "extensions/browser/extension_system.h" #include "extensions/browser/extension_system.h"
#include "extensions/common/api/usb.h" #include "extensions/common/api/usb.h"
...@@ -429,7 +430,7 @@ scoped_refptr<UsbDevice> UsbAsyncApiFunction::GetDeviceOrOrCompleteWithError( ...@@ -429,7 +430,7 @@ scoped_refptr<UsbDevice> UsbAsyncApiFunction::GetDeviceOrOrCompleteWithError(
return NULL; return NULL;
} }
UsbService* service = UsbService::GetInstance(); UsbService* service = device::DeviceClient::Get()->GetUsbService();
if (!service) { if (!service) {
CompleteWithError(kErrorInitService); CompleteWithError(kErrorInitService);
return NULL; return NULL;
...@@ -560,7 +561,7 @@ void UsbFindDevicesFunction::AsyncWorkStart() { ...@@ -560,7 +561,7 @@ void UsbFindDevicesFunction::AsyncWorkStart() {
return; return;
} }
UsbService* service = UsbService::GetInstance(); UsbService* service = device::DeviceClient::Get()->GetUsbService();
if (!service) { if (!service) {
CompleteWithError(kErrorInitService); CompleteWithError(kErrorInitService);
return; return;
...@@ -636,7 +637,7 @@ void UsbGetDevicesFunction::AsyncWorkStart() { ...@@ -636,7 +637,7 @@ void UsbGetDevicesFunction::AsyncWorkStart() {
return; return;
} }
UsbService* service = UsbService::GetInstance(); UsbService* service = device::DeviceClient::Get()->GetUsbService();
if (!service) { if (!service) {
CompleteWithError(kErrorInitService); CompleteWithError(kErrorInitService);
return; return;
......
include_rules = [ include_rules = [
"+components/usb_service", "+components/usb_service",
"+device/core",
"+device/usb", "+device/usb",
] ]
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "components/usb_service/usb_device_filter.h" #include "components/usb_service/usb_device_filter.h"
#include "components/usb_service/usb_device_handle.h" #include "components/usb_service/usb_device_handle.h"
#include "components/usb_service/usb_service.h" #include "components/usb_service/usb_service.h"
#include "device/core/device_client.h"
#include "device/usb/usb_ids.h" #include "device/usb/usb_ids.h"
#include "extensions/common/api/usb_private.h" #include "extensions/common/api/usb_private.h"
...@@ -19,6 +20,7 @@ namespace usb_private = extensions::core_api::usb_private; ...@@ -19,6 +20,7 @@ namespace usb_private = extensions::core_api::usb_private;
namespace GetDevices = usb_private::GetDevices; namespace GetDevices = usb_private::GetDevices;
namespace GetDeviceInfo = usb_private::GetDeviceInfo; namespace GetDeviceInfo = usb_private::GetDeviceInfo;
using content::BrowserThread;
using usb_service::UsbDevice; using usb_service::UsbDevice;
using usb_service::UsbDeviceFilter; using usb_service::UsbDeviceFilter;
using usb_service::UsbDeviceHandle; using usb_service::UsbDeviceHandle;
...@@ -47,7 +49,7 @@ bool UsbPrivateGetDevicesFunction::Prepare() { ...@@ -47,7 +49,7 @@ bool UsbPrivateGetDevicesFunction::Prepare() {
} }
void UsbPrivateGetDevicesFunction::AsyncWorkStart() { void UsbPrivateGetDevicesFunction::AsyncWorkStart() {
UsbService* service = UsbService::GetInstance(); UsbService* service = device::DeviceClient::Get()->GetUsbService();
if (!service) { if (!service) {
CompleteWithError(kErrorInitService); CompleteWithError(kErrorInitService);
return; return;
...@@ -117,7 +119,7 @@ bool UsbPrivateGetDeviceInfoFunction::Prepare() { ...@@ -117,7 +119,7 @@ bool UsbPrivateGetDeviceInfoFunction::Prepare() {
} }
void UsbPrivateGetDeviceInfoFunction::AsyncWorkStart() { void UsbPrivateGetDeviceInfoFunction::AsyncWorkStart() {
UsbService* service = UsbService::GetInstance(); UsbService* service = device::DeviceClient::Get()->GetUsbService();
if (!service) { if (!service) {
CompleteWithError(kErrorInitService); CompleteWithError(kErrorInitService);
return; return;
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
'<(DEPTH)/content/content.gyp:content_gpu', '<(DEPTH)/content/content.gyp:content_gpu',
'<(DEPTH)/content/content.gyp:content_ppapi_plugin', '<(DEPTH)/content/content.gyp:content_ppapi_plugin',
'<(DEPTH)/content/content_shell_and_tests.gyp:content_shell_lib', '<(DEPTH)/content/content_shell_and_tests.gyp:content_shell_lib',
'<(DEPTH)/device/core/core.gyp:device_core',
'<(DEPTH)/device/hid/hid.gyp:device_hid', '<(DEPTH)/device/hid/hid.gyp:device_hid',
'<(DEPTH)/extensions/browser/api/api_registration.gyp:extensions_api_registration', '<(DEPTH)/extensions/browser/api/api_registration.gyp:extensions_api_registration',
'<(DEPTH)/extensions/common/api/api.gyp:extensions_api', '<(DEPTH)/extensions/common/api/api.gyp:extensions_api',
...@@ -71,6 +72,8 @@ ...@@ -71,6 +72,8 @@
'browser/shell_content_browser_client.h', 'browser/shell_content_browser_client.h',
'browser/shell_desktop_controller.cc', 'browser/shell_desktop_controller.cc',
'browser/shell_desktop_controller.h', 'browser/shell_desktop_controller.h',
'browser/shell_device_client.cc',
'browser/shell_device_client.h',
'browser/shell_extension_host_delegate.cc', 'browser/shell_extension_host_delegate.cc',
'browser/shell_extension_host_delegate.h', 'browser/shell_extension_host_delegate.h',
'browser/shell_extension_system.cc', 'browser/shell_extension_system.cc',
......
...@@ -14,6 +14,8 @@ include_rules = [ ...@@ -14,6 +14,8 @@ include_rules = [
"+content/shell/browser/shell_net_log.h", "+content/shell/browser/shell_net_log.h",
# For device backend support. # For device backend support.
"+components/usb_service",
"+device/core",
"+device/hid", "+device/hid",
# Only used in API tests that should be moved to extensions/browser/api/... # Only used in API tests that should be moved to extensions/browser/api/...
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "extensions/shell/browser/shell_browser_context.h" #include "extensions/shell/browser/shell_browser_context.h"
#include "extensions/shell/browser/shell_browser_main_delegate.h" #include "extensions/shell/browser/shell_browser_main_delegate.h"
#include "extensions/shell/browser/shell_desktop_controller.h" #include "extensions/shell/browser/shell_desktop_controller.h"
#include "extensions/shell/browser/shell_device_client.h"
#include "extensions/shell/browser/shell_extension_system.h" #include "extensions/shell/browser/shell_extension_system.h"
#include "extensions/shell/browser/shell_extension_system_factory.h" #include "extensions/shell/browser/shell_extension_system_factory.h"
#include "extensions/shell/browser/shell_extensions_browser_client.h" #include "extensions/shell/browser/shell_extensions_browser_client.h"
...@@ -105,6 +106,8 @@ void ShellBrowserMainParts::PreMainMessageLoopRun() { ...@@ -105,6 +106,8 @@ void ShellBrowserMainParts::PreMainMessageLoopRun() {
// TODO(jamescook): Initialize user_manager::UserManager. // TODO(jamescook): Initialize user_manager::UserManager.
net_log_.reset(new content::ShellNetLog("app_shell")); net_log_.reset(new content::ShellNetLog("app_shell"));
device_client_.reset(new ShellDeviceClient);
extensions_client_.reset(new ShellExtensionsClient()); extensions_client_.reset(new ShellExtensionsClient());
ExtensionsClient::Set(extensions_client_.get()); ExtensionsClient::Set(extensions_client_.get());
......
...@@ -30,6 +30,7 @@ namespace extensions { ...@@ -30,6 +30,7 @@ namespace extensions {
class DesktopController; class DesktopController;
class ShellBrowserContext; class ShellBrowserContext;
class ShellBrowserMainDelegate; class ShellBrowserMainDelegate;
class ShellDeviceClient;
class ShellExtensionsBrowserClient; class ShellExtensionsBrowserClient;
class ShellExtensionsClient; class ShellExtensionsClient;
class ShellExtensionSystem; class ShellExtensionSystem;
...@@ -69,6 +70,7 @@ class ShellBrowserMainParts : public content::BrowserMainParts { ...@@ -69,6 +70,7 @@ class ShellBrowserMainParts : public content::BrowserMainParts {
#endif #endif
scoped_ptr<DesktopController> desktop_controller_; scoped_ptr<DesktopController> desktop_controller_;
scoped_ptr<ShellBrowserContext> browser_context_; scoped_ptr<ShellBrowserContext> browser_context_;
scoped_ptr<ShellDeviceClient> device_client_;
scoped_ptr<ShellExtensionsClient> extensions_client_; scoped_ptr<ShellExtensionsClient> extensions_client_;
scoped_ptr<ShellExtensionsBrowserClient> extensions_browser_client_; scoped_ptr<ShellExtensionsBrowserClient> extensions_browser_client_;
scoped_ptr<net::NetLog> net_log_; scoped_ptr<net::NetLog> net_log_;
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/shell/browser/shell_device_client.h"
#include "base/logging.h"
#include "components/usb_service/usb_service.h"
#include "content/public/browser/browser_thread.h"
namespace extensions {
ShellDeviceClient::ShellDeviceClient() {}
ShellDeviceClient::~ShellDeviceClient() {}
usb_service::UsbService* ShellDeviceClient::GetUsbService() {
return usb_service::UsbService::GetInstance(
content::BrowserThread::GetMessageLoopProxyForThread(
content::BrowserThread::UI));
}
}
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef EXTENSIONS_SHELL_BROWSER_SHELL_DEVICE_CLIENT_H_
#define EXTENSIONS_SHELL_BROWSER_SHELL_DEVICE_CLIENT_H_
#include "device/core/device_client.h"
#include "base/compiler_specific.h"
#include "base/macros.h"
namespace extensions {
// Implementation of device::DeviceClient that returns //device API service
// singletons appropriate for use within app shell.
class ShellDeviceClient : device::DeviceClient {
public:
ShellDeviceClient();
virtual ~ShellDeviceClient();
// device::DeviceClient implementation
virtual usb_service::UsbService* GetUsbService() OVERRIDE;
private:
DISALLOW_COPY_AND_ASSIGN(ShellDeviceClient);
};
}
#endif // EXTENSIONS_SHELL_BROWSER_SHELL_DEVICE_CLIENT_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment