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