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
11namespace sync_file_system {
12
13namespace {
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.
18const char kDisableDriveAPI[] = "disable-drive-api-for-syncfs";
19
20bool is_drive_api_disabled = false;
21
22}  // namespace
23
24SyncStatusCode 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    case google_apis::HTTP_GONE:
57      return SYNC_FILE_ERROR_NOT_FOUND;
58
59    case google_apis::GDATA_FILE_ERROR:
60      return SYNC_FILE_ERROR_FAILED;
61
62    case google_apis::HTTP_FORBIDDEN:
63      return SYNC_STATUS_ACCESS_FORBIDDEN;
64
65    case google_apis::HTTP_RESUME_INCOMPLETE:
66    case google_apis::HTTP_BAD_REQUEST:
67    case google_apis::HTTP_LENGTH_REQUIRED:
68    case google_apis::HTTP_NOT_IMPLEMENTED:
69    case google_apis::GDATA_PARSE_ERROR:
70    case google_apis::GDATA_OTHER_ERROR:
71      return SYNC_STATUS_FAILED;
72
73    case google_apis::GDATA_NO_SPACE:
74      return SYNC_FILE_ERROR_NO_SPACE;
75  }
76
77  // There's a case where DriveService layer returns GDataErrorCode==-1
78  // when network is unavailable. (http://crbug.com/223042)
79  // TODO(kinuko,nhiroki): We should identify from where this undefined error
80  // code is coming.
81  if (error == -1)
82    return SYNC_STATUS_NETWORK_ERROR;
83
84  util::Log(logging::LOG_WARNING,
85            FROM_HERE,
86            "Got unexpected error: %d",
87            static_cast<int>(error));
88  return SYNC_STATUS_FAILED;
89}
90
91void SetDisableDriveAPI(bool flag) {
92  is_drive_api_disabled = flag;
93}
94
95bool IsDriveAPIDisabled() {
96  return is_drive_api_disabled ||
97      CommandLine::ForCurrentProcess()->HasSwitch(kDisableDriveAPI);
98}
99
100ScopedDisableDriveAPI::ScopedDisableDriveAPI()
101    : was_disabled_(IsDriveAPIDisabled()) {
102  SetDisableDriveAPI(true);
103}
104
105ScopedDisableDriveAPI::~ScopedDisableDriveAPI() {
106  DCHECK(IsDriveAPIDisabled());
107  SetDisableDriveAPI(was_disabled_);
108}
109
110}  // namespace sync_file_system
111