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