Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(304)

Side by Side Diff: chrome/browser/sync_file_system/drive_file_sync_client.cc

Issue 14146006: Refactoring: replace SearchInDirectory by SearchByTitle. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/sync_file_system/drive_file_sync_client.h" 5 #include "chrome/browser/sync_file_system/drive_file_sync_client.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <functional> 8 #include <functional>
9 #include <sstream> 9 #include <sstream>
10 #include <string> 10 #include <string>
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 DCHECK(CalledOnValidThread()); 178 DCHECK(CalledOnValidThread());
179 observers_.RemoveObserver(observer); 179 observers_.RemoveObserver(observer);
180 } 180 }
181 181
182 void DriveFileSyncClient::GetDriveDirectoryForSyncRoot( 182 void DriveFileSyncClient::GetDriveDirectoryForSyncRoot(
183 const ResourceIdCallback& callback) { 183 const ResourceIdCallback& callback) {
184 DCHECK(CalledOnValidThread()); 184 DCHECK(CalledOnValidThread());
185 DVLOG(2) << "Getting Drive directory for SyncRoot"; 185 DVLOG(2) << "Getting Drive directory for SyncRoot";
186 186
187 std::string directory_name(kSyncRootDirectoryName); 187 std::string directory_name(kSyncRootDirectoryName);
188 drive_service_->Search( 188 SearchByTitle(directory_name, "",
tzik 2013/04/18 14:26:23 s/""/std::string()/ ?
hidehiko 2013/04/22 04:24:45 Done.
189 FormatTitleQuery(directory_name), 189 base::Bind(&DriveFileSyncClient::DidGetDirectory, AsWeakPtr(),
190 base::Bind(&DriveFileSyncClient::DidGetResourceList, AsWeakPtr(), 190 std::string(), directory_name, callback));
191 base::Bind(&DriveFileSyncClient::DidGetDirectory, AsWeakPtr(),
192 std::string(), directory_name, callback)));
193 } 191 }
194 192
195 void DriveFileSyncClient::GetDriveDirectoryForOrigin( 193 void DriveFileSyncClient::GetDriveDirectoryForOrigin(
196 const std::string& sync_root_resource_id, 194 const std::string& sync_root_resource_id,
197 const GURL& origin, 195 const GURL& origin,
198 const ResourceIdCallback& callback) { 196 const ResourceIdCallback& callback) {
199 DCHECK(CalledOnValidThread()); 197 DCHECK(CalledOnValidThread());
200 DVLOG(2) << "Getting Drive directory for Origin: " << origin; 198 DVLOG(2) << "Getting Drive directory for Origin: " << origin;
201 199
202 std::string directory_name(OriginToDirectoryTitle(origin)); 200 std::string directory_name(OriginToDirectoryTitle(origin));
203 SearchFilesInDirectory( 201 SearchByTitle(
204 sync_root_resource_id, 202 directory_name, sync_root_resource_id,
205 FormatTitleQuery(directory_name),
206 base::Bind(&DriveFileSyncClient::DidGetDirectory, AsWeakPtr(), 203 base::Bind(&DriveFileSyncClient::DidGetDirectory, AsWeakPtr(),
207 sync_root_resource_id, directory_name, callback)); 204 sync_root_resource_id, directory_name, callback));
208 } 205 }
209 206
210 void DriveFileSyncClient::DidGetDirectory( 207 void DriveFileSyncClient::DidGetDirectory(
211 const std::string& parent_resource_id, 208 const std::string& parent_resource_id,
212 const std::string& directory_name, 209 const std::string& directory_name,
213 const ResourceIdCallback& callback, 210 const ResourceIdCallback& callback,
214 google_apis::GDataErrorCode error, 211 google_apis::GDataErrorCode error,
215 scoped_ptr<google_apis::ResourceList> feed) { 212 scoped_ptr<google_apis::ResourceList> feed) {
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 DCHECK(about_resource); 342 DCHECK(about_resource);
346 largest_change_id = about_resource->largest_change_id(); 343 largest_change_id = about_resource->largest_change_id();
347 DVLOG(2) << "Got largest change id: " << largest_change_id; 344 DVLOG(2) << "Got largest change id: " << largest_change_id;
348 } else { 345 } else {
349 DVLOG(2) << "Error on getting largest change id: " << error; 346 DVLOG(2) << "Error on getting largest change id: " << error;
350 } 347 }
351 348
352 callback.Run(error, largest_change_id); 349 callback.Run(error, largest_change_id);
353 } 350 }
354 351
355 void DriveFileSyncClient::SearchFilesInDirectory( 352 void DriveFileSyncClient::SearchByTitle(
353 const std::string& title,
356 const std::string& directory_resource_id, 354 const std::string& directory_resource_id,
357 const std::string& search_query,
358 const ResourceListCallback& callback) { 355 const ResourceListCallback& callback) {
359 DCHECK(CalledOnValidThread()); 356 DCHECK(CalledOnValidThread());
360 DCHECK(!directory_resource_id.empty()); 357 DCHECK(!title.empty());
361 DVLOG(2) << "Searching resources in the directory [" << directory_resource_id 358 DVLOG(2) << "Searching resources in the directory [" << directory_resource_id
362 << "] with query [" << search_query << "]"; 359 << "] with title [" << title << "]";
363 360
364 drive_service_->SearchInDirectory( 361 drive_service_->SearchByTitle(
365 search_query, 362 title,
366 directory_resource_id, 363 directory_resource_id,
367 base::Bind(&DriveFileSyncClient::DidGetResourceList, 364 base::Bind(&DriveFileSyncClient::DidGetResourceList,
368 AsWeakPtr(), callback)); 365 AsWeakPtr(), callback));
369 } 366 }
370 367
371 void DriveFileSyncClient::ListFiles(const std::string& directory_resource_id, 368 void DriveFileSyncClient::ListFiles(const std::string& directory_resource_id,
372 const ResourceListCallback& callback) { 369 const ResourceListCallback& callback) {
373 DCHECK(CalledOnValidThread()); 370 DCHECK(CalledOnValidThread());
374 DVLOG(2) << "Listing resources in the directory [" 371 DVLOG(2) << "Listing resources in the directory ["
375 << directory_resource_id << "]"; 372 << directory_resource_id << "]";
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
563 DVLOG(2) << "Error on getting resource entry:" << error; 560 DVLOG(2) << "Error on getting resource entry:" << error;
564 callback.Run(error, scoped_ptr<google_apis::ResourceEntry>()); 561 callback.Run(error, scoped_ptr<google_apis::ResourceEntry>());
565 return; 562 return;
566 } 563 }
567 564
568 DVLOG(2) << "Got resource entry"; 565 DVLOG(2) << "Got resource entry";
569 DCHECK(entry); 566 DCHECK(entry);
570 callback.Run(error, entry.Pass()); 567 callback.Run(error, entry.Pass());
571 } 568 }
572 569
573 // static
574 std::string DriveFileSyncClient::FormatTitleQuery(const std::string& title) {
575 // TODO(tzik): This pattern matches partial and case-insensitive,
576 // and also matches files in subdirectories.
577 // Refine the query after we migrate to Drive API.
578 std::ostringstream out;
579 out << "title:";
580
581 // Escape single quote and back slash with '\\'.
582 // https://developers.google.com/drive/search-parameters
583 out << '\'';
584 for (std::string::const_iterator itr = title.begin();
585 itr != title.end(); ++itr) {
586 switch (*itr) {
587 case '\'':
588 case '\\':
589 out << '\\' << *itr;
590 break;
591 default:
592 out << *itr;
593 break;
594 }
595 }
596 out << '\'';
597 return out.str();
598 }
599
600 void DriveFileSyncClient::DownloadFileInternal( 570 void DriveFileSyncClient::DownloadFileInternal(
601 const std::string& local_file_md5, 571 const std::string& local_file_md5,
602 const base::FilePath& local_file_path, 572 const base::FilePath& local_file_path,
603 const DownloadFileCallback& callback, 573 const DownloadFileCallback& callback,
604 google_apis::GDataErrorCode error, 574 google_apis::GDataErrorCode error,
605 scoped_ptr<google_apis::ResourceEntry> entry) { 575 scoped_ptr<google_apis::ResourceEntry> entry) {
606 DCHECK(CalledOnValidThread()); 576 DCHECK(CalledOnValidThread());
607 577
608 if (error != google_apis::HTTP_SUCCESS) { 578 if (error != google_apis::HTTP_SUCCESS) {
609 DVLOG(2) << "Error on getting resource entry for download"; 579 DVLOG(2) << "Error on getting resource entry for download";
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
812 const std::string& parent_resource_id, 782 const std::string& parent_resource_id,
813 const std::string& expected_title, 783 const std::string& expected_title,
814 const ResourceEntryCallback& callback) { 784 const ResourceEntryCallback& callback) {
815 DCHECK(CalledOnValidThread()); 785 DCHECK(CalledOnValidThread());
816 DVLOG(2) << "Checking if there's no conflict on entry creation"; 786 DVLOG(2) << "Checking if there's no conflict on entry creation";
817 787
818 const google_apis::GetResourceListCallback& bound_callback = 788 const google_apis::GetResourceListCallback& bound_callback =
819 base::Bind(&DriveFileSyncClient::DidListEntriesToEnsureUniqueness, 789 base::Bind(&DriveFileSyncClient::DidListEntriesToEnsureUniqueness,
820 AsWeakPtr(), parent_resource_id, expected_title, callback); 790 AsWeakPtr(), parent_resource_id, expected_title, callback);
821 791
822 if (parent_resource_id.empty()) { 792 SearchByTitle(expected_title, parent_resource_id, bound_callback);
823 // Here, it is a part of process to create a sync root directory.
824 // The sync root directory may be orphan or be under mydrive directory,
825 // due to historical reason. So, it is necessary to search both just orphan
826 // resources and ones under mydrive.
827 // Unfortunately there is no way to search only from orphan resources,
828 // so here search all the resources. Unreleated results will be filtered
829 // out below.
830 drive_service_->Search(
831 FormatTitleQuery(expected_title),
832 base::Bind(&DriveFileSyncClient::DidGetResourceList, AsWeakPtr(),
833 bound_callback));
834 } else {
835 SearchFilesInDirectory(
836 parent_resource_id,
837 FormatTitleQuery(expected_title),
838 bound_callback);
839 }
840 } 793 }
841 794
842 void DriveFileSyncClient::DidListEntriesToEnsureUniqueness( 795 void DriveFileSyncClient::DidListEntriesToEnsureUniqueness(
843 const std::string& parent_resource_id, 796 const std::string& parent_resource_id,
844 const std::string& expected_title, 797 const std::string& expected_title,
845 const ResourceEntryCallback& callback, 798 const ResourceEntryCallback& callback,
846 google_apis::GDataErrorCode error, 799 google_apis::GDataErrorCode error,
847 scoped_ptr<google_apis::ResourceList> feed) { 800 scoped_ptr<google_apis::ResourceList> feed) {
848 DCHECK(CalledOnValidThread()); 801 DCHECK(CalledOnValidThread());
849 if (error != google_apis::HTTP_SUCCESS) { 802 if (error != google_apis::HTTP_SUCCESS) {
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
969 return; 922 return;
970 for (UploadCallbackMap::iterator iter = upload_callback_map_.begin(); 923 for (UploadCallbackMap::iterator iter = upload_callback_map_.begin();
971 iter != upload_callback_map_.end(); ++iter) { 924 iter != upload_callback_map_.end(); ++iter) {
972 iter->second.Run(error, std::string(), std::string()); 925 iter->second.Run(error, std::string(), std::string());
973 } 926 }
974 upload_callback_map_.clear(); 927 upload_callback_map_.clear();
975 drive_uploader_.reset(new google_apis::DriveUploader(drive_service_.get())); 928 drive_uploader_.reset(new google_apis::DriveUploader(drive_service_.get()));
976 } 929 }
977 930
978 } // namespace sync_file_system 931 } // namespace sync_file_system
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698