summaryrefslogtreecommitdiffstats
path: root/chrome/browser/google_apis/drive_api_url_generator.cc
blob: 1de37043c93779cb7d7af2babb385e5ca1406d67 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) 2012 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 "chrome/browser/google_apis/drive_api_url_generator.h"

#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "net/base/escape.h"
#include "net/base/url_util.h"

namespace google_apis {

namespace {

// Hard coded URLs for communication with a google drive server.
const char kDriveV2AboutUrl[] = "/drive/v2/about";
const char kDriveV2ApplistUrl[] = "/drive/v2/apps";
const char kDriveV2ChangelistUrl[] = "/drive/v2/changes";
const char kDriveV2FilesUrl[] = "/drive/v2/files";
const char kDriveV2FileUrlPrefix[] = "/drive/v2/files/";
const char kDriveV2ChildrenUrlFormat[] = "/drive/v2/files/%s/children";
const char kDriveV2ChildrenUrlForRemovalFormat[] =
    "/drive/v2/files/%s/children/%s";
const char kDriveV2FileCopyUrlFormat[] = "/drive/v2/files/%s/copy";
const char kDriveV2FileTrashUrlFormat[] = "/drive/v2/files/%s/trash";
const char kDriveV2InitiateUploadNewFileUrl[] = "/upload/drive/v2/files";
const char kDriveV2InitiateUploadExistingFileUrlPrefix[] =
    "/upload/drive/v2/files/";

GURL AddResumableUploadParam(const GURL& url) {
  return net::AppendOrReplaceQueryParameter(url, "uploadType", "resumable");
}

GURL AddMaxResultParam(const GURL& url, int max_results) {
  DCHECK_GT(max_results, 0);
  return net::AppendOrReplaceQueryParameter(
      url, "maxResults", base::IntToString(max_results));
}

}  // namespace

DriveApiUrlGenerator::DriveApiUrlGenerator(const GURL& base_url)
    : base_url_(base_url) {
  // Do nothing.
}

DriveApiUrlGenerator::~DriveApiUrlGenerator() {
  // Do nothing.
}

const char DriveApiUrlGenerator::kBaseUrlForProduction[] =
    "https://www.googleapis.com";

GURL DriveApiUrlGenerator::GetAboutUrl() const {
  return base_url_.Resolve(kDriveV2AboutUrl);
}

GURL DriveApiUrlGenerator::GetApplistUrl() const {
  return base_url_.Resolve(kDriveV2ApplistUrl);
}

GURL DriveApiUrlGenerator::GetChangelistUrl(
    bool include_deleted, int64 start_changestamp, int max_results) const {
  DCHECK_GE(start_changestamp, 0);

  GURL url = base_url_.Resolve(kDriveV2ChangelistUrl);
  if (!include_deleted) {
    // If include_deleted is set to "false", set the query parameter,
    // because its default parameter is "true".
    url = net::AppendOrReplaceQueryParameter(url, "includeDeleted", "false");
  }

  if (start_changestamp > 0) {
    url = net::AppendOrReplaceQueryParameter(
        url, "startChangeId", base::Int64ToString(start_changestamp));
  }

  return AddMaxResultParam(url, max_results);
}

GURL DriveApiUrlGenerator::GetFilesUrl() const {
  return base_url_.Resolve(kDriveV2FilesUrl);
}

GURL DriveApiUrlGenerator::GetFilelistUrl(
    const std::string& search_string, int max_results) const {
  GURL url = base_url_.Resolve(kDriveV2FilesUrl);
  url = AddMaxResultParam(url, max_results);
  return search_string.empty() ?
      url :
      net::AppendOrReplaceQueryParameter(url, "q", search_string);
}

GURL DriveApiUrlGenerator::GetFileUrl(const std::string& file_id) const {
  return base_url_.Resolve(kDriveV2FileUrlPrefix + net::EscapePath(file_id));
}

GURL DriveApiUrlGenerator::GetFileCopyUrl(
    const std::string& resource_id) const {
  return base_url_.Resolve(
      base::StringPrintf(kDriveV2FileCopyUrlFormat,
                         net::EscapePath(resource_id).c_str()));
}

GURL DriveApiUrlGenerator::GetFileTouchUrl(
    const std::string& resource_id) const {
  GURL url = base_url_.Resolve(
      kDriveV2FileUrlPrefix + net::EscapePath(resource_id));

  // This parameter is needed to set the modified date.
  url = net::AppendOrReplaceQueryParameter(url, "setModifiedDate", "true");

  // This parameter is needed to set the last viewed by me date. Otherwise
  // the current time is set automatically.
  url = net::AppendOrReplaceQueryParameter(url, "updateViewedDate", "false");

  return url;
}

GURL DriveApiUrlGenerator::GetFileTrashUrl(const std::string& file_id) const {
  return base_url_.Resolve(
      base::StringPrintf(kDriveV2FileTrashUrlFormat,
                         net::EscapePath(file_id).c_str()));
}

GURL DriveApiUrlGenerator::GetChildrenUrl(
    const std::string& resource_id) const {
  return base_url_.Resolve(
      base::StringPrintf(kDriveV2ChildrenUrlFormat,
                         net::EscapePath(resource_id).c_str()));
}

GURL DriveApiUrlGenerator::GetChildrenUrlForRemoval(
    const std::string& folder_id, const std::string& child_id) const {
  return base_url_.Resolve(
      base::StringPrintf(kDriveV2ChildrenUrlForRemovalFormat,
                         net::EscapePath(folder_id).c_str(),
                         net::EscapePath(child_id).c_str()));
}

GURL DriveApiUrlGenerator::GetInitiateUploadNewFileUrl() const {
  return AddResumableUploadParam(
      base_url_.Resolve(kDriveV2InitiateUploadNewFileUrl));
}

GURL DriveApiUrlGenerator::GetInitiateUploadExistingFileUrl(
    const std::string& resource_id) const {
  const GURL& url = base_url_.Resolve(
      kDriveV2InitiateUploadExistingFileUrlPrefix +
      net::EscapePath(resource_id));
  return AddResumableUploadParam(url);
}

}  // namespace google_apis