summaryrefslogtreecommitdiffstats
path: root/ios/web/browser_url_rewriter_impl.cc
blob: a5e1b67d70193c139c9d86f4a0382dc459fd5130 (plain)
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
// 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