Home | History | Annotate | Download | only in common
      1 // Copyright 2013 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 WEBKIT_COMMON_DATA_ELEMENT_H_
      6 #define WEBKIT_COMMON_DATA_ELEMENT_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/files/file_path.h"
     13 #include "base/logging.h"
     14 #include "base/time/time.h"
     15 #include "url/gurl.h"
     16 #include "webkit/common/webkit_common_export.h"
     17 
     18 namespace webkit_common {
     19 
     20 // Represents a base Web data element. This could be either one of
     21 // bytes, file or blob data.
     22 class WEBKIT_COMMON_EXPORT DataElement {
     23  public:
     24   enum Type {
     25     TYPE_UNKNOWN = -1,
     26     TYPE_BYTES,
     27     TYPE_FILE,
     28     TYPE_BLOB,
     29     TYPE_FILE_FILESYSTEM,
     30   };
     31 
     32   DataElement();
     33   ~DataElement();
     34 
     35   Type type() const { return type_; }
     36   const char* bytes() const { return bytes_ ? bytes_ : &buf_[0]; }
     37   const base::FilePath& path() const { return path_; }
     38   const GURL& filesystem_url() const { return filesystem_url_; }
     39 
     40   // TODO(michaeln): crbug/174200, fully switch to using string uuids.
     41   // Note: Identifying blobs by url is being deprecated, but while
     42   // transitioning, there's a little of both going on in the project.
     43   const std::string& blob_uuid() const { return blob_uuid_; }
     44   const GURL& blob_url() const { return blob_url_; }
     45   uint64 offset() const { return offset_; }
     46   uint64 length() const { return length_; }
     47   const base::Time& expected_modification_time() const {
     48     return expected_modification_time_;
     49   }
     50 
     51   // TODO(michaeln): fixup callers to use filesytem_url() and blob_uuid().
     52   const GURL& url() const {
     53     if (type_ == TYPE_FILE_FILESYSTEM)
     54       return filesystem_url_;
     55     return blob_url_;
     56   }
     57 
     58   // Sets TYPE_BYTES data. This copies the given data into the element.
     59   void SetToBytes(const char* bytes, int bytes_len) {
     60     type_ = TYPE_BYTES;
     61     buf_.assign(bytes, bytes + bytes_len);
     62     length_ = buf_.size();
     63   }
     64 
     65   // Sets TYPE_BYTES data. This does NOT copy the given data and the caller
     66   // should make sure the data is alive when this element is accessed.
     67   void SetToSharedBytes(const char* bytes, int bytes_len) {
     68     type_ = TYPE_BYTES;
     69     bytes_ = bytes;
     70     length_ = bytes_len;
     71   }
     72 
     73   // Sets TYPE_FILE data.
     74   void SetToFilePath(const base::FilePath& path) {
     75     SetToFilePathRange(path, 0, kuint64max, base::Time());
     76   }
     77 
     78   // Sets TYPE_BLOB data.
     79   void SetToBlobUrl(const GURL& blob_url) {
     80     SetToBlobUrlRange(blob_url, 0, kuint64max);
     81   }
     82   void SetToBlob(const std::string& uuid) {
     83     SetToBlobRange(uuid, 0, kuint64max);
     84   }
     85 
     86   // Sets TYPE_FILE data with range.
     87   void SetToFilePathRange(const base::FilePath& path,
     88                           uint64 offset, uint64 length,
     89                           const base::Time& expected_modification_time);
     90 
     91   // Sets TYPE_BLOB data with range.
     92   void SetToBlobUrlRange(const GURL& blob_url,
     93                          uint64 offset, uint64 length);
     94   void SetToBlobRange(const std::string& blob_uuid,
     95                       uint64 offset, uint64 length);
     96 
     97   // Sets TYPE_FILE_FILESYSTEM with range.
     98   void SetToFileSystemUrlRange(const GURL& filesystem_url,
     99                                uint64 offset, uint64 length,
    100                                const base::Time& expected_modification_time);
    101 
    102  private:
    103   Type type_;
    104   std::vector<char> buf_;  // For TYPE_BYTES.
    105   const char* bytes_;  // For TYPE_BYTES.
    106   base::FilePath path_;  // For TYPE_FILE.
    107   GURL filesystem_url_;  // For TYPE_FILE_FILESYSTEM.
    108   GURL blob_url_;
    109   std::string blob_uuid_;
    110   uint64 offset_;
    111   uint64 length_;
    112   base::Time expected_modification_time_;
    113 };
    114 
    115 #if defined(UNIT_TEST)
    116 inline bool operator==(const DataElement& a, const DataElement& b) {
    117   if (a.type() != b.type() ||
    118       a.offset() != b.offset() ||
    119       a.length() != b.length())
    120     return false;
    121   switch (a.type()) {
    122     case DataElement::TYPE_BYTES:
    123       return memcmp(a.bytes(), b.bytes(), b.length()) == 0;
    124     case DataElement::TYPE_FILE:
    125       return a.path() == b.path() &&
    126              a.expected_modification_time() == b.expected_modification_time();
    127     case DataElement::TYPE_BLOB:
    128       return a.blob_uuid().empty() ? (a.blob_url() == b.blob_url())
    129                                    : (a.blob_uuid() == b.blob_uuid());
    130     case DataElement::TYPE_FILE_FILESYSTEM:
    131       return a.filesystem_url() == b.filesystem_url();
    132     case DataElement::TYPE_UNKNOWN:
    133       NOTREACHED();
    134       return false;
    135   }
    136   return false;
    137 }
    138 
    139 inline bool operator!=(const DataElement& a, const DataElement& b) {
    140   return !(a == b);
    141 }
    142 #endif  // defined(UNIT_TEST)
    143 
    144 }  // namespace webkit_common
    145 
    146 #endif  // WEBKIT_COMMON_DATA_ELEMENT_H_
    147