Rename GetIconID to GetIconId
[chromium-blink-merge.git] / components / filesystem / file_system_impl.cc
blob7dfe5df3855c21ef5d8af915fe25d752fc8a45fb
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"
17 #include "url/gurl.h"
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
34 namespace filesystem {
36 namespace {
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)
47 : app_(app),
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;
61 base::FilePath path;
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);
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);
86 if (!path.empty()) {
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);
91 } else {
92 callback.Run(FILE_ERROR_FAILED);
96 base::FilePath FileSystemImpl::GetSystemProfileDir() const {
97 base::FilePath path;
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
124 if (!base::PathExists(path))
125 base::CreateDirectory(path);
127 return 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];
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;
155 sanitized_origin->append(encoded, encoded_len);
159 } // namespace filesystem