Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / sync_file_system / drive_backend / uninstall_app_task.cc
blob1a5bf6f0cd560b8974c014a806d4c637186bc684
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/sync_file_system/drive_backend/drive_backend_constants.h"
10 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_util.h"
11 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.h"
12 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h"
13 #include "chrome/browser/sync_file_system/drive_backend/sync_engine_context.h"
14 #include "chrome/browser/sync_file_system/drive_backend/tracker_id_set.h"
15 #include "chrome/browser/sync_file_system/syncable_file_system_util.h"
16 #include "components/drive/drive_api_util.h"
17 #include "components/drive/service/drive_service_interface.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),
27 app_id_(app_id),
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);
39 return;
42 if (uninstall_flag_ == RemoteFileSyncService::UNINSTALL_AND_KEEP_REMOTE) {
43 SyncStatusCode status = metadata_database()->UnregisterApp(app_id_);
44 callback.Run(status);
45 return;
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);
55 return;
58 FileTracker app_root_tracker;
59 if (!metadata_database()->FindTrackerByTrackerID(
60 trackers.active_tracker(), &app_root_tracker)) {
61 NOTREACHED();
62 callback.Run(SYNC_STATUS_FAILED);
63 return;
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(),
73 callback,
74 metadata_database()->GetLargestKnownChangeID()));
77 void UninstallAppTask::DidDeleteAppRoot(const SyncStatusCallback& callback,
78 int64 change_id,
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);
84 return;
87 status = metadata_database()->UnregisterApp(app_id_);
88 callback.Run(status);
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