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
3 // found in the LICENSE file.
5 #include "components/filesystem/file_system_impl.h"
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/files/scoped_file.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "components/filesystem/directory_impl.h"
15 #include "mojo/application/public/cpp/application_connection.h"
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"
33 namespace filesystem
{
37 const char kEscapeChar
= ',';
39 const char kUserDataDir
[] = "user-data-dir";
41 } // namespace filesystem
43 FileSystemImpl::FileSystemImpl(mojo::ApplicationConnection
* connection
,
44 mojo::InterfaceRequest
<FileSystem
> request
)
45 : remote_application_url_(connection
->GetRemoteApplicationURL()),
46 binding_(this, request
.Pass()) {
49 FileSystemImpl::~FileSystemImpl() {
52 void FileSystemImpl::OpenFileSystem(const mojo::String
& file_system
,
53 mojo::InterfaceRequest
<Directory
> directory
,
54 const OpenFileSystemCallback
& callback
) {
55 // Set only if the |DirectoryImpl| will own a temporary directory.
56 scoped_ptr
<base::ScopedTempDir
> temp_dir
;
58 if (file_system
.get() == std::string("temp")) {
59 temp_dir
.reset(new base::ScopedTempDir
);
60 CHECK(temp_dir
->CreateUniqueTempDir());
61 path
= temp_dir
->path();
62 } else if (file_system
.get() == std::string("origin")) {
63 base::FilePath base_profile_dir
= GetSystemProfileDir();
65 // Sanitize the url for disk access.
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
);
74 path
= base_profile_dir
.Append(base::UTF8ToWide(sanitized_origin
));
76 path
= base_profile_dir
.Append(sanitized_origin
);
78 if (!base::PathExists(path
))
79 base::CreateDirectory(path
);
83 new DirectoryImpl(directory
.Pass(), path
, temp_dir
.Pass());
84 callback
.Run(FILE_ERROR_OK
);
86 callback
.Run(FILE_ERROR_FAILED
);
90 base::FilePath
FileSystemImpl::GetSystemProfileDir() const {
93 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
94 if (command_line
->HasSwitch(kUserDataDir
)) {
95 path
= command_line
->GetSwitchValuePath(kUserDataDir
);
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"));
118 if (!base::PathExists(path
))
119 base::CreateDirectory(path
);
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
];
136 if ((ch
== '_') || (ch
== '.') || (ch
== '=') || (ch
== '+') ||
137 (ch
== '-') || (('0' <= ch
) && (ch
<= '9')) ||
138 (('A' <= ch
) && (ch
<= 'Z')) || (('a' <= ch
) && (ch
<= 'z'))) {
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';
149 sanitized_origin
->append(encoded
, encoded_len
);
153 } // namespace filesystem