Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/drive/drive_api_util.h" | 5 #include "chrome/browser/drive/drive_api_util.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/md5.h" | 11 #include "base/md5.h" |
| 12 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/values.h" | 16 #include "base/values.h" |
| 17 #include "google_apis/drive/drive_api_parser.h" | 17 #include "google_apis/drive/drive_api_parser.h" |
| 18 #include "net/base/escape.h" | 18 #include "net/base/escape.h" |
| 19 #include "net/base/io_buffer.h" | |
| 20 #include "net/base/net_errors.h" | |
| 21 #include "storage/browser/blob/file_stream_reader.h" | |
| 19 #include "third_party/re2/re2/re2.h" | 22 #include "third_party/re2/re2/re2.h" |
| 20 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 21 | 24 |
| 22 namespace drive { | 25 namespace drive { |
| 23 namespace util { | 26 namespace util { |
| 24 namespace { | 27 namespace { |
| 25 | 28 |
| 26 struct HostedDocumentKind { | 29 struct HostedDocumentKind { |
| 27 const char* mime_type; | 30 const char* mime_type; |
| 28 const char* extension; | 31 const char* extension; |
| 29 }; | 32 }; |
| 30 | 33 |
| 31 const HostedDocumentKind kHostedDocumentKinds[] = { | 34 const HostedDocumentKind kHostedDocumentKinds[] = { |
| 32 {kGoogleDocumentMimeType, ".gdoc"}, | 35 {kGoogleDocumentMimeType, ".gdoc"}, |
| 33 {kGoogleSpreadsheetMimeType, ".gsheet"}, | 36 {kGoogleSpreadsheetMimeType, ".gsheet"}, |
| 34 {kGooglePresentationMimeType, ".gslides"}, | 37 {kGooglePresentationMimeType, ".gslides"}, |
| 35 {kGoogleDrawingMimeType, ".gdraw"}, | 38 {kGoogleDrawingMimeType, ".gdraw"}, |
| 36 {kGoogleTableMimeType, ".gtable"}, | 39 {kGoogleTableMimeType, ".gtable"}, |
| 37 {kGoogleFormMimeType, ".gform"}, | 40 {kGoogleFormMimeType, ".gform"}, |
| 38 {kGoogleMapMimeType, ".gmaps"}, | 41 {kGoogleMapMimeType, ".gmaps"}, |
| 39 }; | 42 }; |
| 40 | 43 |
| 41 const char kUnknownHostedDocumentExtension[] = ".glink"; | 44 const char kUnknownHostedDocumentExtension[] = ".glink"; |
| 42 | 45 |
| 46 const int kMd5DigestBufferSize = 512 * 1024; // 512 kB. | |
| 47 | |
| 43 } // namespace | 48 } // namespace |
| 44 | 49 |
| 45 std::string EscapeQueryStringValue(const std::string& str) { | 50 std::string EscapeQueryStringValue(const std::string& str) { |
| 46 std::string result; | 51 std::string result; |
| 47 result.reserve(str.size()); | 52 result.reserve(str.size()); |
| 48 for (size_t i = 0; i < str.size(); ++i) { | 53 for (size_t i = 0; i < str.size(); ++i) { |
| 49 if (str[i] == '\\' || str[i] == '\'') { | 54 if (str[i] == '\\' || str[i] == '\'') { |
| 50 result.push_back('\\'); | 55 result.push_back('\\'); |
| 51 } | 56 } |
| 52 result.push_back(str[i]); | 57 result.push_back(str[i]); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 // If resource ID is in the old WAPI format starting with a prefix like | 129 // If resource ID is in the old WAPI format starting with a prefix like |
| 125 // "document:", strip it and return the remaining part. | 130 // "document:", strip it and return the remaining part. |
| 126 std::string stripped_resource_id; | 131 std::string stripped_resource_id; |
| 127 if (RE2::FullMatch(resource_id, "^[a-z-]+(?::|%3A)([\\w-]+)$", | 132 if (RE2::FullMatch(resource_id, "^[a-z-]+(?::|%3A)([\\w-]+)$", |
| 128 &stripped_resource_id)) | 133 &stripped_resource_id)) |
| 129 return stripped_resource_id; | 134 return stripped_resource_id; |
| 130 return resource_id; | 135 return resource_id; |
| 131 } | 136 } |
| 132 | 137 |
| 133 std::string GetMd5Digest(const base::FilePath& file_path) { | 138 std::string GetMd5Digest(const base::FilePath& file_path) { |
| 134 const int kBufferSize = 512 * 1024; // 512kB. | |
| 135 | |
| 136 base::File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); | 139 base::File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 137 if (!file.IsValid()) | 140 if (!file.IsValid()) |
| 138 return std::string(); | 141 return std::string(); |
| 139 | 142 |
| 140 base::MD5Context context; | 143 base::MD5Context context; |
| 141 base::MD5Init(&context); | 144 base::MD5Init(&context); |
| 142 | 145 |
| 143 int64 offset = 0; | 146 int64 offset = 0; |
| 144 scoped_ptr<char[]> buffer(new char[kBufferSize]); | 147 scoped_ptr<char[]> buffer(new char[kMd5DigestBufferSize]); |
| 145 while (true) { | 148 while (true) { |
| 146 int result = file.Read(offset, buffer.get(), kBufferSize); | 149 int result = file.Read(offset, buffer.get(), kMd5DigestBufferSize); |
| 147 if (result < 0) { | 150 if (result < 0) { |
| 148 // Found an error. | 151 // Found an error. |
| 149 return std::string(); | 152 return std::string(); |
| 150 } | 153 } |
| 151 | 154 |
| 152 if (result == 0) { | 155 if (result == 0) { |
| 153 // End of file. | 156 // End of file. |
| 154 break; | 157 break; |
| 155 } | 158 } |
| 156 | 159 |
| 157 offset += result; | 160 offset += result; |
| 158 base::MD5Update(&context, base::StringPiece(buffer.get(), result)); | 161 base::MD5Update(&context, base::StringPiece(buffer.get(), result)); |
| 159 } | 162 } |
| 160 | 163 |
| 161 base::MD5Digest digest; | 164 base::MD5Digest digest; |
| 162 base::MD5Final(&digest, &context); | 165 base::MD5Final(&digest, &context); |
| 163 return MD5DigestToBase16(digest); | 166 return MD5DigestToBase16(digest); |
| 164 } | 167 } |
| 165 | 168 |
| 169 FileStreamMd5Digester::FileStreamMd5Digester() | |
| 170 : buffer_(new net::IOBuffer(kMd5DigestBufferSize)) { | |
| 171 } | |
| 172 | |
| 173 FileStreamMd5Digester::~FileStreamMd5Digester() { | |
| 174 } | |
| 175 | |
| 176 void FileStreamMd5Digester::GetMd5Digest( | |
| 177 scoped_ptr<storage::FileStreamReader> stream_reader, | |
| 178 const ResultCallback& callback) { | |
| 179 reader_ = stream_reader.Pass(); | |
| 180 callback_ = callback; | |
| 181 base::MD5Init(&md5_context_); | |
| 182 | |
| 183 // Start the read/hash. | |
| 184 ReadNextChunk(); | |
| 185 } | |
| 186 | |
| 187 void FileStreamMd5Digester::ReadNextChunk() { | |
| 188 const int result = reader_->Read( | |
| 189 buffer_.get(), kMd5DigestBufferSize, | |
| 190 base::Bind(&FileStreamMd5Digester::OnChunkRead, base::Unretained(this))); | |
| 191 if (result != net::ERR_IO_PENDING) { | |
|
yoshiki
2015/02/04 16:33:57
nit: we don't need braces.
Ben Kwa
2015/02/04 16:49:07
Done.
| |
| 192 OnChunkRead(result); | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 void FileStreamMd5Digester::OnChunkRead(int result) { | |
| 197 if (result < 0) { | |
| 198 // Error - just return empty string. | |
| 199 callback_.Run(""); | |
| 200 return; | |
| 201 } else if (result == 0) { | |
| 202 // EOF. | |
| 203 base::MD5Digest digest; | |
| 204 base::MD5Final(&digest, &md5_context_); | |
| 205 std::string result = MD5DigestToBase16(digest); | |
| 206 callback_.Run(result); | |
| 207 return; | |
| 208 } | |
| 209 | |
| 210 // Read data and digest it. | |
| 211 base::MD5Update(&md5_context_, base::StringPiece(buffer_->data(), result)); | |
| 212 | |
| 213 // Kick off the next read. | |
| 214 ReadNextChunk(); | |
| 215 } | |
| 216 | |
| 166 std::string GetHostedDocumentExtension(const std::string& mime_type) { | 217 std::string GetHostedDocumentExtension(const std::string& mime_type) { |
| 167 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { | 218 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { |
| 168 if (mime_type == kHostedDocumentKinds[i].mime_type) | 219 if (mime_type == kHostedDocumentKinds[i].mime_type) |
| 169 return kHostedDocumentKinds[i].extension; | 220 return kHostedDocumentKinds[i].extension; |
| 170 } | 221 } |
| 171 return kUnknownHostedDocumentExtension; | 222 return kUnknownHostedDocumentExtension; |
| 172 } | 223 } |
| 173 | 224 |
| 174 bool IsKnownHostedDocumentMimeType(const std::string& mime_type) { | 225 bool IsKnownHostedDocumentMimeType(const std::string& mime_type) { |
| 175 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { | 226 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { |
| 176 if (mime_type == kHostedDocumentKinds[i].mime_type) | 227 if (mime_type == kHostedDocumentKinds[i].mime_type) |
| 177 return true; | 228 return true; |
| 178 } | 229 } |
| 179 return false; | 230 return false; |
| 180 } | 231 } |
| 181 | 232 |
| 182 bool HasHostedDocumentExtension(const base::FilePath& path) { | 233 bool HasHostedDocumentExtension(const base::FilePath& path) { |
| 183 const std::string extension = base::FilePath(path.Extension()).AsUTF8Unsafe(); | 234 const std::string extension = base::FilePath(path.Extension()).AsUTF8Unsafe(); |
| 184 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { | 235 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { |
| 185 if (extension == kHostedDocumentKinds[i].extension) | 236 if (extension == kHostedDocumentKinds[i].extension) |
| 186 return true; | 237 return true; |
| 187 } | 238 } |
| 188 return extension == kUnknownHostedDocumentExtension; | 239 return extension == kUnknownHostedDocumentExtension; |
| 189 } | 240 } |
| 190 | 241 |
| 191 } // namespace util | 242 } // namespace util |
| 192 } // namespace drive | 243 } // namespace drive |
| OLD | NEW |