// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_UPLOAD_DATA_PRESENTER_H_ #define CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_UPLOAD_DATA_PRESENTER_H_ #include #include #include "base/gtest_prod_util.h" #include "base/memory/scoped_ptr.h" namespace base { class DictionaryValue; class ListValue; class Value; } namespace extensions { class FormDataParser; } namespace net { class URLRequest; class UploadElement; } namespace extensions { FORWARD_DECLARE_TEST(WebRequestUploadDataPresenterTest, RawData); // UploadDataPresenter is an interface for objects capable to consume a series // of UploadElement and represent this data as a base:Value. // // Workflow for objects implementing this interface: // 1. Call object->FeedNext(element) for each element from the request's body. // 2. Check if object->Succeeded(). // 3. If that check passed then retrieve object->Result(). class UploadDataPresenter { public: virtual ~UploadDataPresenter(); virtual void FeedNext(const net::UploadElement& element) = 0; virtual bool Succeeded() = 0; virtual scoped_ptr Result() = 0; protected: UploadDataPresenter() {} private: DISALLOW_COPY_AND_ASSIGN(UploadDataPresenter); }; // This class passes all the bytes from elements of TYPE_BYTES as a BinaryValue // for each such element. Elements of TYPE_FILE are presented as StringValue // containing the path for that file. class RawDataPresenter : public UploadDataPresenter { public: RawDataPresenter(); virtual ~RawDataPresenter(); // Implementation of UploadDataPresenter. virtual void FeedNext(const net::UploadElement& element) OVERRIDE; virtual bool Succeeded() OVERRIDE; virtual scoped_ptr Result() OVERRIDE; // Appends a dictionary {'key': 'value'} to |list|. // This is a helper function and has nothing to do with RawDataPresenter // directly. However, it is used by FeedNext* methods and also unit-tests // depending on RawDataPresenter. It is here to avoid code duplication. static void AppendResultWithKey( base::ListValue* list, const char* key, base::Value* value); private: // Clears resources and the success flag. void Abort(); void FeedNextBytes(const char* bytes, size_t size); void FeedNextFile(const std::string& filename); FRIEND_TEST_ALL_PREFIXES(WebRequestUploadDataPresenterTest, RawData); bool success_; scoped_ptr list_; }; // This class inspects the contents of elements of TYPE_BYTES. It uses the // parser classes inheriting from FormDataParser to parse the concatenated // content of such elements. If the parsing is successfull, the parsed form is // returned as a DictionaryValue. For example, a form consisting of // // // // would be represented as {"check": ["A", "B"], "text": ["abc"]} (although as a // DictionaryValue, not as a JSON string). class ParsedDataPresenter : public UploadDataPresenter { public: explicit ParsedDataPresenter(const net::URLRequest* request); virtual ~ParsedDataPresenter(); // Implementation of UploadDataPresenter. virtual void FeedNext(const net::UploadElement& element) OVERRIDE; virtual bool Succeeded() OVERRIDE; virtual scoped_ptr Result() OVERRIDE; private: // Clears resources and the success flag. void Abort(); scoped_ptr parser_; bool success_; scoped_ptr dictionary_; }; } // namespace extensions #endif // CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_UPLOAD_DATA_PRESENTER_H_