1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this 2 // source code is governed by a BSD-style license that can be found in the 3 // LICENSE file. 4 5 #ifndef NET_FTP_FTP_DIRECTORY_LISTING_BUFFER_H_ 6 #define NET_FTP_FTP_DIRECTORY_LISTING_BUFFER_H_ 7 8 #include <deque> 9 #include <set> 10 #include <string> 11 12 #include "base/basictypes.h" 13 #include "base/string16.h" 14 #include "base/time.h" 15 #include "net/ftp/ftp_server_type_histograms.h" 16 17 namespace net { 18 19 struct FtpDirectoryListingEntry; 20 class FtpDirectoryListingParser; 21 22 class FtpDirectoryListingBuffer { 23 public: 24 FtpDirectoryListingBuffer(); 25 26 ~FtpDirectoryListingBuffer(); 27 28 // Called when data is received from the data socket. Returns network 29 // error code. 30 int ConsumeData(const char* data, int data_length); 31 32 // Called when all received data has been consumed by this buffer. Tells the 33 // buffer to try to parse remaining raw data and returns network error code. 34 int ProcessRemainingData(); 35 36 bool EntryAvailable() const; 37 38 // Returns the next entry. It is an error to call this function 39 // unless EntryAvailable returns true. 40 FtpDirectoryListingEntry PopEntry(); 41 42 // Returns recognized server type. It is valid to call this function at any 43 // time, although it will return SERVER_UNKNOWN if it doesn't know the answer. 44 FtpServerType GetServerType() const; 45 46 private: 47 typedef std::set<FtpDirectoryListingParser*> ParserSet; 48 49 // Converts the string |from| to detected encoding and stores it in |to|. 50 // Returns true on success. 51 bool ConvertToDetectedEncoding(const std::string& from, string16* to); 52 53 // Tries to extract full lines from the raw buffer, converting them to the 54 // detected encoding. Returns network error code. 55 int ExtractFullLinesFromBuffer(); 56 57 // Tries to parse full lines stored in |lines_|. Returns network error code. 58 int ParseLines(); 59 60 // Called when we received the entire input. Propagates that info to remaining 61 // parsers. Returns network error code. 62 int OnEndOfInput(); 63 64 // Detected encoding of the response (empty if unknown or ASCII). 65 std::string encoding_; 66 67 // Buffer to keep not-yet-split data. 68 std::string buffer_; 69 70 // CRLF-delimited lines, without the CRLF, not yet consumed by parser. 71 std::deque<string16> lines_; 72 73 // A collection of parsers for different listing styles. The parsers are owned 74 // by this FtpDirectoryListingBuffer. 75 ParserSet parsers_; 76 77 // When we're sure about the listing format, its parser is stored in 78 // |current_parser_|. 79 FtpDirectoryListingParser* current_parser_; 80 81 DISALLOW_COPY_AND_ASSIGN(FtpDirectoryListingBuffer); 82 }; 83 84 } // namespace net 85 86 #endif // NET_FTP_FTP_DIRECTORY_LISTING_BUFFER_H_ 87