| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef EXTENSIONS_BROWSER_API_WEB_REQUEST_UPLOAD_DATA_PRESENTER_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_WEB_REQUEST_UPLOAD_DATA_PRESENTER_H_ |
| 6 #define EXTENSIONS_BROWSER_API_WEB_REQUEST_UPLOAD_DATA_PRESENTER_H_ | 6 #define EXTENSIONS_BROWSER_API_WEB_REQUEST_UPLOAD_DATA_PRESENTER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <memory> |
| 10 #include <string> | 11 #include <string> |
| 11 #include <vector> | 12 #include <vector> |
| 12 | 13 |
| 13 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
| 14 #include "base/macros.h" | 15 #include "base/macros.h" |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 | 16 |
| 17 namespace base { | 17 namespace base { |
| 18 class DictionaryValue; | 18 class DictionaryValue; |
| 19 class ListValue; | 19 class ListValue; |
| 20 class Value; | 20 class Value; |
| 21 } | 21 } |
| 22 | 22 |
| 23 namespace extensions { | 23 namespace extensions { |
| 24 class FormDataParser; | 24 class FormDataParser; |
| 25 } | 25 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 50 // | 50 // |
| 51 // Workflow for objects implementing this interface: | 51 // Workflow for objects implementing this interface: |
| 52 // 1. Call object->FeedNext(reader) for each element from the request's body. | 52 // 1. Call object->FeedNext(reader) for each element from the request's body. |
| 53 // 2. Check if object->Succeeded(). | 53 // 2. Check if object->Succeeded(). |
| 54 // 3. If that check passed then retrieve object->Result(). | 54 // 3. If that check passed then retrieve object->Result(). |
| 55 class UploadDataPresenter { | 55 class UploadDataPresenter { |
| 56 public: | 56 public: |
| 57 virtual ~UploadDataPresenter(); | 57 virtual ~UploadDataPresenter(); |
| 58 virtual void FeedNext(const net::UploadElementReader& reader) = 0; | 58 virtual void FeedNext(const net::UploadElementReader& reader) = 0; |
| 59 virtual bool Succeeded() = 0; | 59 virtual bool Succeeded() = 0; |
| 60 virtual scoped_ptr<base::Value> Result() = 0; | 60 virtual std::unique_ptr<base::Value> Result() = 0; |
| 61 | 61 |
| 62 protected: | 62 protected: |
| 63 UploadDataPresenter() {} | 63 UploadDataPresenter() {} |
| 64 | 64 |
| 65 private: | 65 private: |
| 66 DISALLOW_COPY_AND_ASSIGN(UploadDataPresenter); | 66 DISALLOW_COPY_AND_ASSIGN(UploadDataPresenter); |
| 67 }; | 67 }; |
| 68 | 68 |
| 69 // This class passes all the bytes from bytes elements as a BinaryValue for each | 69 // This class passes all the bytes from bytes elements as a BinaryValue for each |
| 70 // such element. File elements are presented as StringValue containing the path | 70 // such element. File elements are presented as StringValue containing the path |
| 71 // for that file. | 71 // for that file. |
| 72 class RawDataPresenter : public UploadDataPresenter { | 72 class RawDataPresenter : public UploadDataPresenter { |
| 73 public: | 73 public: |
| 74 RawDataPresenter(); | 74 RawDataPresenter(); |
| 75 ~RawDataPresenter() override; | 75 ~RawDataPresenter() override; |
| 76 | 76 |
| 77 // Implementation of UploadDataPresenter. | 77 // Implementation of UploadDataPresenter. |
| 78 void FeedNext(const net::UploadElementReader& reader) override; | 78 void FeedNext(const net::UploadElementReader& reader) override; |
| 79 bool Succeeded() override; | 79 bool Succeeded() override; |
| 80 scoped_ptr<base::Value> Result() override; | 80 std::unique_ptr<base::Value> Result() override; |
| 81 | 81 |
| 82 private: | 82 private: |
| 83 void FeedNextBytes(const char* bytes, size_t size); | 83 void FeedNextBytes(const char* bytes, size_t size); |
| 84 void FeedNextFile(const std::string& filename); | 84 void FeedNextFile(const std::string& filename); |
| 85 FRIEND_TEST_ALL_PREFIXES(WebRequestUploadDataPresenterTest, RawData); | 85 FRIEND_TEST_ALL_PREFIXES(WebRequestUploadDataPresenterTest, RawData); |
| 86 | 86 |
| 87 bool success_; | 87 bool success_; |
| 88 scoped_ptr<base::ListValue> list_; | 88 std::unique_ptr<base::ListValue> list_; |
| 89 | 89 |
| 90 DISALLOW_COPY_AND_ASSIGN(RawDataPresenter); | 90 DISALLOW_COPY_AND_ASSIGN(RawDataPresenter); |
| 91 }; | 91 }; |
| 92 | 92 |
| 93 // This class inspects the contents of bytes elements. It uses the | 93 // This class inspects the contents of bytes elements. It uses the |
| 94 // parser classes inheriting from FormDataParser to parse the concatenated | 94 // parser classes inheriting from FormDataParser to parse the concatenated |
| 95 // content of such elements. If the parsing is successful, the parsed form is | 95 // content of such elements. If the parsing is successful, the parsed form is |
| 96 // returned as a DictionaryValue. For example, a form consisting of | 96 // returned as a DictionaryValue. For example, a form consisting of |
| 97 // <input name="check" type="checkbox" value="A" checked /> | 97 // <input name="check" type="checkbox" value="A" checked /> |
| 98 // <input name="check" type="checkbox" value="B" checked /> | 98 // <input name="check" type="checkbox" value="B" checked /> |
| 99 // <input name="text" type="text" value="abc" /> | 99 // <input name="text" type="text" value="abc" /> |
| 100 // would be represented as {"check": ["A", "B"], "text": ["abc"]} (although as a | 100 // would be represented as {"check": ["A", "B"], "text": ["abc"]} (although as a |
| 101 // DictionaryValue, not as a JSON string). | 101 // DictionaryValue, not as a JSON string). |
| 102 class ParsedDataPresenter : public UploadDataPresenter { | 102 class ParsedDataPresenter : public UploadDataPresenter { |
| 103 public: | 103 public: |
| 104 explicit ParsedDataPresenter(const net::URLRequest& request); | 104 explicit ParsedDataPresenter(const net::URLRequest& request); |
| 105 ~ParsedDataPresenter() override; | 105 ~ParsedDataPresenter() override; |
| 106 | 106 |
| 107 // Implementation of UploadDataPresenter. | 107 // Implementation of UploadDataPresenter. |
| 108 void FeedNext(const net::UploadElementReader& reader) override; | 108 void FeedNext(const net::UploadElementReader& reader) override; |
| 109 bool Succeeded() override; | 109 bool Succeeded() override; |
| 110 scoped_ptr<base::Value> Result() override; | 110 std::unique_ptr<base::Value> Result() override; |
| 111 | 111 |
| 112 // Allows to create ParsedDataPresenter without the URLRequest. Uses the | 112 // Allows to create ParsedDataPresenter without the URLRequest. Uses the |
| 113 // parser for "application/x-www-form-urlencoded" form encoding. Only use this | 113 // parser for "application/x-www-form-urlencoded" form encoding. Only use this |
| 114 // in tests. | 114 // in tests. |
| 115 static scoped_ptr<ParsedDataPresenter> CreateForTests(); | 115 static std::unique_ptr<ParsedDataPresenter> CreateForTests(); |
| 116 | 116 |
| 117 private: | 117 private: |
| 118 // This constructor is used in CreateForTests. | 118 // This constructor is used in CreateForTests. |
| 119 explicit ParsedDataPresenter(const std::string& form_type); | 119 explicit ParsedDataPresenter(const std::string& form_type); |
| 120 | 120 |
| 121 // Clears resources and the success flag. | 121 // Clears resources and the success flag. |
| 122 void Abort(); | 122 void Abort(); |
| 123 scoped_ptr<FormDataParser> parser_; | 123 std::unique_ptr<FormDataParser> parser_; |
| 124 bool success_; | 124 bool success_; |
| 125 scoped_ptr<base::DictionaryValue> dictionary_; | 125 std::unique_ptr<base::DictionaryValue> dictionary_; |
| 126 | 126 |
| 127 DISALLOW_COPY_AND_ASSIGN(ParsedDataPresenter); | 127 DISALLOW_COPY_AND_ASSIGN(ParsedDataPresenter); |
| 128 }; | 128 }; |
| 129 | 129 |
| 130 } // namespace extensions | 130 } // namespace extensions |
| 131 | 131 |
| 132 #endif // EXTENSIONS_BROWSER_API_WEB_REQUEST_UPLOAD_DATA_PRESENTER_H_ | 132 #endif // EXTENSIONS_BROWSER_API_WEB_REQUEST_UPLOAD_DATA_PRESENTER_H_ |
| OLD | NEW |