1 // Copyright (c) 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/extensions/api/sync_file_system/sync_file_system_api_helpers.h" 6 7 #include "base/logging.h" 8 #include "webkit/browser/fileapi/file_system_url.h" 9 #include "webkit/common/fileapi/file_system_util.h" 10 11 namespace extensions { 12 13 api::sync_file_system::ServiceStatus SyncServiceStateToExtensionEnum( 14 sync_file_system::SyncServiceState state) { 15 switch (state) { 16 case sync_file_system::SYNC_SERVICE_RUNNING: 17 return api::sync_file_system::SERVICE_STATUS_RUNNING; 18 case sync_file_system::SYNC_SERVICE_AUTHENTICATION_REQUIRED: 19 return api::sync_file_system::SERVICE_STATUS_AUTHENTICATION_REQUIRED; 20 case sync_file_system::SYNC_SERVICE_TEMPORARY_UNAVAILABLE: 21 return api::sync_file_system::SERVICE_STATUS_TEMPORARY_UNAVAILABLE; 22 case sync_file_system::SYNC_SERVICE_DISABLED: 23 return api::sync_file_system::SERVICE_STATUS_DISABLED; 24 } 25 NOTREACHED() << "Invalid state: " << state; 26 return api::sync_file_system::SERVICE_STATUS_NONE; 27 } 28 29 api::sync_file_system::FileStatus SyncFileStatusToExtensionEnum( 30 sync_file_system::SyncFileStatus status) { 31 switch (status) { 32 case sync_file_system::SYNC_FILE_STATUS_SYNCED: 33 return api::sync_file_system::FILE_STATUS_SYNCED; 34 case sync_file_system::SYNC_FILE_STATUS_HAS_PENDING_CHANGES: 35 return api::sync_file_system::FILE_STATUS_PENDING; 36 case sync_file_system::SYNC_FILE_STATUS_CONFLICTING: 37 return api::sync_file_system::FILE_STATUS_CONFLICTING; 38 case sync_file_system::SYNC_FILE_STATUS_UNKNOWN: 39 return api::sync_file_system::FILE_STATUS_NONE; 40 } 41 NOTREACHED() << "Invalid status: " << status; 42 return api::sync_file_system::FILE_STATUS_NONE; 43 } 44 45 api::sync_file_system::SyncAction SyncActionToExtensionEnum( 46 sync_file_system::SyncAction action) { 47 switch (action) { 48 case sync_file_system::SYNC_ACTION_ADDED: 49 return api::sync_file_system::SYNC_ACTION_ADDED; 50 case sync_file_system::SYNC_ACTION_UPDATED: 51 return api::sync_file_system::SYNC_ACTION_UPDATED; 52 case sync_file_system::SYNC_ACTION_DELETED: 53 return api::sync_file_system::SYNC_ACTION_DELETED; 54 case sync_file_system::SYNC_ACTION_NONE: 55 return api::sync_file_system::SYNC_ACTION_NONE; 56 } 57 NOTREACHED() << "Invalid action: " << action; 58 return api::sync_file_system::SYNC_ACTION_NONE; 59 } 60 61 api::sync_file_system::SyncDirection SyncDirectionToExtensionEnum( 62 sync_file_system::SyncDirection direction) { 63 switch (direction) { 64 case sync_file_system::SYNC_DIRECTION_LOCAL_TO_REMOTE: 65 return api::sync_file_system::SYNC_DIRECTION_LOCAL_TO_REMOTE; 66 case sync_file_system::SYNC_DIRECTION_REMOTE_TO_LOCAL: 67 return api::sync_file_system::SYNC_DIRECTION_REMOTE_TO_LOCAL; 68 case sync_file_system::SYNC_DIRECTION_NONE: 69 return api::sync_file_system::SYNC_DIRECTION_NONE; 70 } 71 NOTREACHED() << "Invalid direction: " << direction; 72 return api::sync_file_system::SYNC_DIRECTION_NONE; 73 } 74 75 sync_file_system::ConflictResolutionPolicy 76 ExtensionEnumToConflictResolutionPolicy( 77 api::sync_file_system::ConflictResolutionPolicy policy) { 78 switch (policy) { 79 case api::sync_file_system::CONFLICT_RESOLUTION_POLICY_NONE: 80 return sync_file_system::CONFLICT_RESOLUTION_POLICY_UNKNOWN; 81 case api::sync_file_system::CONFLICT_RESOLUTION_POLICY_LAST_WRITE_WIN: 82 return sync_file_system::CONFLICT_RESOLUTION_POLICY_LAST_WRITE_WIN; 83 case api::sync_file_system::CONFLICT_RESOLUTION_POLICY_MANUAL: 84 return sync_file_system::CONFLICT_RESOLUTION_POLICY_MANUAL; 85 } 86 NOTREACHED() << "Invalid conflict resolution policy: " << policy; 87 return sync_file_system::CONFLICT_RESOLUTION_POLICY_UNKNOWN; 88 } 89 90 api::sync_file_system::ConflictResolutionPolicy 91 ConflictResolutionPolicyToExtensionEnum( 92 sync_file_system::ConflictResolutionPolicy policy) { 93 switch (policy) { 94 case sync_file_system::CONFLICT_RESOLUTION_POLICY_UNKNOWN: 95 return api::sync_file_system::CONFLICT_RESOLUTION_POLICY_NONE; 96 case sync_file_system::CONFLICT_RESOLUTION_POLICY_LAST_WRITE_WIN: 97 return api::sync_file_system::CONFLICT_RESOLUTION_POLICY_LAST_WRITE_WIN; 98 case sync_file_system::CONFLICT_RESOLUTION_POLICY_MANUAL: 99 return api::sync_file_system::CONFLICT_RESOLUTION_POLICY_MANUAL; 100 } 101 NOTREACHED() << "Invalid conflict resolution policy: " << policy; 102 return api::sync_file_system::CONFLICT_RESOLUTION_POLICY_NONE; 103 } 104 105 base::DictionaryValue* CreateDictionaryValueForFileSystemEntry( 106 const fileapi::FileSystemURL& url, 107 sync_file_system::SyncFileType file_type) { 108 if (!url.is_valid() || file_type == sync_file_system::SYNC_FILE_TYPE_UNKNOWN) 109 return NULL; 110 111 std::string file_path = base::FilePath( 112 fileapi::VirtualPath::GetNormalizedFilePath(url.path())).AsUTF8Unsafe(); 113 114 std::string root_url = fileapi::GetFileSystemRootURI( 115 url.origin(), url.mount_type()).spec(); 116 if (!url.filesystem_id().empty()) { 117 root_url.append(url.filesystem_id()); 118 root_url.append("/"); 119 } 120 121 base::DictionaryValue* dict = new base::DictionaryValue; 122 dict->SetString("fileSystemType", 123 fileapi::GetFileSystemTypeString(url.mount_type())); 124 dict->SetString("fileSystemName", 125 fileapi::GetFileSystemName(url.origin(), url.type())); 126 dict->SetString("rootUrl", root_url); 127 dict->SetString("filePath", file_path); 128 dict->SetBoolean("isDirectory", 129 (file_type == sync_file_system::SYNC_FILE_TYPE_DIRECTORY)); 130 131 return dict; 132 } 133 134 } // namespace extensions 135