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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
// Copyright 2015 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/offline_page_utils.h"
#include <algorithm>
#include <memory>
#include <utility>
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/net/net_error_tab_helper.h"
#include "chrome/browser/offline_pages/offline_page_mhtml_archiver.h"
#include "chrome/browser/offline_pages/offline_page_model_factory.h"
#include "chrome/browser/offline_pages/offline_page_origin_utils.h"
#include "chrome/browser/offline_pages/offline_page_tab_helper.h"
#include "chrome/browser/offline_pages/request_coordinator_factory.h"
#include "components/offline_pages/core/background/request_coordinator.h"
#include "components/offline_pages/core/background/save_page_request.h"
#include "components/offline_pages/core/client_namespace_constants.h"
#include "components/offline_pages/core/offline_clock.h"
#include "components/offline_pages/core/offline_page_client_policy.h"
#include "components/offline_pages/core/offline_page_feature.h"
#include "components/offline_pages/core/offline_page_item.h"
#include "components/offline_pages/core/offline_page_item_utils.h"
#include "components/offline_pages/core/offline_page_model.h"
#include "components/offline_pages/core/request_header/offline_page_header.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "net/base/mime_util.h"
#if BUILDFLAG(IS_ANDROID)
#include "chrome/browser/download/android/download_controller_base.h"
#endif // BUILDFLAG(IS_ANDROID)
namespace offline_pages {
namespace {
class OfflinePageComparer {
public:
OfflinePageComparer() = default;
bool operator()(const OfflinePageItem& a, const OfflinePageItem& b) {
return a.creation_time > b.creation_time;
}
};
bool IsSupportedByDownload(content::BrowserContext* browser_context,
const std::string& name_space) {
return GetPolicy(name_space).is_supported_by_download;
}
void CheckDuplicateOngoingDownloads(
content::BrowserContext* browser_context,
const GURL& url,
OfflinePageUtils::DuplicateCheckCallback callback) {
RequestCoordinator* request_coordinator =
RequestCoordinatorFactory::GetForBrowserContext(browser_context);
if (!request_coordinator)
return;
auto request_coordinator_continuation =
[](content::BrowserContext* browser_context, const GURL& url,
OfflinePageUtils::DuplicateCheckCallback callback,
std::vector<std::unique_ptr<SavePageRequest>> requests) {
base::Time latest_request_time;
for (auto& request : requests) {
if (IsSupportedByDownload(browser_context,
request->client_id().name_space) &&
request->url() == url &&
latest_request_time < request->creation_time()) {
latest_request_time = request->creation_time();
}
}
if (latest_request_time.is_null()) {
std::move(callback).Run(
OfflinePageUtils::DuplicateCheckResult::NOT_FOUND);
} else {
std::move(callback).Run(
OfflinePageUtils::DuplicateCheckResult::DUPLICATE_REQUEST_FOUND);
}
};
request_coordinator->GetAllRequests(
base::BindOnce(request_coordinator_continuation, browser_context, url,
std::move(callback)));
}
void DoCalculateSizeBetween(
offline_pages::SizeInBytesCallback callback,
const base::Time& begin_time,
const base::Time& end_time,
const offline_pages::MultipleOfflinePageItemResult& result) {
int64_t total_size = 0;
for (auto& page : result) {
if (begin_time <= page.creation_time && page.creation_time < end_time)
total_size += page.file_size;
}
std::move(callback).Run(total_size);
}
content::WebContents* GetWebContentsByFrameID(int render_process_id,
int render_frame_id) {
content::RenderFrameHost* render_frame_host =
content::RenderFrameHost::FromID(render_process_id, render_frame_id);
if (!render_frame_host)
return NULL;
return content::WebContents::FromRenderFrameHost(render_frame_host);
}
content::WebContents::Getter GetWebContentsGetter(
content::WebContents* web_contents) {
// The FrameTreeNode ID should be used to access the WebContents.
content::FrameTreeNodeId frame_tree_node_id =
web_contents->GetPrimaryMainFrame()->GetFrameTreeNodeId();
if (frame_tree_node_id) {
return base::BindRepeating(content::WebContents::FromFrameTreeNodeId,
frame_tree_node_id);
}
// In other cases, use the RenderProcessHost ID + RenderFrameHost ID to get
// the WebContents.
return base::BindRepeating(
&GetWebContentsByFrameID,
web_contents->GetPrimaryMainFrame()->GetProcess()->GetDeprecatedID(),
web_contents->GetPrimaryMainFrame()->GetRoutingID());
}
void AcquireFileAccessPermissionDoneForScheduleDownload(
const content::WebContents::Getter& wc_getter,
const std::string& name_space,
const GURL& url,
OfflinePageUtils::DownloadUIActionFlags ui_action,
const std::string& request_origin,
bool granted) {
if (!granted)
return;
content::WebContents* web_contents = wc_getter.Run();
if (!web_contents) {
return;
}
OfflinePageTabHelper* tab_helper =
OfflinePageTabHelper::FromWebContents(web_contents);
if (!tab_helper)
return;
tab_helper->ScheduleDownloadHelper(web_contents, name_space, url, ui_action,
request_origin);
}
} // namespace
// static
const base::FilePath::CharType OfflinePageUtils::kMHTMLExtension[] =
FILE_PATH_LITERAL("mhtml");
// static
void OfflinePageUtils::SelectPagesForURL(
SimpleFactoryKey* key,
const GURL& url,
int tab_id,
base::OnceCallback<void(const std::vector<OfflinePageItem>&)> callback) {
PageCriteria criteria;
criteria.url = url;
criteria.pages_for_tab_id = tab_id;
SelectPagesWithCriteria(key, criteria, std::move(callback));
}
// static
void OfflinePageUtils::SelectPagesWithCriteria(
SimpleFactoryKey* key,
const PageCriteria& criteria,
base::OnceCallback<void(const std::vector<OfflinePageItem>&)> callback) {
OfflinePageModel* offline_page_model =
OfflinePageModelFactory::GetForKey(key);
if (!offline_page_model) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback), std::vector<OfflinePageItem>()));
return;
}
offline_page_model->GetPagesWithCriteria(criteria, std::move(callback));
}
const OfflinePageItem* OfflinePageUtils::GetOfflinePageFromWebContents(
content::WebContents* web_contents) {
OfflinePageTabHelper* tab_helper =
OfflinePageTabHelper::FromWebContents(web_contents);
if (!tab_helper)
return nullptr;
const OfflinePageItem* offline_page = tab_helper->offline_page();
if (!offline_page)
return nullptr;
// If a pending navigation that hasn't committed yet, don't return the cached
// offline page that was set at the last commit time. This is to prevent
// from returning the wrong offline page if DidStartNavigation is never called
// to clear it up.
if (!EqualsIgnoringFragment(web_contents->GetVisibleURL(),
web_contents->GetLastCommittedURL())) {
return nullptr;
}
return offline_page;
}
// static
const OfflinePageHeader* OfflinePageUtils::GetOfflineHeaderFromWebContents(
content::WebContents* web_contents) {
OfflinePageTabHelper* tab_helper =
OfflinePageTabHelper::FromWebContents(web_contents);
return tab_helper ? &(tab_helper->offline_header()) : nullptr;
}
// static
bool OfflinePageUtils::IsShowingOfflinePreview(
content::WebContents* web_contents) {
OfflinePageTabHelper* tab_helper =
OfflinePageTabHelper::FromWebContents(web_contents);
return tab_helper && tab_helper->GetOfflinePreviewItem();
}
// static
bool OfflinePageUtils::IsShowingDownloadButtonInErrorPage(
content::WebContents* web_contents) {
chrome_browser_net::NetErrorTabHelper* tab_helper =
chrome_browser_net::NetErrorTabHelper::FromWebContents(web_contents);
return tab_helper && tab_helper->is_showing_download_button_in_error_page();
}
// static
GURL OfflinePageUtils::GetOriginalURLFromWebContents(
content::WebContents* web_contents) {
content::NavigationEntry* entry =
web_contents->GetController().GetLastCommittedEntry();
if (!entry || entry->GetRedirectChain().size() <= 1)
return GURL();
return entry->GetRedirectChain().front();
}
// static
void OfflinePageUtils::CheckDuplicateDownloads(
content::BrowserContext* browser_context,
const GURL& url,
DuplicateCheckCallback callback) {
// First check for finished downloads, that is, saved pages.
OfflinePageModel* offline_page_model =
OfflinePageModelFactory::GetForBrowserContext(browser_context);
if (!offline_page_model)
return;
auto continuation = [](content::BrowserContext* browser_context,
const GURL& url, DuplicateCheckCallback callback,
const std::vector<OfflinePageItem>& pages) {
base::Time latest_saved_time;
for (const auto& offline_page_item : pages) {
if (IsSupportedByDownload(browser_context,
offline_page_item.client_id.name_space) &&
latest_saved_time < offline_page_item.creation_time) {
latest_saved_time = offline_page_item.creation_time;
}
}
if (latest_saved_time.is_null()) {
// Then check for ongoing downloads, that is, requests.
CheckDuplicateOngoingDownloads(browser_context, url, std::move(callback));
} else {
std::move(callback).Run(DuplicateCheckResult::DUPLICATE_PAGE_FOUND);
}
};
PageCriteria criteria;
criteria.url = url;
offline_page_model->GetPagesWithCriteria(
criteria,
base::BindOnce(continuation, browser_context, url, std::move(callback)));
}
// static
void OfflinePageUtils::ScheduleDownload(content::WebContents* web_contents,
const std::string& name_space,
const GURL& url,
DownloadUIActionFlags ui_action,
const std::string& request_origin) {
if (!web_contents)
return;
// Ensure that the storage permission is granted since the archive file is
// going to be placed in the public directory.
AcquireFileAccessPermission(
web_contents,
base::BindOnce(&AcquireFileAccessPermissionDoneForScheduleDownload,
GetWebContentsGetter(web_contents), name_space, url,
ui_action, request_origin));
}
// static
void OfflinePageUtils::ScheduleDownload(content::WebContents* web_contents,
const std::string& name_space,
const GURL& url,
DownloadUIActionFlags ui_action) {
std::string origin =
OfflinePageOriginUtils::GetEncodedOriginAppFor(web_contents);
ScheduleDownload(web_contents, name_space, url, ui_action, origin);
}
// static
bool OfflinePageUtils::CanDownloadAsOfflinePage(
const GURL& url,
const std::string& contents_mime_type) {
return OfflinePageModel::CanSaveURL(url) &&
(net::MatchesMimeType(contents_mime_type, "text/html") ||
net::MatchesMimeType(contents_mime_type, "application/xhtml+xml"));
}
// static
bool OfflinePageUtils::GetCachedOfflinePageSizeBetween(
content::BrowserContext* browser_context,
SizeInBytesCallback callback,
const base::Time& begin_time,
const base::Time& end_time) {
OfflinePageModel* offline_page_model =
OfflinePageModelFactory::GetForBrowserContext(browser_context);
if (!offline_page_model || begin_time > end_time)
return false;
PageCriteria criteria;
criteria.lifetime_type = LifetimeType::TEMPORARY;
offline_page_model->GetPagesWithCriteria(
criteria, base::BindOnce(&DoCalculateSizeBetween, std::move(callback),
begin_time, end_time));
return true;
}
// static
std::string OfflinePageUtils::ExtractOfflineHeaderValueFromNavigationEntry(
content::NavigationEntry* entry) {
std::string extra_headers = entry->GetExtraHeaders();
if (extra_headers.empty())
return std::string();
// The offline header will be the only extra header if it is present.
std::string offline_header_key(offline_pages::kOfflinePageHeader);
offline_header_key += ": ";
if (!base::StartsWith(extra_headers, offline_header_key,
base::CompareCase::INSENSITIVE_ASCII)) {
return std::string();
}
std::string header_value = extra_headers.substr(offline_header_key.length());
if (header_value.find("\n") != std::string::npos)
return std::string();
return header_value;
}
// static
bool OfflinePageUtils::IsShowingTrustedOfflinePage(
content::WebContents* web_contents) {
OfflinePageTabHelper* tab_helper =
OfflinePageTabHelper::FromWebContents(web_contents);
return tab_helper && tab_helper->IsShowingTrustedOfflinePage();
}
// static
void OfflinePageUtils::AcquireFileAccessPermission(
content::WebContents* web_contents,
base::OnceCallback<void(bool)> callback) {
#if BUILDFLAG(IS_ANDROID)
content::WebContents::Getter web_contents_getter =
GetWebContentsGetter(web_contents);
DownloadControllerBase::Get()->AcquireFileAccessPermission(
web_contents_getter, std::move(callback));
#else
// Not needed in other platforms.
std::move(callback).Run(true /*granted*/);
#endif // BUILDFLAG(IS_ANDROID)
}
} // namespace offline_pages
|