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 UploadElement; |
| 27 } |
| 28 |
| 29 namespace extensions { |
| 30 |
| 31 FORWARD_DECLARE_TEST(WebRequestUploadDataPresenterTest, RawData); |
| 32 |
| 33 // UploadDataPresenter is an interface for objects capable to consume a series |
| 34 // of UploadElement and represent this data as a base:Value. |
| 35 // |
| 36 // Workflow for objects implementing this interface: |
| 37 // 1. Call object->FeedNext(element) for each element from the request's body. |
| 38 // 2. Check if object->Succeeded(). |
| 39 // 3. If that check passed then retrieve object->Result(). |
| 40 class UploadDataPresenter { |
| 41 public: |
| 42 virtual ~UploadDataPresenter(); |
| 43 virtual void FeedNext(const net::UploadElement& element) = 0; |
| 44 virtual bool Succeeded() = 0; |
| 45 virtual scoped_ptr<base::Value> Result() = 0; |
| 46 |
| 47 protected: |
| 48 UploadDataPresenter() {} |
| 49 |
| 50 private: |
| 51 DISALLOW_COPY_AND_ASSIGN(UploadDataPresenter); |
| 52 }; |
| 53 |
| 54 // This class passes all the bytes from elements of TYPE_BYTES as a BinaryValue |
| 55 // for each such element. Elements of TYPE_FILE are presented as StringValue |
| 56 // containing the path for that file. |
| 57 class RawDataPresenter : public UploadDataPresenter { |
| 58 public: |
| 59 RawDataPresenter(); |
| 60 virtual ~RawDataPresenter(); |
| 61 |
| 62 // Implementation of UploadDataPresenter. |
| 63 virtual void FeedNext(const net::UploadElement& element) OVERRIDE; |
| 64 virtual bool Succeeded() OVERRIDE; |
| 65 virtual scoped_ptr<base::Value> Result() OVERRIDE; |
| 66 |
| 67 // Appends a dictionary {'key': 'value'} to |list|. |
| 68 // This is a helper function and has nothing to do with RawDataPresenter |
| 69 // directly. However, it is used by FeedNext* methods and also unit-tests |
| 70 // depending on RawDataPresenter. It is here to avoid code duplication. |
| 71 static void AppendResultWithKey( |
| 72 base::ListValue* list, const char* key, base::Value* value); |
| 73 |
| 74 private: |
| 75 // Clears resources and the success flag. |
| 76 void Abort(); |
| 77 |
| 78 void FeedNextBytes(const char* bytes, size_t size); |
| 79 void FeedNextFile(const std::string& filename); |
| 80 FRIEND_TEST_ALL_PREFIXES(WebRequestUploadDataPresenterTest, RawData); |
| 81 |
| 82 bool success_; |
| 83 scoped_ptr<base::ListValue> list_; |
| 84 }; |
| 85 |
| 86 // This class inspects the contents of elements of TYPE_BYTES. It uses the |
| 87 // parser classes inheriting from FormDataParser to parse the concatenated |
| 88 // content of such elements. If the parsing is successfull, the parsed form is |
| 89 // returned as a DictionaryValue. For example, a form consisting of |
| 90 // <input name="check" type="checkbox" value="A" checked /> |
| 91 // <input name="check" type="checkbox" value="B" checked /> |
| 92 // <input name="text" type="text" value="abc" /> |
| 93 // would be represented as {"check": ["A", "B"], "text": ["abc"]} (although as a |
| 94 // DictionaryValue, not as a JSON string). |
| 95 class ParsedDataPresenter : public UploadDataPresenter { |
| 96 public: |
| 97 explicit ParsedDataPresenter(const net::URLRequest* request); |
| 98 virtual ~ParsedDataPresenter(); |
| 99 |
| 100 // Implementation of UploadDataPresenter. |
| 101 virtual void FeedNext(const net::UploadElement& element) OVERRIDE; |
| 102 virtual bool Succeeded() OVERRIDE; |
| 103 virtual scoped_ptr<base::Value> Result() OVERRIDE; |
| 104 |
| 105 private: |
| 106 // Clears resources and the success flag. |
| 107 void Abort(); |
| 108 scoped_ptr<FormDataParser> parser_; |
| 109 bool success_; |
| 110 scoped_ptr<base::DictionaryValue> dictionary_; |
| 111 }; |
| 112 |
| 113 } // namespace extensions |
| 114 |
| 115 #endif // CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_UPLOAD_DATA_PRESENTER_H_ |
OLD | NEW |