Update optimize_png_files.sh to work on msysgit bash.
[chromium-blink-merge.git] / content / child / ftp_directory_listing_response_delegate.cc
blobe6e7feb0edac6013e474f3812cbbdff869020bb6
1 // Copyright 2014 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 "content/child/ftp_directory_listing_response_delegate.h"
7 #include <vector>
9 #include "base/i18n/icu_encoding_detection.h"
10 #include "base/i18n/icu_string_conversions.h"
11 #include "base/logging.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/sys_string_conversions.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/time/time.h"
16 #include "net/base/escape.h"
17 #include "net/base/net_errors.h"
18 #include "net/base/net_util.h"
19 #include "net/ftp/ftp_directory_listing_parser.h"
20 #include "third_party/WebKit/public/platform/WebURL.h"
21 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
22 #include "webkit/child/weburlresponse_extradata_impl.h"
24 using net::FtpDirectoryListingEntry;
26 using blink::WebURLLoader;
27 using blink::WebURLLoaderClient;
28 using blink::WebURLResponse;
30 using webkit_glue::WebURLResponseExtraDataImpl;
32 namespace {
34 base::string16 ConvertPathToUTF16(const std::string& path) {
35 // Per RFC 2640, FTP servers should use UTF-8 or its proper subset ASCII,
36 // but many old FTP servers use legacy encodings. Try UTF-8 first.
37 if (base::IsStringUTF8(path))
38 return base::UTF8ToUTF16(path);
40 // Try detecting the encoding. The sample is rather small though, so it may
41 // fail.
42 std::string encoding;
43 if (base::DetectEncoding(path, &encoding) && !encoding.empty()) {
44 base::string16 path_utf16;
45 if (base::CodepageToUTF16(path, encoding.c_str(),
46 base::OnStringConversionError::SUBSTITUTE,
47 &path_utf16)) {
48 return path_utf16;
52 // Use system native encoding as the last resort.
53 return base::WideToUTF16(base::SysNativeMBToWide(path));
56 } // namespace
58 namespace content {
60 FtpDirectoryListingResponseDelegate::FtpDirectoryListingResponseDelegate(
61 WebURLLoaderClient* client,
62 WebURLLoader* loader,
63 const WebURLResponse& response)
64 : client_(client),
65 loader_(loader) {
66 if (response.extraData()) {
67 // extraData can be NULL during tests.
68 WebURLResponseExtraDataImpl* extra_data =
69 static_cast<WebURLResponseExtraDataImpl*>(response.extraData());
70 extra_data->set_is_ftp_directory_listing(true);
72 Init(response.url());
75 void FtpDirectoryListingResponseDelegate::OnReceivedData(const char* data,
76 int data_len) {
77 buffer_.append(data, data_len);
80 void FtpDirectoryListingResponseDelegate::OnCompletedRequest() {
81 std::vector<FtpDirectoryListingEntry> entries;
82 int rv = net::ParseFtpDirectoryListing(buffer_, base::Time::Now(), &entries);
83 if (rv != net::OK) {
84 SendDataToClient("<script>onListingParsingError();</script>\n");
85 return;
87 for (size_t i = 0; i < entries.size(); i++) {
88 const FtpDirectoryListingEntry& entry = entries[i];
90 // Skip the current and parent directory entries in the listing. Our header
91 // always includes them.
92 if (EqualsASCII(entry.name, ".") || EqualsASCII(entry.name, ".."))
93 continue;
95 bool is_directory = (entry.type == FtpDirectoryListingEntry::DIRECTORY);
96 int64 size = entry.size;
97 if (entry.type != FtpDirectoryListingEntry::FILE)
98 size = 0;
99 SendDataToClient(net::GetDirectoryListingEntry(
100 entry.name, entry.raw_name, is_directory, size, entry.last_modified));
104 void FtpDirectoryListingResponseDelegate::Init(const GURL& response_url) {
105 net::UnescapeRule::Type unescape_rules = net::UnescapeRule::SPACES |
106 net::UnescapeRule::URL_SPECIAL_CHARS;
107 std::string unescaped_path = net::UnescapeURLComponent(response_url.path(),
108 unescape_rules);
109 SendDataToClient(net::GetDirectoryListingHeader(
110 ConvertPathToUTF16(unescaped_path)));
112 // If this isn't top level directory (i.e. the path isn't "/",)
113 // add a link to the parent directory.
114 if (response_url.path().length() > 1) {
115 SendDataToClient(net::GetDirectoryListingEntry(
116 base::ASCIIToUTF16(".."), std::string(), false, 0, base::Time()));
120 void FtpDirectoryListingResponseDelegate::SendDataToClient(
121 const std::string& data) {
122 client_->didReceiveData(loader_, data.data(), data.length(), -1);
125 } // namespace content