Create network_converter for services/network/* types
This commit is contained in:
parent
f1e2304585
commit
c4b8e106c0
11 changed files with 126 additions and 123 deletions
|
@ -20,6 +20,7 @@
|
||||||
#include "atom/common/native_mate_converters/gurl_converter.h"
|
#include "atom/common/native_mate_converters/gurl_converter.h"
|
||||||
#include "atom/common/native_mate_converters/image_converter.h"
|
#include "atom/common/native_mate_converters/image_converter.h"
|
||||||
#include "atom/common/native_mate_converters/net_converter.h"
|
#include "atom/common/native_mate_converters/net_converter.h"
|
||||||
|
#include "atom/common/native_mate_converters/network_converter.h"
|
||||||
#include "atom/common/native_mate_converters/value_converter.h"
|
#include "atom/common/native_mate_converters/value_converter.h"
|
||||||
#include "atom/common/node_includes.h"
|
#include "atom/common/node_includes.h"
|
||||||
#include "atom/common/options_switches.h"
|
#include "atom/common/options_switches.h"
|
||||||
|
|
|
@ -51,6 +51,7 @@
|
||||||
#include "atom/common/native_mate_converters/gurl_converter.h"
|
#include "atom/common/native_mate_converters/gurl_converter.h"
|
||||||
#include "atom/common/native_mate_converters/image_converter.h"
|
#include "atom/common/native_mate_converters/image_converter.h"
|
||||||
#include "atom/common/native_mate_converters/net_converter.h"
|
#include "atom/common/native_mate_converters/net_converter.h"
|
||||||
|
#include "atom/common/native_mate_converters/network_converter.h"
|
||||||
#include "atom/common/native_mate_converters/string16_converter.h"
|
#include "atom/common/native_mate_converters/string16_converter.h"
|
||||||
#include "atom/common/native_mate_converters/value_converter.h"
|
#include "atom/common/native_mate_converters/value_converter.h"
|
||||||
#include "atom/common/options_switches.h"
|
#include "atom/common/options_switches.h"
|
||||||
|
|
|
@ -18,9 +18,6 @@
|
||||||
#include "content/public/browser/web_contents.h"
|
#include "content/public/browser/web_contents.h"
|
||||||
#include "content/public/common/context_menu_params.h"
|
#include "content/public/common/context_menu_params.h"
|
||||||
#include "native_mate/dictionary.h"
|
#include "native_mate/dictionary.h"
|
||||||
#include "services/network/public/cpp/resource_request_body.h"
|
|
||||||
|
|
||||||
using network::ResourceRequestBody;
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
@ -203,98 +200,6 @@ bool Converter<content::StopFindAction>::FromV8(v8::Isolate* isolate,
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
|
||||||
v8::Local<v8::Value> Converter<scoped_refptr<ResourceRequestBody>>::ToV8(
|
|
||||||
v8::Isolate* isolate,
|
|
||||||
const scoped_refptr<ResourceRequestBody>& val) {
|
|
||||||
if (!val)
|
|
||||||
return v8::Null(isolate);
|
|
||||||
auto list = std::make_unique<base::ListValue>();
|
|
||||||
for (const auto& element : *(val->elements())) {
|
|
||||||
auto post_data_dict = std::make_unique<base::DictionaryValue>();
|
|
||||||
auto type = element.type();
|
|
||||||
if (type == ResourceRequestBody::Element::TYPE_BYTES) {
|
|
||||||
std::unique_ptr<base::Value> bytes(base::Value::CreateWithCopiedBuffer(
|
|
||||||
element.bytes(), static_cast<size_t>(element.length())));
|
|
||||||
post_data_dict->SetString("type", "rawData");
|
|
||||||
post_data_dict->Set("bytes", std::move(bytes));
|
|
||||||
} else if (type == ResourceRequestBody::Element::TYPE_FILE) {
|
|
||||||
post_data_dict->SetString("type", "file");
|
|
||||||
post_data_dict->SetKey("filePath",
|
|
||||||
base::Value(element.path().AsUTF8Unsafe()));
|
|
||||||
post_data_dict->SetInteger("offset", static_cast<int>(element.offset()));
|
|
||||||
post_data_dict->SetInteger("length", static_cast<int>(element.length()));
|
|
||||||
post_data_dict->SetDouble(
|
|
||||||
"modificationTime", element.expected_modification_time().ToDoubleT());
|
|
||||||
} else if (type == ResourceRequestBody::Element::TYPE_FILE_FILESYSTEM) {
|
|
||||||
post_data_dict->SetString("type", "fileSystem");
|
|
||||||
post_data_dict->SetKey("fileSystemURL",
|
|
||||||
base::Value(element.filesystem_url().spec()));
|
|
||||||
post_data_dict->SetInteger("offset", static_cast<int>(element.offset()));
|
|
||||||
post_data_dict->SetInteger("length", static_cast<int>(element.length()));
|
|
||||||
post_data_dict->SetDouble(
|
|
||||||
"modificationTime", element.expected_modification_time().ToDoubleT());
|
|
||||||
} else if (type == ResourceRequestBody::Element::TYPE_BLOB) {
|
|
||||||
post_data_dict->SetString("type", "blob");
|
|
||||||
post_data_dict->SetString("blobUUID", element.blob_uuid());
|
|
||||||
}
|
|
||||||
list->Append(std::move(post_data_dict));
|
|
||||||
}
|
|
||||||
return ConvertToV8(isolate, *list);
|
|
||||||
}
|
|
||||||
|
|
||||||
// static
|
|
||||||
bool Converter<scoped_refptr<ResourceRequestBody>>::FromV8(
|
|
||||||
v8::Isolate* isolate,
|
|
||||||
v8::Local<v8::Value> val,
|
|
||||||
scoped_refptr<ResourceRequestBody>* out) {
|
|
||||||
auto list = std::make_unique<base::ListValue>();
|
|
||||||
if (!ConvertFromV8(isolate, val, list.get()))
|
|
||||||
return false;
|
|
||||||
*out = new network::ResourceRequestBody();
|
|
||||||
for (size_t i = 0; i < list->GetSize(); ++i) {
|
|
||||||
base::DictionaryValue* dict = nullptr;
|
|
||||||
std::string type;
|
|
||||||
if (!list->GetDictionary(i, &dict))
|
|
||||||
return false;
|
|
||||||
dict->GetString("type", &type);
|
|
||||||
if (type == "rawData") {
|
|
||||||
base::Value* bytes = nullptr;
|
|
||||||
dict->GetBinary("bytes", &bytes);
|
|
||||||
(*out)->AppendBytes(bytes->GetBlob().data(), bytes->GetBlob().size());
|
|
||||||
} else if (type == "file") {
|
|
||||||
std::string file;
|
|
||||||
int offset = 0, length = -1;
|
|
||||||
double modification_time = 0.0;
|
|
||||||
dict->GetStringWithoutPathExpansion("filePath", &file);
|
|
||||||
dict->GetInteger("offset", &offset);
|
|
||||||
dict->GetInteger("file", &length);
|
|
||||||
dict->GetDouble("modificationTime", &modification_time);
|
|
||||||
(*out)->AppendFileRange(base::FilePath::FromUTF8Unsafe(file),
|
|
||||||
static_cast<uint64_t>(offset),
|
|
||||||
static_cast<uint64_t>(length),
|
|
||||||
base::Time::FromDoubleT(modification_time));
|
|
||||||
} else if (type == "fileSystem") {
|
|
||||||
std::string file_system_url;
|
|
||||||
int offset = 0, length = -1;
|
|
||||||
double modification_time = 0.0;
|
|
||||||
dict->GetStringWithoutPathExpansion("fileSystemURL", &file_system_url);
|
|
||||||
dict->GetInteger("offset", &offset);
|
|
||||||
dict->GetInteger("file", &length);
|
|
||||||
dict->GetDouble("modificationTime", &modification_time);
|
|
||||||
(*out)->AppendFileSystemFileRange(
|
|
||||||
GURL(file_system_url), static_cast<uint64_t>(offset),
|
|
||||||
static_cast<uint64_t>(length),
|
|
||||||
base::Time::FromDoubleT(modification_time));
|
|
||||||
} else if (type == "blob") {
|
|
||||||
std::string uuid;
|
|
||||||
dict->GetString("blobUUID", &uuid);
|
|
||||||
(*out)->AppendBlob(uuid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// static
|
// static
|
||||||
v8::Local<v8::Value> Converter<content::WebContents*>::ToV8(
|
v8::Local<v8::Value> Converter<content::WebContents*>::ToV8(
|
||||||
v8::Isolate* isolate,
|
v8::Isolate* isolate,
|
||||||
|
|
|
@ -19,10 +19,6 @@ struct ContextMenuParams;
|
||||||
class WebContents;
|
class WebContents;
|
||||||
} // namespace content
|
} // namespace content
|
||||||
|
|
||||||
namespace network {
|
|
||||||
class ResourceRequestBody;
|
|
||||||
}
|
|
||||||
|
|
||||||
using ContextMenuParamsWithWebContents =
|
using ContextMenuParamsWithWebContents =
|
||||||
std::pair<content::ContextMenuParams, content::WebContents*>;
|
std::pair<content::ContextMenuParams, content::WebContents*>;
|
||||||
|
|
||||||
|
@ -53,16 +49,6 @@ struct Converter<content::PermissionType> {
|
||||||
const content::PermissionType& val);
|
const content::PermissionType& val);
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
|
||||||
struct Converter<scoped_refptr<network::ResourceRequestBody>> {
|
|
||||||
static v8::Local<v8::Value> ToV8(
|
|
||||||
v8::Isolate* isolate,
|
|
||||||
const scoped_refptr<network::ResourceRequestBody>& val);
|
|
||||||
static bool FromV8(v8::Isolate* isolate,
|
|
||||||
v8::Local<v8::Value> val,
|
|
||||||
scoped_refptr<network::ResourceRequestBody>* out);
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct Converter<content::StopFindAction> {
|
struct Converter<content::StopFindAction> {
|
||||||
static bool FromV8(v8::Isolate* isolate,
|
static bool FromV8(v8::Isolate* isolate,
|
||||||
|
|
90
atom/common/native_mate_converters/network_converter.cc
Normal file
90
atom/common/native_mate_converters/network_converter.cc
Normal file
|
@ -0,0 +1,90 @@
|
||||||
|
// Copyright (c) 2018 GitHub, Inc.
|
||||||
|
// Use of this source code is governed by the MIT license that can be
|
||||||
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
#include "atom/common/native_mate_converters/network_converter.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
#include "atom/common/native_mate_converters/value_converter.h"
|
||||||
|
#include "native_mate/dictionary.h"
|
||||||
|
#include "services/network/public/cpp/resource_request_body.h"
|
||||||
|
|
||||||
|
namespace mate {
|
||||||
|
|
||||||
|
// static
|
||||||
|
v8::Local<v8::Value>
|
||||||
|
Converter<scoped_refptr<network::ResourceRequestBody>>::ToV8(
|
||||||
|
v8::Isolate* isolate,
|
||||||
|
const scoped_refptr<network::ResourceRequestBody>& val) {
|
||||||
|
if (!val)
|
||||||
|
return v8::Null(isolate);
|
||||||
|
std::unique_ptr<base::ListValue> list(new base::ListValue);
|
||||||
|
for (const auto& element : *(val->elements())) {
|
||||||
|
std::unique_ptr<base::DictionaryValue> post_data_dict(
|
||||||
|
new base::DictionaryValue);
|
||||||
|
auto type = element.type();
|
||||||
|
if (type == network::DataElement::TYPE_BYTES) {
|
||||||
|
std::unique_ptr<base::Value> bytes(base::Value::CreateWithCopiedBuffer(
|
||||||
|
element.bytes(), static_cast<size_t>(element.length())));
|
||||||
|
post_data_dict->SetString("type", "rawData");
|
||||||
|
post_data_dict->Set("bytes", std::move(bytes));
|
||||||
|
} else if (type == network::DataElement::TYPE_FILE) {
|
||||||
|
post_data_dict->SetString("type", "file");
|
||||||
|
post_data_dict->SetKey("filePath",
|
||||||
|
base::Value(element.path().AsUTF8Unsafe()));
|
||||||
|
post_data_dict->SetInteger("offset", static_cast<int>(element.offset()));
|
||||||
|
post_data_dict->SetInteger("length", static_cast<int>(element.length()));
|
||||||
|
post_data_dict->SetDouble(
|
||||||
|
"modificationTime", element.expected_modification_time().ToDoubleT());
|
||||||
|
} else if (type == network::DataElement::TYPE_BLOB) {
|
||||||
|
post_data_dict->SetString("type", "blob");
|
||||||
|
post_data_dict->SetString("blobUUID", element.blob_uuid());
|
||||||
|
}
|
||||||
|
list->Append(std::move(post_data_dict));
|
||||||
|
}
|
||||||
|
return ConvertToV8(isolate, *list);
|
||||||
|
}
|
||||||
|
|
||||||
|
// static
|
||||||
|
bool Converter<scoped_refptr<network::ResourceRequestBody>>::FromV8(
|
||||||
|
v8::Isolate* isolate,
|
||||||
|
v8::Local<v8::Value> val,
|
||||||
|
scoped_refptr<network::ResourceRequestBody>* out) {
|
||||||
|
std::unique_ptr<base::ListValue> list(new base::ListValue);
|
||||||
|
if (!ConvertFromV8(isolate, val, list.get()))
|
||||||
|
return false;
|
||||||
|
*out = new network::ResourceRequestBody();
|
||||||
|
for (size_t i = 0; i < list->GetSize(); ++i) {
|
||||||
|
base::DictionaryValue* dict = nullptr;
|
||||||
|
std::string type;
|
||||||
|
if (!list->GetDictionary(i, &dict))
|
||||||
|
return false;
|
||||||
|
dict->GetString("type", &type);
|
||||||
|
if (type == "rawData") {
|
||||||
|
base::Value* bytes = nullptr;
|
||||||
|
dict->GetBinary("bytes", &bytes);
|
||||||
|
(*out)->AppendBytes(bytes->GetBlob().data(), bytes->GetBlob().size());
|
||||||
|
} else if (type == "file") {
|
||||||
|
std::string file;
|
||||||
|
int offset = 0, length = -1;
|
||||||
|
double modification_time = 0.0;
|
||||||
|
dict->GetStringWithoutPathExpansion("filePath", &file);
|
||||||
|
dict->GetInteger("offset", &offset);
|
||||||
|
dict->GetInteger("file", &length);
|
||||||
|
dict->GetDouble("modificationTime", &modification_time);
|
||||||
|
(*out)->AppendFileRange(base::FilePath::FromUTF8Unsafe(file),
|
||||||
|
static_cast<uint64_t>(offset),
|
||||||
|
static_cast<uint64_t>(length),
|
||||||
|
base::Time::FromDoubleT(modification_time));
|
||||||
|
} else if (type == "blob") {
|
||||||
|
std::string uuid;
|
||||||
|
dict->GetString("blobUUID", &uuid);
|
||||||
|
(*out)->AppendBlob(uuid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace mate
|
29
atom/common/native_mate_converters/network_converter.h
Normal file
29
atom/common/native_mate_converters/network_converter.h
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
// Copyright (c) 2018 GitHub, Inc.
|
||||||
|
// Use of this source code is governed by the MIT license that can be
|
||||||
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
#ifndef ATOM_COMMON_NATIVE_MATE_CONVERTERS_NETWORK_CONVERTER_H_
|
||||||
|
#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_NETWORK_CONVERTER_H_
|
||||||
|
|
||||||
|
#include "base/memory/scoped_refptr.h"
|
||||||
|
#include "native_mate/converter.h"
|
||||||
|
|
||||||
|
namespace network {
|
||||||
|
class ResourceRequestBody;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace mate {
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct Converter<scoped_refptr<network::ResourceRequestBody>> {
|
||||||
|
static v8::Local<v8::Value> ToV8(
|
||||||
|
v8::Isolate* isolate,
|
||||||
|
const scoped_refptr<network::ResourceRequestBody>& val);
|
||||||
|
static bool FromV8(v8::Isolate* isolate,
|
||||||
|
v8::Local<v8::Value> val,
|
||||||
|
scoped_refptr<network::ResourceRequestBody>* out);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace mate
|
||||||
|
|
||||||
|
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_NETWORK_CONVERTER_H_
|
|
@ -1153,7 +1153,7 @@ Same as `webContents.capturePage([rect, ]callback)`.
|
||||||
* `httpReferrer` (String | [Referrer](structures/referrer.md)) (optional) - An HTTP Referrer url.
|
* `httpReferrer` (String | [Referrer](structures/referrer.md)) (optional) - An HTTP Referrer url.
|
||||||
* `userAgent` String (optional) - A user agent originating the request.
|
* `userAgent` String (optional) - A user agent originating the request.
|
||||||
* `extraHeaders` String (optional) - Extra headers separated by "\n"
|
* `extraHeaders` String (optional) - Extra headers separated by "\n"
|
||||||
* `postData` ([UploadRawData[]](structures/upload-raw-data.md) | [UploadFile[]](structures/upload-file.md) | [UploadFileSystem[]](structures/upload-file-system.md) | [UploadBlob[]](structures/upload-blob.md)) (optional)
|
* `postData` ([UploadRawData[]](structures/upload-raw-data.md) | [UploadFile[]](structures/upload-file.md) | [UploadBlob[]](structures/upload-blob.md)) (optional)
|
||||||
* `baseURLForDataURL` String (optional) - Base url (with trailing path separator) for files to be loaded by the data url. This is needed only if the specified `url` is a data url and needs to load other files.
|
* `baseURLForDataURL` String (optional) - Base url (with trailing path separator) for files to be loaded by the data url. This is needed only if the specified `url` is a data url and needs to load other files.
|
||||||
|
|
||||||
Same as `webContents.loadURL(url[, options])`.
|
Same as `webContents.loadURL(url[, options])`.
|
||||||
|
|
|
@ -1,11 +0,0 @@
|
||||||
# UploadFileSystem Object
|
|
||||||
|
|
||||||
*(Deprecated)*
|
|
||||||
|
|
||||||
* `type` String - `fileSystem`.
|
|
||||||
* `filsSystemURL` String - FileSystem url to read data for upload.
|
|
||||||
* `offset` Integer - Defaults to `0`.
|
|
||||||
* `length` Integer - Number of bytes to read from `offset`.
|
|
||||||
Defaults to `0`.
|
|
||||||
* `modificationTime` Double - Last Modification time in
|
|
||||||
number of seconds since the UNIX epoch.
|
|
|
@ -634,7 +634,7 @@ for windows with *offscreen rendering* enabled.
|
||||||
* `httpReferrer` (String | [Referrer](structures/referrer.md)) (optional) - An HTTP Referrer url.
|
* `httpReferrer` (String | [Referrer](structures/referrer.md)) (optional) - An HTTP Referrer url.
|
||||||
* `userAgent` String (optional) - A user agent originating the request.
|
* `userAgent` String (optional) - A user agent originating the request.
|
||||||
* `extraHeaders` String (optional) - Extra headers separated by "\n".
|
* `extraHeaders` String (optional) - Extra headers separated by "\n".
|
||||||
* `postData` ([UploadRawData[]](structures/upload-raw-data.md) | [UploadFile[]](structures/upload-file.md) | [UploadFileSystem[]](structures/upload-file-system.md) | [UploadBlob[]](structures/upload-blob.md)) (optional)
|
* `postData` ([UploadRawData[]](structures/upload-raw-data.md) | [UploadFile[]](structures/upload-file.md) | [UploadBlob[]](structures/upload-blob.md)) (optional)
|
||||||
* `baseURLForDataURL` String (optional) - Base url (with trailing path separator) for files to be loaded by the data url. This is needed only if the specified `url` is a data url and needs to load other files.
|
* `baseURLForDataURL` String (optional) - Base url (with trailing path separator) for files to be loaded by the data url. This is needed only if the specified `url` is a data url and needs to load other files.
|
||||||
|
|
||||||
Loads the `url` in the window. The `url` must contain the protocol prefix,
|
Loads the `url` in the window. The `url` must contain the protocol prefix,
|
||||||
|
|
|
@ -309,7 +309,7 @@ webview.addEventListener('dom-ready', () => {
|
||||||
* `httpReferrer` (String | [Referrer](structures/referrer.md)) (optional) - An HTTP Referrer url.
|
* `httpReferrer` (String | [Referrer](structures/referrer.md)) (optional) - An HTTP Referrer url.
|
||||||
* `userAgent` String (optional) - A user agent originating the request.
|
* `userAgent` String (optional) - A user agent originating the request.
|
||||||
* `extraHeaders` String (optional) - Extra headers separated by "\n"
|
* `extraHeaders` String (optional) - Extra headers separated by "\n"
|
||||||
* `postData` ([UploadRawData[]](structures/upload-raw-data.md) | [UploadFile[]](structures/upload-file.md) | [UploadFileSystem[]](structures/upload-file-system.md) | [UploadBlob[]](structures/upload-blob.md)) (optional)
|
* `postData` ([UploadRawData[]](structures/upload-raw-data.md) | [UploadFile[]](structures/upload-file.md) | [UploadBlob[]](structures/upload-blob.md)) (optional)
|
||||||
* `baseURLForDataURL` String (optional) - Base url (with trailing path separator) for files to be loaded by the data url. This is needed only if the specified `url` is a data url and needs to load other files.
|
* `baseURLForDataURL` String (optional) - Base url (with trailing path separator) for files to be loaded by the data url. This is needed only if the specified `url` is a data url and needs to load other files.
|
||||||
|
|
||||||
Loads the `url` in the webview, the `url` must contain the protocol prefix,
|
Loads the `url` in the webview, the `url` must contain the protocol prefix,
|
||||||
|
|
|
@ -499,6 +499,8 @@
|
||||||
'atom/common/native_mate_converters/image_converter.h',
|
'atom/common/native_mate_converters/image_converter.h',
|
||||||
'atom/common/native_mate_converters/net_converter.cc',
|
'atom/common/native_mate_converters/net_converter.cc',
|
||||||
'atom/common/native_mate_converters/net_converter.h',
|
'atom/common/native_mate_converters/net_converter.h',
|
||||||
|
'atom/common/native_mate_converters/network_converter.cc',
|
||||||
|
'atom/common/native_mate_converters/network_converter.h',
|
||||||
'atom/common/native_mate_converters/string16_converter.h',
|
'atom/common/native_mate_converters/string16_converter.h',
|
||||||
'atom/common/native_mate_converters/ui_base_types_converter.h',
|
'atom/common/native_mate_converters/ui_base_types_converter.h',
|
||||||
'atom/common/native_mate_converters/v8_value_converter.cc',
|
'atom/common/native_mate_converters/v8_value_converter.cc',
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue