OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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. | |
4 | |
5 #include "components/profile_service/profile_service_impl.h" | |
6 | |
7 #include "base/files/file_path.h" | |
8 #include "base/files/file_util.h" | |
9 #include "base/files/scoped_temp_dir.h" | |
10 #include "components/filesystem/directory_impl.h" | |
11 #include "components/filesystem/lock_table.h" | |
12 #include "mojo/shell/public/cpp/connection.h" | |
13 | |
14 #if defined(OS_WIN) | |
15 #include "base/strings/sys_string_conversions.h" | |
16 #endif | |
17 | |
18 namespace profile { | |
19 | |
20 namespace { | |
21 | |
22 const char kEscapeChar = ','; | |
23 | |
24 } // namespace | |
25 | |
26 ProfileServiceImpl::ProfileServiceImpl( | |
27 mojo::Connection* connection, | |
28 mojo::InterfaceRequest<ProfileService> request, | |
29 base::FilePath base_profile_dir, | |
30 filesystem::LockTable* lock_table) | |
31 : binding_(this, std::move(request)), | |
32 lock_table_(lock_table) { | |
33 std::string sanitized_origin = BuildSanitizedOrigin( | |
jam
2016/02/27 01:34:45
it wasn't clear to me that GetDirectory returns a
Elliot Glaysher
2016/02/29 23:04:13
My assumption was that the conversion code would l
| |
34 connection->GetRemoteApplicationURL()); | |
35 #if defined(OS_WIN) | |
36 path_ = base_profile_dir.Append(base::UTF8ToWide(sanitized_origin)); | |
37 #else | |
38 path_ = base_profile_dir.Append(sanitized_origin); | |
39 #endif | |
40 if (!base::PathExists(path_)) | |
41 base::CreateDirectory(path_); | |
42 } | |
43 | |
44 ProfileServiceImpl::~ProfileServiceImpl() { | |
45 } | |
46 | |
47 void ProfileServiceImpl::GetDirectory( | |
48 mojo::InterfaceRequest<filesystem::Directory> request) { | |
49 new filesystem::DirectoryImpl(std::move(request), | |
50 path_, | |
51 scoped_ptr<base::ScopedTempDir>(), | |
52 lock_table_); | |
53 } | |
54 | |
55 // static | |
56 std::string ProfileServiceImpl::BuildSanitizedOrigin( | |
57 const std::string& origin) { | |
58 std::string sanitized_origin; | |
59 // We take the origin string, and encode it in a way safe for filesystem | |
60 // access. This is vaguely based on //net/tools/dump_cache/ | |
61 // url_to_filename_encoder.h; that file strips out schemes, and does weird | |
62 // things with subdirectories. We do follow the basic algorithm used there, | |
63 // including using ',' as our escape character. | |
64 for (size_t i = 0; i < origin.length(); ++i) { | |
65 unsigned char ch = origin[i]; | |
66 char encoded[3]; | |
67 int encoded_len; | |
68 if ((ch == '_') || (ch == '.') || (ch == '=') || (ch == '+') || | |
69 (ch == '-') || (('0' <= ch) && (ch <= '9')) || | |
70 (('A' <= ch) && (ch <= 'Z')) || (('a' <= ch) && (ch <= 'z'))) { | |
71 encoded[0] = ch; | |
72 encoded_len = 1; | |
73 } else { | |
74 encoded[0] = kEscapeChar; | |
75 encoded[1] = ch / 16; | |
76 encoded[1] += (encoded[1] >= 10) ? 'A' - 10 : '0'; | |
77 encoded[2] = ch % 16; | |
78 encoded[2] += (encoded[2] >= 10) ? 'A' - 10 : '0'; | |
79 encoded_len = 3; | |
80 } | |
81 sanitized_origin.append(encoded, encoded_len); | |
82 } | |
83 return sanitized_origin; | |
84 } | |
85 | |
86 } // namespace profile | |
OLD | NEW |