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