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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_UPLOAD_DATA_PRESENTER_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_UPLOAD_DATA_PRESENTER_H_
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
15 class DictionaryValue;
20 namespace extensions {
26 class UploadElementReader;
29 namespace extensions {
33 // Helpers shared with unit-tests.
35 // Appends a dictionary {'key': 'value'} to |list|. |list| becomes the owner of
37 void AppendKeyValuePair(const char* key,
39 base::ListValue* list);
43 FORWARD_DECLARE_TEST(WebRequestUploadDataPresenterTest, RawData);
45 // UploadDataPresenter is an interface for objects capable to consume a series
46 // of UploadElementReader and represent this data as a base:Value.
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 {
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;
60 UploadDataPresenter() {}
63 DISALLOW_COPY_AND_ASSIGN(UploadDataPresenter);
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
69 class RawDataPresenter : public UploadDataPresenter {
72 virtual ~RawDataPresenter();
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;
80 void FeedNextBytes(const char* bytes, size_t size);
81 void FeedNextFile(const std::string& filename);
82 FRIEND_TEST_ALL_PREFIXES(WebRequestUploadDataPresenterTest, RawData);
85 scoped_ptr<base::ListValue> list_;
87 DISALLOW_COPY_AND_ASSIGN(RawDataPresenter);
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 {
101 explicit ParsedDataPresenter(const net::URLRequest& request);
102 virtual ~ParsedDataPresenter();
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;
109 // Allows to create ParsedDataPresenter without the URLRequest. Uses the
110 // parser for "application/x-www-form-urlencoded" form encoding. Only use this
112 static scoped_ptr<ParsedDataPresenter> CreateForTests();
115 // This constructor is used in CreateForTests.
116 explicit ParsedDataPresenter(const std::string& form_type);
118 // Clears resources and the success flag.
120 scoped_ptr<FormDataParser> parser_;
122 scoped_ptr<base::DictionaryValue> dictionary_;
124 DISALLOW_COPY_AND_ASSIGN(ParsedDataPresenter);
127 } // namespace extensions
129 #endif // CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_UPLOAD_DATA_PRESENTER_H_