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/drive/file_system_core_util.h"
10 #include "base/basictypes.h"
11 #include "base/bind.h"
12 #include "base/bind_helpers.h"
13 #include "base/files/file_path.h"
14 #include "base/files/file_util.h"
15 #include "base/i18n/icu_string_conversions.h"
16 #include "base/json/json_file_value_serializer.h"
17 #include "base/logging.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/prefs/pref_service.h"
20 #include "base/strings/string_number_conversions.h"
21 #include "base/strings/string_util.h"
22 #include "base/strings/stringprintf.h"
23 #include "base/thread_task_runner_handle.h"
24 #include "base/threading/sequenced_worker_pool.h"
25 #include "components/drive/drive.pb.h"
26 #include "components/drive/drive_pref_names.h"
27 #include "components/drive/job_list.h"
34 std::string
ReadStringFromGDocFile(const base::FilePath
& file_path
,
35 const std::string
& key
) {
36 const int64 kMaxGDocSize
= 4096;
38 if (!base::GetFileSize(file_path
, &file_size
) || file_size
> kMaxGDocSize
) {
39 LOG(WARNING
) << "File too large to be a GDoc file " << file_path
.value();
43 JSONFileValueDeserializer
reader(file_path
);
44 std::string error_message
;
45 scoped_ptr
<base::Value
> root_value(reader
.Deserialize(NULL
, &error_message
));
47 LOG(WARNING
) << "Failed to parse " << file_path
.value() << " as JSON."
48 << " error = " << error_message
;
52 base::DictionaryValue
* dictionary_value
= NULL
;
54 if (!root_value
->GetAsDictionary(&dictionary_value
) ||
55 !dictionary_value
->GetString(key
, &result
)) {
56 LOG(WARNING
) << "No value for the given key is stored in "
57 << file_path
.value() << ". key = " << key
;
66 const base::FilePath
& GetDriveGrandRootPath() {
67 CR_DEFINE_STATIC_LOCAL(
68 base::FilePath
, grand_root_path
,
69 (base::FilePath::FromUTF8Unsafe(kDriveGrandRootDirName
)));
70 return grand_root_path
;
73 const base::FilePath
& GetDriveMyDriveRootPath() {
74 CR_DEFINE_STATIC_LOCAL(
75 base::FilePath
, drive_root_path
,
76 (GetDriveGrandRootPath().AppendASCII(kDriveMyDriveRootDirName
)));
77 return drive_root_path
;
80 std::string
EscapeCacheFileName(const std::string
& filename
) {
81 // This is based on net/base/escape.cc: net::(anonymous namespace)::Escape
83 for (size_t i
= 0; i
< filename
.size(); ++i
) {
85 if (c
== '%' || c
== '.' || c
== '/') {
86 base::StringAppendF(&escaped
, "%%%02X", c
);
94 std::string
UnescapeCacheFileName(const std::string
& filename
) {
95 std::string unescaped
;
96 for (size_t i
= 0; i
< filename
.size(); ++i
) {
98 if (c
== '%' && i
+ 2 < filename
.length()) {
99 c
= (base::HexDigitToInt(filename
[i
+ 1]) << 4) +
100 base::HexDigitToInt(filename
[i
+ 2]);
103 unescaped
.push_back(c
);
108 std::string
NormalizeFileName(const std::string
& input
) {
109 DCHECK(base::IsStringUTF8(input
));
112 if (!base::ConvertToUtf8AndNormalize(input
, base::kCodepageUTF8
, &output
))
114 base::ReplaceChars(output
, "/", "_", &output
);
115 if (!output
.empty() && output
.find_first_not_of('.', 0) == std::string::npos
)
120 void EmptyFileOperationCallback(FileError error
) {
123 bool CreateGDocFile(const base::FilePath
& file_path
,
125 const std::string
& resource_id
) {
126 std::string content
=
127 base::StringPrintf("{\"url\": \"%s\", \"resource_id\": \"%s\"}",
128 url
.spec().c_str(), resource_id
.c_str());
129 return base::WriteFile(file_path
, content
.data(), content
.size()) ==
130 static_cast<int>(content
.size());
133 GURL
ReadUrlFromGDocFile(const base::FilePath
& file_path
) {
134 return GURL(ReadStringFromGDocFile(file_path
, "url"));
137 std::string
ReadResourceIdFromGDocFile(const base::FilePath
& file_path
) {
138 return ReadStringFromGDocFile(file_path
, "resource_id");