1 // Copyright 2013 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_SYNC_FILE_SYSTEM_DRIVE_BACKEND_DRIVE_BACKEND_UTIL_H_ 6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_DRIVE_BACKEND_UTIL_H_ 7 8 #include <string> 9 10 #include "base/bind.h" 11 #include "base/callback.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_vector.h" 14 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h" 15 #include "chrome/browser/sync_file_system/sync_status_code.h" 16 #include "google_apis/drive/gdata_errorcode.h" 17 #include "webkit/common/blob/scoped_file.h" 18 19 namespace google_apis { 20 class ChangeResource; 21 class FileResource; 22 class ResourceEntry; 23 } 24 25 namespace leveldb { 26 class WriteBatch; 27 } 28 29 namespace sync_file_system { 30 namespace drive_backend { 31 32 void PutServiceMetadataToBatch(const ServiceMetadata& service_metadata, 33 leveldb::WriteBatch* batch); 34 void PutFileMetadataToBatch(const FileMetadata& file, 35 leveldb::WriteBatch* batch); 36 void PutFileTrackerToBatch(const FileTracker& tracker, 37 leveldb::WriteBatch* batch); 38 39 void PutFileMetadataDeletionToBatch(const std::string& file_id, 40 leveldb::WriteBatch* batch); 41 void PutFileTrackerDeletionToBatch(int64 tracker_id, 42 leveldb::WriteBatch* batch); 43 44 void PopulateFileDetailsByFileResource( 45 const google_apis::FileResource& file_resource, 46 FileDetails* details); 47 scoped_ptr<FileMetadata> CreateFileMetadataFromFileResource( 48 int64 change_id, 49 const google_apis::FileResource& resource); 50 scoped_ptr<FileMetadata> CreateFileMetadataFromChangeResource( 51 const google_apis::ChangeResource& change); 52 scoped_ptr<FileMetadata> CreateDeletedFileMetadata( 53 int64 change_id, 54 const std::string& file_id); 55 56 // Creates a temporary file in |dir_path|. This must be called on an 57 // IO-allowed task runner, and the runner must be given as |file_task_runner|. 58 webkit_blob::ScopedFile CreateTemporaryFile( 59 base::TaskRunner* file_task_runner); 60 61 std::string FileKindToString(FileKind file_kind); 62 63 bool HasFileAsParent(const FileDetails& details, const std::string& file_id); 64 65 std::string GetMimeTypeFromTitle(const base::FilePath& title); 66 67 SyncStatusCode GDataErrorCodeToSyncStatusCode( 68 google_apis::GDataErrorCode error); 69 70 scoped_ptr<FileTracker> CloneFileTracker(const FileTracker* obj); 71 72 template <typename Src, typename Dest> 73 void AppendContents(const Src& src, Dest* dest) { 74 dest->insert(dest->end(), src.begin(), src.end()); 75 } 76 77 template <typename Container> 78 const typename Container::mapped_type& LookUpMap( 79 const Container& container, 80 const typename Container::key_type& key, 81 const typename Container::mapped_type& default_value) { 82 typename Container::const_iterator found = container.find(key); 83 if (found == container.end()) 84 return default_value; 85 return found->second; 86 } 87 88 template <typename R, typename S, typename T> 89 R ComposeFunction(const base::Callback<T()>& g, 90 const base::Callback<R(S)>& f) { 91 return f.Run(g.Run()); 92 } 93 94 template <typename R, typename S, typename T> 95 base::Callback<R()> CreateComposedFunction( 96 const base::Callback<T()>& g, 97 const base::Callback<R(S)>& f) { 98 return base::Bind(&ComposeFunction<R, S, T>, g, f); 99 } 100 101 } // namespace drive_backend 102 } // namespace sync_file_system 103 104 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_DRIVE_BACKEND_UTIL_H_ 105