1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/offline_pages/android/offline_page_archive_publisher_impl.h"
#include <errno.h>
#include <utility>
#include "base/android/build_info.h"
#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "chrome/browser/offline_pages/android/offline_page_bridge.h"
#include "components/offline_pages/core/archive_manager.h"
#include "components/offline_pages/core/model/offline_page_model_utils.h"
#include "components/offline_pages/core/offline_page_archive_publisher.h"
#include "components/offline_pages/core/offline_store_utils.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "chrome/android/chrome_jni_headers/OfflinePageArchivePublisherBridge_jni.h"
namespace offline_pages {
namespace {
using base::android::ScopedJavaLocalRef;
using offline_pages::SavePageResult;
// Creates a singleton Delegate.
OfflinePageArchivePublisherImpl::Delegate* GetDefaultDelegate() {
static OfflinePageArchivePublisherImpl::Delegate delegate;
return &delegate;
}
bool ShouldUseDownloadsCollection() {
return base::android::BuildInfo::GetInstance()->sdk_int() >=
base::android::SDK_VERSION_Q;
}
// Helper function to do the move and register synchronously. Make sure this is
// called from a background thread.
PublishArchiveResult MoveAndRegisterArchive(
const offline_pages::OfflinePageItem& offline_page,
const base::FilePath& publish_directory,
OfflinePageArchivePublisherImpl::Delegate* delegate) {
// For Android Q+, use the downloads collection rather than DownloadManager.
if (ShouldUseDownloadsCollection()) {
return delegate->AddCompletedDownload(offline_page);
}
OfflinePageItem published_page(offline_page);
// Calculate the new file name.
published_page.file_path =
offline_pages::model_utils::GenerateUniqueFilenameForOfflinePage(
offline_page.title, offline_page.url, publish_directory);
// Create the destination directory if it does not already exist.
if (!publish_directory.empty() && !base::DirectoryExists(publish_directory)) {
base::File::Error file_error;
base::CreateDirectoryAndGetError(publish_directory, &file_error);
}
// Move the file.
bool moved = base::Move(offline_page.file_path, published_page.file_path);
if (!moved) {
DVPLOG(0) << "OfflinePage publishing file move failure " << __func__;
if (!base::PathExists(offline_page.file_path)) {
DVLOG(0) << "Can't copy from non-existent path, from "
<< offline_page.file_path << " " << __func__;
}
if (!base::PathExists(publish_directory)) {
DVLOG(0) << "Target directory does not exist, " << publish_directory
<< " " << __func__;
}
return PublishArchiveResult::Failure(SavePageResult::FILE_MOVE_FAILED);
}
// Tell the download manager about our file, get back an id.
if (!delegate->IsDownloadManagerInstalled()) {
return PublishArchiveResult::Failure(
SavePageResult::ADD_TO_DOWNLOAD_MANAGER_FAILED);
}
return delegate->AddCompletedDownload(published_page);
}
} // namespace
// static
PublishArchiveResult PublishArchiveResult::Failure(
SavePageResult save_page_result) {
return {save_page_result, PublishedArchiveId()};
}
OfflinePageArchivePublisherImpl::OfflinePageArchivePublisherImpl(
ArchiveManager* archive_manager)
: archive_manager_(archive_manager), delegate_(GetDefaultDelegate()) {}
OfflinePageArchivePublisherImpl::~OfflinePageArchivePublisherImpl() = default;
void OfflinePageArchivePublisherImpl::SetDelegateForTesting(
OfflinePageArchivePublisherImpl::Delegate* delegate) {
delegate_ = delegate;
}
void OfflinePageArchivePublisherImpl::PublishArchive(
const OfflinePageItem& offline_page,
const scoped_refptr<base::SequencedTaskRunner>& background_task_runner,
PublishArchiveDoneCallback publish_done_callback) const {
background_task_runner->PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&MoveAndRegisterArchive, offline_page,
archive_manager_->GetPublicArchivesDir(), delegate_),
base::BindOnce(std::move(publish_done_callback), offline_page));
}
void OfflinePageArchivePublisherImpl::UnpublishArchives(
const std::vector<PublishedArchiveId>& publish_ids) const {
std::vector<int64_t> download_manager_ids;
for (auto& id : publish_ids) {
if (id.download_id == kArchivePublishedWithoutDownloadId) {
DCHECK(id.new_file_path.IsContentUri());
base::DeleteFile(id.new_file_path);
} else if (id.download_id != kArchiveNotPublished) {
download_manager_ids.push_back(id.download_id);
}
}
delegate_->Remove(download_manager_ids);
}
// Delegate implementation using Android download manager.
bool OfflinePageArchivePublisherImpl::Delegate::IsDownloadManagerInstalled() {
JNIEnv* env = base::android::AttachCurrentThread();
jboolean is_installed =
Java_OfflinePageArchivePublisherBridge_isAndroidDownloadManagerInstalled(
env);
return is_installed;
}
PublishArchiveResult
OfflinePageArchivePublisherImpl::Delegate::AddCompletedDownload(
const OfflinePageItem& page) {
JNIEnv* env = base::android::AttachCurrentThread();
if (ShouldUseDownloadsCollection()) {
base::FilePath new_file_path = base::FilePath(
Java_OfflinePageArchivePublisherBridge_publishArchiveToDownloadsCollection(
env,
android::OfflinePageBridge::ConvertToJavaOfflinePage(env, page)));
if (new_file_path.empty())
return PublishArchiveResult::Failure(SavePageResult::FILE_MOVE_FAILED);
return {SavePageResult::SUCCESS,
{kArchivePublishedWithoutDownloadId, new_file_path}};
}
// TODO(petewil): Handle empty page title.
std::string page_title = base::UTF16ToUTF8(page.title);
// We use the title for a description, since the add to the download manager
// fails without a description, and we don't have anything better to use.
std::string description = page_title;
std::string path =
offline_pages::store_utils::ToDatabaseFilePath(page.file_path);
std::string uri = page.url.spec();
int64_t download_id =
Java_OfflinePageArchivePublisherBridge_addCompletedDownload(
env, page_title, description, path, page.file_size, uri,
std::string());
DCHECK_NE(download_id, kArchivePublishedWithoutDownloadId);
if (download_id == kArchiveNotPublished)
return PublishArchiveResult::Failure(
SavePageResult::ADD_TO_DOWNLOAD_MANAGER_FAILED);
return {SavePageResult::SUCCESS, {download_id, page.file_path}};
}
int OfflinePageArchivePublisherImpl::Delegate::Remove(
const std::vector<int64_t>& android_download_manager_ids) {
JNIEnv* env = base::android::AttachCurrentThread();
// Build a JNI array with our ID data.
ScopedJavaLocalRef<jlongArray> j_ids =
base::android::ToJavaLongArray(env, android_download_manager_ids);
return Java_OfflinePageArchivePublisherBridge_remove(env, j_ids);
}
base::WeakPtr<OfflinePageArchivePublisher>
OfflinePageArchivePublisherImpl::GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
} // namespace offline_pages
|