summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/web_request/upload_data_presenter.cc
blob: aaa739d157043ab5d6a3c9bee373b1dfb0806419 (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
// 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 "chrome/browser/extensions/api/web_request/upload_data_presenter.h"

#include "base/files/file_path.h"
#include "base/strings/string_util.h"
#include "base/values.h"
#include "chrome/browser/extensions/api/web_request/form_data_parser.h"
#include "chrome/browser/extensions/api/web_request/web_request_api_constants.h"
#include "net/base/upload_bytes_element_reader.h"
#include "net/base/upload_file_element_reader.h"
#include "net/url_request/url_request.h"

using base::BinaryValue;
using base::DictionaryValue;
using base::ListValue;
using base::StringValue;
using base::Value;

namespace keys = extension_web_request_api_constants;

namespace {

// Takes |dictionary| of <string, list of strings> pairs, and gets the list
// for |key|, creating it if necessary.
base::ListValue* GetOrCreateList(base::DictionaryValue* dictionary,
                                 const std::string& key) {
  base::ListValue* list = NULL;
  if (!dictionary->GetList(key, &list)) {
    list = new base::ListValue();
    dictionary->SetWithoutPathExpansion(key, list);
  }
  return list;
}

}  // namespace

namespace extensions {

namespace subtle {

void AppendKeyValuePair(const char* key,
                        base::Value* value,
                        base::ListValue* list) {
  base::DictionaryValue* dictionary = new base::DictionaryValue;
  dictionary->SetWithoutPathExpansion(key, value);
  list->Append(dictionary);
}

}  // namespace subtle

// Implementation of UploadDataPresenter.

UploadDataPresenter::~UploadDataPresenter() {}

// Implementation of RawDataPresenter.

RawDataPresenter::RawDataPresenter()
  : success_(true),
    list_(new base::ListValue) {
}
RawDataPresenter::~RawDataPresenter() {}

void RawDataPresenter::FeedNext(const net::UploadElementReader& reader) {
  if (!success_)
    return;

  if (reader.AsBytesReader()) {
    const net::UploadBytesElementReader* bytes_reader = reader.AsBytesReader();
    FeedNextBytes(bytes_reader->bytes(), bytes_reader->length());
  } else if (reader.AsFileReader()) {
    // Insert the file path instead of the contents, which may be too large.
    const net::UploadFileElementReader* file_reader = reader.AsFileReader();
    FeedNextFile(file_reader->path().AsUTF8Unsafe());
  } else {
    NOTIMPLEMENTED();
  }
}

bool RawDataPresenter::Succeeded() {
  return success_;
}

scoped_ptr<base::Value> RawDataPresenter::Result() {
  if (!success_)
    return scoped_ptr<base::Value>();

  return list_.PassAs<base::Value>();
}

void RawDataPresenter::FeedNextBytes(const char* bytes, size_t size) {
  subtle::AppendKeyValuePair(keys::kRequestBodyRawBytesKey,
                             BinaryValue::CreateWithCopiedBuffer(bytes, size),
                             list_.get());
}

void RawDataPresenter::FeedNextFile(const std::string& filename) {
  // Insert the file path instead of the contents, which may be too large.
  subtle::AppendKeyValuePair(keys::kRequestBodyRawFileKey,
                             new base::StringValue(filename),
                             list_.get());
}

// Implementation of ParsedDataPresenter.

ParsedDataPresenter::ParsedDataPresenter(const net::URLRequest& request)
  : parser_(FormDataParser::Create(request)),
    success_(parser_.get() != NULL),
    dictionary_(success_ ? new base::DictionaryValue() : NULL) {
}

ParsedDataPresenter::~ParsedDataPresenter() {}

void ParsedDataPresenter::FeedNext(const net::UploadElementReader& reader) {
  if (!success_)
    return;

  const net::UploadBytesElementReader* bytes_reader = reader.AsBytesReader();
  if (!bytes_reader) {
    return;
  }
  if (!parser_->SetSource(base::StringPiece(bytes_reader->bytes(),
                                            bytes_reader->length()))) {
    Abort();
    return;
  }

  FormDataParser::Result result;
  while (parser_->GetNextNameValue(&result)) {
    GetOrCreateList(dictionary_.get(), result.name())->Append(
        new base::StringValue(result.value()));
  }
}

bool ParsedDataPresenter::Succeeded() {
  if (success_ && !parser_->AllDataReadOK())
    Abort();
  return success_;
}

scoped_ptr<base::Value> ParsedDataPresenter::Result() {
  if (!success_)
    return scoped_ptr<base::Value>();

  return dictionary_.PassAs<base::Value>();
}

// static
scoped_ptr<ParsedDataPresenter> ParsedDataPresenter::CreateForTests() {
  const std::string form_type("application/x-www-form-urlencoded");
  return scoped_ptr<ParsedDataPresenter>(new ParsedDataPresenter(form_type));
}

ParsedDataPresenter::ParsedDataPresenter(const std::string& form_type)
  : parser_(FormDataParser::CreateFromContentTypeHeader(&form_type)),
    success_(parser_.get() != NULL),
    dictionary_(success_ ? new base::DictionaryValue() : NULL) {
}

void ParsedDataPresenter::Abort() {
  success_ = false;
  dictionary_.reset();
  parser_.reset();
}

}  // namespace extensions