summaryrefslogtreecommitdiffstats
path: root/mojo/fetcher/url_resolver.cc
blob: 7028f894c0980a6bd79695050d69d03f269e8c94 (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
// Copyright 2014 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 "mojo/fetcher/url_resolver.h"

#include "base/base_paths.h"
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "mojo/shell/query_util.h"
#include "mojo/util/filename_util.h"
#include "url/url_util.h"

namespace mojo {
namespace fetcher {

URLResolver::URLResolver(const GURL& mojo_base_url)
    : mojo_base_url_(util::AddTrailingSlashIfNeeded(mojo_base_url)) {
  DCHECK(mojo_base_url_.is_valid());
  // Needed to treat first component of mojo URLs as host, not path.
  url::AddStandardScheme("mojo", url::SCHEME_WITHOUT_AUTHORITY);
}

URLResolver::~URLResolver() {
}

GURL URLResolver::ResolveMojoURL(const GURL& mojo_url) const {
  if (mojo_url.scheme() != "mojo") {
    // The mapping has produced some sort of non-mojo: URL - file:, http:, etc.
    return mojo_url;
  }

  // It's still a mojo: URL, use the default mapping scheme.
  std::string query;
  GURL base_url = shell::GetBaseURLAndQuery(mojo_url, &query);
  const std::string host = base_url.host();
  return mojo_base_url_.Resolve(host + "/" + host + ".mojo" + query);
}

}  // namespace fetcher
}  // namespace mojo