- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync_file_system / drive_backend_v1 / drive_file_sync_util.cc
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 #include "chrome/browser/sync_file_system/drive_backend_v1/drive_file_sync_util.h"
6
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "chrome/browser/sync_file_system/logger.h"
10
11 namespace sync_file_system {
12
13 namespace {
14
15 // A command-line switch to disable Drive API and to make Sync FileSystem API
16 // work on WAPI (http://crbug.com/234557)
17 // TODO(nhiroki): this command-line switch should be temporary.
18 const char kDisableDriveAPI[] = "disable-drive-api-for-syncfs";
19
20 bool is_drive_api_disabled = false;
21
22 }  // namespace
23
24 SyncStatusCode GDataErrorCodeToSyncStatusCode(
25     google_apis::GDataErrorCode error) {
26   // NOTE: Please update DriveFileSyncService::UpdateServiceState when you add
27   // more error code mapping.
28   switch (error) {
29     case google_apis::HTTP_SUCCESS:
30     case google_apis::HTTP_CREATED:
31     case google_apis::HTTP_NO_CONTENT:
32     case google_apis::HTTP_FOUND:
33       return SYNC_STATUS_OK;
34
35     case google_apis::HTTP_NOT_MODIFIED:
36       return SYNC_STATUS_NOT_MODIFIED;
37
38     case google_apis::HTTP_CONFLICT:
39     case google_apis::HTTP_PRECONDITION:
40       return SYNC_STATUS_HAS_CONFLICT;
41
42     case google_apis::HTTP_UNAUTHORIZED:
43       return SYNC_STATUS_AUTHENTICATION_FAILED;
44
45     case google_apis::GDATA_NO_CONNECTION:
46       return SYNC_STATUS_NETWORK_ERROR;
47
48     case google_apis::HTTP_INTERNAL_SERVER_ERROR:
49     case google_apis::HTTP_BAD_GATEWAY:
50     case google_apis::HTTP_SERVICE_UNAVAILABLE:
51     case google_apis::GDATA_CANCELLED:
52     case google_apis::GDATA_NOT_READY:
53       return SYNC_STATUS_SERVICE_TEMPORARILY_UNAVAILABLE;
54
55     case google_apis::HTTP_NOT_FOUND:
56       return SYNC_FILE_ERROR_NOT_FOUND;
57
58     case google_apis::GDATA_FILE_ERROR:
59       return SYNC_FILE_ERROR_FAILED;
60
61     case google_apis::HTTP_FORBIDDEN:
62       return SYNC_STATUS_ACCESS_FORBIDDEN;
63
64     case google_apis::HTTP_RESUME_INCOMPLETE:
65     case google_apis::HTTP_BAD_REQUEST:
66     case google_apis::HTTP_LENGTH_REQUIRED:
67     case google_apis::HTTP_NOT_IMPLEMENTED:
68     case google_apis::GDATA_PARSE_ERROR:
69     case google_apis::GDATA_OTHER_ERROR:
70       return SYNC_STATUS_FAILED;
71
72     case google_apis::GDATA_NO_SPACE:
73       return SYNC_FILE_ERROR_NO_SPACE;
74   }
75
76   // There's a case where DriveService layer returns GDataErrorCode==-1
77   // when network is unavailable. (http://crbug.com/223042)
78   // TODO(kinuko,nhiroki): We should identify from where this undefined error
79   // code is coming.
80   if (error == -1)
81     return SYNC_STATUS_NETWORK_ERROR;
82
83   util::Log(logging::LOG_WARNING,
84             FROM_HERE,
85             "Got unexpected error: %d",
86             static_cast<int>(error));
87   return SYNC_STATUS_FAILED;
88 }
89
90 void SetDisableDriveAPI(bool flag) {
91   is_drive_api_disabled = flag;
92 }
93
94 bool IsDriveAPIDisabled() {
95   return is_drive_api_disabled ||
96       CommandLine::ForCurrentProcess()->HasSwitch(kDisableDriveAPI);
97 }
98
99 ScopedDisableDriveAPI::ScopedDisableDriveAPI()
100     : was_disabled_(IsDriveAPIDisabled()) {
101   SetDisableDriveAPI(true);
102 }
103
104 ScopedDisableDriveAPI::~ScopedDisableDriveAPI() {
105   DCHECK(IsDriveAPIDisabled());
106   SetDisableDriveAPI(was_disabled_);
107 }
108
109 }  // namespace sync_file_system