Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(144)

Unified Diff: chrome/browser/extensions/api/web_request/upload_data_presenter.h

Issue 10694055: Add read-only access to POST data for webRequest's onBeforeRequest (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Dominic's comments Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/web_request/upload_data_presenter.h
diff --git a/chrome/browser/extensions/api/web_request/upload_data_presenter.h b/chrome/browser/extensions/api/web_request/upload_data_presenter.h
new file mode 100644
index 0000000000000000000000000000000000000000..879d3ce6e45abc6c52fc225259b8ac66ae8298df
--- /dev/null
+++ b/chrome/browser/extensions/api/web_request/upload_data_presenter.h
@@ -0,0 +1,126 @@
+// 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 <string>
+#include <vector>
+
+#include "base/memory/scoped_ptr.h"
+// Need to include this, because UploadData::Element cannot be forward declared.
+#include "net/base/upload_data.h"
+
+namespace base {
+class DictionaryValue;
+class ListValue;
+class Value;
+}
+
+namespace extensions {
+class FormDataParser;
+}
+
+namespace net {
+class URLRequest;
+}
+
+namespace extensions {
+
+// UploadDataPresenter is an interface for objects capable to consume a series
+// of UploadData::Element 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::UploadData::Element& element) = 0;
+ virtual bool Succeeded() = 0;
+ virtual scoped_ptr<base::Value> Result() = 0;
+
+ protected:
+ UploadDataPresenter() {}
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(UploadDataPresenter);
+};
+
+// This class checks that chunked transfer encoding (defined in RFC 2616)
+// is not used. If it is, an error string is generated.
+class ChunkedErrorPresenter : public UploadDataPresenter {
+ public:
+ explicit ChunkedErrorPresenter(const net::URLRequest* request);
+ virtual ~ChunkedErrorPresenter();
+
+ // Tests the headers of |request| for transfer encoding.
+ static bool IsTransferEncodingChunked(const net::URLRequest* request);
+
+ // Implementation of UploadDataPresenter.
+ virtual void FeedNext(const net::UploadData::Element& element) OVERRIDE;
+ virtual bool Succeeded() OVERRIDE;
+ virtual scoped_ptr<base::Value> Result() OVERRIDE;
+
+ private:
+ bool chunks_found_;
+};
+
+// 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. TYPE_BLOB elements are ignored, and the
+// presence of TYPE_CHUNK elements causes a failure.
+class RawDataPresenter : public UploadDataPresenter {
+ public:
+ RawDataPresenter();
+ virtual ~RawDataPresenter();
+
+ // Implementation of UploadDataPresenter.
+ virtual void FeedNext(const net::UploadData::Element& element) OVERRIDE;
+ virtual bool Succeeded() OVERRIDE;
+ virtual scoped_ptr<base::Value> Result() OVERRIDE;
+
+ private:
+ // Clears resources and the success flag.
+ void Abort();
+
+ void FeedNextBytes(const std::vector<char>& bytes);
+ void FeedNextFile(const std::string& filename);
+ FRIEND_TEST_ALL_PREFIXES(WebRequestUploadDataPresenterTest, RawData);
+
+ bool success_;
+ scoped_ptr<base::ListValue> 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
+// <input name="check" type="checkbox" value="A" checked />
+// <input name="check" type="checkbox" value="B" checked />
+// <input name="text" type="text" value="abc" />
+// 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::UploadData::Element& element) OVERRIDE;
+ virtual bool Succeeded() OVERRIDE;
+ virtual scoped_ptr<base::Value> Result() OVERRIDE;
+
+ private:
+ // Clears resources and the success flag.
+ void Abort();
+ scoped_ptr<FormDataParser> parser_;
+ bool success_;
+ scoped_ptr<base::DictionaryValue> dictionary_;
+};
+
+} // namespace extensions
+
+#endif // CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_UPLOAD_DATA_PRESENTER_H_

Powered by Google App Engine
This is Rietveld 408576698