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 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: | |
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 DISALLOW_COPY_AND_ASSIGN(RawDataPresenter); | |
88 }; | |
89 | |
90 // This class inspects the contents of bytes elements. It uses the | |
91 // parser classes inheriting from FormDataParser to parse the concatenated | |
92 // content of such elements. If the parsing is successful, the parsed form is | |
93 // returned as a DictionaryValue. For example, a form consisting of | |
94 // <input name="check" type="checkbox" value="A" checked /> | |
95 // <input name="check" type="checkbox" value="B" checked /> | |
96 // <input name="text" type="text" value="abc" /> | |
97 // would be represented as {"check": ["A", "B"], "text": ["abc"]} (although as a | |
98 // DictionaryValue, not as a JSON string). | |
99 class ParsedDataPresenter : public UploadDataPresenter { | |
100 public: | |
101 explicit ParsedDataPresenter(const net::URLRequest& request); | |
102 virtual ~ParsedDataPresenter(); | |
103 | |
104 // Implementation of UploadDataPresenter. | |
105 virtual void FeedNext(const net::UploadElementReader& reader) OVERRIDE; | |
106 virtual bool Succeeded() OVERRIDE; | |
107 virtual scoped_ptr<base::Value> Result() OVERRIDE; | |
108 | |
109 // Allows to create ParsedDataPresenter without the URLRequest. Uses the | |
110 // parser for "application/x-www-form-urlencoded" form encoding. Only use this | |
111 // in tests. | |
112 static scoped_ptr<ParsedDataPresenter> CreateForTests(); | |
113 | |
114 private: | |
115 // This constructor is used in CreateForTests. | |
116 explicit ParsedDataPresenter(const std::string& form_type); | |
117 | |
118 // Clears resources and the success flag. | |
119 void Abort(); | |
120 scoped_ptr<FormDataParser> parser_; | |
121 bool success_; | |
122 scoped_ptr<base::DictionaryValue> dictionary_; | |
123 | |
124 DISALLOW_COPY_AND_ASSIGN(ParsedDataPresenter); | |
125 }; | |
126 | |
127 } // namespace extensions | |
128 | |
129 #endif // CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_UPLOAD_DATA_PRESENTER_H_ | |
OLD | NEW |