Home | History | Annotate | Download | only in drive
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef GOOGLE_APIS_DRIVE_DRIVE_API_REQUESTS_H_
      6 #define GOOGLE_APIS_DRIVE_DRIVE_API_REQUESTS_H_
      7 
      8 #include <string>
      9 
     10 #include "base/callback_forward.h"
     11 #include "base/location.h"
     12 #include "base/sequenced_task_runner.h"
     13 #include "base/task_runner_util.h"
     14 #include "base/time/time.h"
     15 #include "base/values.h"
     16 #include "google_apis/drive/base_requests.h"
     17 #include "google_apis/drive/drive_api_parser.h"
     18 #include "google_apis/drive/drive_api_url_generator.h"
     19 #include "google_apis/drive/drive_common_callbacks.h"
     20 
     21 namespace google_apis {
     22 
     23 // Callback used for requests that the server returns FileResource data
     24 // formatted into JSON value.
     25 typedef base::Callback<void(GDataErrorCode error,
     26                             scoped_ptr<FileResource> entry)>
     27     FileResourceCallback;
     28 
     29 // Callback used for requests that the server returns FileList data
     30 // formatted into JSON value.
     31 typedef base::Callback<void(GDataErrorCode error,
     32                             scoped_ptr<FileList> entry)> FileListCallback;
     33 
     34 // Callback used for requests that the server returns ChangeList data
     35 // formatted into JSON value.
     36 typedef base::Callback<void(GDataErrorCode error,
     37                             scoped_ptr<ChangeList> entry)> ChangeListCallback;
     38 
     39 namespace drive {
     40 
     41 //============================ DriveApiPartialFieldRequest ====================
     42 
     43 // This is base class of the Drive API related requests. All Drive API requests
     44 // support partial request (to improve the performance). The function can be
     45 // shared among the Drive API requests.
     46 // See also https://developers.google.com/drive/performance
     47 class DriveApiPartialFieldRequest : public UrlFetchRequestBase {
     48  public:
     49   explicit DriveApiPartialFieldRequest(RequestSender* sender);
     50   virtual ~DriveApiPartialFieldRequest();
     51 
     52   // Optional parameter.
     53   const std::string& fields() const { return fields_; }
     54   void set_fields(const std::string& fields) { fields_ = fields; }
     55 
     56  protected:
     57   // UrlFetchRequestBase overrides.
     58   virtual GURL GetURL() const OVERRIDE;
     59 
     60   // Derived classes should override GetURLInternal instead of GetURL()
     61   // directly.
     62   virtual GURL GetURLInternal() const = 0;
     63 
     64  private:
     65   std::string fields_;
     66 
     67   DISALLOW_COPY_AND_ASSIGN(DriveApiPartialFieldRequest);
     68 };
     69 
     70 //============================ DriveApiDataRequest ===========================
     71 
     72 // The base class of Drive API related requests that receive a JSON response
     73 // representing |DataType|.
     74 template<class DataType>
     75 class DriveApiDataRequest : public DriveApiPartialFieldRequest {
     76  public:
     77   typedef base::Callback<void(GDataErrorCode error,
     78                               scoped_ptr<DataType> data)> Callback;
     79 
     80   // |callback| is called when the request finishes either by success or by
     81   // failure. On success, a JSON Value object is passed. It must not be null.
     82   DriveApiDataRequest(RequestSender* sender, const Callback& callback)
     83       : DriveApiPartialFieldRequest(sender),
     84         callback_(callback),
     85         weak_ptr_factory_(this) {
     86     DCHECK(!callback_.is_null());
     87   }
     88   virtual ~DriveApiDataRequest() {}
     89 
     90  protected:
     91   // UrlFetchRequestBase overrides.
     92   virtual void ProcessURLFetchResults(const net::URLFetcher* source) OVERRIDE {
     93     GDataErrorCode error = GetErrorCode();
     94     switch (error) {
     95       case HTTP_SUCCESS:
     96       case HTTP_CREATED:
     97         base::PostTaskAndReplyWithResult(
     98             blocking_task_runner(),
     99             FROM_HERE,
    100             base::Bind(&DriveApiDataRequest::Parse, response_writer()->data()),
    101             base::Bind(&DriveApiDataRequest::OnDataParsed,
    102                        weak_ptr_factory_.GetWeakPtr(), error));
    103         break;
    104       default:
    105         RunCallbackOnPrematureFailure(error);
    106         OnProcessURLFetchResultsComplete();
    107         break;
    108     }
    109   }
    110 
    111   virtual void RunCallbackOnPrematureFailure(GDataErrorCode error) OVERRIDE {
    112     callback_.Run(error, scoped_ptr<DataType>());
    113   }
    114 
    115  private:
    116   // Parses the |json| string by using DataType::CreateFrom.
    117   static scoped_ptr<DataType> Parse(const std::string& json) {
    118     scoped_ptr<base::Value> value = ParseJson(json);
    119     return value ? DataType::CreateFrom(*value) : scoped_ptr<DataType>();
    120   }
    121 
    122   // Receives the parsed result and invokes the callback.
    123   void OnDataParsed(GDataErrorCode error, scoped_ptr<DataType> value) {
    124     if (!value)
    125       error = GDATA_PARSE_ERROR;
    126     callback_.Run(error, value.Pass());
    127     OnProcessURLFetchResultsComplete();
    128   }
    129 
    130   const Callback callback_;
    131 
    132   // Note: This should remain the last member so it'll be destroyed and
    133   // invalidate its weak pointers before any other members are destroyed.
    134   base::WeakPtrFactory<DriveApiDataRequest> weak_ptr_factory_;
    135 
    136   DISALLOW_COPY_AND_ASSIGN(DriveApiDataRequest);
    137 };
    138 
    139 //=============================== FilesGetRequest =============================
    140 
    141 // This class performs the request for fetching a file.
    142 // This request is mapped to
    143 // https://developers.google.com/drive/v2/reference/files/get
    144 class FilesGetRequest : public DriveApiDataRequest<FileResource> {
    145  public:
    146   FilesGetRequest(RequestSender* sender,
    147                   const DriveApiUrlGenerator& url_generator,
    148                   const FileResourceCallback& callback);
    149   virtual ~FilesGetRequest();
    150 
    151   // Required parameter.
    152   const std::string& file_id() const { return file_id_; }
    153   void set_file_id(const std::string& file_id) { file_id_ = file_id; }
    154 
    155  protected:
    156   // Overridden from DriveApiDataRequest.
    157   virtual GURL GetURLInternal() const OVERRIDE;
    158 
    159  private:
    160   const DriveApiUrlGenerator url_generator_;
    161   std::string file_id_;
    162 
    163   DISALLOW_COPY_AND_ASSIGN(FilesGetRequest);
    164 };
    165 
    166 //============================ FilesAuthorizeRequest ===========================
    167 
    168 // This class performs request for authorizing an app to access a file.
    169 // This request is mapped to /drive/v2internal/file/authorize internal endpoint.
    170 class FilesAuthorizeRequest : public DriveApiDataRequest<FileResource> {
    171  public:
    172   FilesAuthorizeRequest(RequestSender* sender,
    173                         const DriveApiUrlGenerator& url_generator,
    174                         const FileResourceCallback& callback);
    175   virtual ~FilesAuthorizeRequest();
    176 
    177   // Required parameter.
    178   const std::string& file_id() const { return file_id_; }
    179   void set_file_id(const std::string& file_id) { file_id_ = file_id; }
    180   const std::string& app_id() const { return app_id_; }
    181   void set_app_id(const std::string& app_id) { app_id_ = app_id; }
    182 
    183  protected:
    184   // Overridden from GetDataRequest.
    185   virtual net::URLFetcher::RequestType GetRequestType() const OVERRIDE;
    186 
    187   // Overridden from DriveApiDataRequest.
    188   virtual GURL GetURLInternal() const OVERRIDE;
    189 
    190  private:
    191   const DriveApiUrlGenerator url_generator_;
    192   std::string file_id_;
    193   std::string app_id_;
    194 
    195   DISALLOW_COPY_AND_ASSIGN(FilesAuthorizeRequest);
    196 };
    197 
    198 //============================ FilesInsertRequest =============================
    199 
    200 // This class performs the request for creating a resource.
    201 // This request is mapped to
    202 // https://developers.google.com/drive/v2/reference/files/insert
    203 // See also https://developers.google.com/drive/manage-uploads and
    204 // https://developers.google.com/drive/folder
    205 class FilesInsertRequest : public DriveApiDataRequest<FileResource> {
    206  public:
    207   FilesInsertRequest(RequestSender* sender,
    208                      const DriveApiUrlGenerator& url_generator,
    209                      const FileResourceCallback& callback);
    210   virtual ~FilesInsertRequest();
    211 
    212   // Optional request body.
    213   const base::Time& last_viewed_by_me_date() const {
    214     return last_viewed_by_me_date_;
    215   }
    216   void set_last_viewed_by_me_date(const base::Time& last_viewed_by_me_date) {
    217     last_viewed_by_me_date_ = last_viewed_by_me_date;
    218   }
    219 
    220   const std::string& mime_type() const { return mime_type_; }
    221   void set_mime_type(const std::string& mime_type) {
    222     mime_type_ = mime_type;
    223   }
    224 
    225   const base::Time& modified_date() const { return modified_date_; }
    226   void set_modified_date(const base::Time& modified_date) {
    227     modified_date_ = modified_date;
    228   }
    229 
    230   const std::vector<std::string>& parents() const { return parents_; }
    231   void add_parent(const std::string& parent) { parents_.push_back(parent); }
    232 
    233   const std::string& title() const { return title_; }
    234   void set_title(const std::string& title) { title_ = title; }
    235 
    236  protected:
    237   // Overridden from GetDataRequest.
    238   virtual net::URLFetcher::RequestType GetRequestType() const OVERRIDE;
    239   virtual bool GetContentData(std::string* upload_content_type,
    240                               std::string* upload_content) OVERRIDE;
    241 
    242   // Overridden from DriveApiDataRequest.
    243   virtual GURL GetURLInternal() const OVERRIDE;
    244 
    245  private:
    246   const DriveApiUrlGenerator url_generator_;
    247 
    248   base::Time last_viewed_by_me_date_;
    249   std::string mime_type_;
    250   base::Time modified_date_;
    251   std::vector<std::string> parents_;
    252   std::string title_;
    253 
    254   DISALLOW_COPY_AND_ASSIGN(FilesInsertRequest);
    255 };
    256 
    257 //============================== FilesPatchRequest ============================
    258 
    259 // This class performs the request for patching file metadata.
    260 // This request is mapped to
    261 // https://developers.google.com/drive/v2/reference/files/patch
    262 class FilesPatchRequest : public DriveApiDataRequest<FileResource> {
    263  public:
    264   FilesPatchRequest(RequestSender* sender,
    265                     const DriveApiUrlGenerator& url_generator,
    266                     const FileResourceCallback& callback);
    267   virtual ~FilesPatchRequest();
    268 
    269   // Required parameter.
    270   const std::string& file_id() const { return file_id_; }
    271   void set_file_id(const std::string& file_id) { file_id_ = file_id; }
    272 
    273   // Optional parameter.
    274   bool set_modified_date() const { return set_modified_date_; }
    275   void set_set_modified_date(bool set_modified_date) {
    276     set_modified_date_ = set_modified_date;
    277   }
    278 
    279   bool update_viewed_date() const { return update_viewed_date_; }
    280   void set_update_viewed_date(bool update_viewed_date) {
    281     update_viewed_date_ = update_viewed_date;
    282   }
    283 
    284   // Optional request body.
    285   // Note: "Files: patch" accepts any "Files resource" data, but this class
    286   // only supports limited members of it for now. We can extend it upon
    287   // requirments.
    288   const std::string& title() const { return title_; }
    289   void set_title(const std::string& title) { title_ = title; }
    290 
    291   const base::Time& modified_date() const { return modified_date_; }
    292   void set_modified_date(const base::Time& modified_date) {
    293     modified_date_ = modified_date;
    294   }
    295 
    296   const base::Time& last_viewed_by_me_date() const {
    297     return last_viewed_by_me_date_;
    298   }
    299   void set_last_viewed_by_me_date(const base::Time& last_viewed_by_me_date) {
    300     last_viewed_by_me_date_ = last_viewed_by_me_date;
    301   }
    302 
    303   const std::vector<std::string>& parents() const { return parents_; }
    304   void add_parent(const std::string& parent) { parents_.push_back(parent); }
    305 
    306  protected:
    307   // Overridden from URLFetchRequestBase.
    308   virtual net::URLFetcher::RequestType GetRequestType() const OVERRIDE;
    309   virtual std::vector<std::string> GetExtraRequestHeaders() const OVERRIDE;
    310   virtual bool GetContentData(std::string* upload_content_type,
    311                               std::string* upload_content) OVERRIDE;
    312 
    313   // Overridden from DriveApiDataRequest.
    314   virtual GURL GetURLInternal() const OVERRIDE;
    315 
    316  private:
    317   const DriveApiUrlGenerator url_generator_;
    318 
    319   std::string file_id_;
    320   bool set_modified_date_;
    321   bool update_viewed_date_;
    322 
    323   std::string title_;
    324   base::Time modified_date_;
    325   base::Time last_viewed_by_me_date_;
    326   std::vector<std::string> parents_;
    327 
    328   DISALLOW_COPY_AND_ASSIGN(FilesPatchRequest);
    329 };
    330 
    331 //============================= FilesCopyRequest ==============================
    332 
    333 // This class performs the request for copying a resource.
    334 // This request is mapped to
    335 // https://developers.google.com/drive/v2/reference/files/copy
    336 class FilesCopyRequest : public DriveApiDataRequest<FileResource> {
    337  public:
    338   // Upon completion, |callback| will be called. |callback| must not be null.
    339   FilesCopyRequest(RequestSender* sender,
    340                    const DriveApiUrlGenerator& url_generator,
    341                    const FileResourceCallback& callback);
    342   virtual ~FilesCopyRequest();
    343 
    344   // Required parameter.
    345   const std::string& file_id() const { return file_id_; }
    346   void set_file_id(const std::string& file_id) { file_id_ = file_id; }
    347 
    348   // Optional request body.
    349   const std::vector<std::string>& parents() const { return parents_; }
    350   void add_parent(const std::string& parent) { parents_.push_back(parent); }
    351 
    352   const base::Time& modified_date() const { return modified_date_; }
    353   void set_modified_date(const base::Time& modified_date) {
    354     modified_date_ = modified_date;
    355   }
    356 
    357   const std::string& title() const { return title_; }
    358   void set_title(const std::string& title) { title_ = title; }
    359 
    360  protected:
    361   // Overridden from URLFetchRequestBase.
    362   virtual net::URLFetcher::RequestType GetRequestType() const OVERRIDE;
    363   virtual bool GetContentData(std::string* upload_content_type,
    364                               std::string* upload_content) OVERRIDE;
    365 
    366   // Overridden from DriveApiDataRequest.
    367   virtual GURL GetURLInternal() const OVERRIDE;
    368 
    369  private:
    370   const DriveApiUrlGenerator url_generator_;
    371 
    372   std::string file_id_;
    373   base::Time modified_date_;
    374   std::vector<std::string> parents_;
    375   std::string title_;
    376 
    377   DISALLOW_COPY_AND_ASSIGN(FilesCopyRequest);
    378 };
    379 
    380 //============================= FilesListRequest =============================
    381 
    382 // This class performs the request for fetching FileList.
    383 // The result may contain only first part of the result. The remaining result
    384 // should be able to be fetched by ContinueGetFileListRequest defined below,
    385 // or by FilesListRequest with setting page token.
    386 // This request is mapped to
    387 // https://developers.google.com/drive/v2/reference/files/list
    388 class FilesListRequest : public DriveApiDataRequest<FileList> {
    389  public:
    390   FilesListRequest(RequestSender* sender,
    391                    const DriveApiUrlGenerator& url_generator,
    392                    const FileListCallback& callback);
    393   virtual ~FilesListRequest();
    394 
    395   // Optional parameter
    396   int max_results() const { return max_results_; }
    397   void set_max_results(int max_results) { max_results_ = max_results; }
    398 
    399   const std::string& page_token() const { return page_token_; }
    400   void set_page_token(const std::string& page_token) {
    401     page_token_ = page_token;
    402   }
    403 
    404   const std::string& q() const { return q_; }
    405   void set_q(const std::string& q) { q_ = q; }
    406 
    407  protected:
    408   // Overridden from DriveApiDataRequest.
    409   virtual GURL GetURLInternal() const OVERRIDE;
    410 
    411  private:
    412   const DriveApiUrlGenerator url_generator_;
    413   int max_results_;
    414   std::string page_token_;
    415   std::string q_;
    416 
    417   DISALLOW_COPY_AND_ASSIGN(FilesListRequest);
    418 };
    419 
    420 //========================= FilesListNextPageRequest ==========================
    421 
    422 // There are two ways to obtain next pages of "Files: list" result (if paged).
    423 // 1) Set pageToken and all params used for the initial request.
    424 // 2) Use URL in the nextLink field in the previous response.
    425 // This class implements 2)'s request.
    426 class FilesListNextPageRequest : public DriveApiDataRequest<FileList> {
    427  public:
    428   FilesListNextPageRequest(RequestSender* sender,
    429                            const FileListCallback& callback);
    430   virtual ~FilesListNextPageRequest();
    431 
    432   const GURL& next_link() const { return next_link_; }
    433   void set_next_link(const GURL& next_link) { next_link_ = next_link; }
    434 
    435  protected:
    436   // Overridden from DriveApiDataRequest.
    437   virtual GURL GetURLInternal() const OVERRIDE;
    438 
    439  private:
    440   GURL next_link_;
    441 
    442   DISALLOW_COPY_AND_ASSIGN(FilesListNextPageRequest);
    443 };
    444 
    445 //============================= FilesDeleteRequest =============================
    446 
    447 // This class performs the request for deleting a resource.
    448 // This request is mapped to
    449 // https://developers.google.com/drive/v2/reference/files/delete
    450 class FilesDeleteRequest : public EntryActionRequest {
    451  public:
    452   FilesDeleteRequest(RequestSender* sender,
    453                      const DriveApiUrlGenerator& url_generator,
    454                      const EntryActionCallback& callback);
    455   virtual ~FilesDeleteRequest();
    456 
    457   // Required parameter.
    458   const std::string& file_id() const { return file_id_; }
    459   void set_file_id(const std::string& file_id) { file_id_ = file_id; }
    460   void set_etag(const std::string& etag) { etag_ = etag; }
    461 
    462  protected:
    463   // Overridden from UrlFetchRequestBase.
    464   virtual net::URLFetcher::RequestType GetRequestType() const OVERRIDE;
    465   virtual GURL GetURL() const OVERRIDE;
    466   virtual std::vector<std::string> GetExtraRequestHeaders() const OVERRIDE;
    467 
    468  private:
    469   const DriveApiUrlGenerator url_generator_;
    470   std::string file_id_;
    471   std::string etag_;
    472 
    473   DISALLOW_COPY_AND_ASSIGN(FilesDeleteRequest);
    474 };
    475 
    476 //============================= FilesTrashRequest ==============================
    477 
    478 // This class performs the request for trashing a resource.
    479 // This request is mapped to
    480 // https://developers.google.com/drive/v2/reference/files/trash
    481 class FilesTrashRequest : public DriveApiDataRequest<FileResource> {
    482  public:
    483   FilesTrashRequest(RequestSender* sender,
    484                     const DriveApiUrlGenerator& url_generator,
    485                     const FileResourceCallback& callback);
    486   virtual ~FilesTrashRequest();
    487 
    488   // Required parameter.
    489   const std::string& file_id() const { return file_id_; }
    490   void set_file_id(const std::string& file_id) { file_id_ = file_id; }
    491 
    492  protected:
    493   // Overridden from UrlFetchRequestBase.
    494   virtual net::URLFetcher::RequestType GetRequestType() const OVERRIDE;
    495 
    496   // Overridden from DriveApiDataRequest.
    497   virtual GURL GetURLInternal() const OVERRIDE;
    498 
    499  private:
    500   const DriveApiUrlGenerator url_generator_;
    501   std::string file_id_;
    502 
    503   DISALLOW_COPY_AND_ASSIGN(FilesTrashRequest);
    504 };
    505 
    506 //============================== AboutGetRequest =============================
    507 
    508 // This class performs the request for fetching About data.
    509 // This request is mapped to
    510 // https://developers.google.com/drive/v2/reference/about/get
    511 class AboutGetRequest : public DriveApiDataRequest<AboutResource> {
    512  public:
    513   AboutGetRequest(RequestSender* sender,
    514                   const DriveApiUrlGenerator& url_generator,
    515                   const AboutResourceCallback& callback);
    516   virtual ~AboutGetRequest();
    517 
    518  protected:
    519   // Overridden from DriveApiDataRequest.
    520   virtual GURL GetURLInternal() const OVERRIDE;
    521 
    522  private:
    523   const DriveApiUrlGenerator url_generator_;
    524 
    525   DISALLOW_COPY_AND_ASSIGN(AboutGetRequest);
    526 };
    527 
    528 //============================ ChangesListRequest ============================
    529 
    530 // This class performs the request for fetching ChangeList.
    531 // The result may contain only first part of the result. The remaining result
    532 // should be able to be fetched by ContinueGetFileListRequest defined below.
    533 // or by ChangesListRequest with setting page token.
    534 // This request is mapped to
    535 // https://developers.google.com/drive/v2/reference/changes/list
    536 class ChangesListRequest : public DriveApiDataRequest<ChangeList> {
    537  public:
    538   ChangesListRequest(RequestSender* sender,
    539                      const DriveApiUrlGenerator& url_generator,
    540                      const ChangeListCallback& callback);
    541   virtual ~ChangesListRequest();
    542 
    543   // Optional parameter
    544   bool include_deleted() const { return include_deleted_; }
    545   void set_include_deleted(bool include_deleted) {
    546     include_deleted_ = include_deleted;
    547   }
    548 
    549   int max_results() const { return max_results_; }
    550   void set_max_results(int max_results) { max_results_ = max_results; }
    551 
    552   const std::string& page_token() const { return page_token_; }
    553   void set_page_token(const std::string& page_token) {
    554     page_token_ = page_token;
    555   }
    556 
    557   int64 start_change_id() const { return start_change_id_; }
    558   void set_start_change_id(int64 start_change_id) {
    559     start_change_id_ = start_change_id;
    560   }
    561 
    562  protected:
    563   // Overridden from DriveApiDataRequest.
    564   virtual GURL GetURLInternal() const OVERRIDE;
    565 
    566  private:
    567   const DriveApiUrlGenerator url_generator_;
    568   bool include_deleted_;
    569   int max_results_;
    570   std::string page_token_;
    571   int64 start_change_id_;
    572 
    573   DISALLOW_COPY_AND_ASSIGN(ChangesListRequest);
    574 };
    575 
    576 //======================== ChangesListNextPageRequest =========================
    577 
    578 // There are two ways to obtain next pages of "Changes: list" result (if paged).
    579 // 1) Set pageToken and all params used for the initial request.
    580 // 2) Use URL in the nextLink field in the previous response.
    581 // This class implements 2)'s request.
    582 class ChangesListNextPageRequest : public DriveApiDataRequest<ChangeList> {
    583  public:
    584   ChangesListNextPageRequest(RequestSender* sender,
    585                              const ChangeListCallback& callback);
    586   virtual ~ChangesListNextPageRequest();
    587 
    588   const GURL& next_link() const { return next_link_; }
    589   void set_next_link(const GURL& next_link) { next_link_ = next_link; }
    590 
    591  protected:
    592   // Overridden from DriveApiDataRequest.
    593   virtual GURL GetURLInternal() const OVERRIDE;
    594 
    595  private:
    596   GURL next_link_;
    597 
    598   DISALLOW_COPY_AND_ASSIGN(ChangesListNextPageRequest);
    599 };
    600 
    601 //============================= AppsListRequest ============================
    602 
    603 // This class performs the request for fetching AppList.
    604 // This request is mapped to
    605 // https://developers.google.com/drive/v2/reference/apps/list
    606 class AppsListRequest : public DriveApiDataRequest<AppList> {
    607  public:
    608   AppsListRequest(RequestSender* sender,
    609                   const DriveApiUrlGenerator& url_generator,
    610                   bool use_internal_endpoint,
    611                   const AppListCallback& callback);
    612   virtual ~AppsListRequest();
    613 
    614  protected:
    615   // Overridden from DriveApiDataRequest.
    616   virtual GURL GetURLInternal() const OVERRIDE;
    617 
    618  private:
    619   const DriveApiUrlGenerator url_generator_;
    620   bool use_internal_endpoint_;
    621 
    622   DISALLOW_COPY_AND_ASSIGN(AppsListRequest);
    623 };
    624 
    625 //============================= AppsDeleteRequest ==============================
    626 
    627 // This class performs the request for deleting a Drive app.
    628 // This request is mapped to
    629 // https://developers.google.com/drive/v2/reference/files/trash
    630 class AppsDeleteRequest : public EntryActionRequest {
    631  public:
    632   AppsDeleteRequest(RequestSender* sender,
    633                     const DriveApiUrlGenerator& url_generator,
    634                     const EntryActionCallback& callback);
    635   virtual ~AppsDeleteRequest();
    636 
    637   // Required parameter.
    638   const std::string& app_id() const { return app_id_; }
    639   void set_app_id(const std::string& app_id) { app_id_ = app_id; }
    640 
    641  protected:
    642   // Overridden from UrlFetchRequestBase.
    643   virtual net::URLFetcher::RequestType GetRequestType() const OVERRIDE;
    644   virtual GURL GetURL() const OVERRIDE;
    645 
    646  private:
    647   const DriveApiUrlGenerator url_generator_;
    648   std::string app_id_;
    649 
    650   DISALLOW_COPY_AND_ASSIGN(AppsDeleteRequest);
    651 };
    652 
    653 //========================== ChildrenInsertRequest ============================
    654 
    655 // This class performs the request for inserting a resource to a directory.
    656 // This request is mapped to
    657 // https://developers.google.com/drive/v2/reference/children/insert
    658 class ChildrenInsertRequest : public EntryActionRequest {
    659  public:
    660   ChildrenInsertRequest(RequestSender* sender,
    661                         const DriveApiUrlGenerator& url_generator,
    662                         const EntryActionCallback& callback);
    663   virtual ~ChildrenInsertRequest();
    664 
    665   // Required parameter.
    666   const std::string& folder_id() const { return folder_id_; }
    667   void set_folder_id(const std::string& folder_id) {
    668     folder_id_ = folder_id;
    669   }
    670 
    671   // Required body.
    672   const std::string& id() const { return id_; }
    673   void set_id(const std::string& id) { id_ = id; }
    674 
    675  protected:
    676   // UrlFetchRequestBase overrides.
    677   virtual net::URLFetcher::RequestType GetRequestType() const OVERRIDE;
    678   virtual GURL GetURL() const OVERRIDE;
    679   virtual bool GetContentData(std::string* upload_content_type,
    680                               std::string* upload_content) OVERRIDE;
    681 
    682  private:
    683   const DriveApiUrlGenerator url_generator_;
    684   std::string folder_id_;
    685   std::string id_;
    686 
    687   DISALLOW_COPY_AND_ASSIGN(ChildrenInsertRequest);
    688 };
    689 
    690 //========================== ChildrenDeleteRequest ============================
    691 
    692 // This class performs the request for removing a resource from a directory.
    693 // This request is mapped to
    694 // https://developers.google.com/drive/v2/reference/children/delete
    695 class ChildrenDeleteRequest : public EntryActionRequest {
    696  public:
    697   // |callback| must not be null.
    698   ChildrenDeleteRequest(RequestSender* sender,
    699                         const DriveApiUrlGenerator& url_generator,
    700                         const EntryActionCallback& callback);
    701   virtual ~ChildrenDeleteRequest();
    702 
    703   // Required parameter.
    704   const std::string& child_id() const { return child_id_; }
    705   void set_child_id(const std::string& child_id) {
    706     child_id_ = child_id;
    707   }
    708 
    709   const std::string& folder_id() const { return folder_id_; }
    710   void set_folder_id(const std::string& folder_id) {
    711     folder_id_ = folder_id;
    712   }
    713 
    714  protected:
    715   // UrlFetchRequestBase overrides.
    716   virtual net::URLFetcher::RequestType GetRequestType() const OVERRIDE;
    717   virtual GURL GetURL() const OVERRIDE;
    718 
    719  private:
    720   const DriveApiUrlGenerator url_generator_;
    721   std::string child_id_;
    722   std::string folder_id_;
    723 
    724   DISALLOW_COPY_AND_ASSIGN(ChildrenDeleteRequest);
    725 };
    726 
    727 //======================= InitiateUploadNewFileRequest =======================
    728 
    729 // This class performs the request for initiating the upload of a new file.
    730 class InitiateUploadNewFileRequest : public InitiateUploadRequestBase {
    731  public:
    732   // |parent_resource_id| should be the resource id of the parent directory.
    733   // |title| should be set.
    734   // See also the comments of InitiateUploadRequestBase for more details
    735   // about the other parameters.
    736   InitiateUploadNewFileRequest(RequestSender* sender,
    737                                const DriveApiUrlGenerator& url_generator,
    738                                const std::string& content_type,
    739                                int64 content_length,
    740                                const std::string& parent_resource_id,
    741                                const std::string& title,
    742                                const InitiateUploadCallback& callback);
    743   virtual ~InitiateUploadNewFileRequest();
    744 
    745   // Optional parameters.
    746   const base::Time& modified_date() const { return modified_date_; }
    747   void set_modified_date(const base::Time& modified_date) {
    748     modified_date_ = modified_date;
    749   }
    750   const base::Time& last_viewed_by_me_date() const {
    751     return last_viewed_by_me_date_;
    752   }
    753   void set_last_viewed_by_me_date(const base::Time& last_viewed_by_me_date) {
    754     last_viewed_by_me_date_ = last_viewed_by_me_date;
    755   }
    756 
    757  protected:
    758   // UrlFetchRequestBase overrides.
    759   virtual GURL GetURL() const OVERRIDE;
    760   virtual net::URLFetcher::RequestType GetRequestType() const OVERRIDE;
    761   virtual bool GetContentData(std::string* upload_content_type,
    762                               std::string* upload_content) OVERRIDE;
    763 
    764  private:
    765   const DriveApiUrlGenerator url_generator_;
    766   const std::string parent_resource_id_;
    767   const std::string title_;
    768 
    769   base::Time modified_date_;
    770   base::Time last_viewed_by_me_date_;
    771 
    772   DISALLOW_COPY_AND_ASSIGN(InitiateUploadNewFileRequest);
    773 };
    774 
    775 //==================== InitiateUploadExistingFileRequest =====================
    776 
    777 // This class performs the request for initiating the upload of an existing
    778 // file.
    779 class InitiateUploadExistingFileRequest : public InitiateUploadRequestBase {
    780  public:
    781   // |upload_url| should be the upload_url() of the file
    782   //    (resumable-create-media URL)
    783   // |etag| should be set if it is available to detect the upload confliction.
    784   // See also the comments of InitiateUploadRequestBase for more details
    785   // about the other parameters.
    786   InitiateUploadExistingFileRequest(RequestSender* sender,
    787                                     const DriveApiUrlGenerator& url_generator,
    788                                     const std::string& content_type,
    789                                     int64 content_length,
    790                                     const std::string& resource_id,
    791                                     const std::string& etag,
    792                                     const InitiateUploadCallback& callback);
    793   virtual ~InitiateUploadExistingFileRequest();
    794 
    795 
    796   // Optional parameters.
    797   const std::string& parent_resource_id() const { return parent_resource_id_; }
    798   void set_parent_resource_id(const std::string& parent_resource_id) {
    799     parent_resource_id_ = parent_resource_id;
    800   }
    801   const std::string& title() const { return title_; }
    802   void set_title(const std::string& title) { title_ = title; }
    803   const base::Time& modified_date() const { return modified_date_; }
    804   void set_modified_date(const base::Time& modified_date) {
    805     modified_date_ = modified_date;
    806   }
    807   const base::Time& last_viewed_by_me_date() const {
    808     return last_viewed_by_me_date_;
    809   }
    810   void set_last_viewed_by_me_date(const base::Time& last_viewed_by_me_date) {
    811     last_viewed_by_me_date_ = last_viewed_by_me_date;
    812   }
    813 
    814  protected:
    815   // UrlFetchRequestBase overrides.
    816   virtual GURL GetURL() const OVERRIDE;
    817   virtual net::URLFetcher::RequestType GetRequestType() const OVERRIDE;
    818   virtual std::vector<std::string> GetExtraRequestHeaders() const OVERRIDE;
    819   virtual bool GetContentData(std::string* upload_content_type,
    820                               std::string* upload_content) OVERRIDE;
    821 
    822  private:
    823   const DriveApiUrlGenerator url_generator_;
    824   const std::string resource_id_;
    825   const std::string etag_;
    826 
    827   std::string parent_resource_id_;
    828   std::string title_;
    829   base::Time modified_date_;
    830   base::Time last_viewed_by_me_date_;
    831 
    832   DISALLOW_COPY_AND_ASSIGN(InitiateUploadExistingFileRequest);
    833 };
    834 
    835 // Callback used for ResumeUpload() and GetUploadStatus().
    836 typedef base::Callback<void(
    837     const UploadRangeResponse& response,
    838     scoped_ptr<FileResource> new_resource)> UploadRangeCallback;
    839 
    840 //============================ ResumeUploadRequest ===========================
    841 
    842 // Performs the request for resuming the upload of a file.
    843 class ResumeUploadRequest : public ResumeUploadRequestBase {
    844  public:
    845   // See also ResumeUploadRequestBase's comment for parameters meaning.
    846   // |callback| must not be null. |progress_callback| may be null.
    847   ResumeUploadRequest(RequestSender* sender,
    848                       const GURL& upload_location,
    849                       int64 start_position,
    850                       int64 end_position,
    851                       int64 content_length,
    852                       const std::string& content_type,
    853                       const base::FilePath& local_file_path,
    854                       const UploadRangeCallback& callback,
    855                       const ProgressCallback& progress_callback);
    856   virtual ~ResumeUploadRequest();
    857 
    858  protected:
    859   // UploadRangeRequestBase overrides.
    860   virtual void OnRangeRequestComplete(
    861       const UploadRangeResponse& response,
    862       scoped_ptr<base::Value> value) OVERRIDE;
    863   // content::UrlFetcherDelegate overrides.
    864   virtual void OnURLFetchUploadProgress(const net::URLFetcher* source,
    865                                         int64 current, int64 total) OVERRIDE;
    866 
    867  private:
    868   const UploadRangeCallback callback_;
    869   const ProgressCallback progress_callback_;
    870 
    871   DISALLOW_COPY_AND_ASSIGN(ResumeUploadRequest);
    872 };
    873 
    874 //========================== GetUploadStatusRequest ==========================
    875 
    876 // Performs the request to fetch the current upload status of a file.
    877 class GetUploadStatusRequest : public GetUploadStatusRequestBase {
    878  public:
    879   // See also GetUploadStatusRequestBase's comment for parameters meaning.
    880   // |callback| must not be null.
    881   GetUploadStatusRequest(RequestSender* sender,
    882                          const GURL& upload_url,
    883                          int64 content_length,
    884                          const UploadRangeCallback& callback);
    885   virtual ~GetUploadStatusRequest();
    886 
    887  protected:
    888   // UploadRangeRequestBase overrides.
    889   virtual void OnRangeRequestComplete(
    890       const UploadRangeResponse& response,
    891       scoped_ptr<base::Value> value) OVERRIDE;
    892 
    893  private:
    894   const UploadRangeCallback callback_;
    895 
    896   DISALLOW_COPY_AND_ASSIGN(GetUploadStatusRequest);
    897 };
    898 
    899 //========================== DownloadFileRequest ==========================
    900 
    901 // This class performs the request for downloading of a specified file.
    902 class DownloadFileRequest : public DownloadFileRequestBase {
    903  public:
    904   // See also DownloadFileRequestBase's comment for parameters meaning.
    905   DownloadFileRequest(RequestSender* sender,
    906                       const DriveApiUrlGenerator& url_generator,
    907                       const std::string& resource_id,
    908                       const base::FilePath& output_file_path,
    909                       const DownloadActionCallback& download_action_callback,
    910                       const GetContentCallback& get_content_callback,
    911                       const ProgressCallback& progress_callback);
    912   virtual ~DownloadFileRequest();
    913 
    914   DISALLOW_COPY_AND_ASSIGN(DownloadFileRequest);
    915 };
    916 
    917 //========================== PermissionsInsertRequest ==========================
    918 
    919 // Enumeration type for specifying type of permissions.
    920 enum PermissionType {
    921   PERMISSION_TYPE_ANYONE,
    922   PERMISSION_TYPE_DOMAIN,
    923   PERMISSION_TYPE_GROUP,
    924   PERMISSION_TYPE_USER,
    925 };
    926 
    927 // Enumeration type for specifying the role of permissions.
    928 enum PermissionRole {
    929   PERMISSION_ROLE_OWNER,
    930   PERMISSION_ROLE_READER,
    931   PERMISSION_ROLE_WRITER,
    932   PERMISSION_ROLE_COMMENTER,
    933 };
    934 
    935 // This class performs the request for adding permission on a specified file.
    936 class PermissionsInsertRequest : public EntryActionRequest {
    937  public:
    938   // See https://developers.google.com/drive/v2/reference/permissions/insert.
    939   PermissionsInsertRequest(RequestSender* sender,
    940                            const DriveApiUrlGenerator& url_generator,
    941                            const EntryActionCallback& callback);
    942   virtual ~PermissionsInsertRequest();
    943 
    944   void set_id(const std::string& id) { id_ = id; }
    945   void set_type(PermissionType type) { type_ = type; }
    946   void set_role(PermissionRole role) { role_ = role; }
    947   void set_value(const std::string& value) { value_ = value; }
    948 
    949   // UrlFetchRequestBase overrides.
    950   virtual GURL GetURL() const OVERRIDE;
    951   virtual net::URLFetcher::RequestType GetRequestType() const OVERRIDE;
    952   virtual bool GetContentData(std::string* upload_content_type,
    953                               std::string* upload_content) OVERRIDE;
    954 
    955  private:
    956   const DriveApiUrlGenerator url_generator_;
    957   std::string id_;
    958   PermissionType type_;
    959   PermissionRole role_;
    960   std::string value_;
    961 
    962   DISALLOW_COPY_AND_ASSIGN(PermissionsInsertRequest);
    963 };
    964 
    965 }  // namespace drive
    966 }  // namespace google_apis
    967 
    968 #endif  // GOOGLE_APIS_DRIVE_DRIVE_API_REQUESTS_H_
    969