summaryrefslogtreecommitdiffstats
path: root/webkit/blob/blob_data.h
blob: b5d9460e2e2060d80206d95c1ebd2fa7ec389864 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright (c) 2011 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_BLOB_BLOB_DATA_H_
#define WEBKIT_BLOB_BLOB_DATA_H_

#include <vector>

#include "base/basictypes.h"
#include "base/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/time.h"
#include "googleurl/src/gurl.h"
#include "webkit/blob/blob_export.h"
#include "webkit/blob/deletable_file_reference.h"

namespace WebKit {
class WebBlobData;
}

namespace webkit_blob {

class BLOB_EXPORT BlobData : public base::RefCounted<BlobData> {
 public:
  enum Type {
    TYPE_DATA,
    TYPE_DATA_EXTERNAL,
    TYPE_FILE,
    TYPE_BLOB
  };

  struct BLOB_EXPORT Item {
    Item();
    ~Item();

    void SetToData(const std::string& data) {
      SetToData(data.c_str(), data.size());
    }

    void SetToData(const char* data, size_t length) {
      type = TYPE_DATA;
      this->data.assign(data, length);
      this->offset = 0;
      this->length = length;
    }

    void SetToDataExternal(const char* data, size_t length) {
      type = TYPE_DATA_EXTERNAL;
      this->data_external = data;
      this->offset = 0;
      this->length = length;
    }

    void SetToFile(const FilePath& file_path, uint64 offset, uint64 length,
                   const base::Time& expected_modification_time) {
      type = TYPE_FILE;
      this->file_path = file_path;
      this->offset = offset;
      this->length = length;
      this->expected_modification_time = expected_modification_time;
    }

    void SetToBlob(const GURL& blob_url, uint64 offset, uint64 length) {
      type = TYPE_BLOB;
      this->blob_url = blob_url;
      this->offset = offset;
      this->length = length;
    }

    Type type;
    std::string data;  // For Data type.
    const char* data_external;  // For DataExternal type.
    GURL blob_url;  // For Blob type.
    FilePath file_path;  // For File type.
    base::Time expected_modification_time;  // Also for File type.
    uint64 offset;
    uint64 length;
  };

  BlobData();
  explicit BlobData(const WebKit::WebBlobData& data);

  void AppendData(const std::string& data) {
    AppendData(data.c_str(), data.size());
  }

  void AppendData(const char* data, size_t length) {
    if (length > 0) {
      items_.push_back(Item());
      items_.back().SetToData(data, length);
    }
  }

  void AppendFile(const FilePath& file_path, uint64 offset, uint64 length,
                  const base::Time& expected_modification_time) {
    items_.push_back(Item());
    items_.back().SetToFile(file_path, offset, length,
                            expected_modification_time);
  }

  void AppendBlob(const GURL& blob_url, uint64 offset, uint64 length) {
    items_.push_back(Item());
    items_.back().SetToBlob(blob_url, offset, length);
  }

  void AttachDeletableFileReference(DeletableFileReference* reference) {
    deletable_files_.push_back(reference);
  }

  const std::vector<Item>& items() const { return items_; }

  const std::string& content_type() const { return content_type_; }
  void set_content_type(const std::string& content_type) {
    content_type_ = content_type;
  }

  const std::string& content_disposition() const {
    return content_disposition_;
  }
  void set_content_disposition(const std::string& content_disposition) {
    content_disposition_ = content_disposition;
  }

  int64 GetMemoryUsage() const {
    int64 memory = 0;
    for (std::vector<Item>::const_iterator iter = items_.begin();
         iter != items_.end(); ++iter) {
      if (iter->type == TYPE_DATA)
        memory += iter->data.size();
    }
    return memory;
  }

 private:
  friend class base::RefCounted<BlobData>;

  virtual ~BlobData();

  std::string content_type_;
  std::string content_disposition_;
  std::vector<Item> items_;
  std::vector<scoped_refptr<DeletableFileReference> > deletable_files_;

  DISALLOW_COPY_AND_ASSIGN(BlobData);
};

#if defined(UNIT_TEST)
inline bool operator==(const BlobData::Item& a, const BlobData::Item& b) {
  if (a.type != b.type)
    return false;
  if (a.type == BlobData::TYPE_DATA) {
    return a.data == b.data &&
           a.offset == b.offset &&
           a.length == b.length;
  }
  if (a.type == BlobData::TYPE_FILE) {
    return a.file_path == b.file_path &&
           a.offset == b.offset &&
           a.length == b.length &&
           a.expected_modification_time == b.expected_modification_time;
  }
  if (a.type == BlobData::TYPE_BLOB) {
    return a.blob_url == b.blob_url &&
           a.offset == b.offset &&
           a.length == b.length;
  }
  return false;
}

inline  bool operator!=(const BlobData::Item& a, const BlobData::Item& b) {
  return !(a == b);
}

inline bool operator==(const BlobData& a, const BlobData& b) {
  if (a.content_type() != b.content_type())
    return false;
  if (a.content_disposition() != b.content_disposition())
    return false;
  if (a.items().size() != b.items().size())
    return false;
  for (size_t i = 0; i < a.items().size(); ++i) {
    if (a.items()[i] != b.items()[i])
      return false;
  }
  return true;
}

inline bool operator!=(const BlobData& a, const BlobData& b) {
  return !(a == b);
}
#endif  // defined(UNIT_TEST)

}  // namespace webkit_blob

#endif  // WEBKIT_BLOB_BLOB_DATA_H_