Don't preload rarely seen large images
[chromium-blink-merge.git] / components / filesystem / file_system_impl.cc
blob69d63301ddfb5510cfb633fbb79e9c375656015d
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"
16 #include "url/gurl.h"
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
33 namespace filesystem {
35 namespace {
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;
57 base::FilePath path;
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);
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);
82 if (!path.empty()) {
83 new DirectoryImpl(directory.Pass(), path, temp_dir.Pass());
84 callback.Run(FILE_ERROR_OK);
85 } else {
86 callback.Run(FILE_ERROR_FAILED);
90 base::FilePath FileSystemImpl::GetSystemProfileDir() const {
91 base::FilePath path;
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
118 if (!base::PathExists(path))
119 base::CreateDirectory(path);
121 return 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];
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;
149 sanitized_origin->append(encoded, encoded_len);
153 } // namespace filesystem