blob: 2fb93096ff7032de93368a469c2fbfe6e99764ee [file] [log] [blame]
// Copyright 2021 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 ANDROID_WEBVIEW_NONEMBEDDED_COMPONENT_UPDATER_AW_COMPONENT_UPDATE_SERVICE_H_
#define ANDROID_WEBVIEW_NONEMBEDDED_COMPONENT_UPDATER_AW_COMPONENT_UPDATE_SERVICE_H_
#include <memory>
#include <string>
#include <vector>
#include "base/android/scoped_java_ref.h"
#include "base/callback_forward.h"
#include "base/containers/flat_map.h"
#include "base/gtest_prod_util.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/no_destructor.h"
#include "base/sequence_checker.h"
#include "components/update_client/update_client.h"
#include "components/update_client/update_client_errors.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
namespace android_webview {
using RegisterComponentsCallback =
base::RepeatingCallback<bool(const update_client::CrxComponent&)>;
class TestAwComponentUpdateService;
// Native-side implementation of the AwComponentUpdateService. It
// registers components and installs any updates for registered components.
class AwComponentUpdateService {
public:
static AwComponentUpdateService* GetInstance();
// Callback used for updating components, with an int that represents how many
// components were actually updated.
using UpdateCallback = base::OnceCallback<void(int)>;
void StartComponentUpdateService(UpdateCallback finished_callback);
bool RegisterComponent(const update_client::CrxComponent& component);
void CheckForUpdates(UpdateCallback on_finished);
void IncrementComponentsUpdatedCount();
private:
SEQUENCE_CHECKER(sequence_checker_);
friend base::NoDestructor<AwComponentUpdateService>;
friend TestAwComponentUpdateService;
FRIEND_TEST_ALL_PREFIXES(AwComponentUpdateServiceTest,
TestComponentReadyWhenOffline);
// Accept custom configurator for testing.
explicit AwComponentUpdateService(
scoped_refptr<update_client::Configurator> configurator);
AwComponentUpdateService();
// Virtual for testing.
virtual ~AwComponentUpdateService();
void OnUpdateComplete(update_client::Callback callback,
const base::TimeTicks& start_time,
update_client::Error error);
absl::optional<update_client::CrxComponent> GetComponent(
const std::string& id) const;
std::vector<absl::optional<update_client::CrxComponent>> GetCrxComponents(
const std::vector<std::string>& ids);
void ScheduleUpdatesOfRegisteredComponents(
UpdateCallback on_finished_updates);
// Virtual for testing.
virtual void RegisterComponents(RegisterComponentsCallback register_callback,
base::OnceClosure on_finished);
scoped_refptr<update_client::UpdateClient> update_client_;
// A collection of every registered component.
base::flat_map<std::string, update_client::CrxComponent> components_;
// Maintains the order in which components have been registered. The position
// of a component id in this sequence indicates the priority of the component.
// The sooner the component gets registered, the higher its priority, and
// the closer this component is to the beginning of the vector.
std::vector<std::string> components_order_;
void RecordComponentsUpdated(UpdateCallback on_finished,
update_client::Error error);
// Counts how many components were updated, for UMA logging.
int components_updated_count_ = 0;
base::WeakPtrFactory<AwComponentUpdateService> weak_ptr_factory_{this};
};
} // namespace android_webview
#endif // ANDROID_WEBVIEW_NONEMBEDDED_COMPONENT_UPDATER_AW_COMPONENT_UPDATE_SERVICE_H_