blob: a5e1b67d70193c139c9d86f4a0382dc459fd5130 [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/logging.h"
#include "base/strings/string_util.h"
#include "ios/web/public/browser_state.h"
#include "ios/web/public/web_client.h"
#include "url/gurl.h"
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() {
return Singleton<BrowserURLRewriterImpl>::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