This source file includes following definitions.
- AddStandardUrlParams
- AddInitiateUploadUrlParams
- AddFeedUrlParams
- base_download_url_
- GenerateResourceListUrl
- GenerateSearchByTitleUrl
- GenerateEditUrl
- GenerateEditUrlWithoutParams
- GenerateEditUrlWithEmbedOrigin
- GenerateContentUrl
- GenerateResourceUrlForRemoval
- GenerateInitiateUploadNewFileUrl
- GenerateInitiateUploadExistingFileUrl
- GenerateResourceListRootUrl
- GenerateAccountMetadataUrl
- GenerateDownloadFileUrl
#include "google_apis/drive/gdata_wapi_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"
#include "url/gurl.h"
namespace google_apis {
namespace {
const char kContentURLFormat[] = "/feeds/default/private/full/%s/contents";
const char kResourceURLForRemovalFormat[] =
"/feeds/default/private/full/%s/contents/%s";
const char kGetEditURLPrefix[] = "/feeds/default/private/full/";
const char kResourceListRootURL[] = "/feeds/default/private/full";
const char kAccountMetadataURL[] = "/feeds/metadata/default";
const char kInitiateUploadNewFileURLFormat[] =
"/feeds/upload/create-session/default/private/full/%s/contents";
const char kInitiateUploadExistingFileURLPrefix[] =
"/feeds/upload/create-session/default/private/full/";
const int kMaxDocumentsPerFeed = 500;
const int kMaxDocumentsPerSearchFeed = 100;
const char kGetChangesListURL[] = "/feeds/default/private/changes";
}
const char GDataWapiUrlGenerator::kBaseUrlForProduction[] =
"https://docs.google.com/";
const char GDataWapiUrlGenerator::kBaseDownloadUrlForProduction[] =
"https://www.googledrive.com/host/";
GURL GDataWapiUrlGenerator::AddStandardUrlParams(const GURL& url) {
GURL result = net::AppendOrReplaceQueryParameter(url, "v", "3");
result = net::AppendOrReplaceQueryParameter(result, "alt", "json");
result = net::AppendOrReplaceQueryParameter(result, "showroot", "true");
return result;
}
GURL GDataWapiUrlGenerator::AddInitiateUploadUrlParams(const GURL& url) {
GURL result = net::AppendOrReplaceQueryParameter(url, "convert", "false");
return AddStandardUrlParams(result);
}
GURL GDataWapiUrlGenerator::AddFeedUrlParams(
const GURL& url,
int num_items_to_fetch) {
GURL result = AddStandardUrlParams(url);
result = net::AppendOrReplaceQueryParameter(result, "showfolders", "true");
result = net::AppendOrReplaceQueryParameter(result, "include-shared", "true");
result = net::AppendOrReplaceQueryParameter(
result, "max-results", base::IntToString(num_items_to_fetch));
return result;
}
GDataWapiUrlGenerator::GDataWapiUrlGenerator(const GURL& base_url,
const GURL& base_download_url)
: base_url_(base_url),
base_download_url_(base_download_url) {
}
GDataWapiUrlGenerator::~GDataWapiUrlGenerator() {
}
GURL GDataWapiUrlGenerator::GenerateResourceListUrl(
const GURL& override_url,
int64 start_changestamp,
const std::string& search_string,
const std::string& directory_resource_id) const {
DCHECK_LE(0, start_changestamp);
int max_docs = search_string.empty() ? kMaxDocumentsPerFeed :
kMaxDocumentsPerSearchFeed;
GURL url;
if (!override_url.is_empty()) {
start_changestamp = 0;
url = override_url;
} else if (start_changestamp > 0) {
DCHECK(search_string.empty());
url = base_url_.Resolve(kGetChangesListURL);
} else if (!directory_resource_id.empty()) {
url = base_url_.Resolve(
base::StringPrintf(kContentURLFormat,
net::EscapePath(
directory_resource_id).c_str()));
} else {
url = base_url_.Resolve(kResourceListRootURL);
}
url = AddFeedUrlParams(url, max_docs);
if (start_changestamp) {
url = net::AppendOrReplaceQueryParameter(
url, "start-index", base::Int64ToString(start_changestamp));
}
if (!search_string.empty()) {
url = net::AppendOrReplaceQueryParameter(url, "q", search_string);
}
return url;
}
GURL GDataWapiUrlGenerator::GenerateSearchByTitleUrl(
const std::string& title,
const std::string& directory_resource_id) const {
DCHECK(!title.empty());
GURL url = directory_resource_id.empty() ?
base_url_.Resolve(kResourceListRootURL) :
base_url_.Resolve(base::StringPrintf(
kContentURLFormat, net::EscapePath(directory_resource_id).c_str()));
url = AddFeedUrlParams(url, kMaxDocumentsPerFeed);
url = net::AppendOrReplaceQueryParameter(url, "title", title);
url = net::AppendOrReplaceQueryParameter(url, "title-exact", "true");
return url;
}
GURL GDataWapiUrlGenerator::GenerateEditUrl(
const std::string& resource_id) const {
return AddStandardUrlParams(GenerateEditUrlWithoutParams(resource_id));
}
GURL GDataWapiUrlGenerator::GenerateEditUrlWithoutParams(
const std::string& resource_id) const {
return base_url_.Resolve(kGetEditURLPrefix + net::EscapePath(resource_id));
}
GURL GDataWapiUrlGenerator::GenerateEditUrlWithEmbedOrigin(
const std::string& resource_id, const GURL& embed_origin) const {
GURL url = GenerateEditUrl(resource_id);
if (!embed_origin.is_empty()) {
DCHECK(!embed_origin.has_port());
DCHECK(!embed_origin.has_path() || embed_origin.path() == "/");
const std::string serialized_embed_origin =
embed_origin.scheme() + "://" + embed_origin.host();
url = net::AppendOrReplaceQueryParameter(
url, "embedOrigin", serialized_embed_origin);
}
return url;
}
GURL GDataWapiUrlGenerator::GenerateContentUrl(
const std::string& resource_id) const {
if (resource_id.empty()) {
return GURL();
}
GURL result = base_url_.Resolve(
base::StringPrintf(kContentURLFormat,
net::EscapePath(resource_id).c_str()));
return AddStandardUrlParams(result);
}
GURL GDataWapiUrlGenerator::GenerateResourceUrlForRemoval(
const std::string& parent_resource_id,
const std::string& resource_id) const {
if (resource_id.empty() || parent_resource_id.empty()) {
return GURL();
}
GURL result = base_url_.Resolve(
base::StringPrintf(kResourceURLForRemovalFormat,
net::EscapePath(parent_resource_id).c_str(),
net::EscapePath(resource_id).c_str()));
return AddStandardUrlParams(result);
}
GURL GDataWapiUrlGenerator::GenerateInitiateUploadNewFileUrl(
const std::string& parent_resource_id) const {
GURL result = base_url_.Resolve(
base::StringPrintf(kInitiateUploadNewFileURLFormat,
net::EscapePath(parent_resource_id).c_str()));
return AddInitiateUploadUrlParams(result);
}
GURL GDataWapiUrlGenerator::GenerateInitiateUploadExistingFileUrl(
const std::string& resource_id) const {
GURL result = base_url_.Resolve(
kInitiateUploadExistingFileURLPrefix + net::EscapePath(resource_id));
return AddInitiateUploadUrlParams(result);
}
GURL GDataWapiUrlGenerator::GenerateResourceListRootUrl() const {
return AddStandardUrlParams(base_url_.Resolve(kResourceListRootURL));
}
GURL GDataWapiUrlGenerator::GenerateAccountMetadataUrl(
bool include_installed_apps) const {
GURL result = AddStandardUrlParams(base_url_.Resolve(kAccountMetadataURL));
if (include_installed_apps) {
result = net::AppendOrReplaceQueryParameter(
result, "include-installed-apps", "true");
}
return result;
}
GURL GDataWapiUrlGenerator::GenerateDownloadFileUrl(
const std::string& resource_id) const {
size_t colon = resource_id.find(':');
return base_download_url_.Resolve(net::EscapePath(
colon == std::string::npos ? resource_id
: resource_id.substr(colon + 1)));
}
}