OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_FORM_DATA_PARSER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_FORM_DATA_PARSER_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 // Cannot forward declare StringPiece because it is a typedef. |
| 13 #include "base/string_piece.h" |
| 14 |
| 15 namespace net { |
| 16 class URLRequest; |
| 17 } |
| 18 |
| 19 namespace extensions { |
| 20 |
| 21 // Interface for the form data parsers. |
| 22 class FormDataParser { |
| 23 public: |
| 24 class Result { |
| 25 public: |
| 26 Result(); |
| 27 ~Result(); |
| 28 const std::string& name() const { |
| 29 return name_; |
| 30 } |
| 31 const std::string& value() const { |
| 32 return value_; |
| 33 } |
| 34 void set_name(const base::StringPiece& str) { |
| 35 str.CopyToString(&name_); |
| 36 } |
| 37 void set_value(const base::StringPiece& str) { |
| 38 str.CopyToString(&value_); |
| 39 } |
| 40 void set_name(const std::string& str) { |
| 41 name_ = str; |
| 42 } |
| 43 void set_value(const std::string& str) { |
| 44 value_ = str; |
| 45 } |
| 46 void Reset(); |
| 47 |
| 48 private: |
| 49 std::string name_; |
| 50 std::string value_; |
| 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(Result); |
| 53 }; |
| 54 |
| 55 virtual ~FormDataParser(); |
| 56 |
| 57 // Creates a correct parser instance based on the |request|. Returns NULL |
| 58 // on failure. |
| 59 static scoped_ptr<FormDataParser> Create(const net::URLRequest* request); |
| 60 |
| 61 // Creates a correct parser instance based on |content_type_header|, the |
| 62 // "Content-Type" request header value. If |content_type_header| is NULL, it |
| 63 // defaults to "application/x-www-form-urlencoded". Returns NULL on failure. |
| 64 static scoped_ptr<FormDataParser> Create( |
| 65 const std::string* content_type_header); |
| 66 |
| 67 // Returns true if there was some data, it was well formed and all was read. |
| 68 virtual bool AllDataReadOK() = 0; |
| 69 |
| 70 // Returns the next name-value pair as |result|. After SetSource has |
| 71 // succeeded, this allows to iterate over all pairs in the source. |
| 72 // Returns true as long as a new pair was successfully found. |
| 73 virtual bool GetNextNameValue(Result* result) = 0; |
| 74 |
| 75 // Sets the |source| of the data to be parsed. The ownership is left with the |
| 76 // caller and the source should live until |this| dies or |this->SetSource()| |
| 77 // is called again, whichever comes sooner. Returns true on success. |
| 78 virtual bool SetSource(const base::StringPiece& source) = 0; |
| 79 |
| 80 protected: |
| 81 FormDataParser(); |
| 82 |
| 83 private: |
| 84 DISALLOW_COPY_AND_ASSIGN(FormDataParser); |
| 85 }; |
| 86 |
| 87 } // namespace extensions |
| 88 |
| 89 #endif // CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_FORM_DATA_PARSER_H_ |
OLD | NEW |