summaryrefslogtreecommitdiffstats
path: root/webkit/glue/resource_request_body.cc
blob: 347af0efd925ac9af4d4e4a5e7c38da2fdcca5fb (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
// 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.

#include "webkit/glue/resource_request_body.h"

#include "base/logging.h"
#include "net/base/upload_data.h"
#include "webkit/blob/blob_storage_controller.h"

using webkit_blob::BlobData;
using webkit_blob::BlobStorageController;

namespace webkit_glue {

ResourceRequestBody::ResourceRequestBody() : identifier_(0) {}

void ResourceRequestBody::AppendBytes(const char* bytes, int bytes_len) {
  if (bytes_len > 0) {
    elements_.push_back(Element());
    elements_.back().SetToBytes(bytes, bytes_len);
  }
}

void ResourceRequestBody::AppendFileRange(
    const FilePath& file_path,
    uint64 offset, uint64 length,
    const base::Time& expected_modification_time) {
  elements_.push_back(Element());
  elements_.back().SetToFilePathRange(file_path, offset, length,
                                      expected_modification_time);
}

void ResourceRequestBody::AppendBlob(const GURL& blob_url) {
  elements_.push_back(Element());
  elements_.back().SetToBlobUrl(blob_url);
}

void ResourceRequestBody::AppendFileSystemFileRange(
    const GURL& url, uint64 offset, uint64 length,
    const base::Time& expected_modification_time) {
  elements_.push_back(Element());
  elements_.back().SetToFileSystemUrlRange(url, offset, length,
                                           expected_modification_time);
}

net::UploadData* ResourceRequestBody::ResolveElementsAndCreateUploadData(
    BlobStorageController* blob_controller) {
  // Resolve all blob elements.
  std::vector<const Element*> resolved_elements;
  for (size_t i = 0; i < elements_.size(); ++i) {
    const Element& element = elements_[i];
    if (element.type() == Element::TYPE_BLOB) {
      ResolveBlobReference(blob_controller, element.url(), &resolved_elements);
    } else {
      // No need to resolve, just append the element.
      resolved_elements.push_back(&element);
    }
  }

  net::UploadData* upload_data = new net::UploadData;
  // We attach 'this' to UploadData so that we do not need to copy
  // bytes for TYPE_BYTES.
  upload_data->SetUserData(
      this, new base::UserDataAdapter<ResourceRequestBody>(this));
  std::vector<net::UploadElement>* elements =
      upload_data->elements_mutable();
  for (size_t i = 0; i < resolved_elements.size(); ++i) {
    const Element& element = *resolved_elements[i];
    switch (element.type()) {
      case Element::TYPE_BYTES:
        elements->push_back(net::UploadElement());
        elements->back().SetToSharedBytes(element.bytes(), element.length());
        break;
      case Element::TYPE_FILE:
        elements->push_back(net::UploadElement());
        elements->back().SetToFilePathRange(
            element.path(),
            element.offset(),
            element.length(),
            element.expected_modification_time());
        break;
      case Element::TYPE_FILE_FILESYSTEM:
        // TODO(kinuko): Resolve FileSystemURL before creating UploadData.
        NOTREACHED();
        break;
      case Element::TYPE_BLOB:
        // Blob elements should be resolved beforehand.
        NOTREACHED();
        break;
      case Element::TYPE_UNKNOWN:
        NOTREACHED();
        break;
    }
  }
  upload_data->set_identifier(identifier_);
  return upload_data;
}

ResourceRequestBody::~ResourceRequestBody() {}

void ResourceRequestBody::ResolveBlobReference(
    webkit_blob::BlobStorageController* blob_controller,
    const GURL& blob_url,
    std::vector<const Element*>* resolved_elements) {
  DCHECK(blob_controller);
  BlobData* blob_data = blob_controller->GetBlobDataFromUrl(blob_url);
  DCHECK(blob_data);
  if (!blob_data)
    return;

  // If there is no element in the referred blob data, just return.
  if (blob_data->items().empty())
    return;

  // Ensure the blob and any attached shareable files survive until
  // upload completion.
  SetUserData(blob_data, new base::UserDataAdapter<BlobData>(blob_data));

  // Append the elements in the referred blob data.
  for (size_t i = 0; i < blob_data->items().size(); ++i) {
    const BlobData::Item& item = blob_data->items().at(i);
    DCHECK_NE(BlobData::Item::TYPE_BLOB, item.type());
    resolved_elements->push_back(&item);
  }
}

}  // namespace webkit_glue