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 "google_apis/drive/gdata_wapi_url_generator.h"
6
7#include "base/logging.h"
8#include "base/strings/string_number_conversions.h"
9#include "base/strings/stringprintf.h"
10#include "net/base/escape.h"
11#include "net/base/url_util.h"
12#include "url/gurl.h"
13
14namespace google_apis {
15namespace {
16
17// URL requesting single resource entry whose resource id is followed by this
18// prefix.
19const char kGetEditURLPrefix[] = "/feeds/default/private/full/";
20
21}  // namespace
22
23const char GDataWapiUrlGenerator::kBaseUrlForProduction[] =
24    "https://docs.google.com/";
25
26// static
27GURL GDataWapiUrlGenerator::AddStandardUrlParams(const GURL& url) {
28  GURL result = net::AppendOrReplaceQueryParameter(url, "v", "3");
29  result = net::AppendOrReplaceQueryParameter(result, "alt", "json");
30  result = net::AppendOrReplaceQueryParameter(result, "showroot", "true");
31  return result;
32}
33
34GDataWapiUrlGenerator::GDataWapiUrlGenerator(const GURL& base_url)
35    : base_url_(base_url) {
36}
37
38GDataWapiUrlGenerator::~GDataWapiUrlGenerator() {
39}
40
41GURL GDataWapiUrlGenerator::GenerateEditUrl(
42    const std::string& resource_id) const {
43  return AddStandardUrlParams(GenerateEditUrlWithoutParams(resource_id));
44}
45
46GURL GDataWapiUrlGenerator::GenerateEditUrlWithoutParams(
47    const std::string& resource_id) const {
48  return base_url_.Resolve(kGetEditURLPrefix + net::EscapePath(resource_id));
49}
50
51GURL GDataWapiUrlGenerator::GenerateEditUrlWithEmbedOrigin(
52    const std::string& resource_id, const GURL& embed_origin) const {
53  GURL url = GenerateEditUrl(resource_id);
54  if (!embed_origin.is_empty()) {
55    // Construct a valid serialized embed origin from an url, according to
56    // WD-html5-20110525. Such string has to be built manually, since
57    // GURL::spec() always adds the trailing slash. Moreover, ports are
58    // currently not supported.
59    DCHECK(!embed_origin.has_port());
60    DCHECK(!embed_origin.has_path() || embed_origin.path() == "/");
61    const std::string serialized_embed_origin =
62        embed_origin.scheme() + "://" + embed_origin.host();
63    url = net::AppendOrReplaceQueryParameter(
64        url, "embedOrigin", serialized_embed_origin);
65  }
66  return url;
67}
68
69}  // namespace google_apis
70