Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / sync_file_system / drive_backend / uninstall_app_task.cc
blobe35878531475fb5aaa15817b5043590789346f6b
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"
7 #include "base/bind.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_set.h"
17 #include "chrome/browser/sync_file_system/syncable_file_system_util.h"
18 #include "google_apis/drive/drive_api_parser.h"
19 #include "google_apis/drive/gdata_wapi_parser.h"
21 namespace sync_file_system {
22 namespace drive_backend {
24 UninstallAppTask::UninstallAppTask(SyncEngineContext* sync_context,
25 const std::string& app_id,
26 UninstallFlag uninstall_flag)
27 : sync_context_(sync_context),
28 app_id_(app_id),
29 uninstall_flag_(uninstall_flag),
30 app_root_tracker_id_(0),
31 weak_ptr_factory_(this) {
34 UninstallAppTask::~UninstallAppTask() {
37 void UninstallAppTask::Run(const SyncStatusCallback& callback) {
38 if (!IsContextReady()) {
39 RunSoon(FROM_HERE, base::Bind(callback, SYNC_STATUS_FAILED));
40 return;
43 if (uninstall_flag_ == RemoteFileSyncService::UNINSTALL_AND_KEEP_REMOTE) {
44 metadata_database()->UnregisterApp(app_id_, callback);
45 return;
47 DCHECK_EQ(RemoteFileSyncService::UNINSTALL_AND_PURGE_REMOTE, uninstall_flag_);
49 int64 sync_root_tracker_id = metadata_database()->GetSyncRootTrackerID();
50 TrackerSet trackers;
51 if (!metadata_database()->FindTrackersByParentAndTitle(
52 sync_root_tracker_id, app_id_, &trackers) ||
53 !trackers.has_active()) {
54 RunSoon(FROM_HERE, base::Bind(callback, SYNC_STATUS_OK));
55 return;
58 FileTracker* app_root_tracker = trackers.active_tracker();
59 app_root_tracker_id_ = app_root_tracker->tracker_id();
60 DCHECK(app_root_tracker->has_synced_details());
62 drive_service()->DeleteResource(
63 app_root_tracker->file_id(),
64 std::string(), // etag
65 base::Bind(&UninstallAppTask::DidDeleteAppRoot,
66 weak_ptr_factory_.GetWeakPtr(),
67 callback,
68 metadata_database()->GetLargestKnownChangeID()));
71 void UninstallAppTask::DidDeleteAppRoot(const SyncStatusCallback& callback,
72 int64 change_id,
73 google_apis::GDataErrorCode error) {
74 SyncStatusCode status = GDataErrorCodeToSyncStatusCode(error);
75 if (status != SYNC_STATUS_OK &&
76 error != google_apis::HTTP_NOT_FOUND) {
77 callback.Run(SYNC_STATUS_FAILED);
78 return;
81 metadata_database()->UnregisterApp(app_id_, callback);
84 bool UninstallAppTask::IsContextReady() {
85 return sync_context_->GetMetadataDatabase() &&
86 sync_context_->GetDriveService();
89 MetadataDatabase* UninstallAppTask::metadata_database() {
90 return sync_context_->GetMetadataDatabase();
93 drive::DriveServiceInterface* UninstallAppTask::drive_service() {
94 set_used_network(true);
95 return sync_context_->GetDriveService();
98 } // namespace drive_backend
99 } // namespace sync_file_system