Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / extensions / browser / api / web_request / upload_data_presenter.h
blob54f7f159d6582c5da79031e6b7cd46dd96da18f6
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 EXTENSIONS_BROWSER_API_WEB_REQUEST_UPLOAD_DATA_PRESENTER_H_
6 #define EXTENSIONS_BROWSER_API_WEB_REQUEST_UPLOAD_DATA_PRESENTER_H_
8 #include <string>
9 #include <vector>
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
14 namespace base {
15 class DictionaryValue;
16 class ListValue;
17 class Value;
20 namespace extensions {
21 class FormDataParser;
24 namespace net {
25 class URLRequest;
26 class UploadElementReader;
29 namespace extensions {
31 namespace subtle {
33 // Helpers shared with unit-tests.
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);
41 } // namespace subtle
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 {
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;
59 protected:
60 UploadDataPresenter() {}
62 private:
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
68 // for that file.
69 class RawDataPresenter : public UploadDataPresenter {
70 public:
71 RawDataPresenter();
72 ~RawDataPresenter() override;
74 // Implementation of UploadDataPresenter.
75 void FeedNext(const net::UploadElementReader& reader) override;
76 bool Succeeded() override;
77 scoped_ptr<base::Value> Result() override;
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);
84 bool success_;
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 {
100 public:
101 explicit ParsedDataPresenter(const net::URLRequest& request);
102 ~ParsedDataPresenter() override;
104 // Implementation of UploadDataPresenter.
105 void FeedNext(const net::UploadElementReader& reader) override;
106 bool Succeeded() override;
107 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
111 // in tests.
112 static scoped_ptr<ParsedDataPresenter> CreateForTests();
114 private:
115 // This constructor is used in CreateForTests.
116 explicit ParsedDataPresenter(const std::string& form_type);
118 // Clears resources and the success flag.
119 void Abort();
120 scoped_ptr<FormDataParser> parser_;
121 bool success_;
122 scoped_ptr<base::DictionaryValue> dictionary_;
124 DISALLOW_COPY_AND_ASSIGN(ParsedDataPresenter);
127 } // namespace extensions
129 #endif // EXTENSIONS_BROWSER_API_WEB_REQUEST_UPLOAD_DATA_PRESENTER_H_