Chromium Code Reviews| 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 "chrome/browser/chromeos/arc/fileapi/arc_documents_provider_document.h" | |
| 6 | |
| 7 #include <set> | |
| 8 | |
| 9 #include "base/memory/ptr_util.h" | |
| 10 #include "base/strings/string_util.h" | |
| 11 #include "base/strings/stringprintf.h" | |
| 12 #include "chrome/browser/chromeos/arc/fileapi/arc_documents_provider_util.h" | |
| 13 #include "net/base/mime_util.h" | |
| 14 | |
| 15 namespace arc { | |
| 16 | |
| 17 ArcDocumentsProviderDocument::ArcDocumentsProviderDocument( | |
| 18 const std::string& document_id, | |
| 19 bool is_directory) | |
| 20 : document_id_(document_id), | |
| 21 is_directory_(is_directory), | |
| 22 children_(is_directory ? new ChildMap() : nullptr) {} | |
| 23 | |
| 24 ArcDocumentsProviderDocument::~ArcDocumentsProviderDocument() = default; | |
| 25 | |
| 26 // static | |
| 27 ArcDocumentsProviderDocument* ArcDocumentsProviderDocument::CreateFromDocument( | |
| 28 const mojom::Document& document) { | |
| 29 return new ArcDocumentsProviderDocument( | |
| 30 document.document_id, document.mime_type == kAndroidDirectoryMimeType); | |
| 31 } | |
| 32 | |
| 33 ArcDocumentsProviderDocument* ArcDocumentsProviderDocument::Lookup( | |
| 34 const base::FilePath& path) { | |
| 35 if (path.empty()) { | |
|
Luis Héctor Chávez
2016/12/15 02:40:31
nit: elide braces for all the one-line if blocks.
Shuhei Takahashi
2016/12/15 06:50:57
Done.
| |
| 36 return this; | |
| 37 } | |
| 38 ArcDocumentsProviderDocument* current = this; | |
| 39 std::vector<base::FilePath::StringType> components; | |
| 40 path.GetComponents(&components); | |
| 41 for (const base::FilePath::StringType& component : components) { | |
| 42 if (!current->is_directory()) { | |
| 43 return nullptr; | |
| 44 } | |
| 45 auto iter = current->children_->find(component); | |
| 46 if (iter == current->children_->end()) { | |
| 47 return nullptr; | |
| 48 } | |
| 49 current = iter->second.get(); | |
| 50 } | |
| 51 return current; | |
| 52 } | |
| 53 | |
| 54 void ArcDocumentsProviderDocument::UpdateWithChildDocuments( | |
| 55 const std::vector<mojom::DocumentPtr>& children) { | |
| 56 DCHECK(is_directory_); | |
| 57 | |
| 58 ChildMap& old_map = *children_; | |
| 59 ChildMap new_map; | |
| 60 | |
| 61 std::set<mojom::Document*> processed; | |
|
Luis Héctor Chávez
2016/12/15 02:40:31
How large do you think these maps/sets are going t
Shuhei Takahashi
2016/12/15 06:50:57
It is the number of files in a single directory, s
Luis Héctor Chávez
2016/12/15 23:54:26
sort of. depends on the load factor. On 64-bit mac
| |
| 62 | |
| 63 // If a document is in |old_map|, keep the mapping for them. | |
| 64 { | |
| 65 std::map<std::string, base::FilePath::StringType> reverse_old_map; | |
| 66 for (const auto& pair : old_map) { | |
| 67 reverse_old_map[pair.second->document_id()] = pair.first; | |
| 68 } | |
| 69 for (const mojom::DocumentPtr& child : children) { | |
| 70 auto iter = reverse_old_map.find(child->document_id); | |
| 71 if (iter != reverse_old_map.end()) { | |
|
Luis Héctor Chávez
2016/12/15 02:40:31
nit:
if (iter == reverse_old_map.end())
continu
Shuhei Takahashi
2016/12/15 06:50:57
Done.
| |
| 72 new_map[iter->second].reset(CreateFromDocument(*child)); | |
| 73 processed.insert(&*child); | |
| 74 } | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 // Process new documents without name conflicts. | |
| 79 std::map<base::FilePath::StringType, int> name_counters; | |
|
Luis Héctor Chávez
2016/12/15 02:40:31
Can you also wrap this in a block to ensure |name_
Shuhei Takahashi
2016/12/15 06:50:57
Done.
| |
| 80 for (const mojom::DocumentPtr& child : children) { | |
| 81 if (processed.count(&*child) > 0) { | |
| 82 continue; | |
| 83 } | |
| 84 name_counters[GetFileNameForDocument(child)] += 1; | |
| 85 } | |
| 86 for (const mojom::DocumentPtr& child : children) { | |
| 87 if (processed.count(&*child) > 0) { | |
| 88 continue; | |
| 89 } | |
| 90 base::FilePath::StringType filename = GetFileNameForDocument(child); | |
| 91 DCHECK(name_counters[filename] >= 1); | |
| 92 if (name_counters[filename] == 1 && new_map.count(filename) == 0) { | |
| 93 new_map[filename].reset(CreateFromDocument(*child)); | |
| 94 processed.insert(&*child); | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 // Finally, deal with conflicts. | |
| 99 std::map<std::string, int> suffix_counters; | |
| 100 for (const mojom::DocumentPtr& child : children) { | |
| 101 if (processed.count(&*child) > 0) { | |
| 102 continue; | |
| 103 } | |
| 104 | |
| 105 base::FilePath::StringType filename = GetFileNameForDocument(child); | |
| 106 | |
| 107 if (new_map.count(filename) > 0) { | |
| 108 // Resolve a conflict by adding a suffix. | |
| 109 int& suffix_counter = suffix_counters[filename]; | |
| 110 if (suffix_counter == 0) { | |
| 111 suffix_counter = 1; | |
| 112 } | |
| 113 while (true) { | |
| 114 std::string suffix = base::StringPrintf(" %d", suffix_counter); | |
| 115 ++suffix_counter; | |
|
Luis Héctor Chávez
2016/12/15 02:40:31
Why not move this above L114? That way you don't n
Shuhei Takahashi
2016/12/15 06:50:57
Sounds like a good idea.
| |
| 116 base::FilePath::StringType new_filename = | |
| 117 base::FilePath(filename).InsertBeforeExtensionASCII(suffix).value(); | |
| 118 if (new_map.count(new_filename) == 0) { | |
| 119 filename = new_filename; | |
| 120 break; | |
| 121 } | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 new_map[filename].reset(CreateFromDocument(*child)); | |
| 126 processed.insert(&*child); | |
| 127 } | |
| 128 | |
| 129 DCHECK_EQ(processed.size(), children.size()); | |
| 130 | |
| 131 old_map.swap(new_map); | |
| 132 } | |
| 133 | |
| 134 // static | |
| 135 base::FilePath::StringType ArcDocumentsProviderDocument::GetFileNameForDocument( | |
| 136 const mojom::DocumentPtr& document) { | |
| 137 base::FilePath::StringType filename = document->display_name; | |
| 138 | |
| 139 // Replace path separators appearing in the file name. | |
| 140 // Chrome OS is POSIX and kSeparators is "/". | |
| 141 base::ReplaceChars(filename, base::FilePath::kSeparators, "_", &filename); | |
| 142 | |
| 143 // Do not allow "." and "..", though this is very unlikely to happen. | |
| 144 if (filename == base::FilePath::kCurrentDirectory) { | |
| 145 filename = "dot"; | |
| 146 } else if (filename == base::FilePath::kParentDirectory) { | |
| 147 filename = "dotdot"; | |
| 148 } | |
| 149 | |
| 150 // Since Chrome detects MIME type from file name extensions, we need to change | |
| 151 // the file name extension of the document if it does not match with its MIME | |
| 152 // type. | |
| 153 // For example, Audio Media Provider presents a music file with its title as | |
| 154 // the file name. | |
| 155 base::FilePath::StringType extension = | |
| 156 base::ToLowerASCII(base::FilePath(filename).Extension()); | |
| 157 if (!extension.empty()) { | |
| 158 extension = extension.substr(1); // Strip the leading dot. | |
| 159 } | |
| 160 std::vector<base::FilePath::StringType> possible_extensions; | |
| 161 net::GetExtensionsForMimeType(document->mime_type, &possible_extensions); | |
| 162 if (!possible_extensions.empty() && | |
| 163 std::find(possible_extensions.begin(), possible_extensions.end(), | |
| 164 extension) == possible_extensions.end()) { | |
| 165 std::string new_extension; | |
| 166 if (!net::GetPreferredExtensionForMimeType(document->mime_type, | |
| 167 &new_extension)) { | |
| 168 new_extension = possible_extensions[0]; | |
| 169 } | |
| 170 filename = | |
| 171 base::StringPrintf("%s.%s", filename.c_str(), new_extension.c_str()); | |
| 172 } | |
| 173 | |
| 174 return filename; | |
| 175 } | |
| 176 | |
| 177 } // namespace arc | |
| OLD | NEW |