1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ios/web/browser_url_rewriter_impl.h"
7 #include "base/logging.h"
8 #include "base/strings/string_util.h"
9 #include "ios/web/public/browser_state.h"
10 #include "ios/web/public/web_client.h"
17 // The scheme used to view the source of a page using WebUI in content/.
18 const char kViewSourceScheme
[] = "view-source";
20 // Handles rewriting view-source URLs for what we'll actually load. Since
21 // WebUI-based view-source isn't supported on iOS, simply strip out the scheme
22 // and load the URL. This is to gracefully handle tabs synced from other
23 // platforms with the "view-source:" scheme.
24 static bool HandleViewSource(GURL
* url
, BrowserState
* browser_state
) {
26 if (url
->SchemeIs(kViewSourceScheme
)) {
27 // Load the inner URL instead.
28 *url
= GURL(url
->GetContent());
36 BrowserURLRewriter
* BrowserURLRewriter::GetInstance() {
37 return BrowserURLRewriterImpl::GetInstance();
41 bool BrowserURLRewriter::RewriteURLWithWriters(
43 web::BrowserState
* browser_state
,
44 const std::vector
<BrowserURLRewriter::URLRewriter
>& rewriters
) {
45 bool rewritten
= false;
46 for (URLRewriter rewriter
: rewriters
) {
47 if ((rewritten
= rewriter(url
, browser_state
)))
54 BrowserURLRewriterImpl
* BrowserURLRewriterImpl::GetInstance() {
55 return base::Singleton
<BrowserURLRewriterImpl
>::get();
58 BrowserURLRewriterImpl::BrowserURLRewriterImpl() {
59 web::WebClient
* web_client
= web::GetWebClient();
61 web_client
->PostBrowserURLRewriterCreation(this);
64 AddURLRewriter(&HandleViewSource
);
67 BrowserURLRewriterImpl::~BrowserURLRewriterImpl() {
70 void BrowserURLRewriterImpl::AddURLRewriter(URLRewriter rewriter
) {
72 url_rewriters_
.push_back(rewriter
);
75 bool BrowserURLRewriterImpl::RewriteURLIfNecessary(
77 BrowserState
* browser_state
) {
78 return BrowserURLRewriter::RewriteURLWithWriters(url
, browser_state
,