Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / components / drive / file_system_core_util.cc
blob1e9f00ba994962be56f03c0bd240233e899cdbf5
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"
7 #include <string>
8 #include <vector>
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"
29 namespace drive {
30 namespace util {
32 namespace {
34 std::string ReadStringFromGDocFile(const base::FilePath& file_path,
35 const std::string& key) {
36 const int64 kMaxGDocSize = 4096;
37 int64 file_size = 0;
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();
40 return std::string();
43 JSONFileValueDeserializer reader(file_path);
44 std::string error_message;
45 scoped_ptr<base::Value> root_value(reader.Deserialize(NULL, &error_message));
46 if (!root_value) {
47 LOG(WARNING) << "Failed to parse " << file_path.value() << " as JSON."
48 << " error = " << error_message;
49 return std::string();
52 base::DictionaryValue* dictionary_value = NULL;
53 std::string result;
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;
58 return std::string();
61 return result;
64 } // namespace
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
82 std::string escaped;
83 for (size_t i = 0; i < filename.size(); ++i) {
84 char c = filename[i];
85 if (c == '%' || c == '.' || c == '/') {
86 base::StringAppendF(&escaped, "%%%02X", c);
87 } else {
88 escaped.push_back(c);
91 return escaped;
94 std::string UnescapeCacheFileName(const std::string& filename) {
95 std::string unescaped;
96 for (size_t i = 0; i < filename.size(); ++i) {
97 char c = filename[i];
98 if (c == '%' && i + 2 < filename.length()) {
99 c = (base::HexDigitToInt(filename[i + 1]) << 4) +
100 base::HexDigitToInt(filename[i + 2]);
101 i += 2;
103 unescaped.push_back(c);
105 return unescaped;
108 std::string NormalizeFileName(const std::string& input) {
109 DCHECK(base::IsStringUTF8(input));
111 std::string output;
112 if (!base::ConvertToUtf8AndNormalize(input, base::kCodepageUTF8, &output))
113 output = input;
114 base::ReplaceChars(output, "/", "_", &output);
115 if (!output.empty() && output.find_first_not_of('.', 0) == std::string::npos)
116 output = "_";
117 return output;
120 void EmptyFileOperationCallback(FileError error) {
123 bool CreateGDocFile(const base::FilePath& file_path,
124 const GURL& url,
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");
141 } // namespace util
142 } // namespace drive