Home | History | Annotate | Download | only in browser
      1 // Copyright (c) 2012 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.
      4 
      5 #include "content/browser/browser_url_handler_impl.h"
      6 
      7 #include "base/command_line.h"
      8 #include "base/strings/string_util.h"
      9 #include "cc/base/switches.h"
     10 #include "content/browser/frame_host/debug_urls.h"
     11 #include "content/browser/webui/web_ui_impl.h"
     12 #include "content/public/browser/content_browser_client.h"
     13 #include "content/public/common/url_constants.h"
     14 #include "url/gurl.h"
     15 
     16 namespace content {
     17 
     18 // Handles rewriting view-source URLs for what we'll actually load.
     19 static bool HandleViewSource(GURL* url, BrowserContext* browser_context) {
     20   if (url->SchemeIs(kViewSourceScheme)) {
     21     // Load the inner URL instead.
     22     *url = GURL(url->GetContent());
     23 
     24     // Bug 26129: limit view-source to view the content and not any
     25     // other kind of 'active' url scheme like 'javascript' or 'data'.
     26     static const char* const default_allowed_sub_schemes[] = {
     27         url::kHttpScheme,
     28         url::kHttpsScheme,
     29         url::kFtpScheme,
     30         kChromeDevToolsScheme,
     31         kChromeUIScheme,
     32         url::kFileScheme,
     33         url::kFileSystemScheme
     34     };
     35 
     36     // Merge all the schemes for which view-source is allowed by default, with
     37     // the WebUI schemes defined by the ContentBrowserClient.
     38     std::vector<std::string> all_allowed_sub_schemes;
     39     for (size_t i = 0; i < arraysize(default_allowed_sub_schemes); ++i)
     40       all_allowed_sub_schemes.push_back(default_allowed_sub_schemes[i]);
     41     GetContentClient()->browser()->GetAdditionalWebUISchemes(
     42         &all_allowed_sub_schemes);
     43 
     44     bool is_sub_scheme_allowed = false;
     45     for (size_t i = 0; i < all_allowed_sub_schemes.size(); ++i) {
     46       if (url->SchemeIs(all_allowed_sub_schemes[i].c_str())) {
     47         is_sub_scheme_allowed = true;
     48         break;
     49       }
     50     }
     51 
     52     if (!is_sub_scheme_allowed) {
     53       *url = GURL(url::kAboutBlankURL);
     54       return false;
     55     }
     56 
     57     return true;
     58   }
     59   return false;
     60 }
     61 
     62 // Turns a non view-source URL into the corresponding view-source URL.
     63 static bool ReverseViewSource(GURL* url, BrowserContext* browser_context) {
     64   // No action necessary if the URL is already view-source:
     65   if (url->SchemeIs(kViewSourceScheme))
     66     return false;
     67 
     68   url::Replacements<char> repl;
     69   repl.SetScheme(kViewSourceScheme,
     70                  url::Component(0, strlen(kViewSourceScheme)));
     71   repl.SetPath(url->spec().c_str(), url::Component(0, url->spec().size()));
     72   *url = url->ReplaceComponents(repl);
     73   return true;
     74 }
     75 
     76 static bool DebugURLHandler(GURL* url, BrowserContext* browser_context) {
     77   // If running inside the Telemetry test harness, allow automated
     78   // navigations to access browser-side debug URLs. They must use the
     79   // chrome:// scheme, since the about: scheme won't be rewritten in
     80   // this code path.
     81   if (CommandLine::ForCurrentProcess()->HasSwitch(
     82           cc::switches::kEnableGpuBenchmarking)) {
     83     if (HandleDebugURL(*url, PAGE_TRANSITION_FROM_ADDRESS_BAR)) {
     84       return true;
     85     }
     86   }
     87 
     88   // Circumvent processing URLs that the renderer process will handle.
     89   return IsRendererDebugURL(*url);
     90 }
     91 
     92 // static
     93 BrowserURLHandler* BrowserURLHandler::GetInstance() {
     94   return BrowserURLHandlerImpl::GetInstance();
     95 }
     96 
     97 // static
     98 BrowserURLHandler::URLHandler BrowserURLHandler::null_handler() {
     99   // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/details/520043/error-converting-from-null-to-a-pointer-type-in-std-pair
    100   return NULL;
    101 }
    102 
    103 // static
    104 BrowserURLHandlerImpl* BrowserURLHandlerImpl::GetInstance() {
    105   return Singleton<BrowserURLHandlerImpl>::get();
    106 }
    107 
    108 BrowserURLHandlerImpl::BrowserURLHandlerImpl() {
    109   AddHandlerPair(&DebugURLHandler, BrowserURLHandlerImpl::null_handler());
    110 
    111   GetContentClient()->browser()->BrowserURLHandlerCreated(this);
    112 
    113   // view-source:
    114   AddHandlerPair(&HandleViewSource, &ReverseViewSource);
    115 }
    116 
    117 BrowserURLHandlerImpl::~BrowserURLHandlerImpl() {
    118 }
    119 
    120 void BrowserURLHandlerImpl::AddHandlerPair(URLHandler handler,
    121                                            URLHandler reverse_handler) {
    122   url_handlers_.push_back(HandlerPair(handler, reverse_handler));
    123 }
    124 
    125 void BrowserURLHandlerImpl::RewriteURLIfNecessary(
    126     GURL* url,
    127     BrowserContext* browser_context,
    128     bool* reverse_on_redirect) {
    129   for (size_t i = 0; i < url_handlers_.size(); ++i) {
    130     URLHandler handler = *url_handlers_[i].first;
    131     if (handler && handler(url, browser_context)) {
    132       *reverse_on_redirect = (url_handlers_[i].second != NULL);
    133       return;
    134     }
    135   }
    136 }
    137 
    138 bool BrowserURLHandlerImpl::ReverseURLRewrite(
    139     GURL* url, const GURL& original, BrowserContext* browser_context) {
    140   for (size_t i = 0; i < url_handlers_.size(); ++i) {
    141     URLHandler reverse_rewriter = *url_handlers_[i].second;
    142     if (reverse_rewriter) {
    143       GURL test_url(original);
    144       URLHandler handler = *url_handlers_[i].first;
    145       if (!handler) {
    146         if (reverse_rewriter(url, browser_context))
    147           return true;
    148       } else if (handler(&test_url, browser_context)) {
    149         return reverse_rewriter(url, browser_context);
    150       }
    151     }
    152   }
    153   return false;
    154 }
    155 
    156 }  // namespace content
    157