| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/filesystem/file_system_impl.h" | 5 #include "components/filesystem/file_system_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/command_line.h" | |
| 11 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 12 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 13 #include "base/files/scoped_file.h" | 12 #include "base/files/scoped_file.h" |
| 14 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 15 #include "base/logging.h" | 14 #include "base/logging.h" |
| 16 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 17 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 18 #include "components/filesystem/directory_impl.h" | 17 #include "components/filesystem/directory_impl.h" |
| 19 #include "mojo/shell/public/cpp/connection.h" | 18 #include "mojo/shell/public/cpp/connection.h" |
| 20 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 21 | 20 |
| 22 #if defined(OS_WIN) | |
| 23 #include "base/base_paths_win.h" | |
| 24 #include "base/path_service.h" | |
| 25 #include "base/strings/utf_string_conversions.h" | |
| 26 #elif defined(OS_ANDROID) | |
| 27 #include "base/base_paths_android.h" | |
| 28 #include "base/path_service.h" | |
| 29 #elif defined(OS_LINUX) | |
| 30 #include "base/environment.h" | |
| 31 #include "base/nix/xdg_util.h" | |
| 32 #elif defined(OS_MACOSX) | |
| 33 #include "base/base_paths_mac.h" | |
| 34 #include "base/path_service.h" | |
| 35 #endif | |
| 36 | |
| 37 namespace filesystem { | 21 namespace filesystem { |
| 38 | 22 |
| 39 namespace { | |
| 40 | |
| 41 const char kEscapeChar = ','; | |
| 42 | |
| 43 const char kUserDataDir[] = "user-data-dir"; | |
| 44 | |
| 45 } // namespace filesystem | |
| 46 | |
| 47 FileSystemImpl::FileSystemImpl(mojo::Connection* connection, | 23 FileSystemImpl::FileSystemImpl(mojo::Connection* connection, |
| 48 mojo::InterfaceRequest<FileSystem> request, | 24 mojo::InterfaceRequest<FileSystem> request, |
| 25 base::FilePath user_data_dir, |
| 49 LockTable* lock_table) | 26 LockTable* lock_table) |
| 50 : remote_application_url_(connection->GetRemoteApplicationURL()), | 27 : remote_application_url_(connection->GetRemoteApplicationURL()), |
| 51 binding_(this, std::move(request)), | 28 binding_(this, std::move(request)), |
| 52 lock_table_(lock_table) {} | 29 lock_table_(lock_table), |
| 30 user_data_dir_(user_data_dir) {} |
| 53 | 31 |
| 54 FileSystemImpl::~FileSystemImpl() { | 32 FileSystemImpl::~FileSystemImpl() { |
| 55 } | 33 } |
| 56 | 34 |
| 57 void FileSystemImpl::OpenFileSystem(const mojo::String& file_system, | 35 void FileSystemImpl::OpenTempDirectory( |
| 58 mojo::InterfaceRequest<Directory> directory, | 36 mojo::InterfaceRequest<Directory> directory, |
| 59 FileSystemClientPtr client, | 37 const OpenTempDirectoryCallback& callback) { |
| 60 const OpenFileSystemCallback& callback) { | |
| 61 // Set only if the |DirectoryImpl| will own a temporary directory. | 38 // Set only if the |DirectoryImpl| will own a temporary directory. |
| 62 scoped_ptr<base::ScopedTempDir> temp_dir; | 39 scoped_ptr<base::ScopedTempDir> temp_dir(new base::ScopedTempDir); |
| 63 base::FilePath path; | 40 CHECK(temp_dir->CreateUniqueTempDir()); |
| 64 if (file_system.get() == std::string("temp")) { | |
| 65 temp_dir.reset(new base::ScopedTempDir); | |
| 66 CHECK(temp_dir->CreateUniqueTempDir()); | |
| 67 path = temp_dir->path(); | |
| 68 } else if (file_system.get() == std::string("origin")) { | |
| 69 base::FilePath base_profile_dir = GetSystemProfileDir(); | |
| 70 | 41 |
| 71 // Sanitize the url for disk access. | 42 base::FilePath path = temp_dir->path(); |
| 72 // | 43 new DirectoryImpl( |
| 73 // TODO(erg): While it's currently impossible, we need to deal with http:// | 44 std::move(directory), path, std::move(temp_dir), lock_table_); |
| 74 // URLs that have a path. (Or make the decision that these file systems are | 45 callback.Run(FileError::OK); |
| 75 // path bound, not origin bound.) | |
| 76 std::string sanitized_origin; | |
| 77 BuildSanitizedOrigin(remote_application_url_, &sanitized_origin); | |
| 78 | |
| 79 #if defined(OS_WIN) | |
| 80 path = base_profile_dir.Append(base::UTF8ToWide(sanitized_origin)); | |
| 81 #else | |
| 82 path = base_profile_dir.Append(sanitized_origin); | |
| 83 #endif | |
| 84 if (!base::PathExists(path)) | |
| 85 base::CreateDirectory(path); | |
| 86 } | |
| 87 | |
| 88 if (!path.empty()) { | |
| 89 new DirectoryImpl( | |
| 90 std::move(directory), path, std::move(temp_dir), lock_table_); | |
| 91 callback.Run(FileError::OK); | |
| 92 } else { | |
| 93 callback.Run(FileError::FAILED); | |
| 94 } | |
| 95 } | 46 } |
| 96 | 47 |
| 97 base::FilePath FileSystemImpl::GetSystemProfileDir() const { | 48 void FileSystemImpl::OpenPersistentFileSystem( |
| 98 base::FilePath path; | 49 mojo::InterfaceRequest<Directory> directory, |
| 99 | 50 const OpenPersistentFileSystemCallback& callback) { |
| 100 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 51 scoped_ptr<base::ScopedTempDir> temp_dir; |
| 101 if (command_line->HasSwitch(kUserDataDir)) { | 52 base::FilePath path = user_data_dir_; |
| 102 path = command_line->GetSwitchValuePath(kUserDataDir); | |
| 103 } else { | |
| 104 #if defined(OS_WIN) | |
| 105 CHECK(PathService::Get(base::DIR_LOCAL_APP_DATA, &path)); | |
| 106 path = path.Append(FILE_PATH_LITERAL("mandoline")); | |
| 107 #elif defined(OS_LINUX) | |
| 108 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
| 109 base::FilePath config_dir( | |
| 110 base::nix::GetXDGDirectory(env.get(), | |
| 111 base::nix::kXdgConfigHomeEnvVar, | |
| 112 base::nix::kDotConfigDir)); | |
| 113 path = config_dir.Append("mandoline"); | |
| 114 #elif defined(OS_MACOSX) | |
| 115 CHECK(PathService::Get(base::DIR_APP_DATA, &path)); | |
| 116 path = path.Append("Mandoline Shell"); | |
| 117 #elif defined(OS_ANDROID) | |
| 118 CHECK(PathService::Get(base::DIR_ANDROID_APP_DATA, &path)); | |
| 119 path = path.Append(FILE_PATH_LITERAL("mandoline")); | |
| 120 #else | |
| 121 NOTIMPLEMENTED(); | |
| 122 #endif | |
| 123 } | |
| 124 | |
| 125 if (!base::PathExists(path)) | 53 if (!base::PathExists(path)) |
| 126 base::CreateDirectory(path); | 54 base::CreateDirectory(path); |
| 127 | 55 |
| 128 return path; | 56 new DirectoryImpl( |
| 129 } | 57 std::move(directory), path, std::move(temp_dir), lock_table_); |
| 130 | 58 callback.Run(FileError::OK); |
| 131 void FileSystemImpl::BuildSanitizedOrigin( | |
| 132 const std::string& origin, | |
| 133 std::string* sanitized_origin) { | |
| 134 // We take the origin string, and encode it in a way safe for filesystem | |
| 135 // access. This is vaguely based on //net/tools/dump_cache/ | |
| 136 // url_to_filename_encoder.h; that file strips out schemes, and does weird | |
| 137 // things with subdirectories. We do follow the basic algorithm used there, | |
| 138 // including using ',' as our escape character. | |
| 139 for (size_t i = 0; i < origin.length(); ++i) { | |
| 140 unsigned char ch = origin[i]; | |
| 141 char encoded[3]; | |
| 142 int encoded_len; | |
| 143 if ((ch == '_') || (ch == '.') || (ch == '=') || (ch == '+') || | |
| 144 (ch == '-') || (('0' <= ch) && (ch <= '9')) || | |
| 145 (('A' <= ch) && (ch <= 'Z')) || (('a' <= ch) && (ch <= 'z'))) { | |
| 146 encoded[0] = ch; | |
| 147 encoded_len = 1; | |
| 148 } else { | |
| 149 encoded[0] = kEscapeChar; | |
| 150 encoded[1] = ch / 16; | |
| 151 encoded[1] += (encoded[1] >= 10) ? 'A' - 10 : '0'; | |
| 152 encoded[2] = ch % 16; | |
| 153 encoded[2] += (encoded[2] >= 10) ? 'A' - 10 : '0'; | |
| 154 encoded_len = 3; | |
| 155 } | |
| 156 sanitized_origin->append(encoded, encoded_len); | |
| 157 } | |
| 158 } | 59 } |
| 159 | 60 |
| 160 } // namespace filesystem | 61 } // namespace filesystem |
| OLD | NEW |