Refactor update_client to use base::OnceCallback.

For correctness, change the type of callbacks which are invoked one time 
only from base::Callback to base::OnceCallback. Same for closures.

This is a mechanical change.
BUG=780524

Cq-Include-Trybots: master.tryserver.blink:linux_trusty_blink_rel
Change-Id: I29c9b2bab9e83695f0ac041a95a23545ef852a81
Reviewed-on: https://chromium-review.googlesource.com/741037
Reviewed-by: Devlin <[email protected]>
Reviewed-by: Julian Pastarmov <[email protected]>
Reviewed-by: Joshua Pawlicki <[email protected]>
Commit-Queue: Sorin Jianu <[email protected]>
Cr-Commit-Position: refs/heads/master@{#513520}
diff --git a/components/component_updater/component_updater_service_unittest.cc b/components/component_updater/component_updater_service_unittest.cc
index fafb1bb..4dfbc93 100644
--- a/components/component_updater/component_updater_service_unittest.cc
+++ b/components/component_updater/component_updater_service_unittest.cc
@@ -7,6 +7,7 @@
 #include <limits>
 #include <memory>
 #include <string>
+#include <utility>
 #include <vector>
 
 #include "base/bind.h"
@@ -40,6 +41,7 @@
 using ::testing::Invoke;
 using ::testing::Mock;
 using ::testing::Return;
+using ::testing::Unused;
 
 namespace component_updater {
 
@@ -47,10 +49,17 @@
  public:
   MockInstaller();
 
+  // gMock does not support mocking functions with parameters which have
+  // move semantics. This function is a shim to work around it.
+  void Install(const base::FilePath& unpack_path,
+               const std::string& public_key,
+               update_client::CrxInstaller::Callback callback) {
+    DoInstall(unpack_path, callback);
+  }
+
   MOCK_METHOD1(OnUpdateError, void(int error));
-  MOCK_METHOD3(Install,
+  MOCK_METHOD2(DoInstall,
                void(const base::FilePath& unpack_path,
-                    const std::string& public_key,
                     const update_client::CrxInstaller::Callback& callback));
   MOCK_METHOD2(GetInstalledFile,
                bool(const std::string& file, base::FilePath* installed_file));
@@ -64,17 +73,19 @@
  public:
   MockUpdateClient();
 
+  // gMock does not support mocking functions with parameters which have
+  // move semantics. This function is a shim to work around it.
   void Install(const std::string& id,
-               const CrxDataCallback& crx_data_callback,
+               CrxDataCallback crx_data_callback,
                Callback callback) override {
-    DoInstall(id, crx_data_callback);
+    DoInstall(id, std::move(crx_data_callback));
     std::move(callback).Run(update_client::Error::NONE);
   }
 
   void Update(const std::vector<std::string>& ids,
-              const CrxDataCallback& crx_data_callback,
+              CrxDataCallback crx_data_callback,
               Callback callback) override {
-    DoUpdate(ids, crx_data_callback);
+    DoUpdate(ids, std::move(crx_data_callback));
     std::move(callback).Run(update_client::Error::NONE);
   }
 
@@ -130,7 +141,7 @@
   MockUpdateClient& update_client() { return *update_client_; }
   ComponentUpdateService& component_updater() { return *component_updater_; }
   scoped_refptr<TestConfigurator> configurator() const { return config_; }
-  base::Closure quit_closure() const { return quit_closure_; }
+  base::OnceClosure quit_closure() { return runloop_.QuitClosure(); }
   static void ReadyCallback() {}
 
  protected:
@@ -139,7 +150,6 @@
  private:
   base::test::ScopedTaskEnvironment scoped_task_environment_;
   base::RunLoop runloop_;
-  const base::Closure quit_closure_ = runloop_.QuitClosure();
 
   scoped_refptr<TestConfigurator> config_ =
       base::MakeRefCounted<TestConfigurator>();
@@ -182,8 +192,8 @@
 void OnDemandTester::OnDemand(ComponentUpdateService* cus,
                               const std::string& id) {
   cus->GetOnDemandUpdater().OnDemandUpdate(
-      id,
-      base::Bind(&OnDemandTester::OnDemandComplete, base::Unretained(this)));
+      id, base::BindOnce(&OnDemandTester::OnDemandComplete,
+                         base::Unretained(this)));
 }
 
 void OnDemandTester::OnDemandComplete(update_client::Error error) {
@@ -193,7 +203,8 @@
 std::unique_ptr<ComponentUpdateService> TestComponentUpdateServiceFactory(
     const scoped_refptr<Configurator>& config) {
   DCHECK(config);
-  return base::MakeUnique<CrxUpdateService>(config, new MockUpdateClient());
+  return base::MakeUnique<CrxUpdateService>(
+      config, base::MakeRefCounted<MockUpdateClient>());
 }
 
 ComponentUpdaterTest::ComponentUpdaterTest() {
@@ -237,25 +248,25 @@
 TEST_F(ComponentUpdaterTest, RegisterComponent) {
   class LoopHandler {
    public:
-    LoopHandler(int max_cnt, const base::Closure& quit_closure)
-        : max_cnt_(max_cnt), quit_closure_(quit_closure) {}
+    LoopHandler(int max_cnt, base::OnceClosure quit_closure)
+        : max_cnt_(max_cnt), quit_closure_(std::move(quit_closure)) {}
 
-    void OnUpdate(const std::vector<std::string>& ids,
-                  const UpdateClient::CrxDataCallback& crx_data_callback) {
+    void OnUpdate(const std::vector<std::string>& ids, Unused) {
       static int cnt = 0;
       ++cnt;
       if (cnt >= max_cnt_)
-        quit_closure_.Run();
+        std::move(quit_closure_).Run();
     }
 
    private:
     const int max_cnt_;
-    base::Closure quit_closure_;
+    base::OnceClosure quit_closure_;
   };
 
   base::HistogramTester ht;
 
-  scoped_refptr<MockInstaller> installer(new MockInstaller());
+  scoped_refptr<MockInstaller> installer =
+      base::MakeRefCounted<MockInstaller>();
   EXPECT_CALL(*installer, Uninstall()).WillOnce(Return(true));
 
   using update_client::jebg_hash;
@@ -302,8 +313,7 @@
    public:
     explicit LoopHandler(int max_cnt) : max_cnt_(max_cnt) {}
 
-    void OnInstall(const std::string& ids,
-                   const UpdateClient::CrxDataCallback& crx_data_callback) {
+    void OnInstall(const std::string& ids, Unused) {
       static int cnt = 0;
       ++cnt;
       if (cnt >= max_cnt_) {
@@ -339,7 +349,7 @@
   CrxComponent crx_component;
   crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash));
   crx_component.version = base::Version("0.9");
-  crx_component.installer = new MockInstaller();
+  crx_component.installer = base::MakeRefCounted<MockInstaller>();
 
   LoopHandler loop_handler(1);
   EXPECT_CALL(update_client(), DoInstall("jebgalgnebhfojomionfpkfelancnnkf", _))
@@ -365,20 +375,19 @@
 TEST_F(ComponentUpdaterTest, MaybeThrottle) {
   class LoopHandler {
    public:
-    LoopHandler(int max_cnt, const base::Closure& quit_closure)
-        : max_cnt_(max_cnt), quit_closure_(quit_closure) {}
+    LoopHandler(int max_cnt, base::OnceClosure quit_closure)
+        : max_cnt_(max_cnt), quit_closure_(std::move(quit_closure)) {}
 
-    void OnInstall(const std::string& ids,
-                   const UpdateClient::CrxDataCallback& crx_data_callback) {
+    void OnInstall(const std::string& ids, Unused) {
       static int cnt = 0;
       ++cnt;
       if (cnt >= max_cnt_)
-        quit_closure_.Run();
+        std::move(quit_closure_).Run();
     }
 
    private:
     const int max_cnt_;
-    base::Closure quit_closure_;
+    base::OnceClosure quit_closure_;
   };
 
   base::HistogramTester ht;
@@ -386,7 +395,8 @@
   auto config = configurator();
   config->SetInitialDelay(3600);
 
-  scoped_refptr<MockInstaller> installer(new MockInstaller());
+  scoped_refptr<MockInstaller> installer =
+      base::MakeRefCounted<MockInstaller>();
 
   using update_client::jebg_hash;
   CrxComponent crx_component;
@@ -402,7 +412,7 @@
   EXPECT_TRUE(component_updater().RegisterComponent(crx_component));
   component_updater().MaybeThrottle(
       "jebgalgnebhfojomionfpkfelancnnkf",
-      base::Bind(&ComponentUpdaterTest::ReadyCallback));
+      base::BindOnce(&ComponentUpdaterTest::ReadyCallback));
 
   RunThreads();