Initialize GamepadService in BrowserThreadsStarted

The gamepad service needs a service manager connector in order
to connect to the device service. This CL adds a startup step in
BrowserThreadsStarted to provide the service manager connector.
This connector should only be used from the polling thread.

BUG=801717

Change-Id: Ia56479fb9e300f0b52b682bd7a905e9271efa7a9
Reviewed-on: https://chromium-review.googlesource.com/c/1362102
Reviewed-by: Ovidio Henriquez <odejesush@chromium.org>
Reviewed-by: Antoine Labour <piman@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Commit-Queue: Matt Reynolds <mattreynolds@chromium.org>
Cr-Commit-Position: refs/heads/master@{#614472}
diff --git a/content/browser/browser_main_loop.cc b/content/browser/browser_main_loop.cc
index bce899c..154b8398 100644
--- a/content/browser/browser_main_loop.cc
+++ b/content/browser/browser_main_loop.cc
@@ -1318,6 +1318,14 @@
     midi_service_.reset(new midi::MidiService);
   }
 
+  {
+    TRACE_EVENT0("startup", "BrowserThreadsStarted::Subsystem:GamepadService");
+    device::GamepadService::GetInstance()->StartUp(
+        content::ServiceManagerConnection::GetForProcess()
+            ->GetConnector()
+            ->Clone());
+  }
+
 #if defined(OS_WIN)
   if (base::FeatureList::IsEnabled(features::kHighDynamicRange))
     HDRProxy::Initialize();
diff --git a/device/gamepad/BUILD.gn b/device/gamepad/BUILD.gn
index 265dbbf..d390612 100644
--- a/device/gamepad/BUILD.gn
+++ b/device/gamepad/BUILD.gn
@@ -106,6 +106,10 @@
     "//third_party/blink/public:blink_headers",
   ]
 
+  public_deps = [
+    "//services/device/public/mojom",
+  ]
+
   defines = [ "DEVICE_GAMEPAD_IMPLEMENTATION" ]
 
   if (is_win) {
diff --git a/device/gamepad/gamepad_service.cc b/device/gamepad/gamepad_service.cc
index bb9995a..8282f74dd 100644
--- a/device/gamepad/gamepad_service.cc
+++ b/device/gamepad/gamepad_service.cc
@@ -15,6 +15,7 @@
 #include "device/gamepad/gamepad_consumer.h"
 #include "device/gamepad/gamepad_data_fetcher.h"
 #include "device/gamepad/gamepad_provider.h"
+#include "services/service_manager/public/cpp/connector.h"
 
 namespace device {
 
@@ -56,6 +57,15 @@
   return g_gamepad_service;
 }
 
+void GamepadService::StartUp(
+    std::unique_ptr<service_manager::Connector> service_manager_connector) {
+  service_manager_connector_ = std::move(service_manager_connector);
+}
+
+service_manager::Connector* GamepadService::GetConnector() {
+  return service_manager_connector_.get();
+}
+
 void GamepadService::ConsumerBecameActive(device::GamepadConsumer* consumer) {
   DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
 
diff --git a/device/gamepad/gamepad_service.h b/device/gamepad/gamepad_service.h
index 12723ac4b..85feb220 100644
--- a/device/gamepad/gamepad_service.h
+++ b/device/gamepad/gamepad_service.h
@@ -26,6 +26,10 @@
 class GamepadServiceTestConstructor;
 }
 
+namespace service_manager {
+class Connector;
+}
+
 namespace device {
 class GamepadConsumer;
 class GamepadDataFetcher;
@@ -40,6 +44,11 @@
   // Returns the GamepadService singleton.
   static GamepadService* GetInstance();
 
+  void StartUp(
+      std::unique_ptr<service_manager::Connector> service_manager_connector);
+
+  service_manager::Connector* GetConnector();
+
   // Increments the number of users of the provider. The Provider is running
   // when there's > 0 users, and is paused when the count drops to 0.
   // consumer is registered to listen for gamepad connections. If this is the
@@ -150,6 +159,8 @@
 
   bool gesture_callback_pending_;
 
+  std::unique_ptr<service_manager::Connector> service_manager_connector_;
+
   DISALLOW_COPY_AND_ASSIGN(GamepadService);
 };
 
diff --git a/device/gamepad/public/mojom/BUILD.gn b/device/gamepad/public/mojom/BUILD.gn
index 1bd367a..3cd7845 100644
--- a/device/gamepad/public/mojom/BUILD.gn
+++ b/device/gamepad/public/mojom/BUILD.gn
@@ -11,6 +11,7 @@
 
   deps = [
     "//mojo/public/mojom/base",
+    "//services/service_manager/public/mojom",
   ]
 
   output_prefix = "gamepad_mojom"