Roll src/third_party/WebKit 9301d6f:4619053 (svn 201058:201059)
[chromium-blink-merge.git] / ios / web / browser_url_rewriter_impl.cc
bloba5e1b67d70193c139c9d86f4a0382dc459fd5130
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"
11 #include "url/gurl.h"
13 namespace web {
15 namespace {
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) {
25 DCHECK(url);
26 if (url->SchemeIs(kViewSourceScheme)) {
27 // Load the inner URL instead.
28 *url = GURL(url->GetContent());
29 return true;
31 return false;
33 } // namespace
35 // static
36 BrowserURLRewriter* BrowserURLRewriter::GetInstance() {
37 return BrowserURLRewriterImpl::GetInstance();
40 // static
41 bool BrowserURLRewriter::RewriteURLWithWriters(
42 GURL* url,
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)))
48 break;
50 return rewritten;
53 // static
54 BrowserURLRewriterImpl* BrowserURLRewriterImpl::GetInstance() {
55 return Singleton<BrowserURLRewriterImpl>::get();
58 BrowserURLRewriterImpl::BrowserURLRewriterImpl() {
59 web::WebClient* web_client = web::GetWebClient();
60 if (web_client)
61 web_client->PostBrowserURLRewriterCreation(this);
63 // view-source:
64 AddURLRewriter(&HandleViewSource);
67 BrowserURLRewriterImpl::~BrowserURLRewriterImpl() {
70 void BrowserURLRewriterImpl::AddURLRewriter(URLRewriter rewriter) {
71 DCHECK(rewriter);
72 url_rewriters_.push_back(rewriter);
75 bool BrowserURLRewriterImpl::RewriteURLIfNecessary(
76 GURL* url,
77 BrowserState* browser_state) {
78 return BrowserURLRewriter::RewriteURLWithWriters(url, browser_state,
79 url_rewriters_);
82 } // namespace web