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.
5 #include "chrome/browser/sync_file_system/drive_backend/uninstall_app_task.h"
8 #include "base/location.h"
9 #include "chrome/browser/drive/drive_api_util.h"
10 #include "chrome/browser/drive/drive_service_interface.h"
11 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_constants.h"
12 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_util.h"
13 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.h"
14 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h"
15 #include "chrome/browser/sync_file_system/drive_backend/sync_engine_context.h"
16 #include "chrome/browser/sync_file_system/drive_backend/tracker_id_set.h"
17 #include "chrome/browser/sync_file_system/syncable_file_system_util.h"
18 #include "google_apis/drive/drive_api_parser.h"
20 namespace sync_file_system
{
21 namespace drive_backend
{
23 UninstallAppTask::UninstallAppTask(SyncEngineContext
* sync_context
,
24 const std::string
& app_id
,
25 UninstallFlag uninstall_flag
)
26 : sync_context_(sync_context
),
28 uninstall_flag_(uninstall_flag
),
29 app_root_tracker_id_(0),
30 weak_ptr_factory_(this) {
33 UninstallAppTask::~UninstallAppTask() {
36 void UninstallAppTask::RunExclusive(const SyncStatusCallback
& callback
) {
37 if (!IsContextReady()) {
38 callback
.Run(SYNC_STATUS_FAILED
);
42 if (uninstall_flag_
== RemoteFileSyncService::UNINSTALL_AND_KEEP_REMOTE
) {
43 SyncStatusCode status
= metadata_database()->UnregisterApp(app_id_
);
47 DCHECK_EQ(RemoteFileSyncService::UNINSTALL_AND_PURGE_REMOTE
, uninstall_flag_
);
49 int64 sync_root_tracker_id
= metadata_database()->GetSyncRootTrackerID();
50 TrackerIDSet trackers
;
51 if (!metadata_database()->FindTrackersByParentAndTitle(
52 sync_root_tracker_id
, app_id_
, &trackers
) ||
53 !trackers
.has_active()) {
54 callback
.Run(SYNC_STATUS_OK
);
58 FileTracker app_root_tracker
;
59 if (!metadata_database()->FindTrackerByTrackerID(
60 trackers
.active_tracker(), &app_root_tracker
)) {
62 callback
.Run(SYNC_STATUS_FAILED
);
65 app_root_tracker_id_
= app_root_tracker
.tracker_id();
66 DCHECK(app_root_tracker
.has_synced_details());
68 drive_service()->DeleteResource(
69 app_root_tracker
.file_id(),
70 std::string(), // etag
71 base::Bind(&UninstallAppTask::DidDeleteAppRoot
,
72 weak_ptr_factory_
.GetWeakPtr(),
74 metadata_database()->GetLargestKnownChangeID()));
77 void UninstallAppTask::DidDeleteAppRoot(const SyncStatusCallback
& callback
,
79 google_apis::DriveApiErrorCode error
) {
80 SyncStatusCode status
= DriveApiErrorCodeToSyncStatusCode(error
);
81 if (status
!= SYNC_STATUS_OK
&&
82 error
!= google_apis::HTTP_NOT_FOUND
) {
83 callback
.Run(SYNC_STATUS_FAILED
);
87 status
= metadata_database()->UnregisterApp(app_id_
);
91 bool UninstallAppTask::IsContextReady() {
92 return sync_context_
->GetMetadataDatabase() &&
93 sync_context_
->GetDriveService();
96 MetadataDatabase
* UninstallAppTask::metadata_database() {
97 return sync_context_
->GetMetadataDatabase();
100 drive::DriveServiceInterface
* UninstallAppTask::drive_service() {
101 set_used_network(true);
102 return sync_context_
->GetDriveService();
105 } // namespace drive_backend
106 } // namespace sync_file_system