blob: e07c0fb38d60861a2e8795d0c16b9186092ce445 [file] [log] [blame]
// Copyright 2018 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 <string>
#include <vector>
#include "base/callback.h"
#include "base/macros.h"
#include "chrome/common/available_offline_content.mojom.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
// Notice: this file is only included on OS_ANDROID.
// Wraps calls from the renderer thread to the AvailableOfflineContentProvider,
// and records related UMA.
class AvailableOfflineContentHelper {
using AvailableContentCallback =
base::OnceCallback<void(bool list_visible_by_prefs,
const std::string& offline_content_json)>;
// Fetch available offline content and return a JSON representation.
// Calls callback once with the return value. An empty string
// is returned if no offline content is available.
// Note: A call to Reset, or deletion of this object will prevent the callback
// from running.
void FetchAvailableContent(AvailableContentCallback callback);
// These methods just forward to the AvailableOfflineContentProvider.
void LaunchItem(const std::string& id, const std::string& name_space);
void LaunchDownloadsPage();
void ListVisibilityChanged(bool is_visible);
// Abort previous requests and free the mojo connection.
void Reset();
using Binder = base::RepeatingCallback<void(
static void OverrideBinderForTesting(Binder binder);
void AvailableContentReceived(
AvailableContentCallback callback,
bool list_visible_by_prefs,
std::vector<chrome::mojom::AvailableOfflineContentPtr> content);
// Binds |provider_| if necessary. Returns true if the provider is bound.
bool BindProvider();
mojo::Remote<chrome::mojom::AvailableOfflineContentProvider> provider_;
// This is the result of the last FetchAvailableContent call. It is retained
// only so that metrics can be recorded properly on call to LaunchItem().
std::vector<chrome::mojom::AvailableOfflineContentPtr> fetched_content_;
// Records if the last received content message indicated that prefetched
// articles are available or not.
bool has_prefetched_content_ = false;