Commit 56fd6dd2 authored by Sorin Jianu's avatar Sorin Jianu Committed by Commit Bot

Refactor the code to update all apps out of the updater main.

This is mechanical work before registering the app ids with the
updater lands. There is some common registration code that is
common on both install and update execution path. It is helpful
to have the update apps code in its own compilation unit, just as
install is.

R=waffles


Bug: 1020285
Change-Id: I06590cf1d73a5d22ae79d288de564c0040442678
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1893406Reviewed-by: default avatarJoshua Pawlicki <waffles@chromium.org>
Commit-Queue: Sorin Jianu <sorin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#711399}
parent 1744bd04
......@@ -40,6 +40,8 @@ if (is_win || is_mac) {
"prefs.h",
"unzipper.cc",
"unzipper.h",
"update_apps.cc",
"update_apps.h",
"updater.cc",
"updater.h",
"util.cc",
......
// Copyright 2019 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/updater/update_apps.h"
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/bind.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/scoped_refptr.h"
#include "base/message_loop/message_pump_type.h"
#include "base/optional.h"
#include "base/run_loop.h"
#include "base/task/post_task.h"
#include "base/task/single_thread_task_executor.h"
#include "base/threading/thread_restrictions.h"
#include "base/threading/thread_task_runner_handle.h"
#include "chrome/updater/configurator.h"
#include "chrome/updater/installer.h"
#include "chrome/updater/updater_constants.h"
#include "components/prefs/pref_service.h"
#include "components/update_client/crx_update_item.h"
#include "components/update_client/update_client.h"
namespace updater {
namespace {
class Observer : public update_client::UpdateClient::Observer {
public:
explicit Observer(scoped_refptr<update_client::UpdateClient> update_client)
: update_client_(update_client) {}
// Overrides for update_client::UpdateClient::Observer.
void OnEvent(Events event, const std::string& id) override {
update_client_->GetCrxUpdateState(id, &crx_update_item_);
}
const update_client::CrxUpdateItem& crx_update_item() const {
return crx_update_item_;
}
private:
scoped_refptr<update_client::UpdateClient> update_client_;
update_client::CrxUpdateItem crx_update_item_;
DISALLOW_COPY_AND_ASSIGN(Observer);
};
} // namespace
int UpdateApps() {
auto installer = base::MakeRefCounted<Installer>(kUpdaterAppId);
installer->FindInstallOfApp();
const auto component = installer->MakeCrxComponent();
base::SingleThreadTaskExecutor main_task_executor(base::MessagePumpType::UI);
base::RunLoop runloop;
DCHECK(base::ThreadTaskRunnerHandle::IsSet());
auto config = base::MakeRefCounted<Configurator>();
{
base::ScopedDisallowBlocking no_blocking_allowed;
auto update_client = update_client::UpdateClientFactory(config);
Observer observer(update_client);
update_client->AddObserver(&observer);
const std::vector<std::string> ids = {installer->app_id()};
update_client->Update(
ids,
base::BindOnce(
[](const update_client::CrxComponent& component,
const std::vector<std::string>& ids)
-> std::vector<base::Optional<update_client::CrxComponent>> {
DCHECK_EQ(1u, ids.size());
return {component};
},
component),
false,
base::BindOnce(
[](base::OnceClosure closure, update_client::Error error) {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(
[](base::OnceClosure quit_closure) {
std::move(quit_closure).Run();
},
std::move(closure)));
},
runloop.QuitWhenIdleClosure()));
runloop.Run();
const auto& update_item = observer.crx_update_item();
switch (update_item.state) {
case update_client::ComponentState::kUpdated:
VLOG(1) << "Update success.";
break;
case update_client::ComponentState::kUpToDate:
VLOG(1) << "No updates.";
break;
case update_client::ComponentState::kUpdateError:
VLOG(1) << "Updater error: " << update_item.error_code << ".";
break;
default:
NOTREACHED();
break;
}
update_client->RemoveObserver(&observer);
update_client = nullptr;
}
{
base::RunLoop runloop;
config->GetPrefService()->CommitPendingWrite(base::BindOnce(
[](base::OnceClosure quit_closure) { std::move(quit_closure).Run(); },
runloop.QuitWhenIdleClosure()));
runloop.Run();
}
return 0;
}
} // namespace updater
// Copyright 2019 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_UPDATER_UPDATE_APPS_H_
#define CHROME_UPDATER_UPDATE_APPS_H_
namespace updater {
// Updates all registered applications.
int UpdateApps();
} // namespace updater
#endif // CHROME_UPDATER_UPDATE_APPS_H_
......@@ -4,55 +4,29 @@
#include "chrome/updater/updater.h"
#include <stdint.h>
#include <iterator>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/at_exit.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/scoped_refptr.h"
#include "base/message_loop/message_pump_type.h"
#include "base/optional.h"
#include "base/run_loop.h"
#include "base/stl_util.h"
#include "base/task/post_task.h"
#include "base/task/single_thread_task_executor.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/task_runner.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread_restrictions.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/updater/configurator.h"
#include "chrome/updater/crash_client.h"
#include "chrome/updater/crash_reporter.h"
#include "chrome/updater/installer.h"
#include "chrome/updater/update_apps.h"
#include "chrome/updater/updater_constants.h"
#include "chrome/updater/updater_version.h"
#include "chrome/updater/util.h"
#include "components/crash/core/common/crash_key.h"
#include "components/prefs/pref_service.h"
#include "components/update_client/crx_update_item.h"
#include "components/update_client/update_client.h"
#if defined(OS_WIN)
#include "chrome/updater/win/install_app.h"
#include "chrome/updater/win/setup/uninstall.h"
#endif
// To install the updater, run:
// To install the updater on Windows, run:
// "updater.exe --install --enable-logging --v=1 --vmodule=*/chrome/updater/*"
// from the build directory. The program needs a number of dependencies which
// are available in the build out directory.
// are available in the |out| directory of the build.
// To uninstall, run "updater.exe --uninstall" from its install directory or
// from the build out directory. Doing this will make the program delete its
// install directory using a shim cmd script.
......@@ -68,26 +42,6 @@ void ThreadPoolStop() {
base::ThreadPoolInstance::Get()->Shutdown();
}
class Observer : public update_client::UpdateClient::Observer {
public:
explicit Observer(scoped_refptr<update_client::UpdateClient> update_client)
: update_client_(update_client) {}
// Overrides for update_client::UpdateClient::Observer.
void OnEvent(Events event, const std::string& id) override {
update_client_->GetCrxUpdateState(id, &crx_update_item_);
}
const update_client::CrxUpdateItem& crx_update_item() const {
return crx_update_item_;
}
private:
scoped_refptr<update_client::UpdateClient> update_client_;
update_client::CrxUpdateItem crx_update_item_;
DISALLOW_COPY_AND_ASSIGN(Observer);
};
// The log file is created in DIR_LOCAL_APP_DATA or DIR_APP_DATA.
void InitLogging(const base::CommandLine& command_line) {
logging::LoggingSettings settings;
......@@ -125,6 +79,10 @@ void TerminateUpdaterMain() {
ThreadPoolStop();
}
int UpdaterUpdateApps() {
return UpdateApps();
}
int UpdaterInstallApp() {
#if defined(OS_WIN)
// TODO(sorin): pick up the app id from the tag. https://crbug.com/1014298
......@@ -144,79 +102,6 @@ int UpdaterUninstall() {
#endif
}
int UpdaterUpdateApps() {
auto installer = base::MakeRefCounted<Installer>(kUpdaterAppId);
installer->FindInstallOfApp();
const auto component = installer->MakeCrxComponent();
base::SingleThreadTaskExecutor main_task_executor(base::MessagePumpType::UI);
base::RunLoop runloop;
DCHECK(base::ThreadTaskRunnerHandle::IsSet());
auto config = base::MakeRefCounted<Configurator>();
{
base::ScopedDisallowBlocking no_blocking_allowed;
auto update_client = update_client::UpdateClientFactory(config);
Observer observer(update_client);
update_client->AddObserver(&observer);
const std::vector<std::string> ids = {installer->app_id()};
update_client->Update(
ids,
base::BindOnce(
[](const update_client::CrxComponent& component,
const std::vector<std::string>& ids)
-> std::vector<base::Optional<update_client::CrxComponent>> {
DCHECK_EQ(1u, ids.size());
return {component};
},
component),
false,
base::BindOnce(
[](base::OnceClosure closure, update_client::Error error) {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(
[](base::OnceClosure quit_closure) {
std::move(quit_closure).Run();
},
std::move(closure)));
},
runloop.QuitWhenIdleClosure()));
runloop.Run();
const auto& update_item = observer.crx_update_item();
switch (update_item.state) {
case update_client::ComponentState::kUpdated:
VLOG(1) << "Update success.";
break;
case update_client::ComponentState::kUpToDate:
VLOG(1) << "No updates.";
break;
case update_client::ComponentState::kUpdateError:
VLOG(1) << "Updater error: " << update_item.error_code << ".";
break;
default:
NOTREACHED();
break;
}
update_client->RemoveObserver(&observer);
update_client = nullptr;
}
{
base::RunLoop runloop;
config->GetPrefService()->CommitPendingWrite(base::BindOnce(
[](base::OnceClosure quit_closure) { std::move(quit_closure).Run(); },
runloop.QuitWhenIdleClosure()));
runloop.Run();
}
return 0;
}
} // namespace
int HandleUpdaterCommands(const base::CommandLine* command_line) {
......
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