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