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 CHROME_BROWSER_DRIVE_FAKE_DRIVE_SERVICE_H_
      6 #define CHROME_BROWSER_DRIVE_FAKE_DRIVE_SERVICE_H_
      7 
      8 #include "base/files/file_path.h"
      9 #include "base/values.h"
     10 #include "chrome/browser/drive/drive_service_interface.h"
     11 #include "google_apis/drive/auth_service_interface.h"
     12 
     13 namespace drive {
     14 
     15 // This class implements a fake DriveService which acts like a real Drive
     16 // service. The fake service works as follows:
     17 //
     18 // 1) Load JSON files and construct the in-memory resource list.
     19 // 2) Return valid responses based on the the in-memory resource list.
     20 // 3) Update the in-memory resource list by requests like DeleteResource().
     21 class FakeDriveService : public DriveServiceInterface {
     22  public:
     23   FakeDriveService();
     24   virtual ~FakeDriveService();
     25 
     26   // Loads the resource list for WAPI. Returns true on success.
     27   bool LoadResourceListForWapi(const std::string& relative_path);
     28 
     29   // Loads the account metadata for WAPI. Returns true on success.  Also adds
     30   // the largest changestamp in the account metadata to the existing
     31   // entries. The changestamp information will be used to generate change
     32   // lists in GetResourceList() when non-zero |start_changestamp| is
     33   // specified.
     34   bool LoadAccountMetadataForWapi(const std::string& relative_path);
     35 
     36   // Loads the app list for Drive API. Returns true on success.
     37   bool LoadAppListForDriveApi(const std::string& relative_path);
     38 
     39   // Changes the offline state. All functions fail with GDATA_NO_CONNECTION
     40   // when offline. By default the offline state is false.
     41   void set_offline(bool offline) { offline_ = offline; }
     42 
     43   // GetAllResourceList never returns result when this is set to true.
     44   // Used to emulate the real server's slowness.
     45   void set_never_return_all_resource_list(bool value) {
     46     never_return_all_resource_list_ = value;
     47   }
     48 
     49   // Changes the default max results returned from GetResourceList().
     50   // By default, it's set to 0, which is unlimited.
     51   void set_default_max_results(int default_max_results) {
     52     default_max_results_ = default_max_results;
     53   }
     54 
     55   // Sets the url to the test server to be used as a base for generated share
     56   // urls to the share dialog.
     57   void set_share_url_base(const GURL& share_url_base) {
     58     share_url_base_ = share_url_base;
     59   }
     60 
     61   // Changes the quota fields returned from GetAboutResource().
     62   void SetQuotaValue(int64 used, int64 total);
     63 
     64   // Returns the largest changestamp, which starts from 0 by default. See
     65   // also comments at LoadAccountMetadataForWapi().
     66   int64 largest_changestamp() const { return largest_changestamp_; }
     67 
     68   // Returns the number of times the resource list is successfully loaded by
     69   // GetAllResourceList().
     70   int resource_list_load_count() const { return resource_list_load_count_; }
     71 
     72   // Returns the number of times the resource list is successfully loaded by
     73   // GetChangeList().
     74   int change_list_load_count() const { return change_list_load_count_; }
     75 
     76   // Returns the number of times the resource list is successfully loaded by
     77   // GetResourceListInDirectory().
     78   int directory_load_count() const { return directory_load_count_; }
     79 
     80   // Returns the number of times the about resource is successfully loaded
     81   // by GetAboutResource().
     82   int about_resource_load_count() const {
     83     return about_resource_load_count_;
     84   }
     85 
     86   // Returns the number of times the app list is successfully loaded by
     87   // GetAppList().
     88   int app_list_load_count() const { return app_list_load_count_; }
     89 
     90   // Returns the number of times GetAllResourceList are blocked due to
     91   // set_never_return_all_resource_list().
     92   int blocked_resource_list_load_count() const {
     93     return blocked_resource_list_load_count_;
     94   }
     95 
     96   // Returns the file path whose request is cancelled just before this method
     97   // invocation.
     98   const base::FilePath& last_cancelled_file() const {
     99     return last_cancelled_file_;
    100   }
    101 
    102   // Returns the (fake) URL for the link.
    103   static GURL GetFakeLinkUrl(const std::string& resource_id);
    104 
    105   // DriveServiceInterface Overrides
    106   virtual void Initialize(const std::string& account_id) OVERRIDE;
    107   virtual void AddObserver(DriveServiceObserver* observer) OVERRIDE;
    108   virtual void RemoveObserver(DriveServiceObserver* observer) OVERRIDE;
    109   virtual bool CanSendRequest() const OVERRIDE;
    110   virtual ResourceIdCanonicalizer GetResourceIdCanonicalizer() const OVERRIDE;
    111   virtual std::string GetRootResourceId() const OVERRIDE;
    112   virtual bool HasAccessToken() const OVERRIDE;
    113   virtual void RequestAccessToken(
    114       const google_apis::AuthStatusCallback& callback) OVERRIDE;
    115   virtual bool HasRefreshToken() const OVERRIDE;
    116   virtual void ClearAccessToken() OVERRIDE;
    117   virtual void ClearRefreshToken() OVERRIDE;
    118   virtual google_apis::CancelCallback GetAllResourceList(
    119       const google_apis::GetResourceListCallback& callback) OVERRIDE;
    120   virtual google_apis::CancelCallback GetResourceListInDirectory(
    121       const std::string& directory_resource_id,
    122       const google_apis::GetResourceListCallback& callback) OVERRIDE;
    123   // See the comment for EntryMatchWidthQuery() in .cc file for details about
    124   // the supported search query types.
    125   virtual google_apis::CancelCallback Search(
    126       const std::string& search_query,
    127       const google_apis::GetResourceListCallback& callback) OVERRIDE;
    128   virtual google_apis::CancelCallback SearchByTitle(
    129       const std::string& title,
    130       const std::string& directory_resource_id,
    131       const google_apis::GetResourceListCallback& callback) OVERRIDE;
    132   virtual google_apis::CancelCallback GetChangeList(
    133       int64 start_changestamp,
    134       const google_apis::GetResourceListCallback& callback) OVERRIDE;
    135   virtual google_apis::CancelCallback GetRemainingChangeList(
    136       const GURL& next_link,
    137       const google_apis::GetResourceListCallback& callback) OVERRIDE;
    138   virtual google_apis::CancelCallback GetRemainingFileList(
    139       const GURL& next_link,
    140       const google_apis::GetResourceListCallback& callback) OVERRIDE;
    141   virtual google_apis::CancelCallback GetResourceEntry(
    142       const std::string& resource_id,
    143       const google_apis::GetResourceEntryCallback& callback) OVERRIDE;
    144   virtual google_apis::CancelCallback GetShareUrl(
    145       const std::string& resource_id,
    146       const GURL& embed_origin,
    147       const google_apis::GetShareUrlCallback& callback) OVERRIDE;
    148   virtual google_apis::CancelCallback GetAboutResource(
    149       const google_apis::AboutResourceCallback& callback) OVERRIDE;
    150   virtual google_apis::CancelCallback GetAppList(
    151       const google_apis::AppListCallback& callback) OVERRIDE;
    152   virtual google_apis::CancelCallback DeleteResource(
    153       const std::string& resource_id,
    154       const std::string& etag,
    155       const google_apis::EntryActionCallback& callback) OVERRIDE;
    156   virtual google_apis::CancelCallback TrashResource(
    157       const std::string& resource_id,
    158       const google_apis::EntryActionCallback& callback) OVERRIDE;
    159   virtual google_apis::CancelCallback DownloadFile(
    160       const base::FilePath& local_cache_path,
    161       const std::string& resource_id,
    162       const google_apis::DownloadActionCallback& download_action_callback,
    163       const google_apis::GetContentCallback& get_content_callback,
    164       const google_apis::ProgressCallback& progress_callback) OVERRIDE;
    165   virtual google_apis::CancelCallback CopyResource(
    166       const std::string& resource_id,
    167       const std::string& parent_resource_id,
    168       const std::string& new_title,
    169       const base::Time& last_modified,
    170       const google_apis::GetResourceEntryCallback& callback) OVERRIDE;
    171   virtual google_apis::CancelCallback UpdateResource(
    172       const std::string& resource_id,
    173       const std::string& parent_resource_id,
    174       const std::string& new_title,
    175       const base::Time& last_modified,
    176       const base::Time& last_viewed_by_me,
    177       const google_apis::GetResourceEntryCallback& callback) OVERRIDE;
    178   virtual google_apis::CancelCallback RenameResource(
    179       const std::string& resource_id,
    180       const std::string& new_title,
    181       const google_apis::EntryActionCallback& callback) OVERRIDE;
    182   virtual google_apis::CancelCallback AddResourceToDirectory(
    183       const std::string& parent_resource_id,
    184       const std::string& resource_id,
    185       const google_apis::EntryActionCallback& callback) OVERRIDE;
    186   virtual google_apis::CancelCallback RemoveResourceFromDirectory(
    187       const std::string& parent_resource_id,
    188       const std::string& resource_id,
    189       const google_apis::EntryActionCallback& callback) OVERRIDE;
    190   virtual google_apis::CancelCallback AddNewDirectory(
    191       const std::string& parent_resource_id,
    192       const std::string& directory_title,
    193       const google_apis::GetResourceEntryCallback& callback) OVERRIDE;
    194   virtual google_apis::CancelCallback InitiateUploadNewFile(
    195       const std::string& content_type,
    196       int64 content_length,
    197       const std::string& parent_resource_id,
    198       const std::string& title,
    199       const google_apis::InitiateUploadCallback& callback) OVERRIDE;
    200   virtual google_apis::CancelCallback InitiateUploadExistingFile(
    201       const std::string& content_type,
    202       int64 content_length,
    203       const std::string& resource_id,
    204       const std::string& etag,
    205       const google_apis::InitiateUploadCallback& callback) OVERRIDE;
    206   virtual google_apis::CancelCallback ResumeUpload(
    207       const GURL& upload_url,
    208       int64 start_position,
    209       int64 end_position,
    210       int64 content_length,
    211       const std::string& content_type,
    212       const base::FilePath& local_file_path,
    213       const google_apis::UploadRangeCallback& callback,
    214       const google_apis::ProgressCallback& progress_callback) OVERRIDE;
    215   virtual google_apis::CancelCallback GetUploadStatus(
    216       const GURL& upload_url,
    217       int64 content_length,
    218       const google_apis::UploadRangeCallback& callback) OVERRIDE;
    219   virtual google_apis::CancelCallback AuthorizeApp(
    220       const std::string& resource_id,
    221       const std::string& app_id,
    222       const google_apis::AuthorizeAppCallback& callback) OVERRIDE;
    223   virtual google_apis::CancelCallback GetResourceListInDirectoryByWapi(
    224       const std::string& directory_resource_id,
    225       const google_apis::GetResourceListCallback& callback) OVERRIDE;
    226   virtual google_apis::CancelCallback GetRemainingResourceList(
    227       const GURL& next_link,
    228       const google_apis::GetResourceListCallback& callback) OVERRIDE;
    229 
    230   // Adds a new file with the given parameters. On success, returns
    231   // HTTP_CREATED with the parsed entry.
    232   // |callback| must not be null.
    233   void AddNewFile(const std::string& content_type,
    234                   const std::string& content_data,
    235                   const std::string& parent_resource_id,
    236                   const std::string& title,
    237                   bool shared_with_me,
    238                   const google_apis::GetResourceEntryCallback& callback);
    239 
    240   // Sets the last modified time for an entry specified by |resource_id|.
    241   // On success, returns HTTP_SUCCESS with the parsed entry.
    242   // |callback| must not be null.
    243   void SetLastModifiedTime(
    244       const std::string& resource_id,
    245       const base::Time& last_modified_time,
    246       const google_apis::GetResourceEntryCallback& callback);
    247 
    248  private:
    249   struct UploadSession;
    250 
    251   // Returns a pointer to the entry that matches |resource_id|, or NULL if
    252   // not found.
    253   base::DictionaryValue* FindEntryByResourceId(const std::string& resource_id);
    254 
    255   // Returns a pointer to the entry that matches |content_url|, or NULL if
    256   // not found.
    257   base::DictionaryValue* FindEntryByContentUrl(const GURL& content_url);
    258 
    259   // Returns a new resource ID, which looks like "resource_id_<num>" where
    260   // <num> is a monotonically increasing number starting from 1.
    261   std::string GetNewResourceId();
    262 
    263   // Increments |largest_changestamp_| and adds the new changestamp.
    264   void AddNewChangestamp(base::DictionaryValue* entry);
    265 
    266   // Update ETag of |entry| based on |largest_changestamp_|.
    267   void UpdateETag(base::DictionaryValue* entry);
    268 
    269   // Adds a new entry based on the given parameters. |entry_kind| should be
    270   // "file" or "folder". Returns a pointer to the newly added entry, or NULL
    271   // if failed.
    272   const base::DictionaryValue* AddNewEntry(
    273     const std::string& content_type,
    274     const std::string& content_data,
    275     const std::string& parent_resource_id,
    276     const std::string& title,
    277     bool shared_with_me,
    278     const std::string& entry_kind);
    279 
    280   // Core implementation of GetResourceList.
    281   // This method returns the slice of the all matched entries, and its range
    282   // is between |start_offset| (inclusive) and |start_offset| + |max_results|
    283   // (exclusive).
    284   // Increments *load_counter by 1 before it returns successfully.
    285   void GetResourceListInternal(
    286       int64 start_changestamp,
    287       const std::string& search_query,
    288       const std::string& directory_resource_id,
    289       int start_offset,
    290       int max_results,
    291       int* load_counter,
    292       const google_apis::GetResourceListCallback& callback);
    293 
    294   // Returns new upload session URL.
    295   GURL GetNewUploadSessionUrl();
    296 
    297   scoped_ptr<base::DictionaryValue> resource_list_value_;
    298   scoped_ptr<base::DictionaryValue> account_metadata_value_;
    299   scoped_ptr<base::Value> app_info_value_;
    300   std::map<GURL, UploadSession> upload_sessions_;
    301   int64 largest_changestamp_;
    302   int64 published_date_seq_;
    303   int64 next_upload_sequence_number_;
    304   int default_max_results_;
    305   int resource_id_count_;
    306   int resource_list_load_count_;
    307   int change_list_load_count_;
    308   int directory_load_count_;
    309   int about_resource_load_count_;
    310   int app_list_load_count_;
    311   int blocked_resource_list_load_count_;
    312   bool offline_;
    313   bool never_return_all_resource_list_;
    314   base::FilePath last_cancelled_file_;
    315   GURL share_url_base_;
    316 
    317   DISALLOW_COPY_AND_ASSIGN(FakeDriveService);
    318 };
    319 
    320 }  // namespace drive
    321 
    322 #endif  // CHROME_BROWSER_DRIVE_FAKE_DRIVE_SERVICE_H_
    323