1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_ENGINE_INITIALIZER_H_
#define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_ENGINE_INITIALIZER_H_
#include <stdint.h>
#include <memory>
#include <string>
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/sync_file_system/drive_backend/sync_task.h"
#include "chrome/browser/sync_file_system/sync_callbacks.h"
#include "google_apis/common/api_error_codes.h"
#include "google_apis/drive/drive_common_callbacks.h"
namespace google_apis {
class AboutResource;
class FileList;
class FileResource;
} // namespace google_apis
namespace leveldb {
class Env;
}
namespace sync_file_system {
namespace drive_backend {
class MetadataDatabase;
class SyncEngineContext;
// This class performs initializion sequence of SyncEngine.
//
// After initialize sequence completed, the Database must have
// - Largest change ID,
// - Sync-root folder and its tracker,
// - All children of sync-root folder that have inactive and non-dirty
// trackers.
//
// The initialization sequence is:
// - Open database and load its contents,
// - If the database is already populated, complete the sequence.
// - Get AboutResource to get the largest change ID and the Drive root folder
// ID.
// - Find the remote sync-root folder, whose title is
// "Chrome Syncable FileSystem" and has no parent.
// Note that if the initialization is interrupted by the browser restart or
// an error, the sequence leaves the folder in the Drive root folder. So, if
// we find the folder in the Drive root folder, handle it as the sync-root
// folder.
// - Create the remote sync-root folder if we don't have.
// - Detach the remote sync-root folder from its parent if it has.
// - Fetch the folder contents of the remote sync-root folder.
// The contents are likely registered as app-root folders, but handle them
// as regular inactive folders until they are registered explicitly.
// - Populate database with the largest change ID, the sync-root folder and
// its contents.
//
class SyncEngineInitializer : public SyncTask {
public:
SyncEngineInitializer(SyncEngineContext* sync_context,
const base::FilePath& database_path,
leveldb::Env* env_override);
SyncEngineInitializer(const SyncEngineInitializer&) = delete;
SyncEngineInitializer& operator=(const SyncEngineInitializer&) = delete;
~SyncEngineInitializer() override;
void RunPreflight(std::unique_ptr<SyncTaskToken> token) override;
std::unique_ptr<MetadataDatabase> PassMetadataDatabase();
private:
void GetAboutResource(std::unique_ptr<SyncTaskToken> token);
void DidGetAboutResource(
std::unique_ptr<SyncTaskToken> token,
google_apis::ApiErrorCode error,
std::unique_ptr<google_apis::AboutResource> about_resource);
void FindSyncRoot(std::unique_ptr<SyncTaskToken> token);
void DidFindSyncRoot(std::unique_ptr<SyncTaskToken> token,
google_apis::ApiErrorCode error,
std::unique_ptr<google_apis::FileList> file_list);
void CreateSyncRoot(std::unique_ptr<SyncTaskToken> token);
void DidCreateSyncRoot(std::unique_ptr<SyncTaskToken> token,
google_apis::ApiErrorCode error,
std::unique_ptr<google_apis::FileResource> entry);
void DetachSyncRoot(std::unique_ptr<SyncTaskToken> token);
void DidDetachSyncRoot(std::unique_ptr<SyncTaskToken> token,
google_apis::ApiErrorCode error);
void ListAppRootFolders(std::unique_ptr<SyncTaskToken> token);
void DidListAppRootFolders(std::unique_ptr<SyncTaskToken> token,
google_apis::ApiErrorCode error,
std::unique_ptr<google_apis::FileList> file_list);
void PopulateDatabase(std::unique_ptr<SyncTaskToken> token);
raw_ptr<SyncEngineContext> sync_context_; // Not owned.
raw_ptr<leveldb::Env> env_override_;
google_apis::CancelCallbackOnce cancel_callback_;
base::FilePath database_path_;
int find_sync_root_retry_count_;
std::unique_ptr<MetadataDatabase> metadata_database_;
std::vector<std::unique_ptr<google_apis::FileResource>> app_root_folders_;
int64_t largest_change_id_;
std::string root_folder_id_;
std::unique_ptr<google_apis::FileResource> sync_root_folder_;
base::WeakPtrFactory<SyncEngineInitializer> weak_ptr_factory_{this};
};
} // namespace drive_backend
} // namespace sync_file_system
#endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_ENGINE_INITIALIZER_H_
|