blob: 8137fe81264c0d17fde1bb81366fdbe7af2df3b8 [file] [log] [blame]
// Copyright 2015 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 "ios/web/browser_url_rewriter_impl.h"
#include "base/check.h"
#include "base/no_destructor.h"
#include "base/strings/string_util.h"
#include "ios/web/public/browser_state.h"
#import "ios/web/public/web_client.h"
#include "url/gurl.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace web {
namespace {
// The scheme used to view the source of a page using WebUI in content/.
const char kViewSourceScheme[] = "view-source";
// Handles rewriting view-source URLs for what we'll actually load. Since
// WebUI-based view-source isn't supported on iOS, simply strip out the scheme
// and load the URL. This is to gracefully handle tabs synced from other
// platforms with the "view-source:" scheme.
static bool HandleViewSource(GURL* url, BrowserState* browser_state) {
DCHECK(url);
if (url->SchemeIs(kViewSourceScheme)) {
// Load the inner URL instead.
*url = GURL(url->GetContent());
return true;
}
return false;
}
} // namespace
// static
BrowserURLRewriter* BrowserURLRewriter::GetInstance() {
return BrowserURLRewriterImpl::GetInstance();
}
// static
bool BrowserURLRewriter::RewriteURLWithWriters(
GURL* url,
web::BrowserState* browser_state,
const std::vector<BrowserURLRewriter::URLRewriter>& rewriters) {
bool rewritten = false;
for (URLRewriter rewriter : rewriters) {
if ((rewritten = rewriter(url, browser_state)))
break;
}
return rewritten;
}
// static
BrowserURLRewriterImpl* BrowserURLRewriterImpl::GetInstance() {
static base::NoDestructor<BrowserURLRewriterImpl> instance;
return instance.get();
}
BrowserURLRewriterImpl::BrowserURLRewriterImpl() {
web::WebClient* web_client = web::GetWebClient();
if (web_client)
web_client->PostBrowserURLRewriterCreation(this);
// view-source:
AddURLRewriter(&HandleViewSource);
}
BrowserURLRewriterImpl::~BrowserURLRewriterImpl() {}
void BrowserURLRewriterImpl::AddURLRewriter(URLRewriter rewriter) {
DCHECK(rewriter);
url_rewriters_.push_back(rewriter);
}
bool BrowserURLRewriterImpl::RewriteURLIfNecessary(
GURL* url,
BrowserState* browser_state) {
return BrowserURLRewriter::RewriteURLWithWriters(url, browser_state,
url_rewriters_);
}
} // namespace web