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

Unified Diff: webkit/base/data_element.h

Issue 10827414: Factor out common Element struct from BlobData and ResourceRequestBody (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: webkit/common -> webkit/base 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
« no previous file with comments | « content/public/common/common_param_traits.cc ('k') | webkit/base/data_element.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webkit/base/data_element.h
diff --git a/webkit/base/data_element.h b/webkit/base/data_element.h
new file mode 100644
index 0000000000000000000000000000000000000000..61737e64d9b94c82d438dc1a4b9ae568f1228294
--- /dev/null
+++ b/webkit/base/data_element.h
@@ -0,0 +1,116 @@
+// 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 WEBKIT_BASE_DATA_ELEMENT_H_
+#define WEBKIT_BASE_DATA_ELEMENT_H_
+
+#include <vector>
+
+#include "base/basictypes.h"
+#include "base/file_path.h"
+#include "base/logging.h"
+#include "base/time.h"
+#include "googleurl/src/gurl.h"
+#include "webkit/base/webkit_base_export.h"
+
+namespace webkit_base {
+
+// Represents a base Web data element. This could be either one of
+// bytes, file or blob data.
+class WEBKIT_BASE_EXPORT DataElement {
+ public:
+ enum Type {
+ TYPE_UNKNOWN = -1,
+ TYPE_BYTES,
+ TYPE_FILE,
+ TYPE_BLOB,
+ };
+
+ DataElement();
+ ~DataElement();
+
+ Type type() const { return type_; }
+ const char* bytes() const { return bytes_ ? bytes_ : &buf_[0]; }
+ const FilePath& path() const { return path_; }
+ const GURL& url() const { return url_; }
+ uint64 offset() const { return offset_; }
+ uint64 length() const { return length_; }
+ const base::Time& expected_modification_time() const {
+ return expected_modification_time_;
+ }
+
+ // Sets TYPE_BYTES data. This copies the given data into the element.
+ void SetToBytes(const char* bytes, int bytes_len) {
+ type_ = TYPE_BYTES;
+ buf_.assign(bytes, bytes + bytes_len);
+ length_ = buf_.size();
+ }
+
+ // Sets TYPE_BYTES data. This does NOT copy the given data and the caller
+ // should make sure the data is alive when this element is accessed.
+ void SetToSharedBytes(const char* bytes, int bytes_len) {
+ type_ = TYPE_BYTES;
+ bytes_ = bytes;
+ length_ = bytes_len;
+ }
+
+ // Sets TYPE_FILE data.
+ void SetToFilePath(const FilePath& path) {
+ SetToFilePathRange(path, 0, kuint64max, base::Time());
+ }
+
+ // Sets TYPE_BLOB data.
+ void SetToBlobUrl(const GURL& blob_url) {
+ SetToBlobUrlRange(blob_url, 0, kuint64max);
+ }
+
+ // Sets TYPE_FILE data with range.
+ void SetToFilePathRange(const FilePath& path,
+ uint64 offset, uint64 length,
+ const base::Time& expected_modification_time);
+
+ // Sets TYPE_BLOB data with range.
+ void SetToBlobUrlRange(const GURL& blob_url,
+ uint64 offset, uint64 length);
+
+ private:
+ Type type_;
+ std::vector<char> buf_;
+ const char* bytes_;
+ FilePath path_;
+ GURL url_;
+ uint64 offset_;
+ uint64 length_;
+ base::Time expected_modification_time_;
+};
+
+#if defined(UNIT_TEST)
+inline bool operator==(const DataElement& a, const DataElement& b) {
+ if (a.type() != b.type() ||
+ a.offset() != b.offset() ||
+ a.length() != b.length())
+ return false;
+ switch (a.type()) {
+ case DataElement::TYPE_BYTES:
+ return memcmp(a.bytes(), b.bytes(), b.length()) == 0;
+ case DataElement::TYPE_FILE:
+ return a.path() == b.path() &&
+ a.expected_modification_time() == b.expected_modification_time();
+ case DataElement::TYPE_BLOB:
+ return a.url() == b.url();
+ case DataElement::TYPE_UNKNOWN:
+ NOTREACHED();
+ return false;
+ }
+ return false;
+}
+
+inline bool operator!=(const DataElement& a, const DataElement& b) {
+ return !(a == b);
+}
+#endif // defined(UNIT_TEST)
+
+} // namespace webkit_base
+
+#endif // WEBKIT_BASE_DATA_ELEMENT_H_
« no previous file with comments | « content/public/common/common_param_traits.cc ('k') | webkit/base/data_element.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698