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 "chrome/browser/google_apis/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 // Changes the default max results returned from GetResourceList(). 44 // By default, it's set to 0, which is unlimited. 45 void set_default_max_results(int default_max_results) { 46 default_max_results_ = default_max_results; 47 } 48 49 // Sets the url to the test server to be used as a base for generated share 50 // urls to the share dialog. 51 void set_share_url_base(const GURL& share_url_base) { 52 share_url_base_ = share_url_base; 53 } 54 55 // Changes the quota fields returned from GetAboutResource(). 56 void SetQuotaValue(int64 used, int64 total); 57 58 // Returns the largest changestamp, which starts from 0 by default. See 59 // also comments at LoadAccountMetadataForWapi(). 60 int64 largest_changestamp() const { return largest_changestamp_; } 61 62 // Returns the number of times the resource list is successfully loaded by 63 // GetAllResourceList(). 64 int resource_list_load_count() const { return resource_list_load_count_; } 65 66 // Returns the number of times the resource list is successfully loaded by 67 // GetChangeList(). 68 int change_list_load_count() const { return change_list_load_count_; } 69 70 // Returns the number of times the resource list is successfully loaded by 71 // GetResourceListInDirectory(). 72 int directory_load_count() const { return directory_load_count_; } 73 74 // Returns the number of times the about resource is successfully loaded 75 // by GetAboutResource(). 76 int about_resource_load_count() const { 77 return about_resource_load_count_; 78 } 79 80 // Returns the number of times the app list is successfully loaded by 81 // GetAppList(). 82 int app_list_load_count() const { return app_list_load_count_; } 83 84 // Returns the file path whose request is cancelled just before this method 85 // invocation. 86 const base::FilePath& last_cancelled_file() const { 87 return last_cancelled_file_; 88 } 89 90 // Returns the (fake) URL for the link. 91 static GURL GetFakeLinkUrl(const std::string& resource_id); 92 93 // DriveServiceInterface Overrides 94 virtual void Initialize() OVERRIDE; 95 virtual void AddObserver(DriveServiceObserver* observer) OVERRIDE; 96 virtual void RemoveObserver(DriveServiceObserver* observer) OVERRIDE; 97 virtual bool CanSendRequest() const OVERRIDE; 98 virtual std::string CanonicalizeResourceId( 99 const std::string& resource_id) const OVERRIDE; 100 virtual std::string GetRootResourceId() const OVERRIDE; 101 virtual bool HasAccessToken() const OVERRIDE; 102 virtual void RequestAccessToken( 103 const google_apis::AuthStatusCallback& callback) OVERRIDE; 104 virtual bool HasRefreshToken() const OVERRIDE; 105 virtual void ClearAccessToken() OVERRIDE; 106 virtual void ClearRefreshToken() OVERRIDE; 107 virtual google_apis::CancelCallback GetAllResourceList( 108 const google_apis::GetResourceListCallback& callback) OVERRIDE; 109 virtual google_apis::CancelCallback GetResourceListInDirectory( 110 const std::string& directory_resource_id, 111 const google_apis::GetResourceListCallback& callback) OVERRIDE; 112 // See the comment for EntryMatchWidthQuery() in .cc file for details about 113 // the supported search query types. 114 virtual google_apis::CancelCallback Search( 115 const std::string& search_query, 116 const google_apis::GetResourceListCallback& callback) OVERRIDE; 117 virtual google_apis::CancelCallback SearchByTitle( 118 const std::string& title, 119 const std::string& directory_resource_id, 120 const google_apis::GetResourceListCallback& callback) OVERRIDE; 121 virtual google_apis::CancelCallback GetChangeList( 122 int64 start_changestamp, 123 const google_apis::GetResourceListCallback& callback) OVERRIDE; 124 virtual google_apis::CancelCallback ContinueGetResourceList( 125 const GURL& override_url, 126 const google_apis::GetResourceListCallback& callback) OVERRIDE; 127 virtual google_apis::CancelCallback GetResourceEntry( 128 const std::string& resource_id, 129 const google_apis::GetResourceEntryCallback& callback) OVERRIDE; 130 virtual google_apis::CancelCallback GetShareUrl( 131 const std::string& resource_id, 132 const GURL& embed_origin, 133 const google_apis::GetShareUrlCallback& callback) OVERRIDE; 134 virtual google_apis::CancelCallback GetAboutResource( 135 const google_apis::GetAboutResourceCallback& callback) OVERRIDE; 136 virtual google_apis::CancelCallback GetAppList( 137 const google_apis::GetAppListCallback& callback) OVERRIDE; 138 virtual google_apis::CancelCallback DeleteResource( 139 const std::string& resource_id, 140 const std::string& etag, 141 const google_apis::EntryActionCallback& callback) OVERRIDE; 142 virtual google_apis::CancelCallback DownloadFile( 143 const base::FilePath& local_cache_path, 144 const std::string& resource_id, 145 const google_apis::DownloadActionCallback& download_action_callback, 146 const google_apis::GetContentCallback& get_content_callback, 147 const google_apis::ProgressCallback& progress_callback) OVERRIDE; 148 virtual google_apis::CancelCallback CopyResource( 149 const std::string& resource_id, 150 const std::string& parent_resource_id, 151 const std::string& new_title, 152 const google_apis::GetResourceEntryCallback& callback) OVERRIDE; 153 // The new resource ID for the copied document will look like 154 // |resource_id| + "_copied". 155 virtual google_apis::CancelCallback CopyHostedDocument( 156 const std::string& resource_id, 157 const std::string& new_title, 158 const google_apis::GetResourceEntryCallback& callback) OVERRIDE; 159 virtual google_apis::CancelCallback RenameResource( 160 const std::string& resource_id, 161 const std::string& new_title, 162 const google_apis::EntryActionCallback& callback) OVERRIDE; 163 virtual google_apis::CancelCallback TouchResource( 164 const std::string& resource_id, 165 const base::Time& modified_date, 166 const base::Time& last_viewed_by_me_date, 167 const google_apis::GetResourceEntryCallback& callback) OVERRIDE; 168 virtual google_apis::CancelCallback AddResourceToDirectory( 169 const std::string& parent_resource_id, 170 const std::string& resource_id, 171 const google_apis::EntryActionCallback& callback) OVERRIDE; 172 virtual google_apis::CancelCallback RemoveResourceFromDirectory( 173 const std::string& parent_resource_id, 174 const std::string& resource_id, 175 const google_apis::EntryActionCallback& callback) OVERRIDE; 176 virtual google_apis::CancelCallback AddNewDirectory( 177 const std::string& parent_resource_id, 178 const std::string& directory_title, 179 const google_apis::GetResourceEntryCallback& callback) OVERRIDE; 180 virtual google_apis::CancelCallback InitiateUploadNewFile( 181 const std::string& content_type, 182 int64 content_length, 183 const std::string& parent_resource_id, 184 const std::string& title, 185 const google_apis::InitiateUploadCallback& callback) OVERRIDE; 186 virtual google_apis::CancelCallback InitiateUploadExistingFile( 187 const std::string& content_type, 188 int64 content_length, 189 const std::string& resource_id, 190 const std::string& etag, 191 const google_apis::InitiateUploadCallback& callback) OVERRIDE; 192 virtual google_apis::CancelCallback ResumeUpload( 193 const GURL& upload_url, 194 int64 start_position, 195 int64 end_position, 196 int64 content_length, 197 const std::string& content_type, 198 const base::FilePath& local_file_path, 199 const google_apis::UploadRangeCallback& callback, 200 const google_apis::ProgressCallback& progress_callback) OVERRIDE; 201 virtual google_apis::CancelCallback GetUploadStatus( 202 const GURL& upload_url, 203 int64 content_length, 204 const google_apis::UploadRangeCallback& callback) OVERRIDE; 205 virtual google_apis::CancelCallback AuthorizeApp( 206 const std::string& resource_id, 207 const std::string& app_id, 208 const google_apis::AuthorizeAppCallback& callback) OVERRIDE; 209 210 // Adds a new file with the given parameters. On success, returns 211 // HTTP_CREATED with the parsed entry. 212 // |callback| must not be null. 213 void AddNewFile(const std::string& content_type, 214 const std::string& content_data, 215 const std::string& parent_resource_id, 216 const std::string& title, 217 bool shared_with_me, 218 const google_apis::GetResourceEntryCallback& callback); 219 220 // Sets the last modified time for an entry specified by |resource_id|. 221 // On success, returns HTTP_SUCCESS with the parsed entry. 222 // |callback| must not be null. 223 void SetLastModifiedTime( 224 const std::string& resource_id, 225 const base::Time& last_modified_time, 226 const google_apis::GetResourceEntryCallback& callback); 227 228 private: 229 struct UploadSession; 230 231 // Returns a pointer to the entry that matches |resource_id|, or NULL if 232 // not found. 233 base::DictionaryValue* FindEntryByResourceId(const std::string& resource_id); 234 235 // Returns a pointer to the entry that matches |content_url|, or NULL if 236 // not found. 237 base::DictionaryValue* FindEntryByContentUrl(const GURL& content_url); 238 239 // Returns a new resource ID, which looks like "resource_id_<num>" where 240 // <num> is a monotonically increasing number starting from 1. 241 std::string GetNewResourceId(); 242 243 // Increments |largest_changestamp_| and adds the new changestamp and ETag to 244 // |entry|. 245 void AddNewChangestampAndETag(base::DictionaryValue* entry); 246 247 // Adds a new entry based on the given parameters. |entry_kind| should be 248 // "file" or "folder". Returns a pointer to the newly added entry, or NULL 249 // if failed. 250 const base::DictionaryValue* AddNewEntry( 251 const std::string& content_type, 252 const std::string& content_data, 253 const std::string& parent_resource_id, 254 const std::string& title, 255 bool shared_with_me, 256 const std::string& entry_kind); 257 258 // Core implementation of GetResourceList. 259 // This method returns the slice of the all matched entries, and its range 260 // is between |start_offset| (inclusive) and |start_offset| + |max_results| 261 // (exclusive). 262 // Increments *load_counter by 1 before it returns successfully. 263 void GetResourceListInternal( 264 int64 start_changestamp, 265 const std::string& search_query, 266 const std::string& directory_resource_id, 267 int start_offset, 268 int max_results, 269 int* load_counter, 270 const google_apis::GetResourceListCallback& callback); 271 272 // Returns new upload session URL. 273 GURL GetNewUploadSessionUrl(); 274 275 scoped_ptr<base::DictionaryValue> resource_list_value_; 276 scoped_ptr<base::DictionaryValue> account_metadata_value_; 277 scoped_ptr<base::Value> app_info_value_; 278 std::map<GURL, UploadSession> upload_sessions_; 279 int64 largest_changestamp_; 280 int64 published_date_seq_; 281 int64 next_upload_sequence_number_; 282 int default_max_results_; 283 int resource_id_count_; 284 int resource_list_load_count_; 285 int change_list_load_count_; 286 int directory_load_count_; 287 int about_resource_load_count_; 288 int app_list_load_count_; 289 bool offline_; 290 base::FilePath last_cancelled_file_; 291 GURL share_url_base_; 292 293 DISALLOW_COPY_AND_ASSIGN(FakeDriveService); 294 }; 295 296 } // namespace drive 297 298 #endif // CHROME_BROWSER_DRIVE_FAKE_DRIVE_SERVICE_H_ 299