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) 57d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/browser/drive/drive_api_service.h" 62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string> 82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <vector> 92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/bind.h" 11f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/sequenced_task_runner.h" 12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/stringprintf.h" 132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/task_runner_util.h" 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/values.h" 15eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "chrome/browser/drive/drive_api_util.h" 162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/browser_thread.h" 17a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "google_apis/drive/auth_service.h" 18a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "google_apis/drive/drive_api_parser.h" 19a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "google_apis/drive/drive_api_requests.h" 20a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "google_apis/drive/gdata_errorcode.h" 21a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "google_apis/drive/gdata_wapi_parser.h" 22a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "google_apis/drive/gdata_wapi_requests.h" 23a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "google_apis/drive/request_sender.h" 244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "net/url_request/url_request_context_getter.h" 252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using content::BrowserThread; 27eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::AppList; 28424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)using google_apis::AppListCallback; 297dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochusing google_apis::AuthStatusCallback; 30eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::AuthorizeAppCallback; 31eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::CancelCallback; 32eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::ChangeList; 33eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::DownloadActionCallback; 34eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::EntryActionCallback; 35eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::FileList; 36eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::FileResource; 37eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::GDATA_OTHER_ERROR; 38eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::GDATA_PARSE_ERROR; 39eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::GDataErrorCode; 40424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)using google_apis::AboutResourceCallback; 41eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::GetContentCallback; 42eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::GetResourceEntryCallback; 43a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)using google_apis::GetResourceEntryRequest; 44eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::GetResourceListCallback; 4558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)using google_apis::GetResourceListRequest; 46bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdochusing google_apis::GetShareUrlCallback; 47bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdochusing google_apis::HTTP_NOT_IMPLEMENTED; 48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::HTTP_SUCCESS; 49eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::InitiateUploadCallback; 50a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)using google_apis::Link; 51eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::ProgressCallback; 52eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::RequestSender; 53eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::ResourceEntry; 54eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::ResourceList; 55eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::UploadRangeCallback; 56eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::UploadRangeResponse; 57424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)using google_apis::drive::AboutGetRequest; 58424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)using google_apis::drive::AppsListRequest; 59424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)using google_apis::drive::ChangesListRequest; 6058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)using google_apis::drive::ChangesListNextPageRequest; 6158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)using google_apis::drive::ChildrenDeleteRequest; 6258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)using google_apis::drive::ChildrenInsertRequest; 63eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::drive::DownloadFileRequest; 64424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)using google_apis::drive::FilesCopyRequest; 65424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)using google_apis::drive::FilesGetRequest; 66424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)using google_apis::drive::FilesInsertRequest; 67424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)using google_apis::drive::FilesPatchRequest; 68424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)using google_apis::drive::FilesListRequest; 6958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)using google_apis::drive::FilesListNextPageRequest; 70d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)using google_apis::drive::FilesDeleteRequest; 71424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)using google_apis::drive::FilesTrashRequest; 72eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::drive::GetUploadStatusRequest; 73eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::drive::InitiateUploadExistingFileRequest; 74eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::drive::InitiateUploadNewFileRequest; 75eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing google_apis::drive::ResumeUploadRequest; 76eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 77eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochnamespace drive { 782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace { 802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// OAuth2 scopes for Drive API. 822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const char kDriveScope[] = "https://www.googleapis.com/auth/drive"; 832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const char kDriveAppsReadonlyScope[] = 842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) "https://www.googleapis.com/auth/drive.apps.readonly"; 852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 86424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Mime type to create a directory. 87424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)const char kFolderMimeType[] = "application/vnd.google-apps.folder"; 88424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Max number of file entries to be fetched in a single http request. 908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// 918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// The larger the number is, 928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// - The total running time to fetch the whole file list will become shorter. 938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// - The running time for a single request tends to become longer. 948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Since the file list fetching is a completely background task, for our side, 958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// only the total time matters. However, the server seems to have a time limit 968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// per single request, which disables us to set the largest value (1000). 978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// TODO(kinaba): make it larger when the server gets faster. 988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)const int kMaxNumFilesResourcePerRequest = 250; 99c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const int kMaxNumFilesResourcePerRequestForSearch = 50; 1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 10168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)// For performance, we declare all fields we use. 10268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const char kAboutResourceFields[] = 10368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) "kind,quotaBytesTotal,quotaBytesUsed,largestChangeId,rootFolderId"; 10468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const char kFileResourceFields[] = 10568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) "kind,id,title,createdDate,sharedWithMeDate,downloadUrl,mimeType," 1064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) "md5Checksum,fileSize,labels/trashed,imageMediaMetadata/width," 1074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) "imageMediaMetadata/height,imageMediaMetadata/rotation,etag," 1084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) "parents/parentLink,selfLink,thumbnailLink,alternateLink,embedLink," 109a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) "modifiedDate,lastViewedByMeDate,shared"; 11068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const char kFileResourceOpenWithLinksFields[] = 11168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) "kind,id,openWithLinks/*"; 11268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const char kFileListFields[] = 11368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) "kind,items(kind,id,title,createdDate,sharedWithMeDate,downloadUrl," 1144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) "mimeType,md5Checksum,fileSize,labels/trashed,imageMediaMetadata/width," 1154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) "imageMediaMetadata/height,imageMediaMetadata/rotation,etag," 1164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) "parents/parentLink,selfLink,thumbnailLink,alternateLink,embedLink," 117a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) "modifiedDate,lastViewedByMeDate,shared),nextLink"; 11868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const char kChangeListFields[] = 11968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) "kind,items(file(kind,id,title,createdDate,sharedWithMeDate,downloadUrl," 1204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) "mimeType,md5Checksum,fileSize,labels/trashed,imageMediaMetadata/width," 1214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) "imageMediaMetadata/height,imageMediaMetadata/rotation,etag," 1224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) "parents/parentLink,selfLink,thumbnailLink,alternateLink,embedLink," 123a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) "modifiedDate,lastViewedByMeDate,shared),deleted,id,fileId),nextLink," 1244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) "largestChangeId"; 12568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) 1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Callback invoked when the parsing of resource list is completed, 1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// regardless whether it is succeeded or not. 12858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void DidConvertToResourceListOnBlockingPool( 1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const GetResourceListCallback& callback, 1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) scoped_ptr<ResourceList> resource_list) { 131c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) GDataErrorCode error = resource_list ? HTTP_SUCCESS : GDATA_PARSE_ERROR; 132c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) callback.Run(error, resource_list.Pass()); 1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 135424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Converts the FileResource value to ResourceEntry and runs |callback| on the 1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// UI thread. 137424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void ConvertFileEntryToResourceEntryAndRun( 1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const GetResourceEntryCallback& callback, 1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) GDataErrorCode error, 1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) scoped_ptr<FileResource> value) { 1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(!callback.is_null()); 1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (!value) { 1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) callback.Run(error, scoped_ptr<ResourceEntry>()); 1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return; 1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Converting to ResourceEntry is cheap enough to do on UI thread. 1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) scoped_ptr<ResourceEntry> entry = 151d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) util::ConvertFileResourceToResourceEntry(*value); 1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (!entry) { 1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) callback.Run(GDATA_PARSE_ERROR, scoped_ptr<ResourceEntry>()); 1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return; 1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) callback.Run(error, entry.Pass()); 1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 160d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Thin adapter of ConvertFileListToResourceList. 161424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)scoped_ptr<ResourceList> ConvertFileListToResourceList( 162424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) scoped_ptr<FileList> file_list) { 163d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return util::ConvertFileListToResourceList(*file_list); 164424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} 165424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 166424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Converts the FileList value to ResourceList on blocking pool and runs 167424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// |callback| on the UI thread. 168424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void ConvertFileListToResourceListOnBlockingPoolAndRun( 169424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) scoped_refptr<base::TaskRunner> blocking_task_runner, 170424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const GetResourceListCallback& callback, 171424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) GDataErrorCode error, 172424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) scoped_ptr<FileList> value) { 1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(!callback.is_null()); 1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (!value) { 177424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) callback.Run(error, scoped_ptr<ResourceList>()); 1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return; 1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 1802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 181424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // Convert the value on blocking pool. 182424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) base::PostTaskAndReplyWithResult( 183424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) blocking_task_runner.get(), 184424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) FROM_HERE, 185424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) base::Bind(&ConvertFileListToResourceList, base::Passed(&value)), 18658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) base::Bind(&DidConvertToResourceListOnBlockingPool, callback)); 187424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} 188424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 189d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Thin adapter of ConvertChangeListToResourceList. 190424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)scoped_ptr<ResourceList> ConvertChangeListToResourceList( 191424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) scoped_ptr<ChangeList> change_list) { 192d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return util::ConvertChangeListToResourceList(*change_list); 193424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} 194424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 195424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Converts the FileList value to ResourceList on blocking pool and runs 196424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// |callback| on the UI thread. 197424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void ConvertChangeListToResourceListOnBlockingPoolAndRun( 198424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) scoped_refptr<base::TaskRunner> blocking_task_runner, 199424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const GetResourceListCallback& callback, 200424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) GDataErrorCode error, 201424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) scoped_ptr<ChangeList> value) { 202424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 203424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) DCHECK(!callback.is_null()); 204424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 205424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) if (!value) { 206424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) callback.Run(error, scoped_ptr<ResourceList>()); 2072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return; 2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 210424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // Convert the value on blocking pool. 211424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) base::PostTaskAndReplyWithResult( 212424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) blocking_task_runner.get(), 213424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) FROM_HERE, 214424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) base::Bind(&ConvertChangeListToResourceList, base::Passed(&value)), 21558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) base::Bind(&DidConvertToResourceListOnBlockingPool, callback)); 2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 21858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Converts the FileResource value to ResourceEntry for upload range request, 2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// and runs |callback| on the UI thread. 22058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void ConvertFileResourceToResourceEntryForUploadRangeAndRun( 2212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const UploadRangeCallback& callback, 2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const UploadRangeResponse& response, 2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) scoped_ptr<FileResource> value) { 2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(!callback.is_null()); 2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (!value) { 2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) callback.Run(response, scoped_ptr<ResourceEntry>()); 2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return; 2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 2312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Converting to ResourceEntry is cheap enough to do on UI thread. 2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) scoped_ptr<ResourceEntry> entry = 234d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) util::ConvertFileResourceToResourceEntry(*value); 2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (!entry) { 2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) callback.Run(UploadRangeResponse(GDATA_PARSE_ERROR, 2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) response.start_position_received, 2382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) response.end_position_received), 2392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) scoped_ptr<ResourceEntry>()); 2402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return; 2412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 2422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) callback.Run(response, entry.Pass()); 2442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 2452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 246a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void ExtractOpenUrlAndRun(const std::string& app_id, 247a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) const AuthorizeAppCallback& callback, 248a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) GDataErrorCode error, 249a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) scoped_ptr<FileResource> value) { 250a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 251a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) DCHECK(!callback.is_null()); 252a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 253a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) if (!value) { 254a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) callback.Run(error, GURL()); 255a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) return; 256a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) } 257a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 258a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) const std::vector<FileResource::OpenWithLink>& open_with_links = 259a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) value->open_with_links(); 260a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) for (size_t i = 0; i < open_with_links.size(); ++i) { 261a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) if (open_with_links[i].app_id == app_id) { 262a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) callback.Run(HTTP_SUCCESS, open_with_links[i].open_url); 263a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) return; 264a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) } 265a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) } 266a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 267a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) // Not found. 268a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) callback.Run(GDATA_OTHER_ERROR, GURL()); 269a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)} 270a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 2713551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Ignores the |entry|, and runs the |callback|. 2723551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void EntryActionCallbackAdapter( 2733551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const EntryActionCallback& callback, 2743551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) GDataErrorCode error, scoped_ptr<FileResource> entry) { 2753551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) callback.Run(error); 2763551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 2773551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// The resource ID for the root directory for Drive API is defined in the spec: 2792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// https://developers.google.com/drive/folder 2802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const char kDriveApiRootDirectoryResourceId[] = "root"; 2812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} // namespace 2832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DriveAPIService::DriveAPIService( 285558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch OAuth2TokenService* oauth2_token_service, 2862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) net::URLRequestContextGetter* url_request_context_getter, 287f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) base::SequencedTaskRunner* blocking_task_runner, 2882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const GURL& base_url, 289eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const GURL& base_download_url, 290a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) const GURL& wapi_base_url, 2912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& custom_user_agent) 292558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch : oauth2_token_service_(oauth2_token_service), 293558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch url_request_context_getter_(url_request_context_getter), 294eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch blocking_task_runner_(blocking_task_runner), 295eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch url_generator_(base_url, base_download_url), 296a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) wapi_url_generator_(wapi_base_url, base_download_url), 2972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) custom_user_agent_(custom_user_agent) { 2982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 2992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 3002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DriveAPIService::~DriveAPIService() { 3022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 303868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (sender_.get()) 304868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) sender_->auth_service()->RemoveObserver(this); 3052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 3062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 30768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)void DriveAPIService::Initialize(const std::string& account_id) { 3082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 3092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::vector<std::string> scopes; 3112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) scopes.push_back(kDriveScope); 3122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) scopes.push_back(kDriveAppsReadonlyScope); 313a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) 314a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // GData WAPI token. These are for GetShareUrl(). 315a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) scopes.push_back(util::kDocsListScope); 316a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) scopes.push_back(util::kDriveAppsScope); 317a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) 3189ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch sender_.reset(new RequestSender( 31968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) new google_apis::AuthService(oauth2_token_service_, 32068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) account_id, 3214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) url_request_context_getter_.get(), 32268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) scopes), 3234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) url_request_context_getter_.get(), 32468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) blocking_task_runner_.get(), 32568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) custom_user_agent_)); 326868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) sender_->auth_service()->AddObserver(this); 3272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 3282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void DriveAPIService::AddObserver(DriveServiceObserver* observer) { 3302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) observers_.AddObserver(observer); 3312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 3322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void DriveAPIService::RemoveObserver(DriveServiceObserver* observer) { 3342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) observers_.RemoveObserver(observer); 3352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 3362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 337868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool DriveAPIService::CanSendRequest() const { 3382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 3392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return HasRefreshToken(); 3412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 3422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 343d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)ResourceIdCanonicalizer DriveAPIService::GetResourceIdCanonicalizer() const { 344d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return base::Bind(&drive::util::CanonicalizeResourceId); 3452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 3462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)std::string DriveAPIService::GetRootResourceId() const { 3482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return kDriveApiRootDirectoryResourceId; 3492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 3502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)CancelCallback DriveAPIService::GetAllResourceList( 352c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const GetResourceListCallback& callback) { 353c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 354c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) DCHECK(!callback.is_null()); 355c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 35658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) FilesListRequest* request = new FilesListRequest( 357424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) sender_.get(), url_generator_, 35858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) base::Bind(&ConvertFileListToResourceListOnBlockingPoolAndRun, 359424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) blocking_task_runner_, callback)); 36058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) request->set_max_results(kMaxNumFilesResourcePerRequest); 36158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) request->set_q("trashed = false"); // Exclude trashed files. 36268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) request->set_fields(kFileListFields); 363424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return sender_->StartRequestWithRetry(request); 364c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 365c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 3667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)CancelCallback DriveAPIService::GetResourceListInDirectory( 3672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& directory_resource_id, 3682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const GetResourceListCallback& callback) { 3692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 370c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) DCHECK(!directory_resource_id.empty()); 3712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(!callback.is_null()); 3722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 373c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Because children.list method on Drive API v2 returns only the list of 374c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // children's references, but we need all file resource list. 375c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // So, here we use files.list method instead, with setting parents query. 376c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // After the migration from GData WAPI to Drive API v2, we should clean the 377eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // code up by moving the responsibility to include "parents" in the query 378c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // to client side. 379c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // We aren't interested in files in trash in this context, neither. 380424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) FilesListRequest* request = new FilesListRequest( 381424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) sender_.get(), url_generator_, 382424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) base::Bind(&ConvertFileListToResourceListOnBlockingPoolAndRun, 383424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) blocking_task_runner_, callback)); 384424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) request->set_max_results(kMaxNumFilesResourcePerRequest); 385424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) request->set_q(base::StringPrintf( 386424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) "'%s' in parents and trashed = false", 387424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) drive::util::EscapeQueryStringValue(directory_resource_id).c_str())); 38868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) request->set_fields(kFileListFields); 389424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return sender_->StartRequestWithRetry(request); 390c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 3912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)CancelCallback DriveAPIService::Search( 3937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) const std::string& search_query, 3947d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) const GetResourceListCallback& callback) { 395c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 396c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) DCHECK(!search_query.empty()); 397c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) DCHECK(!callback.is_null()); 398c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 399424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) FilesListRequest* request = new FilesListRequest( 400424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) sender_.get(), url_generator_, 401424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) base::Bind(&ConvertFileListToResourceListOnBlockingPoolAndRun, 402424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) blocking_task_runner_, callback)); 403424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) request->set_max_results(kMaxNumFilesResourcePerRequestForSearch); 404424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) request->set_q(drive::util::TranslateQuery(search_query)); 40568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) request->set_fields(kFileListFields); 406424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return sender_->StartRequestWithRetry(request); 4072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 4082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 4097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)CancelCallback DriveAPIService::SearchByTitle( 410c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const std::string& title, 411c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const std::string& directory_resource_id, 4122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const GetResourceListCallback& callback) { 4132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 414c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) DCHECK(!title.empty()); 4152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(!callback.is_null()); 4162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 417c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) std::string query; 418c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::StringAppendF(&query, "title = '%s'", 419c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) drive::util::EscapeQueryStringValue(title).c_str()); 420c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (!directory_resource_id.empty()) { 421c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::StringAppendF( 422c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) &query, " and '%s' in parents", 423c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) drive::util::EscapeQueryStringValue(directory_resource_id).c_str()); 424c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 425c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) query += " and trashed = false"; 426c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 427424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) FilesListRequest* request = new FilesListRequest( 428424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) sender_.get(), url_generator_, 429424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) base::Bind(&ConvertFileListToResourceListOnBlockingPoolAndRun, 430424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) blocking_task_runner_, callback)); 431424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) request->set_max_results(kMaxNumFilesResourcePerRequest); 432424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) request->set_q(query); 43368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) request->set_fields(kFileListFields); 434424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return sender_->StartRequestWithRetry(request); 4352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 4362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 4377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)CancelCallback DriveAPIService::GetChangeList( 4387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) int64 start_changestamp, 4397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) const GetResourceListCallback& callback) { 4402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 4412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(!callback.is_null()); 4422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 443424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) ChangesListRequest* request = new ChangesListRequest( 444424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) sender_.get(), url_generator_, 445424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) base::Bind(&ConvertChangeListToResourceListOnBlockingPoolAndRun, 446424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) blocking_task_runner_, callback)); 447424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) request->set_max_results(kMaxNumFilesResourcePerRequest); 448424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) request->set_start_change_id(start_changestamp); 44968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) request->set_fields(kChangeListFields); 450424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return sender_->StartRequestWithRetry(request); 451c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 452c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 453424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)CancelCallback DriveAPIService::GetRemainingChangeList( 45458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) const GURL& next_link, 455424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const GetResourceListCallback& callback) { 456424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 45758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) DCHECK(!next_link.is_empty()); 458424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) DCHECK(!callback.is_null()); 459424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 46058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) ChangesListNextPageRequest* request = new ChangesListNextPageRequest( 46158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) sender_.get(), 46258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) base::Bind(&ConvertChangeListToResourceListOnBlockingPoolAndRun, 46358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) blocking_task_runner_, callback)); 46458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) request->set_next_link(next_link); 46568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) request->set_fields(kChangeListFields); 46658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) return sender_->StartRequestWithRetry(request); 467424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} 468424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 469424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)CancelCallback DriveAPIService::GetRemainingFileList( 47058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) const GURL& next_link, 471424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const GetResourceListCallback& callback) { 472424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 47358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) DCHECK(!next_link.is_empty()); 474424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) DCHECK(!callback.is_null()); 475424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 47658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) FilesListNextPageRequest* request = new FilesListNextPageRequest( 47758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) sender_.get(), 47858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) base::Bind(&ConvertFileListToResourceListOnBlockingPoolAndRun, 47958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) blocking_task_runner_, callback)); 48058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) request->set_next_link(next_link); 48168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) request->set_fields(kFileListFields); 48258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) return sender_->StartRequestWithRetry(request); 483424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} 484424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 4857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)CancelCallback DriveAPIService::GetResourceEntry( 4862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& resource_id, 4872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const GetResourceEntryCallback& callback) { 4882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 4892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(!callback.is_null()); 4902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 491424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) FilesGetRequest* request = new FilesGetRequest( 492424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) sender_.get(), url_generator_, 493424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) base::Bind(&ConvertFileEntryToResourceEntryAndRun, callback)); 494424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) request->set_file_id(resource_id); 49568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) request->set_fields(kFileResourceFields); 496424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return sender_->StartRequestWithRetry(request); 4972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 4982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 499bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben MurdochCancelCallback DriveAPIService::GetShareUrl( 500bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch const std::string& resource_id, 501bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch const GURL& embed_origin, 502bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch const GetShareUrlCallback& callback) { 503bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 504bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch DCHECK(!callback.is_null()); 505bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch 506a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // Unfortunately "share url" is not yet supported on Drive API v2. 507a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // So, as a fallback, we use GData WAPI protocol for this method. 508a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // TODO(hidehiko): Get rid of this implementation when share url is 509a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // supported on Drive API v2. 510a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) return sender_->StartRequestWithRetry( 511a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) new GetResourceEntryRequest(sender_.get(), 512a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) wapi_url_generator_, 513a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) resource_id, 514a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) embed_origin, 515a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) base::Bind(&util::ParseShareUrlAndRun, 516a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) callback))); 517bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch} 518bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch 5197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)CancelCallback DriveAPIService::GetAboutResource( 520424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const AboutResourceCallback& callback) { 5212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 5222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(!callback.is_null()); 5232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 52468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) AboutGetRequest* request = 52568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) new AboutGetRequest(sender_.get(), url_generator_, callback); 52668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) request->set_fields(kAboutResourceFields); 52768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) return sender_->StartRequestWithRetry(request); 5282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 5292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 530424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)CancelCallback DriveAPIService::GetAppList(const AppListCallback& callback) { 5312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 5322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(!callback.is_null()); 5332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 534424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return sender_->StartRequestWithRetry( 535424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) new AppsListRequest(sender_.get(), url_generator_, callback)); 5362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 5372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 5387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)CancelCallback DriveAPIService::DownloadFile( 5392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::FilePath& local_cache_path, 540eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const std::string& resource_id, 5412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const DownloadActionCallback& download_action_callback, 542c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const GetContentCallback& get_content_callback, 543c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const ProgressCallback& progress_callback) { 5442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 5452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(!download_action_callback.is_null()); 5462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // get_content_callback may be null. 5472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 5487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return sender_->StartRequestWithRetry( 549868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) new DownloadFileRequest(sender_.get(), 550eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch url_generator_, 551eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch resource_id, 552eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch local_cache_path, 553868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) download_action_callback, 554868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) get_content_callback, 555eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch progress_callback)); 5562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 5572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 5587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)CancelCallback DriveAPIService::DeleteResource( 5592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& resource_id, 5602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& etag, 5612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const EntryActionCallback& callback) { 5622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 5632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(!callback.is_null()); 5642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 565d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles) FilesDeleteRequest* request = new FilesDeleteRequest( 566d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles) sender_.get(), url_generator_, callback); 567d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles) request->set_file_id(resource_id); 568d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles) request->set_etag(etag); 569d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles) return sender_->StartRequestWithRetry(request); 570d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)} 571d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles) 572d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)CancelCallback DriveAPIService::TrashResource( 573d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles) const std::string& resource_id, 574d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles) const EntryActionCallback& callback) { 575d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 576d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles) DCHECK(!callback.is_null()); 577d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles) 578424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) FilesTrashRequest* request = new FilesTrashRequest( 579424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) sender_.get(), url_generator_, 580424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) base::Bind(&EntryActionCallbackAdapter, callback)); 581424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) request->set_file_id(resource_id); 58268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) request->set_fields(kFileResourceFields); 583424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return sender_->StartRequestWithRetry(request); 5842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 5852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 5867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)CancelCallback DriveAPIService::AddNewDirectory( 5872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& parent_resource_id, 5887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch const std::string& directory_title, 5892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const GetResourceEntryCallback& callback) { 5902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 5912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(!callback.is_null()); 5922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 593424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) FilesInsertRequest* request = new FilesInsertRequest( 594424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) sender_.get(), url_generator_, 595424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) base::Bind(&ConvertFileEntryToResourceEntryAndRun, callback)); 596424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) request->set_mime_type(kFolderMimeType); 597424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) request->add_parent(parent_resource_id); 598424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) request->set_title(directory_title); 59968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) request->set_fields(kFileResourceFields); 600424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return sender_->StartRequestWithRetry(request); 6012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 6022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 6037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)CancelCallback DriveAPIService::CopyResource( 60490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const std::string& resource_id, 60590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const std::string& parent_resource_id, 6067dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch const std::string& new_title, 60768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) const base::Time& last_modified, 60890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const GetResourceEntryCallback& callback) { 60990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 61090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) DCHECK(!callback.is_null()); 61190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 612424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) FilesCopyRequest* request = new FilesCopyRequest( 613424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) sender_.get(), url_generator_, 614424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) base::Bind(&ConvertFileEntryToResourceEntryAndRun, callback)); 615424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) request->set_file_id(resource_id); 616424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) request->add_parent(parent_resource_id); 617424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) request->set_title(new_title); 61868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) request->set_modified_date(last_modified); 61968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) request->set_fields(kFileResourceFields); 620424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return sender_->StartRequestWithRetry(request); 62190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 62290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 623f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)CancelCallback DriveAPIService::UpdateResource( 6243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const std::string& resource_id, 6253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const std::string& parent_resource_id, 6263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const std::string& new_title, 62768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) const base::Time& last_modified, 628f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const base::Time& last_viewed_by_me, 6293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const GetResourceEntryCallback& callback) { 6303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 6313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) DCHECK(!callback.is_null()); 6323551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 633424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) FilesPatchRequest* request = new FilesPatchRequest( 634424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) sender_.get(), url_generator_, 635424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) base::Bind(&ConvertFileEntryToResourceEntryAndRun, callback)); 636424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) request->set_file_id(resource_id); 637424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) request->set_title(new_title); 638424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) if (!parent_resource_id.empty()) 639424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) request->add_parent(parent_resource_id); 64068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) if (!last_modified.is_null()) { 641f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // Need to set setModifiedDate to true to overwrite modifiedDate. 64268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) request->set_set_modified_date(true); 64368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) request->set_modified_date(last_modified); 64468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) } 645f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) if (!last_viewed_by_me.is_null()) { 646f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // Need to set updateViewedDate to false, otherwise the lastViewedByMeDate 647f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // will be set to the request time (not the specified time via request). 648f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) request->set_update_viewed_date(false); 649f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) request->set_last_viewed_by_me_date(last_viewed_by_me); 650f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) } 65168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) request->set_fields(kFileResourceFields); 652424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return sender_->StartRequestWithRetry(request); 6533551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 6543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 6557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)CancelCallback DriveAPIService::RenameResource( 6562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& resource_id, 6577dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch const std::string& new_title, 6582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const EntryActionCallback& callback) { 6592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 6602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(!callback.is_null()); 6612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 662424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) FilesPatchRequest* request = new FilesPatchRequest( 663424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) sender_.get(), url_generator_, 664424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) base::Bind(&EntryActionCallbackAdapter, callback)); 665424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) request->set_file_id(resource_id); 666424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) request->set_title(new_title); 66768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) request->set_fields(kFileResourceFields); 668424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return sender_->StartRequestWithRetry(request); 6692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 6702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 6717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)CancelCallback DriveAPIService::AddResourceToDirectory( 6722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& parent_resource_id, 6732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& resource_id, 6742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const EntryActionCallback& callback) { 6752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 6762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(!callback.is_null()); 6772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 67858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) ChildrenInsertRequest* request = 67958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) new ChildrenInsertRequest(sender_.get(), url_generator_, callback); 68058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) request->set_folder_id(parent_resource_id); 68158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) request->set_id(resource_id); 68258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) return sender_->StartRequestWithRetry(request); 6832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 6842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 6857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)CancelCallback DriveAPIService::RemoveResourceFromDirectory( 6862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& parent_resource_id, 6872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& resource_id, 6882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const EntryActionCallback& callback) { 6892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 6902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(!callback.is_null()); 6912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 69258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) ChildrenDeleteRequest* request = 69358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) new ChildrenDeleteRequest(sender_.get(), url_generator_, callback); 69458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) request->set_child_id(resource_id); 69558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) request->set_folder_id(parent_resource_id); 69658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) return sender_->StartRequestWithRetry(request); 6972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 6982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 6997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)CancelCallback DriveAPIService::InitiateUploadNewFile( 7002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& content_type, 7012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int64 content_length, 7022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& parent_resource_id, 7032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& title, 7042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const InitiateUploadCallback& callback) { 7052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 7062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(!callback.is_null()); 7072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 7087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return sender_->StartRequestWithRetry( 709eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch new InitiateUploadNewFileRequest( 710868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) sender_.get(), 7112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) url_generator_, 7122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) content_type, 7132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) content_length, 7142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) parent_resource_id, 7152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) title, 7162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) callback)); 7172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 7182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 7197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)CancelCallback DriveAPIService::InitiateUploadExistingFile( 7202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& content_type, 7212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int64 content_length, 7222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& resource_id, 7232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& etag, 7242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const InitiateUploadCallback& callback) { 7252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 7262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(!callback.is_null()); 7272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 7287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return sender_->StartRequestWithRetry( 729eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch new InitiateUploadExistingFileRequest( 730868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) sender_.get(), 7312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) url_generator_, 7322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) content_type, 7332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) content_length, 7342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) resource_id, 7352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) etag, 7362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) callback)); 7372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 7382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 7397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)CancelCallback DriveAPIService::ResumeUpload( 7402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const GURL& upload_url, 7412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int64 start_position, 7422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int64 end_position, 7432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int64 content_length, 7442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& content_type, 745a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) const base::FilePath& local_file_path, 746c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const UploadRangeCallback& callback, 747c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const ProgressCallback& progress_callback) { 7482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 7492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(!callback.is_null()); 7502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 7517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return sender_->StartRequestWithRetry( 752eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch new ResumeUploadRequest( 753868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) sender_.get(), 7542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) upload_url, 7552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) start_position, 7562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) end_position, 7572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) content_length, 7582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) content_type, 759a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) local_file_path, 76058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) base::Bind(&ConvertFileResourceToResourceEntryForUploadRangeAndRun, 76158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) callback), 762c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) progress_callback)); 7632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 7642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 7657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)CancelCallback DriveAPIService::GetUploadStatus( 7662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const GURL& upload_url, 7672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int64 content_length, 7682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const UploadRangeCallback& callback) { 7692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 7702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(!callback.is_null()); 7712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 772eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch return sender_->StartRequestWithRetry(new GetUploadStatusRequest( 773868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) sender_.get(), 77490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) upload_url, 77590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) content_length, 77658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) base::Bind(&ConvertFileResourceToResourceEntryForUploadRangeAndRun, 77758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) callback))); 7782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 7792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 7807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)CancelCallback DriveAPIService::AuthorizeApp( 7812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& resource_id, 7822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& app_id, 7832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const AuthorizeAppCallback& callback) { 7842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 7852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(!callback.is_null()); 7862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 787424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) FilesGetRequest* request = new FilesGetRequest( 788424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) sender_.get(), url_generator_, 789424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) base::Bind(&ExtractOpenUrlAndRun, app_id, callback)); 790424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) request->set_file_id(resource_id); 79168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) request->set_fields(kFileResourceOpenWithLinksFields); 792424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return sender_->StartRequestWithRetry(request); 7932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 7942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 79558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)CancelCallback DriveAPIService::GetResourceListInDirectoryByWapi( 79658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) const std::string& directory_resource_id, 79758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) const google_apis::GetResourceListCallback& callback) { 79858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 79958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) DCHECK(!directory_resource_id.empty()); 80058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) DCHECK(!callback.is_null()); 80158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 80258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) return sender_->StartRequestWithRetry( 80358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) new GetResourceListRequest(sender_.get(), 80458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) wapi_url_generator_, 80558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) GURL(), // No override url 80658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 0, // start changestamp 80758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) std::string(), // empty search query 80858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) directory_resource_id, 80958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) callback)); 81058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)} 81158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 81258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)CancelCallback DriveAPIService::GetRemainingResourceList( 81358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) const GURL& next_link, 81458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) const google_apis::GetResourceListCallback& callback) { 81558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 81658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) DCHECK(!next_link.is_empty()); 81758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) DCHECK(!callback.is_null()); 81858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 81958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) return sender_->StartRequestWithRetry( 82058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) new GetResourceListRequest(sender_.get(), 82158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) wapi_url_generator_, 82258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) next_link, 82358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 0, // start changestamp 82458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) std::string(), // empty search query 82558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) std::string(), // no directory resource id 82658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) callback)); 82758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)} 82858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 8292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool DriveAPIService::HasAccessToken() const { 8302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 831868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return sender_->auth_service()->HasAccessToken(); 8322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 8332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 8347dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid DriveAPIService::RequestAccessToken(const AuthStatusCallback& callback) { 8352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 8367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch DCHECK(!callback.is_null()); 8377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 8387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch const std::string access_token = sender_->auth_service()->access_token(); 8397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch if (!access_token.empty()) { 8407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch callback.Run(google_apis::HTTP_NOT_MODIFIED, access_token); 8417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch return; 8427dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch } 8437dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 8447dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch // Retrieve the new auth token. 8457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch sender_->auth_service()->StartAuthentication(callback); 8467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch} 8472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 8487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochbool DriveAPIService::HasRefreshToken() const { 8497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 850868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return sender_->auth_service()->HasRefreshToken(); 8512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 8522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 8532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void DriveAPIService::ClearAccessToken() { 8542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 8557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch sender_->auth_service()->ClearAccessToken(); 8562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 8572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 8582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void DriveAPIService::ClearRefreshToken() { 8592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 8607dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch sender_->auth_service()->ClearRefreshToken(); 8612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 8622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 8632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void DriveAPIService::OnOAuth2RefreshTokenChanged() { 8642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 865868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (CanSendRequest()) { 8662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) FOR_EACH_OBSERVER( 867868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) DriveServiceObserver, observers_, OnReadyToSendRequests()); 8682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } else if (!HasRefreshToken()) { 8692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) FOR_EACH_OBSERVER( 8702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DriveServiceObserver, observers_, OnRefreshTokenInvalid()); 8712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 8722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 8732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 874eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} // namespace drive 875