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" |
13 #include "mojo/application/public/cpp/application_connection.h" | 15 #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 |
14 | 32 |
15 namespace filesystem { | 33 namespace filesystem { |
16 | 34 |
| 35 namespace { |
| 36 |
| 37 const char kEscapeChar = ','; |
| 38 |
| 39 const char kUserDataDir[] = "user-data-dir"; |
| 40 |
| 41 } // namespace filesystem |
| 42 |
17 FileSystemImpl::FileSystemImpl(mojo::ApplicationConnection* connection, | 43 FileSystemImpl::FileSystemImpl(mojo::ApplicationConnection* connection, |
18 mojo::InterfaceRequest<FileSystem> request) | 44 mojo::InterfaceRequest<FileSystem> request) |
19 : remote_application_url_(connection->GetRemoteApplicationURL()), | 45 : remote_application_url_(connection->GetRemoteApplicationURL()), |
20 binding_(this, request.Pass()) { | 46 binding_(this, request.Pass()) { |
21 } | 47 } |
22 | 48 |
23 FileSystemImpl::~FileSystemImpl() { | 49 FileSystemImpl::~FileSystemImpl() { |
24 } | 50 } |
25 | 51 |
26 void FileSystemImpl::OpenFileSystem(const mojo::String& file_system, | 52 void FileSystemImpl::OpenFileSystem(const mojo::String& file_system, |
27 mojo::InterfaceRequest<Directory> directory, | 53 mojo::InterfaceRequest<Directory> directory, |
28 const OpenFileSystemCallback& callback) { | 54 const OpenFileSystemCallback& callback) { |
29 // Set only if the |DirectoryImpl| will own a temporary directory. | 55 // Set only if the |DirectoryImpl| will own a temporary directory. |
30 scoped_ptr<base::ScopedTempDir> temp_dir; | 56 scoped_ptr<base::ScopedTempDir> temp_dir; |
31 base::FilePath path; | 57 base::FilePath path; |
32 if (file_system.get() == std::string("temp")) { | 58 if (file_system.get() == std::string("temp")) { |
33 temp_dir.reset(new base::ScopedTempDir); | 59 temp_dir.reset(new base::ScopedTempDir); |
34 CHECK(temp_dir->CreateUniqueTempDir()); | 60 CHECK(temp_dir->CreateUniqueTempDir()); |
35 path = temp_dir->path(); | 61 path = temp_dir->path(); |
36 } else if (file_system.get() == std::string("origin")) { | 62 } else if (file_system.get() == std::string("origin")) { |
37 // TODO(erg): We should serve a persistent directory based on the | 63 base::FilePath base_profile_dir = GetSystemProfileDir(); |
38 // subdirectory |remote_application_url_| of a profile directory. | 64 |
| 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); |
39 } | 80 } |
40 | 81 |
41 if (!path.empty()) { | 82 if (!path.empty()) { |
42 new DirectoryImpl(directory.Pass(), path, temp_dir.Pass()); | 83 new DirectoryImpl(directory.Pass(), path, temp_dir.Pass()); |
43 callback.Run(FILE_ERROR_OK); | 84 callback.Run(FILE_ERROR_OK); |
44 } else { | 85 } else { |
45 callback.Run(FILE_ERROR_FAILED); | 86 callback.Run(FILE_ERROR_FAILED); |
46 } | 87 } |
47 } | 88 } |
48 | 89 |
| 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 |
49 } // namespace filesystem | 153 } // namespace filesystem |
OLD | NEW |