| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/extensions/file_manager/private_api_mount.h" | 5 #include "chrome/browser/chromeos/extensions/file_manager/private_api_mount.h" |
| 6 | 6 |
| 7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
| 8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "chrome/browser/chromeos/drive/file_system_interface.h" | 9 #include "chrome/browser/chromeos/drive/file_system_interface.h" |
| 10 #include "chrome/browser/chromeos/drive/file_system_util.h" | 10 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 11 #include "chrome/browser/chromeos/drive/logging.h" | 11 #include "chrome/browser/chromeos/drive/logging.h" |
| 12 #include "chrome/browser/chromeos/extensions/file_manager/event_router.h" | 12 #include "chrome/browser/chromeos/extensions/file_manager/event_router.h" |
| 13 #include "chrome/browser/chromeos/extensions/file_manager/file_browser_private_a
pi.h" | 13 #include "chrome/browser/chromeos/extensions/file_manager/file_browser_private_a
pi.h" |
| 14 #include "chrome/browser/chromeos/extensions/file_manager/private_api_util.h" | 14 #include "chrome/browser/chromeos/extensions/file_manager/private_api_util.h" |
| 15 #include "chrome/browser/chromeos/file_manager/fileapi_util.h" | 15 #include "chrome/browser/chromeos/file_manager/fileapi_util.h" |
| 16 #include "chrome/browser/chromeos/file_manager/volume_manager.h" | 16 #include "chrome/browser/chromeos/file_manager/volume_manager.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chromeos/disks/disk_mount_manager.h" | 18 #include "chromeos/disks/disk_mount_manager.h" |
| 19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "ui/shell_dialogs/selected_file_info.h" | 20 #include "ui/shell_dialogs/selected_file_info.h" |
| 21 | 21 |
| 22 using chromeos::disks::DiskMountManager; | 22 using chromeos::disks::DiskMountManager; |
| 23 using content::BrowserThread; | 23 using content::BrowserThread; |
| 24 | 24 |
| 25 namespace extensions { | 25 namespace extensions { |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 // Returns string representaion of VolumeType. | |
| 29 std::string VolumeTypeToString(file_manager::VolumeType type) { | |
| 30 switch (type) { | |
| 31 case file_manager::VOLUME_TYPE_GOOGLE_DRIVE: | |
| 32 return "drive"; | |
| 33 case file_manager::VOLUME_TYPE_DOWNLOADS_DIRECTORY: | |
| 34 // Return empty string here for backword compatibility. | |
| 35 // TODO(hidehiko): Fix this to return "downloads". | |
| 36 return ""; | |
| 37 case file_manager::VOLUME_TYPE_REMOVABLE_DISK_PARTITION: | |
| 38 return "device"; | |
| 39 case file_manager::VOLUME_TYPE_MOUNTED_ARCHIVE_FILE: | |
| 40 return "file"; | |
| 41 } | |
| 42 | |
| 43 NOTREACHED(); | |
| 44 return ""; | |
| 45 } | |
| 46 | |
| 47 // Returns the Value of the |volume_info|. | 28 // Returns the Value of the |volume_info|. |
| 48 base::Value* CreateValueFromVolumeInfo( | 29 base::Value* CreateValueFromVolumeInfo( |
| 49 const file_manager::VolumeInfo& volume_info, | 30 const file_manager::VolumeInfo& volume_info, |
| 50 Profile* profile, | 31 Profile* profile, |
| 51 const std::string& extension_id) { | 32 const std::string& extension_id) { |
| 52 base::DictionaryValue* result = new base::DictionaryValue; | 33 base::DictionaryValue* result = new base::DictionaryValue; |
| 53 std::string mount_type = VolumeTypeToString(volume_info.type); | 34 std::string volume_type = |
| 54 if (!mount_type.empty()) | 35 file_manager::util::VolumeTypeToStringEnum(volume_info.type); |
| 55 result->SetString("mountType", mount_type); | 36 if (!volume_type.empty()) |
| 37 result->SetString("volumeType", volume_type); |
| 56 | 38 |
| 57 if (!volume_info.source_path.empty()) | 39 if (!volume_info.source_path.empty()) |
| 58 result->SetString("sourcePath", volume_info.source_path.value()); | 40 result->SetString("sourcePath", volume_info.source_path.value()); |
| 59 | 41 |
| 60 // Convert mount point path to relative path with the external file system | 42 // Convert mount point path to relative path with the external file system |
| 61 // exposed within File API. | 43 // exposed within File API. |
| 62 base::FilePath relative_path; | 44 base::FilePath relative_path; |
| 63 if (file_manager::util::ConvertAbsoluteFilePathToRelativeFileSystemPath( | 45 if (file_manager::util::ConvertAbsoluteFilePathToRelativeFileSystemPath( |
| 64 profile, extension_id, volume_info.mount_path, &relative_path)) | 46 profile, extension_id, volume_info.mount_path, &relative_path)) |
| 65 result->SetString("mountPath", relative_path.value()); | 47 result->SetString("mountPath", relative_path.value()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 79 } | 61 } |
| 80 | 62 |
| 81 bool FileBrowserPrivateAddMountFunction::RunImpl() { | 63 bool FileBrowserPrivateAddMountFunction::RunImpl() { |
| 82 // The third argument is simply ignored. | 64 // The third argument is simply ignored. |
| 83 if (args_->GetSize() != 2 && args_->GetSize() != 3) { | 65 if (args_->GetSize() != 2 && args_->GetSize() != 3) { |
| 84 error_ = "Invalid argument count"; | 66 error_ = "Invalid argument count"; |
| 85 return false; | 67 return false; |
| 86 } | 68 } |
| 87 | 69 |
| 88 std::string file_url; | 70 std::string file_url; |
| 89 if (!args_->GetString(0, &file_url)) { | 71 if (!args_->GetString(0, &file_url)) |
| 90 return false; | 72 return false; |
| 91 } | |
| 92 | 73 |
| 93 std::string mount_type_str; | 74 std::string mount_type; |
| 94 if (!args_->GetString(1, &mount_type_str)) { | 75 if (!args_->GetString(1, &mount_type)) |
| 95 return false; | 76 return false; |
| 96 } | |
| 97 | 77 |
| 98 drive::util::Log(logging::LOG_INFO, | 78 drive::util::Log(logging::LOG_INFO, |
| 99 "%s[%d] called. (source: '%s', type:'%s')", | 79 "%s[%d] called. (source: '%s', type:'%s')", |
| 100 name().c_str(), | 80 name().c_str(), |
| 101 request_id(), | 81 request_id(), |
| 102 file_url.empty() ? "(none)" : file_url.c_str(), | 82 file_url.empty() ? "(none)" : file_url.c_str(), |
| 103 mount_type_str.c_str()); | 83 mount_type.c_str()); |
| 104 set_log_on_completion(true); | 84 set_log_on_completion(true); |
| 105 | 85 |
| 106 // Set default return source path to the empty string. | 86 if (mount_type == "drive") { |
| 107 SetResult(new base::StringValue("")); | 87 // Dispatch fake 'mounted' event because JS code depends on it. |
| 88 // TODO(hashimoto): Remove this redanduncy. |
| 89 file_manager::FileBrowserPrivateAPI::Get(profile_)->event_router()-> |
| 90 OnFileSystemMounted(); |
| 108 | 91 |
| 109 chromeos::MountType mount_type = | 92 // Pass back the drive mount point path as source path. |
| 110 DiskMountManager::MountTypeFromString(mount_type_str); | 93 const std::string& drive_path = |
| 111 switch (mount_type) { | 94 drive::util::GetDriveMountPointPathAsString(); |
| 112 case chromeos::MOUNT_TYPE_INVALID: { | 95 SetResult(new base::StringValue(drive_path)); |
| 113 error_ = "Invalid mount type"; | 96 SendResponse(true); |
| 114 SendResponse(false); | 97 } else if (mount_type == "archive") { |
| 115 break; | 98 const base::FilePath path = file_manager::util::GetLocalPathFromURL( |
| 99 render_view_host(), profile(), GURL(file_url)); |
| 100 |
| 101 if (path.empty()) |
| 102 return false; |
| 103 |
| 104 const base::FilePath::StringType display_name = path.BaseName().value(); |
| 105 |
| 106 // Check if the source path is under Drive cache directory. |
| 107 if (drive::util::IsUnderDriveMountPoint(path)) { |
| 108 drive::FileSystemInterface* file_system = |
| 109 drive::util::GetFileSystemByProfile(profile()); |
| 110 if (!file_system) |
| 111 return false; |
| 112 |
| 113 file_system->MarkCacheFileAsMounted( |
| 114 drive::util::ExtractDrivePath(path), |
| 115 base::Bind(&FileBrowserPrivateAddMountFunction::OnMountedStateSet, |
| 116 this, display_name)); |
| 117 } else { |
| 118 OnMountedStateSet(display_name, drive::FILE_ERROR_OK, path); |
| 116 } | 119 } |
| 117 case chromeos::MOUNT_TYPE_GOOGLE_DRIVE: { | 120 } else { |
| 118 // Dispatch fake 'mounted' event because JS code depends on it. | 121 error_ = "Invalid mount type"; |
| 119 // TODO(hashimoto): Remove this redanduncy. | 122 return false; |
| 120 file_manager::FileBrowserPrivateAPI::Get(profile_)->event_router()-> | |
| 121 OnFileSystemMounted(); | |
| 122 | |
| 123 // Pass back the drive mount point path as source path. | |
| 124 const std::string& drive_path = | |
| 125 drive::util::GetDriveMountPointPathAsString(); | |
| 126 SetResult(new base::StringValue(drive_path)); | |
| 127 SendResponse(true); | |
| 128 break; | |
| 129 } | |
| 130 default: { | |
| 131 const base::FilePath path = file_manager::util::GetLocalPathFromURL( | |
| 132 render_view_host(), profile(), GURL(file_url)); | |
| 133 | |
| 134 if (path.empty()) { | |
| 135 SendResponse(false); | |
| 136 break; | |
| 137 } | |
| 138 | |
| 139 const base::FilePath::StringType display_name = path.BaseName().value(); | |
| 140 | |
| 141 // Check if the source path is under Drive cache directory. | |
| 142 if (drive::util::IsUnderDriveMountPoint(path)) { | |
| 143 drive::FileSystemInterface* file_system = | |
| 144 drive::util::GetFileSystemByProfile(profile()); | |
| 145 if (!file_system) { | |
| 146 SendResponse(false); | |
| 147 break; | |
| 148 } | |
| 149 file_system->MarkCacheFileAsMounted( | |
| 150 drive::util::ExtractDrivePath(path), | |
| 151 base::Bind(&FileBrowserPrivateAddMountFunction::OnMountedStateSet, | |
| 152 this, mount_type_str, display_name)); | |
| 153 } else { | |
| 154 OnMountedStateSet(mount_type_str, display_name, | |
| 155 drive::FILE_ERROR_OK, path); | |
| 156 } | |
| 157 break; | |
| 158 } | |
| 159 } | 123 } |
| 160 | 124 |
| 161 return true; | 125 return true; |
| 162 } | 126 } |
| 163 | 127 |
| 164 void FileBrowserPrivateAddMountFunction::OnMountedStateSet( | 128 void FileBrowserPrivateAddMountFunction::OnMountedStateSet( |
| 165 const std::string& mount_type, | |
| 166 const base::FilePath::StringType& file_name, | 129 const base::FilePath::StringType& file_name, |
| 167 drive::FileError error, | 130 drive::FileError error, |
| 168 const base::FilePath& file_path) { | 131 const base::FilePath& file_path) { |
| 169 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 170 | 133 |
| 171 if (error != drive::FILE_ERROR_OK) { | 134 if (error != drive::FILE_ERROR_OK) { |
| 172 SendResponse(false); | 135 SendResponse(false); |
| 173 return; | 136 return; |
| 174 } | 137 } |
| 175 | 138 |
| 176 DiskMountManager* disk_mount_manager = DiskMountManager::GetInstance(); | 139 DiskMountManager* disk_mount_manager = DiskMountManager::GetInstance(); |
| 177 // Pass back the actual source path of the mount point. | 140 // Pass back the actual source path of the mount point. |
| 178 SetResult(new base::StringValue(file_path.value())); | 141 SetResult(new base::StringValue(file_path.value())); |
| 179 SendResponse(true); | 142 SendResponse(true); |
| 180 // MountPath() takes a std::string. | 143 // MountPath() takes a std::string. |
| 181 disk_mount_manager->MountPath( | 144 disk_mount_manager->MountPath( |
| 182 file_path.AsUTF8Unsafe(), base::FilePath(file_name).Extension(), | 145 file_path.AsUTF8Unsafe(), base::FilePath(file_name).Extension(), |
| 183 file_name, DiskMountManager::MountTypeFromString(mount_type)); | 146 file_name, chromeos::MOUNT_TYPE_ARCHIVE); |
| 184 } | 147 } |
| 185 | 148 |
| 186 FileBrowserPrivateRemoveMountFunction::FileBrowserPrivateRemoveMountFunction() { | 149 FileBrowserPrivateRemoveMountFunction::FileBrowserPrivateRemoveMountFunction() { |
| 187 } | 150 } |
| 188 | 151 |
| 189 FileBrowserPrivateRemoveMountFunction:: | 152 FileBrowserPrivateRemoveMountFunction:: |
| 190 ~FileBrowserPrivateRemoveMountFunction() { | 153 ~FileBrowserPrivateRemoveMountFunction() { |
| 191 } | 154 } |
| 192 | 155 |
| 193 bool FileBrowserPrivateRemoveMountFunction::RunImpl() { | 156 bool FileBrowserPrivateRemoveMountFunction::RunImpl() { |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 logging::LOG_INFO, | 230 logging::LOG_INFO, |
| 268 "%s[%d] succeeded. (results: '[%s]', %" PRIuS " mount points)", | 231 "%s[%d] succeeded. (results: '[%s]', %" PRIuS " mount points)", |
| 269 name().c_str(), request_id(), log_string.c_str(), result->GetSize()); | 232 name().c_str(), request_id(), log_string.c_str(), result->GetSize()); |
| 270 | 233 |
| 271 SetResult(result); | 234 SetResult(result); |
| 272 SendResponse(true); | 235 SendResponse(true); |
| 273 return true; | 236 return true; |
| 274 } | 237 } |
| 275 | 238 |
| 276 } // namespace extensions | 239 } // namespace extensions |
| OLD | NEW |