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