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 UploadElementReader; 27 } 28 29 namespace extensions { 30 31 namespace subtle { 32 33 // Helpers shared with unit-tests. 34 35 // Appends a dictionary {'key': 'value'} to |list|. |list| becomes the owner of 36 // |value|. 37 void AppendKeyValuePair(const char* key, 38 base::Value* value, 39 base::ListValue* list); 40 41 } // namespace subtle 42 43 FORWARD_DECLARE_TEST(WebRequestUploadDataPresenterTest, RawData); 44 45 // UploadDataPresenter is an interface for objects capable to consume a series 46 // of UploadElementReader and represent this data as a base:Value. 47 // 48 // Workflow for objects implementing this interface: 49 // 1. Call object->FeedNext(reader) for each element from the request's body. 50 // 2. Check if object->Succeeded(). 51 // 3. If that check passed then retrieve object->Result(). 52 class UploadDataPresenter { 53 public: 54 virtual ~UploadDataPresenter(); 55 virtual void FeedNext(const net::UploadElementReader& reader) = 0; 56 virtual bool Succeeded() = 0; 57 virtual scoped_ptr<base::Value> Result() = 0; 58 59 protected: UploadDataPresenter()60 UploadDataPresenter() {} 61 62 private: 63 DISALLOW_COPY_AND_ASSIGN(UploadDataPresenter); 64 }; 65 66 // This class passes all the bytes from bytes elements as a BinaryValue for each 67 // such element. File elements are presented as StringValue containing the path 68 // for that file. 69 class RawDataPresenter : public UploadDataPresenter { 70 public: 71 RawDataPresenter(); 72 virtual ~RawDataPresenter(); 73 74 // Implementation of UploadDataPresenter. 75 virtual void FeedNext(const net::UploadElementReader& reader) OVERRIDE; 76 virtual bool Succeeded() OVERRIDE; 77 virtual scoped_ptr<base::Value> Result() OVERRIDE; 78 79 private: 80 void FeedNextBytes(const char* bytes, size_t size); 81 void FeedNextFile(const std::string& filename); 82 FRIEND_TEST_ALL_PREFIXES(WebRequestUploadDataPresenterTest, RawData); 83 84 bool success_; 85 scoped_ptr<base::ListValue> list_; 86 }; 87 88 // This class inspects the contents of bytes elements. It uses the 89 // parser classes inheriting from FormDataParser to parse the concatenated 90 // content of such elements. If the parsing is successful, the parsed form is 91 // returned as a DictionaryValue. For example, a form consisting of 92 // <input name="check" type="checkbox" value="A" checked /> 93 // <input name="check" type="checkbox" value="B" checked /> 94 // <input name="text" type="text" value="abc" /> 95 // would be represented as {"check": ["A", "B"], "text": ["abc"]} (although as a 96 // DictionaryValue, not as a JSON string). 97 class ParsedDataPresenter : public UploadDataPresenter { 98 public: 99 explicit ParsedDataPresenter(const net::URLRequest& request); 100 virtual ~ParsedDataPresenter(); 101 102 // Implementation of UploadDataPresenter. 103 virtual void FeedNext(const net::UploadElementReader& reader) OVERRIDE; 104 virtual bool Succeeded() OVERRIDE; 105 virtual scoped_ptr<base::Value> Result() OVERRIDE; 106 107 // Allows to create ParsedDataPresenter without the URLRequest. Uses the 108 // parser for "application/x-www-form-urlencoded" form encoding. Only use this 109 // in tests. 110 static scoped_ptr<ParsedDataPresenter> CreateForTests(); 111 112 private: 113 // This constructor is used in CreateForTests. 114 explicit ParsedDataPresenter(const std::string& form_type); 115 116 // Clears resources and the success flag. 117 void Abort(); 118 scoped_ptr<FormDataParser> parser_; 119 bool success_; 120 scoped_ptr<base::DictionaryValue> dictionary_; 121 }; 122 123 } // namespace extensions 124 125 #endif // CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_UPLOAD_DATA_PRESENTER_H_ 126