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_UPLOAD_DATA_PRESENTER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_UPLOAD_DATA_PRESENTER_H_ |
| 7 |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 // Need to include this, because UploadData::Element cannot be forward declared. |
| 13 #include "net/base/upload_data.h" |
| 14 |
| 15 namespace base { |
| 16 class DictionaryValue; |
| 17 class ListValue; |
| 18 class Value; |
| 19 } |
| 20 |
| 21 namespace extensions { |
| 22 class FormDataParser; |
| 23 } |
| 24 |
| 25 namespace net { |
| 26 class URLRequest; |
| 27 } |
| 28 |
| 29 namespace extensions { |
| 30 |
| 31 // UploadDataPresenter is an interface for objects capable to consume a series |
| 32 // of UploadData::Element and represent this data as a base:Value. |
| 33 // |
| 34 // Workflow for objects implementing this interface: |
| 35 // 1. Call object->FeedNext(element) for each element from the request's body. |
| 36 // 2. Check if object->Succeeded(). |
| 37 // 3. If that check passed then retrieve object->Result(). |
| 38 class UploadDataPresenter { |
| 39 public: |
| 40 virtual ~UploadDataPresenter(); |
| 41 virtual void FeedNext(const net::UploadData::Element& element) = 0; |
| 42 virtual bool Succeeded() = 0; |
| 43 virtual scoped_ptr<base::Value> Result() = 0; |
| 44 |
| 45 protected: |
| 46 UploadDataPresenter() {} |
| 47 |
| 48 private: |
| 49 DISALLOW_COPY_AND_ASSIGN(UploadDataPresenter); |
| 50 }; |
| 51 |
| 52 // This class checks that chunked transfer encoding (defined in RFC 2616) |
| 53 // is not used. If it is, an error string is generated. |
| 54 class ChunkedErrorPresenter : public UploadDataPresenter { |
| 55 public: |
| 56 explicit ChunkedErrorPresenter(const net::URLRequest* request); |
| 57 virtual ~ChunkedErrorPresenter(); |
| 58 |
| 59 // Tests the headers of |request| for transfer encoding. |
| 60 static bool IsTransferEncodingChunked(const net::URLRequest* request); |
| 61 |
| 62 // Implementation of UploadDataPresenter. |
| 63 virtual void FeedNext(const net::UploadData::Element& element) OVERRIDE; |
| 64 virtual bool Succeeded() OVERRIDE; |
| 65 virtual scoped_ptr<base::Value> Result() OVERRIDE; |
| 66 |
| 67 private: |
| 68 bool chunks_found_; |
| 69 }; |
| 70 |
| 71 // This class passes all the bytes from elements of TYPE_BYTES as a BinaryValue |
| 72 // for each such element. Elements of TYPE_FILE are presented as StringValue |
| 73 // containing the path for that file. TYPE_BLOB elements are ignored, and the |
| 74 // presence of TYPE_CHUNK elements causes a failure. |
| 75 class RawDataPresenter : public UploadDataPresenter { |
| 76 public: |
| 77 RawDataPresenter(); |
| 78 virtual ~RawDataPresenter(); |
| 79 |
| 80 // Implementation of UploadDataPresenter. |
| 81 virtual void FeedNext(const net::UploadData::Element& element) OVERRIDE; |
| 82 virtual bool Succeeded() OVERRIDE; |
| 83 virtual scoped_ptr<base::Value> Result() OVERRIDE; |
| 84 |
| 85 private: |
| 86 // Clears resources and the success flag. |
| 87 void Abort(); |
| 88 |
| 89 void FeedNextBytes(const std::vector<char>& bytes); |
| 90 void FeedNextFile(const std::string& filename); |
| 91 FRIEND_TEST_ALL_PREFIXES(WebRequestUploadDataPresenterTest, RawData); |
| 92 |
| 93 bool success_; |
| 94 scoped_ptr<base::ListValue> list_; |
| 95 }; |
| 96 |
| 97 // This class inspects the contents of elements of TYPE_BYTES. It uses the |
| 98 // parser classes inheriting from FormDataParser to parse the concatenated |
| 99 // content of such elements. If the parsing is successfull, the parsed form is |
| 100 // returned as a DictionaryValue. For example, a form consisting of |
| 101 // <input name="check" type="checkbox" value="A" checked /> |
| 102 // <input name="check" type="checkbox" value="B" checked /> |
| 103 // <input name="text" type="text" value="abc" /> |
| 104 // would be represented as {"check": ["A", "B"], "text": ["abc"]} (although as a |
| 105 // DictionaryValue, not as a JSON string). |
| 106 class ParsedDataPresenter : public UploadDataPresenter { |
| 107 public: |
| 108 explicit ParsedDataPresenter(const net::URLRequest* request); |
| 109 virtual ~ParsedDataPresenter(); |
| 110 |
| 111 // Implementation of UploadDataPresenter. |
| 112 virtual void FeedNext(const net::UploadData::Element& element) OVERRIDE; |
| 113 virtual bool Succeeded() OVERRIDE; |
| 114 virtual scoped_ptr<base::Value> Result() OVERRIDE; |
| 115 |
| 116 private: |
| 117 // Clears resources and the success flag. |
| 118 void Abort(); |
| 119 scoped_ptr<FormDataParser> parser_; |
| 120 bool success_; |
| 121 scoped_ptr<base::DictionaryValue> dictionary_; |
| 122 }; |
| 123 |
| 124 } // namespace extensions |
| 125 |
| 126 #endif // CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_UPLOAD_DATA_PRESENTER_H_ |
OLD | NEW |