12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "google_apis/drive/gdata_wapi_url_generator.h"
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/logging.h"
8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/strings/string_number_conversions.h"
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/stringprintf.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "net/base/escape.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "net/base/url_util.h"
12eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/gurl.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace google_apis {
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// URL requesting single resource entry whose resource id is followed by this
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// prefix.
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const char kGetEditURLPrefix[] = "/feeds/default/private/full/";
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const char GDataWapiUrlGenerator::kBaseUrlForProduction[] =
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    "https://docs.google.com/";
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)GURL GDataWapiUrlGenerator::AddStandardUrlParams(const GURL& url) {
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GURL result = net::AppendOrReplaceQueryParameter(url, "v", "3");
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  result = net::AppendOrReplaceQueryParameter(result, "alt", "json");
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  result = net::AppendOrReplaceQueryParameter(result, "showroot", "true");
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return result;
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
340529e5d033099cbfc42635f6f6183833b09dff6eBen MurdochGDataWapiUrlGenerator::GDataWapiUrlGenerator(const GURL& base_url)
350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    : base_url_(base_url) {
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)GDataWapiUrlGenerator::~GDataWapiUrlGenerator() {
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)GURL GDataWapiUrlGenerator::GenerateEditUrl(
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& resource_id) const {
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return AddStandardUrlParams(GenerateEditUrlWithoutParams(resource_id));
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)GURL GDataWapiUrlGenerator::GenerateEditUrlWithoutParams(
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& resource_id) const {
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return base_url_.Resolve(kGetEditURLPrefix + net::EscapePath(resource_id));
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
51bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben MurdochGURL GDataWapiUrlGenerator::GenerateEditUrlWithEmbedOrigin(
52bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    const std::string& resource_id, const GURL& embed_origin) const {
53bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  GURL url = GenerateEditUrl(resource_id);
54bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  if (!embed_origin.is_empty()) {
55bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    // Construct a valid serialized embed origin from an url, according to
56bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    // WD-html5-20110525. Such string has to be built manually, since
57bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    // GURL::spec() always adds the trailing slash. Moreover, ports are
58bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    // currently not supported.
59bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    DCHECK(!embed_origin.has_port());
60bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    DCHECK(!embed_origin.has_path() || embed_origin.path() == "/");
61bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    const std::string serialized_embed_origin =
62bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch        embed_origin.scheme() + "://" + embed_origin.host();
63bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    url = net::AppendOrReplaceQueryParameter(
64bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch        url, "embedOrigin", serialized_embed_origin);
65bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  }
66bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  return url;
67bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch}
68bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace google_apis
70