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