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 "components/filesystem/file_system_app.h"
16 #include "mojo/application/public/cpp/application_connection.h"
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"
34 namespace filesystem
{
38 const char kEscapeChar
= ',';
40 const char kUserDataDir
[] = "user-data-dir";
42 } // namespace filesystem
44 FileSystemImpl::FileSystemImpl(FileSystemApp
* app
,
45 mojo::ApplicationConnection
* connection
,
46 mojo::InterfaceRequest
<FileSystem
> request
)
48 remote_application_url_(connection
->GetRemoteApplicationURL()),
49 binding_(this, request
.Pass()) {
52 FileSystemImpl::~FileSystemImpl() {
55 void FileSystemImpl::OpenFileSystem(const mojo::String
& file_system
,
56 mojo::InterfaceRequest
<Directory
> directory
,
57 FileSystemClientPtr client
,
58 const OpenFileSystemCallback
& callback
) {
59 // Set only if the |DirectoryImpl| will own a temporary directory.
60 scoped_ptr
<base::ScopedTempDir
> temp_dir
;
62 if (file_system
.get() == std::string("temp")) {
63 temp_dir
.reset(new base::ScopedTempDir
);
64 CHECK(temp_dir
->CreateUniqueTempDir());
65 path
= temp_dir
->path();
66 } else if (file_system
.get() == std::string("origin")) {
67 base::FilePath base_profile_dir
= GetSystemProfileDir();
69 // Sanitize the url for disk access.
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
);
78 path
= base_profile_dir
.Append(base::UTF8ToWide(sanitized_origin
));
80 path
= base_profile_dir
.Append(sanitized_origin
);
82 if (!base::PathExists(path
))
83 base::CreateDirectory(path
);
87 DirectoryImpl
* dir_impl
=
88 new DirectoryImpl(directory
.Pass(), path
, temp_dir
.Pass());
89 app_
->RegisterDirectoryToClient(dir_impl
, client
.Pass());
90 callback
.Run(FILE_ERROR_OK
);
92 callback
.Run(FILE_ERROR_FAILED
);
96 base::FilePath
FileSystemImpl::GetSystemProfileDir() const {
99 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
100 if (command_line
->HasSwitch(kUserDataDir
)) {
101 path
= command_line
->GetSwitchValuePath(kUserDataDir
);
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"));
124 if (!base::PathExists(path
))
125 base::CreateDirectory(path
);
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
];
142 if ((ch
== '_') || (ch
== '.') || (ch
== '=') || (ch
== '+') ||
143 (ch
== '-') || (('0' <= ch
) && (ch
<= '9')) ||
144 (('A' <= ch
) && (ch
<= 'Z')) || (('a' <= ch
) && (ch
<= 'z'))) {
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';
155 sanitized_origin
->append(encoded
, encoded_len
);
159 } // namespace filesystem