summaryrefslogtreecommitdiffstats
path: root/webkit/blob/blob_storage_controller.cc
blob: 6490c663d33f29441c3e1d85363d60650e51dae4 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// 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.

#include "webkit/blob/blob_storage_controller.h"

#include "base/logging.h"
#include "googleurl/src/gurl.h"
#include "net/base/upload_data.h"
#include "webkit/blob/blob_data.h"

namespace webkit_blob {

namespace {

// We can't use GURL directly for these hash fragment manipulations
// since it doesn't have specific knowlege of the BlobURL format. GURL
// treats BlobURLs as if they were PathURLs which don't support hash
// fragments.

bool BlobUrlHasRef(const GURL& url) {
  return url.spec().find('#') != std::string::npos;
}

GURL ClearBlobUrlRef(const GURL& url) {
  size_t hash_pos = url.spec().find('#');
  if (hash_pos == std::string::npos)
    return url;
  return GURL(url.spec().substr(0, hash_pos));
}

static const int64 kMaxMemoryUsage = 1024 * 1024 * 1024;  // 1G

}  // namespace

BlobStorageController::BlobStorageController()
    : memory_usage_(0) {
}

BlobStorageController::~BlobStorageController() {
}

void BlobStorageController::StartBuildingBlob(const GURL& url) {
  DCHECK(url.SchemeIs("blob"));
  DCHECK(!BlobUrlHasRef(url));
  BlobData* blob_data = new BlobData;
  unfinalized_blob_map_[url.spec()] = blob_data;
  IncrementBlobDataUsage(blob_data);
}

void BlobStorageController::AppendBlobDataItem(
    const GURL& url, const BlobData::Item& item) {
  DCHECK(url.SchemeIs("blob"));
  DCHECK(!BlobUrlHasRef(url));
  BlobMap::iterator found = unfinalized_blob_map_.find(url.spec());
  if (found == unfinalized_blob_map_.end())
    return;
  BlobData* target_blob_data = found->second;
  DCHECK(target_blob_data);

  memory_usage_ -= target_blob_data->GetMemoryUsage();

  // The blob data is stored in the "canonical" way. That is, it only contains a
  // list of Data and File items.
  // 1) The Data item is denoted by the raw data and the range.
  // 2) The File item is denoted by the file path, the range and the expected
  //    modification time.
  // All the Blob items in the passing blob data are resolved and expanded into
  // a set of Data and File items.

  switch (item.type) {
    case BlobData::TYPE_DATA:
      // WebBlobData does not allow partial data.
      DCHECK(!(item.offset) && item.length == item.data.size());
      target_blob_data->AppendData(item.data.c_str(), item.data.size());
      break;
    case BlobData::TYPE_DATA_EXTERNAL:
      DCHECK(!item.offset);
      target_blob_data->AppendData(item.data_external, item.length);
      break;
    case BlobData::TYPE_FILE:
      AppendFileItem(target_blob_data,
                     item.file_path,
                     item.offset,
                     item.length,
                     item.expected_modification_time);
      break;
    case BlobData::TYPE_BLOB:
      BlobData* src_blob_data = GetBlobDataFromUrl(item.blob_url);
      DCHECK(src_blob_data);
      if (src_blob_data)
        AppendStorageItems(target_blob_data,
                           src_blob_data,
                           item.offset,
                           item.length);
      break;
  }

  memory_usage_ += target_blob_data->GetMemoryUsage();

  // If we're using too much memory, drop this blob.
  // TODO(michaeln): Blob memory storage does not yet spill over to disk,
  // until it does, we'll prevent memory usage over a max amount.
  if (memory_usage_ > kMaxMemoryUsage)
    RemoveBlob(url);
}

void BlobStorageController::FinishBuildingBlob(
    const GURL& url, const std::string& content_type) {
  DCHECK(url.SchemeIs("blob"));
  DCHECK(!BlobUrlHasRef(url));
  BlobMap::iterator found = unfinalized_blob_map_.find(url.spec());
  if (found == unfinalized_blob_map_.end())
    return;
  found->second->set_content_type(content_type);
  blob_map_[url.spec()] = found->second;
  unfinalized_blob_map_.erase(found);
}

void BlobStorageController::AddFinishedBlob(const GURL& url,
                                            const BlobData* data) {
  StartBuildingBlob(url);
  for (std::vector<BlobData::Item>::const_iterator iter =
           data->items().begin();
       iter != data->items().end(); ++iter) {
    AppendBlobDataItem(url, *iter);
  }
  FinishBuildingBlob(url, data->content_type());
}

void BlobStorageController::CloneBlob(
    const GURL& url, const GURL& src_url) {
  DCHECK(url.SchemeIs("blob"));
  DCHECK(!BlobUrlHasRef(url));

  BlobData* blob_data = GetBlobDataFromUrl(src_url);
  DCHECK(blob_data);
  if (!blob_data)
    return;

  blob_map_[url.spec()] = blob_data;
  IncrementBlobDataUsage(blob_data);
}

void BlobStorageController::RemoveBlob(const GURL& url) {
  DCHECK(url.SchemeIs("blob"));
  DCHECK(!BlobUrlHasRef(url));

  if (!RemoveFromMapHelper(&unfinalized_blob_map_, url))
    RemoveFromMapHelper(&blob_map_, url);
}

bool BlobStorageController::RemoveFromMapHelper(
    BlobMap* map, const GURL& url) {
  BlobMap::iterator found = map->find(url.spec());
  if (found == map->end())
    return false;
  if (DecrementBlobDataUsage(found->second))
    memory_usage_ -= found->second->GetMemoryUsage();
  map->erase(found);
  return true;
}


BlobData* BlobStorageController::GetBlobDataFromUrl(const GURL& url) {
  BlobMap::iterator found = blob_map_.find(
      BlobUrlHasRef(url) ? ClearBlobUrlRef(url).spec() : url.spec());
  return (found != blob_map_.end()) ? found->second : NULL;
}

void BlobStorageController::ResolveBlobReferencesInUploadData(
    net::UploadData* upload_data) {
  DCHECK(upload_data);

  std::vector<net::UploadData::Element>* uploads = upload_data->elements();
  std::vector<net::UploadData::Element>::iterator iter;
  for (iter = uploads->begin(); iter != uploads->end();) {
    if (iter->type() != net::UploadData::TYPE_BLOB) {
      iter++;
      continue;
    }

    // Find the referred blob data.
    BlobData* blob_data = GetBlobDataFromUrl(iter->blob_url());
    DCHECK(blob_data);
    if (!blob_data) {
      // TODO(jianli): We should probably fail uploading the data
      iter++;
      continue;
    }

    // Remove this element.
    iter = uploads->erase(iter);

    // If there is no element in the referred blob data, continue the loop.
    // Note that we should not increase iter since it already points to the one
    // after the removed element.
    if (blob_data->items().empty())
      continue;

    // Insert the elements in the referred blob data.
    // Note that we traverse from the bottom so that the elements can be
    // inserted in the original order.
    for (size_t i = blob_data->items().size(); i > 0; --i) {
      iter = uploads->insert(iter, net::UploadData::Element());

      const BlobData::Item& item = blob_data->items().at(i - 1);
      switch (item.type) {
        case BlobData::TYPE_DATA:
          // TODO(jianli): Figure out how to avoid copying the data.
          iter->SetToBytes(
              &item.data.at(0) + static_cast<int>(item.offset),
              static_cast<int>(item.length));
          break;
        case BlobData::TYPE_FILE:
          // TODO(michaeln): Ensure that any temp files survive till the
          // net::URLRequest is done with the upload.
          iter->SetToFilePathRange(
              item.file_path,
              item.offset,
              item.length,
              item.expected_modification_time);
          break;
        default:
          NOTREACHED();
          break;
      }
    }
  }
}

void BlobStorageController::AppendStorageItems(
    BlobData* target_blob_data, BlobData* src_blob_data,
    uint64 offset, uint64 length) {
  DCHECK(target_blob_data && src_blob_data &&
         length != static_cast<uint64>(-1));

  std::vector<BlobData::Item>::const_iterator iter =
      src_blob_data->items().begin();
  if (offset) {
    for (; iter != src_blob_data->items().end(); ++iter) {
      if (offset >= iter->length)
        offset -= iter->length;
      else
        break;
    }
  }

  for (; iter != src_blob_data->items().end() && length > 0; ++iter) {
    uint64 current_length = iter->length - offset;
    uint64 new_length = current_length > length ? length : current_length;
    if (iter->type == BlobData::TYPE_DATA) {
      target_blob_data->AppendData(
          iter->data.c_str() + static_cast<size_t>(iter->offset + offset),
          static_cast<uint32>(new_length));
    } else {
      DCHECK(iter->type == BlobData::TYPE_FILE);
      AppendFileItem(target_blob_data,
                     iter->file_path,
                     iter->offset + offset,
                     new_length,
                     iter->expected_modification_time);
    }
    length -= new_length;
    offset = 0;
  }
}

void BlobStorageController::AppendFileItem(
    BlobData* target_blob_data,
    const FilePath& file_path, uint64 offset, uint64 length,
    const base::Time& expected_modification_time) {
  target_blob_data->AppendFile(file_path, offset, length,
                               expected_modification_time);

  // It may be a temporary file that should be deleted when no longer needed.
  scoped_refptr<DeletableFileReference> deletable_file =
      DeletableFileReference::Get(file_path);
  if (deletable_file)
    target_blob_data->AttachDeletableFileReference(deletable_file);
}

void BlobStorageController::IncrementBlobDataUsage(BlobData* blob_data) {
  blob_data_usage_count_[blob_data] += 1;
}

bool BlobStorageController::DecrementBlobDataUsage(BlobData* blob_data) {
  BlobDataUsageMap::iterator found = blob_data_usage_count_.find(blob_data);
  DCHECK(found != blob_data_usage_count_.end());
  if (--(found->second))
    return false;  // Still in use
  blob_data_usage_count_.erase(found);
  return true;
}

}  // namespace webkit_blob