1c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// found in the LICENSE file.
4c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/extensions/api/sync_file_system/sync_file_system_api_helpers.h"
6c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
7c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/logging.h"
81320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/file_system_url.h"
91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/common/fileapi/file_system_util.h"
10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace extensions {
12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
13c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)api::sync_file_system::ServiceStatus SyncServiceStateToExtensionEnum(
14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    sync_file_system::SyncServiceState state) {
15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  switch (state) {
16c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case sync_file_system::SYNC_SERVICE_RUNNING:
17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return api::sync_file_system::SERVICE_STATUS_RUNNING;
18c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case sync_file_system::SYNC_SERVICE_AUTHENTICATION_REQUIRED:
19c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return api::sync_file_system::SERVICE_STATUS_AUTHENTICATION_REQUIRED;
20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case sync_file_system::SYNC_SERVICE_TEMPORARY_UNAVAILABLE:
21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return api::sync_file_system::SERVICE_STATUS_TEMPORARY_UNAVAILABLE;
22c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case sync_file_system::SYNC_SERVICE_DISABLED:
23c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return api::sync_file_system::SERVICE_STATUS_DISABLED;
24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
25c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  NOTREACHED() << "Invalid state: " << state;
26c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return api::sync_file_system::SERVICE_STATUS_NONE;
27c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
28c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)api::sync_file_system::FileStatus SyncFileStatusToExtensionEnum(
30c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    sync_file_system::SyncFileStatus status) {
31c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  switch (status) {
32c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case sync_file_system::SYNC_FILE_STATUS_SYNCED:
33c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return api::sync_file_system::FILE_STATUS_SYNCED;
34c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case sync_file_system::SYNC_FILE_STATUS_HAS_PENDING_CHANGES:
35c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return api::sync_file_system::FILE_STATUS_PENDING;
36c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case sync_file_system::SYNC_FILE_STATUS_CONFLICTING:
37c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return api::sync_file_system::FILE_STATUS_CONFLICTING;
38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case sync_file_system::SYNC_FILE_STATUS_UNKNOWN:
39c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return api::sync_file_system::FILE_STATUS_NONE;
40c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
41c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  NOTREACHED() << "Invalid status: " << status;
42c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return api::sync_file_system::FILE_STATUS_NONE;
43c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)api::sync_file_system::SyncAction SyncActionToExtensionEnum(
46c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    sync_file_system::SyncAction action) {
47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  switch (action) {
48c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case sync_file_system::SYNC_ACTION_ADDED:
49c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return api::sync_file_system::SYNC_ACTION_ADDED;
50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case sync_file_system::SYNC_ACTION_UPDATED:
51c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return api::sync_file_system::SYNC_ACTION_UPDATED;
52c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case sync_file_system::SYNC_ACTION_DELETED:
53c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return api::sync_file_system::SYNC_ACTION_DELETED;
54c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case sync_file_system::SYNC_ACTION_NONE:
55c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return api::sync_file_system::SYNC_ACTION_NONE;
56c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  NOTREACHED() << "Invalid action: " << action;
58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return api::sync_file_system::SYNC_ACTION_NONE;
59c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
60c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
61c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)api::sync_file_system::SyncDirection SyncDirectionToExtensionEnum(
62c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    sync_file_system::SyncDirection direction) {
63c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  switch (direction) {
64c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case sync_file_system::SYNC_DIRECTION_LOCAL_TO_REMOTE:
65c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return api::sync_file_system::SYNC_DIRECTION_LOCAL_TO_REMOTE;
66c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case sync_file_system::SYNC_DIRECTION_REMOTE_TO_LOCAL:
67c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return api::sync_file_system::SYNC_DIRECTION_REMOTE_TO_LOCAL;
68c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case sync_file_system::SYNC_DIRECTION_NONE:
69c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return api::sync_file_system::SYNC_DIRECTION_NONE;
70c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
71c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  NOTREACHED() << "Invalid direction: " << direction;
72c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return api::sync_file_system::SYNC_DIRECTION_NONE;
73c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
74c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
75c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)sync_file_system::ConflictResolutionPolicy
76c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)ExtensionEnumToConflictResolutionPolicy(
77c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    api::sync_file_system::ConflictResolutionPolicy policy) {
78c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  switch (policy) {
79c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case api::sync_file_system::CONFLICT_RESOLUTION_POLICY_NONE:
80eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      return sync_file_system::CONFLICT_RESOLUTION_POLICY_UNKNOWN;
81c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case api::sync_file_system::CONFLICT_RESOLUTION_POLICY_LAST_WRITE_WIN:
82eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      return sync_file_system::CONFLICT_RESOLUTION_POLICY_LAST_WRITE_WIN;
83c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case api::sync_file_system::CONFLICT_RESOLUTION_POLICY_MANUAL:
84eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      return sync_file_system::CONFLICT_RESOLUTION_POLICY_MANUAL;
85c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
86c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  NOTREACHED() << "Invalid conflict resolution policy: " << policy;
87eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return sync_file_system::CONFLICT_RESOLUTION_POLICY_UNKNOWN;
88c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
89c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
90c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)api::sync_file_system::ConflictResolutionPolicy
91c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)ConflictResolutionPolicyToExtensionEnum(
92c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    sync_file_system::ConflictResolutionPolicy policy) {
93c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  switch (policy) {
94eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    case sync_file_system::CONFLICT_RESOLUTION_POLICY_UNKNOWN:
95c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return api::sync_file_system::CONFLICT_RESOLUTION_POLICY_NONE;
96eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    case sync_file_system::CONFLICT_RESOLUTION_POLICY_LAST_WRITE_WIN:
97c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        return api::sync_file_system::CONFLICT_RESOLUTION_POLICY_LAST_WRITE_WIN;
98eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    case sync_file_system::CONFLICT_RESOLUTION_POLICY_MANUAL:
99c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return api::sync_file_system::CONFLICT_RESOLUTION_POLICY_MANUAL;
100a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    case sync_file_system::CONFLICT_RESOLUTION_POLICY_MAX:
101a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      NOTREACHED();
102a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return api::sync_file_system::CONFLICT_RESOLUTION_POLICY_NONE;
103c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
104c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  NOTREACHED() << "Invalid conflict resolution policy: " << policy;
105c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return api::sync_file_system::CONFLICT_RESOLUTION_POLICY_NONE;
106c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
107c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
108c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)base::DictionaryValue* CreateDictionaryValueForFileSystemEntry(
10903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const storage::FileSystemURL& url,
110c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    sync_file_system::SyncFileType file_type) {
111c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!url.is_valid() || file_type == sync_file_system::SYNC_FILE_TYPE_UNKNOWN)
112c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return NULL;
113c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
11403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  std::string file_path =
11503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      base::FilePath(storage::VirtualPath::GetNormalizedFilePath(url.path()))
11603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)          .AsUTF8Unsafe();
117c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
11803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  std::string root_url =
11903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      storage::GetFileSystemRootURI(url.origin(), url.mount_type()).spec();
120c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!url.filesystem_id().empty()) {
121c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    root_url.append(url.filesystem_id());
122c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    root_url.append("/");
123c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
124c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
125c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::DictionaryValue* dict = new base::DictionaryValue;
126c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  dict->SetString("fileSystemType",
12703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                  storage::GetFileSystemTypeString(url.mount_type()));
128c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  dict->SetString("fileSystemName",
12903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                  storage::GetFileSystemName(url.origin(), url.type()));
130c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  dict->SetString("rootUrl", root_url);
131c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  dict->SetString("filePath", file_path);
132c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  dict->SetBoolean("isDirectory",
133c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                   (file_type == sync_file_system::SYNC_FILE_TYPE_DIRECTORY));
134c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
135c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return dict;
136c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
137c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
138c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace extensions
139