Home | History | Annotate | Download | only in blob
      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 WEBKIT_COMMON_BLOB_BLOB_DATA_H_
      6 #define WEBKIT_COMMON_BLOB_BLOB_DATA_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/files/file_path.h"
     13 #include "base/memory/ref_counted.h"
     14 #include "base/time/time.h"
     15 #include "url/gurl.h"
     16 #include "webkit/common/blob/shareable_file_reference.h"
     17 #include "webkit/common/data_element.h"
     18 #include "webkit/common/webkit_storage_common_export.h"
     19 
     20 namespace webkit_blob {
     21 
     22 class WEBKIT_STORAGE_COMMON_EXPORT BlobData
     23     : public base::RefCounted<BlobData> {
     24  public:
     25   typedef webkit_common::DataElement Item;
     26 
     27   // TODO(michaeln): remove the empty ctor when we fully transition to uuids.
     28   BlobData();
     29   explicit BlobData(const std::string& uuid);
     30 
     31   void AppendData(const std::string& data) {
     32     AppendData(data.c_str(), data.size());
     33   }
     34 
     35   void AppendData(const char* data, size_t length);
     36 
     37   void AppendFile(const base::FilePath& file_path, uint64 offset, uint64 length,
     38                   const base::Time& expected_modification_time);
     39 
     40   // Note: Identifying blobs by url is being deprecated, but while transitioning
     41   // there's a little of both going on in the project.
     42   void AppendBlob(const GURL& blob_url, uint64 offset, uint64 length);
     43   void AppendBlob(const std::string& uuid, uint64 offset, uint64 length);
     44 
     45   void AppendFileSystemFile(const GURL& url, uint64 offset, uint64 length,
     46                             const base::Time& expected_modification_time);
     47 
     48   void AttachShareableFileReference(ShareableFileReference* reference) {
     49     shareable_files_.push_back(reference);
     50   }
     51 
     52   const std::string& uuid() const { return uuid_; }
     53   const std::vector<Item>& items() const { return items_; }
     54   const std::string& content_type() const { return content_type_; }
     55   void set_content_type(const std::string& content_type) {
     56     content_type_ = content_type;
     57   }
     58 
     59   const std::string& content_disposition() const {
     60     return content_disposition_;
     61   }
     62   void set_content_disposition(const std::string& content_disposition) {
     63     content_disposition_ = content_disposition;
     64   }
     65 
     66   int64 GetMemoryUsage() const;
     67 
     68  private:
     69   friend class base::RefCounted<BlobData>;
     70   virtual ~BlobData();
     71 
     72   std::string uuid_;
     73   std::string content_type_;
     74   std::string content_disposition_;
     75   std::vector<Item> items_;
     76   std::vector<scoped_refptr<ShareableFileReference> > shareable_files_;
     77 
     78   DISALLOW_COPY_AND_ASSIGN(BlobData);
     79 };
     80 
     81 #if defined(UNIT_TEST)
     82 inline bool operator==(const BlobData& a, const BlobData& b) {
     83   if (a.content_type() != b.content_type())
     84     return false;
     85   if (a.content_disposition() != b.content_disposition())
     86     return false;
     87   if (a.items().size() != b.items().size())
     88     return false;
     89   for (size_t i = 0; i < a.items().size(); ++i) {
     90     if (a.items()[i] != b.items()[i])
     91       return false;
     92   }
     93   return true;
     94 }
     95 
     96 inline bool operator!=(const BlobData& a, const BlobData& b) {
     97   return !(a == b);
     98 }
     99 #endif  // defined(UNIT_TEST)
    100 
    101 }  // namespace webkit_blob
    102 
    103 #endif  // WEBKIT_COMMON_BLOB_BLOB_DATA_H_
    104