Commit d01dc6dc authored by kozyatinskiy's avatar kozyatinskiy Committed by Commit bot

[inspector] use own copy of third_party/inspector_protocol

When Chromium and V8 use the same version of inspector_protocol, any protocol change takes at least 4 patches and 3 waiting for rolls.
To simplify this process we need to have two diffrent versions of inspector_protocol in Chromium and V8. Current state of inspector_protocol was extracted into separate repository [1]. This CL puts last version of inspector_protocol into third_party/inspector_protocol and removes dependency on inspector_protocol in Webkit.

[1] https://chromium.googlesource.com/deps/inspector_protocol/

BUG=chromium:637032
R=dgozman@chromium.org
CQ_INCLUDE_TRYBOTS=master.tryserver.blink:linux_precise_blink_rel

Review-Url: https://codereview.chromium.org/2447323002
Cr-Commit-Position: refs/heads/master@{#40655}
parent 96d12ac6
......@@ -71,7 +71,6 @@ shell_g
/third_party/llvm
/third_party/llvm-build
/third_party/markupsafe
/third_party/WebKit
/tools/clang
/tools/gcmole/gcmole-tools
/tools/gcmole/gcmole-tools.tar.gz
......
......@@ -19,8 +19,6 @@ deps = {
Var("chromium_url") + "/chromium/buildtools.git" + "@" + "39b1db2ab4aa4b2ccaa263c29bdf63e7c1ee28aa",
"v8/base/trace_event/common":
Var("chromium_url") + "/chromium/src/base/trace_event/common.git" + "@" + "06294c8a4a6f744ef284cd63cfe54dbf61eea290",
"v8/third_party/WebKit/Source/platform/inspector_protocol":
Var("chromium_url") + "/chromium/src/third_party/WebKit/Source/platform/inspector_protocol.git" + "@" + "3280c57c4c575ce82ccd13e4a403492fb4ca624b",
"v8/third_party/jinja2":
Var("chromium_url") + "/chromium/src/third_party/jinja2.git" + "@" + "b61a2c009a579593a259c1b300e0ad02bf48fd78",
"v8/third_party/markupsafe":
......
......@@ -4,7 +4,7 @@
import("../../gni/v8.gni")
_inspector_protocol = "//third_party/WebKit/Source/platform/inspector_protocol"
_inspector_protocol = v8_path_prefix + "/third_party/inspector_protocol"
import("$_inspector_protocol/inspector_protocol.gni")
_protocol_generated = [
......@@ -51,6 +51,7 @@ inspector_protocol_generate("protocol_generated_sources") {
":protocol_compatibility",
]
inspector_protocol_dir = _inspector_protocol
out_dir = target_gen_dir
config_file = "inspector_protocol_config.json"
inputs = [
......
......@@ -4,11 +4,11 @@
{
'variables': {
'protocol_path': '<(PRODUCT_DIR)/../../third_party/WebKit/Source/platform/inspector_protocol',
'protocol_path': '<(PRODUCT_DIR)/../../third_party/inspector_protocol',
},
'includes': [
'inspector.gypi',
'<(PRODUCT_DIR)/../../../third_party/WebKit/Source/platform/inspector_protocol/inspector_protocol.gypi',
'<(PRODUCT_DIR)/../../../third_party/inspector_protocol/inspector_protocol.gypi',
],
'targets': [
{ 'target_name': 'inspector_injected_script',
......
This diff is collapsed.
This diff is collapsed.
#!/usr/bin/env python
# Copyright 2016 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.
import os.path
import sys
try:
import json
except ImportError:
import simplejson as json
def main(argv):
if len(argv) < 1:
sys.stderr.write("Usage: %s <protocol-1> [<protocol-2> [, <protocol-3>...]] <output-file>\n" % sys.argv[0])
return 1
domains = []
version = None
for protocol in argv[:-1]:
file_name = os.path.normpath(protocol)
if not os.path.isfile(file_name):
sys.stderr.write("Cannot find %s\n" % file_name)
return 1
input_file = open(file_name, "r")
json_string = input_file.read()
parsed_json = json.loads(json_string)
domains += parsed_json["domains"]
version = parsed_json["version"]
output_file = open(argv[-1], "w")
json.dump({"version": version, "domains": domains}, output_file, indent=4, sort_keys=False, separators=(',', ': '))
output_file.close()
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
// Copyright 2016 The Chromium Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
set noparent
alph@chromium.org
caseq@chromium.org
dgozman@chromium.org
jochen@chromium.org
kozyatinskiy@chromium.org
pfeldman@chromium.org
yangguo@chromium.org
\ No newline at end of file
Name: inspector protocol
Short Name: inspector_protocol
URL: https://chromium.googlesource.com/deps/inspector_protocol/
Version: 0
Revision: 6c15061ecf7168e520d33633b217e029e74760a7
License: BSD
License File: LICENSE
Security Critical: no
Description:
src/inspector uses these scripts to generate handlers from protocol
description.
Local modifications:
- This only includes the lib/ and templates/ directories, scripts, build
and the LICENSE files.
\ No newline at end of file
# Copyright 2016 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.
# This template will generate inspector protocol source code. The code will
# not be compiled, use get_target_outputs(<name>) to compile them.
#
# Inputs
#
# config_file (required)
# Path to json file specifying inspector protocol configuration.
#
# out_dir (required)
# Path to put the generated files in. It must be inside output or
# generated file directory.
#
# outputs (required)
# Files generated. Relative to out_dir.
#
# inputs (optional)
# Extra inputs specified by the config file.
template("inspector_protocol_generate") {
assert(defined(invoker.config_file))
assert(defined(invoker.out_dir))
assert(defined(invoker.outputs))
assert(defined(invoker.inspector_protocol_dir))
inspector_protocol_dir = invoker.inspector_protocol_dir
action(target_name) {
script = "$inspector_protocol_dir/CodeGenerator.py"
inputs = [
invoker.config_file,
"$inspector_protocol_dir/lib/Allocator_h.template",
"$inspector_protocol_dir/lib/Array_h.template",
"$inspector_protocol_dir/lib/BackendCallback_h.template",
"$inspector_protocol_dir/lib/Collections_h.template",
"$inspector_protocol_dir/lib/DispatcherBase_cpp.template",
"$inspector_protocol_dir/lib/DispatcherBase_h.template",
"$inspector_protocol_dir/lib/ErrorSupport_cpp.template",
"$inspector_protocol_dir/lib/ErrorSupport_h.template",
"$inspector_protocol_dir/lib/Forward_h.template",
"$inspector_protocol_dir/lib/FrontendChannel_h.template",
"$inspector_protocol_dir/lib/Maybe_h.template",
"$inspector_protocol_dir/lib/Object_cpp.template",
"$inspector_protocol_dir/lib/Object_h.template",
"$inspector_protocol_dir/lib/Parser_cpp.template",
"$inspector_protocol_dir/lib/Parser_h.template",
"$inspector_protocol_dir/lib/Protocol_cpp.template",
"$inspector_protocol_dir/lib/ValueConversions_h.template",
"$inspector_protocol_dir/lib/Values_cpp.template",
"$inspector_protocol_dir/lib/Values_h.template",
"$inspector_protocol_dir/templates/Exported_h.template",
"$inspector_protocol_dir/templates/Imported_h.template",
"$inspector_protocol_dir/templates/TypeBuilder_cpp.template",
"$inspector_protocol_dir/templates/TypeBuilder_h.template",
]
if (defined(invoker.inputs)) {
inputs += invoker.inputs
}
args = [
"--jinja_dir",
rebase_path("//third_party/", root_build_dir), # jinja is in chromium's third_party
"--output_base",
rebase_path(invoker.out_dir, root_build_dir),
"--config",
rebase_path(invoker.config_file, root_build_dir),
]
outputs = get_path_info(rebase_path(invoker.outputs, ".", invoker.out_dir),
"abspath")
forward_variables_from(invoker,
[
"visibility",
"deps",
"public_deps",
])
}
}
# Copyright 2016 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.
{
'variables': {
'inspector_protocol_files': [
'lib/Allocator_h.template',
'lib/Array_h.template',
'lib/BackendCallback_h.template',
'lib/Collections_h.template',
'lib/DispatcherBase_cpp.template',
'lib/DispatcherBase_h.template',
'lib/ErrorSupport_cpp.template',
'lib/ErrorSupport_h.template',
'lib/Forward_h.template',
'lib/FrontendChannel_h.template',
'lib/Maybe_h.template',
'lib/Object_cpp.template',
'lib/Object_h.template',
'lib/Parser_cpp.template',
'lib/Parser_h.template',
'lib/Protocol_cpp.template',
'lib/ValueConversions_h.template',
'lib/Values_cpp.template',
'lib/Values_h.template',
'templates/Exported_h.template',
'templates/Imported_h.template',
'templates/TypeBuilder_cpp.template',
'templates/TypeBuilder_h.template',
'CodeGenerator.py',
]
}
}
// Copyright 2016 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 {{"_".join(config.protocol.namespace)}}_Allocator_h
#define {{"_".join(config.protocol.namespace)}}_Allocator_h
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
enum NotNullTagEnum { NotNullLiteral };
#define PROTOCOL_DISALLOW_NEW() \
private: \
void* operator new(size_t) = delete; \
void* operator new(size_t, NotNullTagEnum, void*) = delete; \
void* operator new(size_t, void*) = delete; \
public:
#define PROTOCOL_DISALLOW_COPY(ClassName) \
private: \
ClassName(const ClassName&) = delete; \
ClassName& operator=(const ClassName&) = delete
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}
#endif // !defined({{"_".join(config.protocol.namespace)}}_Allocator_h)
// Copyright 2016 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 {{"_".join(config.protocol.namespace)}}_Array_h
#define {{"_".join(config.protocol.namespace)}}_Array_h
//#include "ErrorSupport.h"
//#include "Forward.h"
//#include "ValueConversions.h"
//#include "Values.h"
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
template<typename T>
class Array {
public:
static std::unique_ptr<Array<T>> create()
{
return wrapUnique(new Array<T>());
}
static std::unique_ptr<Array<T>> parse(protocol::Value* value, ErrorSupport* errors)
{
protocol::ListValue* array = ListValue::cast(value);
if (!array) {
errors->addError("array expected");
return nullptr;
}
std::unique_ptr<Array<T>> result(new Array<T>());
errors->push();
for (size_t i = 0; i < array->size(); ++i) {
errors->setName(StringUtil::fromInteger(i));
std::unique_ptr<T> item = ValueConversions<T>::parse(array->at(i), errors);
result->m_vector.push_back(std::move(item));
}
errors->pop();
if (errors->hasErrors())
return nullptr;
return result;
}
void addItem(std::unique_ptr<T> value)
{
m_vector.push_back(std::move(value));
}
size_t length()
{
return m_vector.size();
}
T* get(size_t index)
{
return m_vector[index].get();
}
std::unique_ptr<protocol::ListValue> serialize()
{
std::unique_ptr<protocol::ListValue> result = ListValue::create();
for (auto& item : m_vector)
result->pushValue(ValueConversions<T>::serialize(item));
return result;
}
private:
std::vector<std::unique_ptr<T>> m_vector;
};
template<typename T>
class ArrayBase {
public:
static std::unique_ptr<Array<T>> create()
{
return wrapUnique(new Array<T>());
}
static std::unique_ptr<Array<T>> parse(protocol::Value* value, ErrorSupport* errors)
{
protocol::ListValue* array = ListValue::cast(value);
if (!array) {
errors->addError("array expected");
return nullptr;
}
errors->push();
std::unique_ptr<Array<T>> result(new Array<T>());
for (size_t i = 0; i < array->size(); ++i) {
errors->setName(StringUtil::fromInteger(i));
T item = ValueConversions<T>::parse(array->at(i), errors);
result->m_vector.push_back(item);
}
errors->pop();
if (errors->hasErrors())
return nullptr;
return result;
}
void addItem(const T& value)
{
m_vector.push_back(value);
}
size_t length()
{
return m_vector.size();
}
T get(size_t index)
{
return m_vector[index];
}
std::unique_ptr<protocol::ListValue> serialize()
{
std::unique_ptr<protocol::ListValue> result = ListValue::create();
for (auto& item : m_vector)
result->pushValue(ValueConversions<T>::serialize(item));
return result;
}
private:
std::vector<T> m_vector;
};
template<> class Array<String> : public ArrayBase<String> {};
template<> class Array<int> : public ArrayBase<int> {};
template<> class Array<double> : public ArrayBase<double> {};
template<> class Array<bool> : public ArrayBase<bool> {};
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}
#endif // !defined({{"_".join(config.protocol.namespace)}}_Array_h)
// Copyright (c) 2016 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 {{"_".join(config.protocol.namespace)}}_BackendCallback_h
#define {{"_".join(config.protocol.namespace)}}_BackendCallback_h
//#include "Forward.h"
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
class {{config.lib.export_macro}} BackendCallback {
public:
virtual ~BackendCallback() { }
virtual void sendFailure(const ErrorString&) = 0;
};
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}
#endif // !defined({{"_".join(config.protocol.namespace)}}_BackendCallback_h)
// Copyright 2016 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 {{"_".join(config.protocol.namespace)}}_Collections_h
#define {{"_".join(config.protocol.namespace)}}_Collections_h
#include "{{config.protocol.package}}/Forward.h"
#include <cstddef>
#if defined(__APPLE__) && !defined(_LIBCPP_VERSION)
#include <map>
#include <set>
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
template <class Key, class T> using HashMap = std::map<Key, T>;
template <class Key> using HashSet = std::set<Key>;
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}
#else
#include <unordered_map>
#include <unordered_set>
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
template <class Key, class T> using HashMap = std::unordered_map<Key, T>;
template <class Key> using HashSet = std::unordered_set<Key>;
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}
#endif // defined(__APPLE__) && !defined(_LIBCPP_VERSION)
#endif // !defined({{"_".join(config.protocol.namespace)}}_Collections_h)
// Copyright 2016 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 "DispatcherBase.h"
//#include "Parser.h"
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
// static
const char DispatcherBase::kInvalidRequest[] = "Invalid request";
DispatcherBase::WeakPtr::WeakPtr(DispatcherBase* dispatcher) : m_dispatcher(dispatcher) { }
DispatcherBase::WeakPtr::~WeakPtr()
{
if (m_dispatcher)
m_dispatcher->m_weakPtrs.erase(this);
}
DispatcherBase::Callback::Callback(std::unique_ptr<DispatcherBase::WeakPtr> backendImpl, int callId)
: m_backendImpl(std::move(backendImpl))
, m_callId(callId) { }
DispatcherBase::Callback::~Callback() = default;
void DispatcherBase::Callback::dispose()
{
m_backendImpl = nullptr;
}
void DispatcherBase::Callback::sendIfActive(std::unique_ptr<protocol::DictionaryValue> partialMessage, const ErrorString& invocationError)
{
if (!m_backendImpl || !m_backendImpl->get())
return;
m_backendImpl->get()->sendResponse(m_callId, invocationError, nullptr, std::move(partialMessage));
m_backendImpl = nullptr;
}
DispatcherBase::DispatcherBase(FrontendChannel* frontendChannel)
: m_frontendChannel(frontendChannel) { }
DispatcherBase::~DispatcherBase()
{
clearFrontend();
}
// static
bool DispatcherBase::getCommandName(const String& message, String* result)
{
std::unique_ptr<protocol::Value> value = parseJSON(message);
if (!value)
return false;
protocol::DictionaryValue* object = DictionaryValue::cast(value.get());
if (!object)
return false;
if (!object->getString("method", result))
return false;
return true;
}
void DispatcherBase::sendResponse(int callId, const ErrorString& invocationError, ErrorSupport* errors, std::unique_ptr<protocol::DictionaryValue> result)
{
if (invocationError.length() || (errors && errors->hasErrors())) {
reportProtocolError(callId, ServerError, invocationError, errors);
return;
}
std::unique_ptr<protocol::DictionaryValue> responseMessage = DictionaryValue::create();
responseMessage->setInteger("id", callId);
responseMessage->setObject("result", std::move(result));
if (m_frontendChannel)
m_frontendChannel->sendProtocolResponse(callId, responseMessage->toJSONString());
}
void DispatcherBase::sendResponse(int callId, const ErrorString& invocationError, std::unique_ptr<protocol::DictionaryValue> result)
{
sendResponse(callId, invocationError, nullptr, std::move(result));
}
void DispatcherBase::sendResponse(int callId, const ErrorString& invocationError)
{
sendResponse(callId, invocationError, nullptr, DictionaryValue::create());
}
static void reportProtocolErrorTo(FrontendChannel* frontendChannel, int callId, DispatcherBase::CommonErrorCode code, const String& errorMessage, ErrorSupport* errors)
{
if (!frontendChannel)
return;
std::unique_ptr<protocol::DictionaryValue> error = DictionaryValue::create();
error->setInteger("code", code);
error->setString("message", errorMessage);
DCHECK(error);
if (errors && errors->hasErrors())
error->setString("data", errors->errors());
std::unique_ptr<protocol::DictionaryValue> message = DictionaryValue::create();
message->setObject("error", std::move(error));
message->setInteger("id", callId);
frontendChannel->sendProtocolResponse(callId, message->toJSONString());
}
void DispatcherBase::reportProtocolError(int callId, CommonErrorCode code, const String& errorMessage, ErrorSupport* errors)
{
reportProtocolErrorTo(m_frontendChannel, callId, code, errorMessage, errors);
}
void DispatcherBase::clearFrontend()
{
m_frontendChannel = nullptr;
for (auto& weak : m_weakPtrs)
weak->dispose();
m_weakPtrs.clear();
}
std::unique_ptr<DispatcherBase::WeakPtr> DispatcherBase::weakPtr()
{
std::unique_ptr<DispatcherBase::WeakPtr> weak(new DispatcherBase::WeakPtr(this));
m_weakPtrs.insert(weak.get());
return weak;
}
UberDispatcher::UberDispatcher(FrontendChannel* frontendChannel)
: m_frontendChannel(frontendChannel) { }
void UberDispatcher::registerBackend(const String& name, std::unique_ptr<protocol::DispatcherBase> dispatcher)
{
m_dispatchers[name] = std::move(dispatcher);
}
void UberDispatcher::dispatch(std::unique_ptr<Value> parsedMessage)
{
if (!parsedMessage)
return;
std::unique_ptr<protocol::DictionaryValue> messageObject = DictionaryValue::cast(std::move(parsedMessage));
if (!messageObject)
return;
int callId = 0;
protocol::Value* callIdValue = messageObject->get("id");
bool success = callIdValue && callIdValue->asInteger(&callId);
if (!success)
return;
protocol::Value* methodValue = messageObject->get("method");
String method;
success = methodValue && methodValue->asString(&method);
if (!success)
return;
size_t dotIndex = method.find(".");
if (dotIndex == StringUtil::kNotFound) {
reportProtocolErrorTo(m_frontendChannel, callId, DispatcherBase::MethodNotFound, "'" + method + "' wasn't found", nullptr);
return;
}
String domain = StringUtil::substring(method, 0, dotIndex);
auto it = m_dispatchers.find(domain);
if (it == m_dispatchers.end()) {
reportProtocolErrorTo(m_frontendChannel, callId, DispatcherBase::MethodNotFound, "'" + method + "' wasn't found", nullptr);
return;
}
it->second->dispatch(callId, method, std::move(messageObject));
}
UberDispatcher::~UberDispatcher() = default;
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}
// Copyright 2016 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 {{"_".join(config.protocol.namespace)}}_DispatcherBase_h
#define {{"_".join(config.protocol.namespace)}}_DispatcherBase_h
//#include "BackendCallback.h"
//#include "Collections.h"
//#include "ErrorSupport.h"
//#include "Forward.h"
//#include "Values.h"
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
class WeakPtr;
class {{config.lib.export_macro}} DispatcherBase {
PROTOCOL_DISALLOW_COPY(DispatcherBase);
public:
static const char kInvalidRequest[];
class {{config.lib.export_macro}} WeakPtr {
public:
explicit WeakPtr(DispatcherBase*);
~WeakPtr();
DispatcherBase* get() { return m_dispatcher; }
void dispose() { m_dispatcher = nullptr; }
private:
DispatcherBase* m_dispatcher;
};
class {{config.lib.export_macro}} Callback : public protocol::BackendCallback {
public:
Callback(std::unique_ptr<WeakPtr> backendImpl, int callId);
virtual ~Callback();
void dispose();
protected:
void sendIfActive(std::unique_ptr<protocol::DictionaryValue> partialMessage, const ErrorString& invocationError);
private:
std::unique_ptr<WeakPtr> m_backendImpl;
int m_callId;
};
explicit DispatcherBase(FrontendChannel*);
virtual ~DispatcherBase();
enum CommonErrorCode {
ParseError = -32700,
InvalidRequest = -32600,
MethodNotFound = -32601,
InvalidParams = -32602,
InternalError = -32603,
ServerError = -32000,
};
static bool getCommandName(const String& message, String* result);
virtual void dispatch(int callId, const String& method, std::unique_ptr<protocol::DictionaryValue> messageObject) = 0;
void sendResponse(int callId, const ErrorString&, ErrorSupport*, std::unique_ptr<protocol::DictionaryValue> result);
void sendResponse(int callId, const ErrorString&, std::unique_ptr<protocol::DictionaryValue> result);
void sendResponse(int callId, const ErrorString&);
void reportProtocolError(int callId, CommonErrorCode, const String& errorMessage, ErrorSupport* errors);
void clearFrontend();
std::unique_ptr<WeakPtr> weakPtr();
private:
FrontendChannel* m_frontendChannel;
protocol::HashSet<WeakPtr*> m_weakPtrs;
};
class {{config.lib.export_macro}} UberDispatcher {
PROTOCOL_DISALLOW_COPY(UberDispatcher);
public:
explicit UberDispatcher(FrontendChannel*);
void registerBackend(const String& name, std::unique_ptr<protocol::DispatcherBase>);
void dispatch(std::unique_ptr<Value> message);
FrontendChannel* channel() { return m_frontendChannel; }
virtual ~UberDispatcher();
private:
FrontendChannel* m_frontendChannel;
protocol::HashMap<String, std::unique_ptr<protocol::DispatcherBase>> m_dispatchers;
};
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}
#endif // !defined({{"_".join(config.protocol.namespace)}}_DispatcherBase_h)
// Copyright 2016 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 "ErrorSupport.h"
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
ErrorSupport::ErrorSupport() : m_errorString(nullptr) { }
ErrorSupport::ErrorSupport(String* errorString) : m_errorString(errorString) { }
ErrorSupport::~ErrorSupport()
{
if (m_errorString && hasErrors()) {
StringBuilder builder;
builder.append("Internal error(s): ");
builder.append(errors());
*m_errorString = builder.toString();
}
}
void ErrorSupport::setName(const String& name)
{
DCHECK(m_path.size());
m_path[m_path.size() - 1] = name;
}
void ErrorSupport::push()
{
m_path.push_back(String());
}
void ErrorSupport::pop()
{
m_path.pop_back();
}
void ErrorSupport::addError(const String& error)
{
StringBuilder builder;
for (size_t i = 0; i < m_path.size(); ++i) {
if (i)
builder.append('.');
builder.append(m_path[i]);
}
builder.append(": ");
builder.append(error);
m_errors.push_back(builder.toString());
}
bool ErrorSupport::hasErrors()
{
return m_errors.size();
}
String ErrorSupport::errors()
{
StringBuilder builder;
for (size_t i = 0; i < m_errors.size(); ++i) {
if (i)
builder.append("; ");
builder.append(m_errors[i]);
}
return builder.toString();
}
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}
// Copyright 2016 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 {{"_".join(config.protocol.namespace)}}_ErrorSupport_h
#define {{"_".join(config.protocol.namespace)}}_ErrorSupport_h
//#include "Forward.h"
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
class {{config.lib.export_macro}} ErrorSupport {
public:
ErrorSupport();
ErrorSupport(String* errorString);
~ErrorSupport();
void push();
void setName(const String&);
void pop();
void addError(const String&);
bool hasErrors();
String errors();
private:
std::vector<String> m_path;
std::vector<String> m_errors;
String* m_errorString;
};
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}
#endif // !defined({{"_".join(config.protocol.namespace)}}_ErrorSupport_h)
// Copyright 2016 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 {{"_".join(config.protocol.namespace)}}_Forward_h
#define {{"_".join(config.protocol.namespace)}}_Forward_h
{% if config.lib.export_header %}
#include {{format_include(config.lib.export_header)}}
{% endif %}
#include {{format_include(config.lib.platform_header)}}
#include {{format_include(config.lib.string_header)}}
#include <vector>
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
template<typename T> class Array;
class DictionaryValue;
using ErrorString = String;
class ErrorSupport;
class FundamentalValue;
class ListValue;
template<typename T> class Maybe;
class Object;
class SerializedValue;
class StringValue;
class UberDispatcher;
class Value;
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}
#endif // !defined({{"_".join(config.protocol.namespace)}}_Forward_h)
// Copyright 2016 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 {{"_".join(config.protocol.namespace)}}_FrontendChannel_h
#define {{"_".join(config.protocol.namespace)}}_FrontendChannel_h
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
class {{config.lib.export_macro}} FrontendChannel {
public:
virtual ~FrontendChannel() { }
virtual void sendProtocolResponse(int callId, const String& message) = 0;
virtual void sendProtocolNotification(const String& message) = 0;
virtual void flushProtocolNotifications() = 0;
};
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}
#endif // !defined({{"_".join(config.protocol.namespace)}}_FrontendChannel_h)
// Copyright 2016 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 {{"_".join(config.protocol.namespace)}}_Maybe_h
#define {{"_".join(config.protocol.namespace)}}_Maybe_h
//#include "Forward.h"
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
template<typename T>
class Maybe {
public:
Maybe() : m_value() { }
Maybe(std::unique_ptr<T> value) : m_value(std::move(value)) { }
void operator=(std::unique_ptr<T> value) { m_value = std::move(value); }
T* fromJust() const { DCHECK(m_value); return m_value.get(); }
T* fromMaybe(T* defaultValue) const { return m_value ? m_value.get() : defaultValue; }
bool isJust() const { return !!m_value; }
std::unique_ptr<T> takeJust() { DCHECK(m_value); return m_value.release(); }
private:
std::unique_ptr<T> m_value;
};
template<typename T>
class MaybeBase {
public:
MaybeBase() : m_isJust(false) { }
MaybeBase(T value) : m_isJust(true), m_value(value) { }
void operator=(T value) { m_value = value; m_isJust = true; }
T fromJust() const { DCHECK(m_isJust); return m_value; }
T fromMaybe(const T& defaultValue) const { return m_isJust ? m_value : defaultValue; }
bool isJust() const { return m_isJust; }
T takeJust() { DCHECK(m_isJust); return m_value; }
protected:
bool m_isJust;
T m_value;
};
template<>
class Maybe<bool> : public MaybeBase<bool> {
public:
Maybe() { }
Maybe(bool value) : MaybeBase(value) { }
using MaybeBase::operator=;
};
template<>
class Maybe<int> : public MaybeBase<int> {
public:
Maybe() { }
Maybe(int value) : MaybeBase(value) { }
using MaybeBase::operator=;
};
template<>
class Maybe<double> : public MaybeBase<double> {
public:
Maybe() { }
Maybe(double value) : MaybeBase(value) { }
using MaybeBase::operator=;
};
template<>
class Maybe<String> : public MaybeBase<String> {
public:
Maybe() { }
Maybe(const String& value) : MaybeBase(value) { }
using MaybeBase::operator=;
};
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}
#endif // !defined({{"_".join(config.protocol.namespace)}}_Maybe_h)
// Copyright 2016 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 "Object.h"
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
std::unique_ptr<Object> Object::parse(protocol::Value* value, ErrorSupport* errors)
{
protocol::DictionaryValue* object = DictionaryValue::cast(value);
if (!object) {
errors->addError("object expected");
return nullptr;
}
return wrapUnique(new Object(wrapUnique(static_cast<DictionaryValue*>(object->clone().release()))));
}
std::unique_ptr<protocol::DictionaryValue> Object::serialize() const
{
return DictionaryValue::cast(m_object->clone());
}
std::unique_ptr<Object> Object::clone() const
{
return wrapUnique(new Object(DictionaryValue::cast(m_object->clone())));
}
Object::Object(std::unique_ptr<protocol::DictionaryValue> object) : m_object(std::move(object)) { }
Object::~Object() { }
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}
// Copyright 2016 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 {{"_".join(config.protocol.namespace)}}_Object_h
#define {{"_".join(config.protocol.namespace)}}_Object_h
//#include "ErrorSupport.h"
//#include "Forward.h"
//#include "Values.h"
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
class {{config.lib.export_macro}} Object {
public:
static std::unique_ptr<Object> parse(protocol::Value*, ErrorSupport*);
~Object();
std::unique_ptr<protocol::DictionaryValue> serialize() const;
std::unique_ptr<Object> clone() const;
private:
explicit Object(std::unique_ptr<protocol::DictionaryValue>);
std::unique_ptr<protocol::DictionaryValue> m_object;
};
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}
#endif // !defined({{"_".join(config.protocol.namespace)}}_Object_h)
This diff is collapsed.
// Copyright 2016 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 {{"_".join(config.protocol.namespace)}}_Parser_h
#define {{"_".join(config.protocol.namespace)}}_Parser_h
//#include "Forward.h"
//#include "Values.h"
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
{{config.lib.export_macro}} std::unique_ptr<Value> parseJSON(const uint8_t*, unsigned);
{{config.lib.export_macro}} std::unique_ptr<Value> parseJSON(const uint16_t*, unsigned);
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}
#endif // !defined({{"_".join(config.protocol.namespace)}}_Parser_h)
// This file is generated.
// Copyright 2016 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 "{{config.protocol.package}}/Protocol.h"
#include <algorithm>
#include <cmath>
#include <cstring>
// Copyright 2016 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 {{"_".join(config.protocol.namespace)}}_ValueConversions_h
#define {{"_".join(config.protocol.namespace)}}_ValueConversions_h
//#include "ErrorSupport.h"
//#include "Forward.h"
//#include "Values.h"
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
template<typename T>
struct ValueConversions {
static std::unique_ptr<T> parse(protocol::Value* value, ErrorSupport* errors)
{
return T::parse(value, errors);
}
static std::unique_ptr<protocol::Value> serialize(T* value)
{
return value->serialize();
}
static std::unique_ptr<protocol::Value> serialize(const std::unique_ptr<T>& value)
{
return value->serialize();
}
};
template<>
struct ValueConversions<bool> {
static bool parse(protocol::Value* value, ErrorSupport* errors)
{
bool result = false;
bool success = value ? value->asBoolean(&result) : false;
if (!success)
errors->addError("boolean value expected");
return result;
}
static std::unique_ptr<protocol::Value> serialize(bool value)
{
return FundamentalValue::create(value);
}
};
template<>
struct ValueConversions<int> {
static int parse(protocol::Value* value, ErrorSupport* errors)
{
int result = 0;
bool success = value ? value->asInteger(&result) : false;
if (!success)
errors->addError("integer value expected");
return result;
}
static std::unique_ptr<protocol::Value> serialize(int value)
{
return FundamentalValue::create(value);
}
};
template<>
struct ValueConversions<double> {
static double parse(protocol::Value* value, ErrorSupport* errors)
{
double result = 0;
bool success = value ? value->asDouble(&result) : false;
if (!success)
errors->addError("double value expected");
return result;
}
static std::unique_ptr<protocol::Value> serialize(double value)
{
return FundamentalValue::create(value);
}
};
template<>
struct ValueConversions<String> {
static String parse(protocol::Value* value, ErrorSupport* errors)
{
String result;
bool success = value ? value->asString(&result) : false;
if (!success)
errors->addError("string value expected");
return result;
}
static std::unique_ptr<protocol::Value> serialize(const String& value)
{
return StringValue::create(value);
}
};
template<>
struct ValueConversions<Value> {
static std::unique_ptr<Value> parse(protocol::Value* value, ErrorSupport* errors)
{
bool success = !!value;
if (!success) {
errors->addError("value expected");
return nullptr;
}
return value->clone();
}
static std::unique_ptr<protocol::Value> serialize(Value* value)
{
return value->clone();
}
static std::unique_ptr<protocol::Value> serialize(const std::unique_ptr<Value>& value)
{
return value->clone();
}
};
template<>
struct ValueConversions<DictionaryValue> {
static std::unique_ptr<DictionaryValue> parse(protocol::Value* value, ErrorSupport* errors)
{
bool success = value && value->type() == protocol::Value::TypeObject;
if (!success)
errors->addError("object expected");
return DictionaryValue::cast(value->clone());
}
static std::unique_ptr<protocol::Value> serialize(DictionaryValue* value)
{
return value->clone();
}
static std::unique_ptr<protocol::Value> serialize(const std::unique_ptr<DictionaryValue>& value)
{
return value->clone();
}
};
template<>
struct ValueConversions<ListValue> {
static std::unique_ptr<ListValue> parse(protocol::Value* value, ErrorSupport* errors)
{
bool success = value && value->type() == protocol::Value::TypeArray;
if (!success)
errors->addError("list expected");
return ListValue::cast(value->clone());
}
static std::unique_ptr<protocol::Value> serialize(ListValue* value)
{
return value->clone();
}
static std::unique_ptr<protocol::Value> serialize(const std::unique_ptr<ListValue>& value)
{
return value->clone();
}
};
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}
#endif // !defined({{"_".join(config.protocol.namespace)}}_ValueConversions_h)
// Copyright 2016 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 "Values.h"
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
namespace {
const char* const nullValueString = "null";
const char* const trueValueString = "true";
const char* const falseValueString = "false";
inline bool escapeChar(uint16_t c, StringBuilder* dst)
{
switch (c) {
case '\b': dst->append("\\b"); break;
case '\f': dst->append("\\f"); break;
case '\n': dst->append("\\n"); break;
case '\r': dst->append("\\r"); break;
case '\t': dst->append("\\t"); break;
case '\\': dst->append("\\\\"); break;
case '"': dst->append("\\\""); break;
default:
return false;
}
return true;
}
const char hexDigits[17] = "0123456789ABCDEF";
void appendUnsignedAsHex(uint16_t number, StringBuilder* dst)
{
dst->append("\\u");
for (size_t i = 0; i < 4; ++i) {
uint16_t c = hexDigits[(number & 0xF000) >> 12];
dst->append(c);
number <<= 4;
}
}
void escapeStringForJSON(const String& str, StringBuilder* dst)
{
for (unsigned i = 0; i < str.length(); ++i) {
uint16_t c = str[i];
if (!escapeChar(c, dst)) {
if (c < 32 || c > 126 || c == '<' || c == '>') {
// 1. Escaping <, > to prevent script execution.
// 2. Technically, we could also pass through c > 126 as UTF8, but this
// is also optional. It would also be a pain to implement here.
appendUnsignedAsHex(c, dst);
} else {
dst->append(c);
}
}
}
}
void doubleQuoteStringForJSON(const String& str, StringBuilder* dst)
{
dst->append('"');
escapeStringForJSON(str, dst);
dst->append('"');
}
} // anonymous namespace
bool Value::asBoolean(bool*) const
{
return false;
}
bool Value::asDouble(double*) const
{
return false;
}
bool Value::asInteger(int*) const
{
return false;
}
bool Value::asString(String*) const
{
return false;
}
bool Value::asSerialized(String*) const
{
return false;
}
String Value::toJSONString() const
{
StringBuilder result;
StringUtil::builderReserve(result, 512);
writeJSON(&result);
return result.toString();
}
void Value::writeJSON(StringBuilder* output) const
{
DCHECK(m_type == TypeNull);
output->append(nullValueString, 4);
}
std::unique_ptr<Value> Value::clone() const
{
return Value::null();
}
bool FundamentalValue::asBoolean(bool* output) const
{
if (type() != TypeBoolean)
return false;
*output = m_boolValue;
return true;
}
bool FundamentalValue::asDouble(double* output) const
{
if (type() == TypeDouble) {
*output = m_doubleValue;
return true;
}
if (type() == TypeInteger) {
*output = m_integerValue;
return true;
}
return false;
}
bool FundamentalValue::asInteger(int* output) const
{
if (type() != TypeInteger)
return false;
*output = m_integerValue;
return true;
}
void FundamentalValue::writeJSON(StringBuilder* output) const
{
DCHECK(type() == TypeBoolean || type() == TypeInteger || type() == TypeDouble);
if (type() == TypeBoolean) {
if (m_boolValue)
output->append(trueValueString, 4);
else
output->append(falseValueString, 5);
} else if (type() == TypeDouble) {
if (!std::isfinite(m_doubleValue)) {
output->append(nullValueString, 4);
return;
}
output->append(StringUtil::fromDouble(m_doubleValue));
} else if (type() == TypeInteger) {
output->append(StringUtil::fromInteger(m_integerValue));
}
}
std::unique_ptr<Value> FundamentalValue::clone() const
{
switch (type()) {
case TypeDouble: return FundamentalValue::create(m_doubleValue);
case TypeInteger: return FundamentalValue::create(m_integerValue);
case TypeBoolean: return FundamentalValue::create(m_boolValue);
default:
DCHECK(false);
}
return nullptr;
}
bool StringValue::asString(String* output) const
{
*output = m_stringValue;
return true;
}
void StringValue::writeJSON(StringBuilder* output) const
{
DCHECK(type() == TypeString);
doubleQuoteStringForJSON(m_stringValue, output);
}
std::unique_ptr<Value> StringValue::clone() const
{
return StringValue::create(m_stringValue);
}
bool SerializedValue::asSerialized(String* output) const
{
*output = m_serializedValue;
return true;
}
void SerializedValue::writeJSON(StringBuilder* output) const
{
DCHECK(type() == TypeSerialized);
output->append(m_serializedValue);
}
std::unique_ptr<Value> SerializedValue::clone() const
{
return SerializedValue::create(m_serializedValue);
}
DictionaryValue::~DictionaryValue()
{
}
void DictionaryValue::setBoolean(const String& name, bool value)
{
setValue(name, FundamentalValue::create(value));
}
void DictionaryValue::setInteger(const String& name, int value)
{
setValue(name, FundamentalValue::create(value));
}
void DictionaryValue::setDouble(const String& name, double value)
{
setValue(name, FundamentalValue::create(value));
}
void DictionaryValue::setString(const String& name, const String& value)
{
setValue(name, StringValue::create(value));
}
void DictionaryValue::setValue(const String& name, std::unique_ptr<Value> value)
{
set(name, value);
}
void DictionaryValue::setObject(const String& name, std::unique_ptr<DictionaryValue> value)
{
set(name, value);
}
void DictionaryValue::setArray(const String& name, std::unique_ptr<ListValue> value)
{
set(name, value);
}
bool DictionaryValue::getBoolean(const String& name, bool* output) const
{
protocol::Value* value = get(name);
if (!value)
return false;
return value->asBoolean(output);
}
bool DictionaryValue::getInteger(const String& name, int* output) const
{
Value* value = get(name);
if (!value)
return false;
return value->asInteger(output);
}
bool DictionaryValue::getDouble(const String& name, double* output) const
{
Value* value = get(name);
if (!value)
return false;
return value->asDouble(output);
}
bool DictionaryValue::getString(const String& name, String* output) const
{
protocol::Value* value = get(name);
if (!value)
return false;
return value->asString(output);
}
DictionaryValue* DictionaryValue::getObject(const String& name) const
{
return DictionaryValue::cast(get(name));
}
protocol::ListValue* DictionaryValue::getArray(const String& name) const
{
return ListValue::cast(get(name));
}
protocol::Value* DictionaryValue::get(const String& name) const
{
Dictionary::const_iterator it = m_data.find(name);
if (it == m_data.end())
return nullptr;
return it->second.get();
}
DictionaryValue::Entry DictionaryValue::at(size_t index) const
{
const String key = m_order[index];
return std::make_pair(key, m_data.find(key)->second.get());
}
bool DictionaryValue::booleanProperty(const String& name, bool defaultValue) const
{
bool result = defaultValue;
getBoolean(name, &result);
return result;
}
int DictionaryValue::integerProperty(const String& name, int defaultValue) const
{
int result = defaultValue;
getInteger(name, &result);
return result;
}
double DictionaryValue::doubleProperty(const String& name, double defaultValue) const
{
double result = defaultValue;
getDouble(name, &result);
return result;
}
void DictionaryValue::remove(const String& name)
{
m_data.erase(name);
m_order.erase(std::remove(m_order.begin(), m_order.end(), name), m_order.end());
}
void DictionaryValue::writeJSON(StringBuilder* output) const
{
output->append('{');
for (size_t i = 0; i < m_order.size(); ++i) {
Dictionary::const_iterator it = m_data.find(m_order[i]);
CHECK(it != m_data.end());
if (i)
output->append(',');
doubleQuoteStringForJSON(it->first, output);
output->append(':');
it->second->writeJSON(output);
}
output->append('}');
}
std::unique_ptr<Value> DictionaryValue::clone() const
{
std::unique_ptr<DictionaryValue> result = DictionaryValue::create();
for (size_t i = 0; i < m_order.size(); ++i) {
String key = m_order[i];
Dictionary::const_iterator value = m_data.find(key);
DCHECK(value != m_data.cend() && value->second);
result->setValue(key, value->second->clone());
}
return std::move(result);
}
DictionaryValue::DictionaryValue()
: Value(TypeObject)
{
}
ListValue::~ListValue()
{
}
void ListValue::writeJSON(StringBuilder* output) const
{
output->append('[');
bool first = true;
for (const std::unique_ptr<protocol::Value>& value : m_data) {
if (!first)
output->append(',');
value->writeJSON(output);
first = false;
}
output->append(']');
}
std::unique_ptr<Value> ListValue::clone() const
{
std::unique_ptr<ListValue> result = ListValue::create();
for (const std::unique_ptr<protocol::Value>& value : m_data)
result->pushValue(value->clone());
return std::move(result);
}
ListValue::ListValue()
: Value(TypeArray)
{
}
void ListValue::pushValue(std::unique_ptr<protocol::Value> value)
{
DCHECK(value);
m_data.push_back(std::move(value));
}
protocol::Value* ListValue::at(size_t index)
{
DCHECK_LT(index, m_data.size());
return m_data[index].get();
}
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}
// Copyright 2016 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 {{"_".join(config.protocol.namespace)}}_Values_h
#define {{"_".join(config.protocol.namespace)}}_Values_h
//#include "Allocator.h"
//#include "Collections.h"
//#include "Forward.h"
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
class ListValue;
class DictionaryValue;
class Value;
class {{config.lib.export_macro}} Value {
PROTOCOL_DISALLOW_COPY(Value);
public:
virtual ~Value() { }
static std::unique_ptr<Value> null()
{
return wrapUnique(new Value());
}
enum ValueType {
TypeNull = 0,
TypeBoolean,
TypeInteger,
TypeDouble,
TypeString,
TypeObject,
TypeArray,
TypeSerialized
};
ValueType type() const { return m_type; }
bool isNull() const { return m_type == TypeNull; }
virtual bool asBoolean(bool* output) const;
virtual bool asDouble(double* output) const;
virtual bool asInteger(int* output) const;
virtual bool asString(String* output) const;
virtual bool asSerialized(String* output) const;
String toJSONString() const;
virtual void writeJSON(StringBuilder* output) const;
virtual std::unique_ptr<Value> clone() const;
protected:
Value() : m_type(TypeNull) { }
explicit Value(ValueType type) : m_type(type) { }
private:
friend class DictionaryValue;
friend class ListValue;
ValueType m_type;
};
class {{config.lib.export_macro}} FundamentalValue : public Value {
public:
static std::unique_ptr<FundamentalValue> create(bool value)
{
return wrapUnique(new FundamentalValue(value));
}
static std::unique_ptr<FundamentalValue> create(int value)
{
return wrapUnique(new FundamentalValue(value));
}
static std::unique_ptr<FundamentalValue> create(double value)
{
return wrapUnique(new FundamentalValue(value));
}
bool asBoolean(bool* output) const override;
bool asDouble(double* output) const override;
bool asInteger(int* output) const override;
void writeJSON(StringBuilder* output) const override;
std::unique_ptr<Value> clone() const override;
private:
explicit FundamentalValue(bool value) : Value(TypeBoolean), m_boolValue(value) { }
explicit FundamentalValue(int value) : Value(TypeInteger), m_integerValue(value) { }
explicit FundamentalValue(double value) : Value(TypeDouble), m_doubleValue(value) { }
union {
bool m_boolValue;
double m_doubleValue;
int m_integerValue;
};
};
class {{config.lib.export_macro}} StringValue : public Value {
public:
static std::unique_ptr<StringValue> create(const String& value)
{
return wrapUnique(new StringValue(value));
}
static std::unique_ptr<StringValue> create(const char* value)
{
return wrapUnique(new StringValue(value));
}
bool asString(String* output) const override;
void writeJSON(StringBuilder* output) const override;
std::unique_ptr<Value> clone() const override;
private:
explicit StringValue(const String& value) : Value(TypeString), m_stringValue(value) { }
explicit StringValue(const char* value) : Value(TypeString), m_stringValue(value) { }
String m_stringValue;
};
class {{config.lib.export_macro}} SerializedValue : public Value {
public:
static std::unique_ptr<SerializedValue> create(const String& value)
{
return wrapUnique(new SerializedValue(value));
}
bool asSerialized(String* output) const override;
void writeJSON(StringBuilder* output) const override;
std::unique_ptr<Value> clone() const override;
private:
explicit SerializedValue(const String& value) : Value(TypeSerialized), m_serializedValue(value) { }
String m_serializedValue;
};
class {{config.lib.export_macro}} DictionaryValue : public Value {
public:
using Entry = std::pair<String, Value*>;
static std::unique_ptr<DictionaryValue> create()
{
return wrapUnique(new DictionaryValue());
}
static DictionaryValue* cast(Value* value)
{
if (!value || value->type() != TypeObject)
return nullptr;
return static_cast<DictionaryValue*>(value);
}
static std::unique_ptr<DictionaryValue> cast(std::unique_ptr<Value> value)
{
return wrapUnique(DictionaryValue::cast(value.release()));
}
void writeJSON(StringBuilder* output) const override;
std::unique_ptr<Value> clone() const override;
size_t size() const { return m_data.size(); }
void setBoolean(const String& name, bool);
void setInteger(const String& name, int);
void setDouble(const String& name, double);
void setString(const String& name, const String&);
void setValue(const String& name, std::unique_ptr<Value>);
void setObject(const String& name, std::unique_ptr<DictionaryValue>);
void setArray(const String& name, std::unique_ptr<ListValue>);
bool getBoolean(const String& name, bool* output) const;
bool getInteger(const String& name, int* output) const;
bool getDouble(const String& name, double* output) const;
bool getString(const String& name, String* output) const;
DictionaryValue* getObject(const String& name) const;
ListValue* getArray(const String& name) const;
Value* get(const String& name) const;
Entry at(size_t index) const;
bool booleanProperty(const String& name, bool defaultValue) const;
int integerProperty(const String& name, int defaultValue) const;
double doubleProperty(const String& name, double defaultValue) const;
void remove(const String& name);
~DictionaryValue() override;
private:
DictionaryValue();
template<typename T>
void set(const String& key, std::unique_ptr<T>& value)
{
DCHECK(value);
bool isNew = m_data.find(key) == m_data.end();
m_data[key] = std::move(value);
if (isNew)
m_order.push_back(key);
}
using Dictionary = protocol::HashMap<String, std::unique_ptr<Value>>;
Dictionary m_data;
std::vector<String> m_order;
};
class {{config.lib.export_macro}} ListValue : public Value {
public:
static std::unique_ptr<ListValue> create()
{
return wrapUnique(new ListValue());
}
static ListValue* cast(Value* value)
{
if (!value || value->type() != TypeArray)
return nullptr;
return static_cast<ListValue*>(value);
}
static std::unique_ptr<ListValue> cast(std::unique_ptr<Value> value)
{
return wrapUnique(ListValue::cast(value.release()));
}
~ListValue() override;
void writeJSON(StringBuilder* output) const override;
std::unique_ptr<Value> clone() const override;
void pushValue(std::unique_ptr<Value>);
Value* at(size_t index);
size_t size() const { return m_data.size(); }
private:
ListValue();
std::vector<std::unique_ptr<Value>> m_data;
};
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}
#endif // {{"_".join(config.protocol.namespace)}}_Values_h
// This file is generated
// Copyright (c) 2016 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 {{"_".join(config.protocol.namespace)}}_{{domain.domain}}_api_h
#define {{"_".join(config.protocol.namespace)}}_{{domain.domain}}_api_h
{% if config.exported.export_header %}
#include {{format_include(config.exported.export_header)}}
{% endif %}
#include {{format_include(config.exported.string_header)}}
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
namespace {{domain.domain}} {
namespace API {
// ------------- Enums.
{% for type in domain.types %}
{% if ("enum" in type) and type.exported %}
namespace {{type.id}}Enum {
{% for literal in type.enum %}
{{config.exported.export_macro}} extern const char* {{ literal | dash_to_camelcase}};
{% endfor %}
} // {{type.id}}Enum
{% endif %}
{% endfor %}
{% for command in join_arrays(domain, ["commands", "events"]) %}
{% for param in join_arrays(command, ["parameters", "returns"]) %}
{% if ("enum" in param) and (param.exported) %}
namespace {{command.name | to_title_case}} {
namespace {{param.name | to_title_case}}Enum {
{% for literal in param.enum %}
{{config.exported.export_macro}} extern const char* {{ literal | dash_to_camelcase}};
{% endfor %}
} // {{param.name | to_title_case}}Enum
} // {{command.name | to_title_case }}
{% endif %}
{% endfor %}
{% endfor %}
// ------------- Types.
{% for type in domain.types %}
{% if not (type.type == "object") or not ("properties" in type) or not (type.exported) %}{% continue %}{% endif %}
class {{config.exported.export_macro}} {{type.id}} {
public:
virtual {{config.exported.string_out}} toJSONString() const = 0;
virtual ~{{type.id}}() { }
static std::unique_ptr<protocol::{{domain.domain}}::API::{{type.id}}> fromJSONString(const {{config.exported.string_in}}& json);
};
{% endfor %}
} // namespace API
} // namespace {{domain.domain}}
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}
#endif // !defined({{"_".join(config.protocol.namespace)}}_{{domain.domain}}_api_h)
// This file is generated
// Copyright (c) 2016 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 {{"_".join(config.protocol.namespace)}}_{{domain.domain}}_imported_h
#define {{"_".join(config.protocol.namespace)}}_{{domain.domain}}_imported_h
#include "{{config.protocol.package}}/Protocol.h"
#include {{format_include(config.imported.header if config.imported.header else "\"%s/%s.h\"" % (config.imported.package, domain.domain))}}
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
{% for type in domain.types %}
{% if not (type.type == "object") or not ("properties" in type) or not (type.exported) %}{% continue %}{% endif %}
template<>
struct ValueConversions<{{"::".join(config.imported.namespace)}}::{{domain.domain}}::API::{{type.id}}> {
static std::unique_ptr<{{"::".join(config.imported.namespace)}}::{{domain.domain}}::API::{{type.id}}> parse(protocol::Value* value, ErrorSupport* errors)
{
if (!value) {
errors->addError("value expected");
return nullptr;
}
String json = value->toJSONString();
auto result = {{"::".join(config.imported.namespace)}}::{{domain.domain}}::API::{{type.id}}::fromJSONString({{config.imported.to_imported_string % "json"}});
if (!result)
errors->addError("cannot parse");
return result;
}
static std::unique_ptr<protocol::Value> serialize(const {{"::".join(config.imported.namespace)}}::{{domain.domain}}::API::{{type.id}}* value)
{
auto json = value->toJSONString();
return SerializedValue::create({{config.imported.from_imported_string % "std::move(json)"}});
}
static std::unique_ptr<protocol::Value> serialize(const std::unique_ptr<{{"::".join(config.imported.namespace)}}::{{domain.domain}}::API::{{type.id}}>& value)
{
return serialize(value.get());
}
};
{% endfor %}
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}
#endif // !defined({{"_".join(config.protocol.namespace)}}_{{domain.domain}}_imported_h)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment