From 7dc7ee1c4195351eee08d0fe5cef172c3c201a0f Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 18 Sep 2015 11:06:38 +0800 Subject: [PATCH] Move the converters for blink structures to another file It makes the api::WebContents smaller. --- atom/browser/api/atom_api_web_contents.cc | 73 +------------------ atom/browser/api/atom_api_web_contents.h | 6 +- .../native_mate_converters/blink_converter.cc | 65 +++++++++++++++++ .../native_mate_converters/blink_converter.h | 45 ++++++++++++ filenames.gypi | 2 + 5 files changed, 120 insertions(+), 71 deletions(-) create mode 100644 atom/common/native_mate_converters/blink_converter.cc create mode 100644 atom/common/native_mate_converters/blink_converter.h diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 2231a591f732..d4cb6cd227d5 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -15,6 +15,7 @@ #include "atom/browser/web_view_guest_delegate.h" #include "atom/common/api/api_messages.h" #include "atom/common/api/event_emitter_caller.h" +#include "atom/common/native_mate_converters/blink_converter.h" #include "atom/common/native_mate_converters/callback.h" #include "atom/common/native_mate_converters/file_path_converter.h" #include "atom/common/native_mate_converters/gfx_converter.h" @@ -45,7 +46,6 @@ #include "net/http/http_response_headers.h" #include "net/url_request/static_http_user_agent_settings.h" #include "net/url_request/url_request_context.h" -#include "third_party/WebKit/public/web/WebDeviceEmulationParams.h" #include "atom/common/node_includes.h" @@ -642,78 +642,11 @@ bool WebContents::IsDevToolsOpened() { return managed_web_contents()->IsDevToolsViewShowing(); } -void WebContents::EnableDeviceEmulation(const base::DictionaryValue& dict) { +void WebContents::EnableDeviceEmulation( + const blink::WebDeviceEmulationParams& params) { if (type_ == REMOTE) return; - blink::WebDeviceEmulationParams params; - - if (dict.HasKey("screenPosition")) { - std::string screen_position; - if (!dict.GetString("screenPosition", &screen_position)) - return; - - screen_position = base::StringToLowerASCII(screen_position); - - if (screen_position == "mobile") { - params.screenPosition = blink::WebDeviceEmulationParams::Mobile; - } else if (screen_position == "desktop") { - params.screenPosition = blink::WebDeviceEmulationParams::Desktop; - } else { - return; - } - } - - if (dict.HasKey("screenSize")) { - if (!dict.GetInteger("screenSize.width", ¶ms.screenSize.width)) - return; - if (!dict.GetInteger("screenSize.height", ¶ms.screenSize.height)) - return; - } - - if (dict.HasKey("viewPosition")) { - if (!dict.GetInteger("viewPosition.x", ¶ms.viewPosition.x)) - return; - if (!dict.GetInteger("viewPosition.y", ¶ms.viewPosition.y)) - return; - } - - if (dict.HasKey("deviceScaleFactor")) { - double device_scale_factor; - if (!dict.GetDouble("deviceScaleFactor", &device_scale_factor)) - return; - params.deviceScaleFactor = static_cast(device_scale_factor); - } - - if (dict.HasKey("viewSize")) { - if (!dict.GetInteger("viewSize.width", ¶ms.viewSize.width)) - return; - if (!dict.GetInteger("viewSize.height", ¶ms.viewSize.height)) - return; - } - - if (dict.HasKey("fitToView")) { - if (!dict.GetBoolean("fitToView", ¶ms.fitToView)) - return; - } - - if (dict.HasKey("offset")) { - double x, y; - if (!dict.GetDouble("offset.x", &x)) - return; - if (!dict.GetDouble("offset.y", &y)) - return; - params.offset.x = static_cast(x); - params.offset.y = static_cast(y); - } - - if (dict.HasKey("scale")) { - double scale; - if (!dict.GetDouble("scale", &scale)) - return; - params.scale = static_cast(scale); - } - Send(new ViewMsg_EnableDeviceEmulation(routing_id(), params)); } diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index c765e5cbf4e4..0ce47237e494 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -15,6 +15,10 @@ #include "native_mate/handle.h" #include "ui/gfx/image/image.h" +namespace blink { +struct WebDeviceEmulationParams; +} + namespace brightray { class InspectableWebContents; } @@ -74,7 +78,7 @@ class WebContents : public mate::TrackableObject, void CloseDevTools(); bool IsDevToolsOpened(); void ToggleDevTools(); - void EnableDeviceEmulation(const base::DictionaryValue&); + void EnableDeviceEmulation(const blink::WebDeviceEmulationParams& params); void DisableDeviceEmulation(); void InspectElement(int x, int y); void InspectServiceWorker(); diff --git a/atom/common/native_mate_converters/blink_converter.cc b/atom/common/native_mate_converters/blink_converter.cc new file mode 100644 index 000000000000..9225f548380e --- /dev/null +++ b/atom/common/native_mate_converters/blink_converter.cc @@ -0,0 +1,65 @@ +// Copyright (c) 2015 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/blink_converter.h" + +#include "base/strings/string_util.h" +#include "native_mate/dictionary.h" +#include "third_party/WebKit/public/web/WebDeviceEmulationParams.h" + +namespace mate { + +bool Converter::FromV8( + v8::Isolate* isolate, v8::Local val, blink::WebFloatPoint* out) { + mate::Dictionary dict; + if (!ConvertFromV8(isolate, val, &dict)) + return false; + return dict.Get("x", &out->x) && dict.Get("y", &out->y); +} + +bool Converter::FromV8( + v8::Isolate* isolate, v8::Local val, blink::WebPoint* out) { + mate::Dictionary dict; + if (!ConvertFromV8(isolate, val, &dict)) + return false; + return dict.Get("x", &out->x) && dict.Get("y", &out->y); +} + +bool Converter::FromV8( + v8::Isolate* isolate, v8::Local val, blink::WebSize* out) { + mate::Dictionary dict; + if (!ConvertFromV8(isolate, val, &dict)) + return false; + return dict.Get("width", &out->width) && dict.Get("height", &out->height); +} + +bool Converter::FromV8( + v8::Isolate* isolate, v8::Local val, + blink::WebDeviceEmulationParams* out) { + mate::Dictionary dict; + if (!ConvertFromV8(isolate, val, &dict)) + return false; + + std::string screen_position; + if (dict.Get("screenPosition", &screen_position)) { + screen_position = base::StringToLowerASCII(screen_position); + if (screen_position == "mobile") + out->screenPosition = blink::WebDeviceEmulationParams::Mobile; + else if (screen_position == "desktop") + out->screenPosition = blink::WebDeviceEmulationParams::Desktop; + else + return false; + } + + dict.Get("screenSize", &out->screenSize); + dict.Get("viewPosition", &out->viewPosition); + dict.Get("deviceScaleFactor", &out->deviceScaleFactor); + dict.Get("viewSize", &out->viewSize); + dict.Get("fitToView", &out->fitToView); + dict.Get("offset", &out->offset); + dict.Get("scale", &out->scale); + return true; +} + +} // namespace mate diff --git a/atom/common/native_mate_converters/blink_converter.h b/atom/common/native_mate_converters/blink_converter.h new file mode 100644 index 000000000000..bbd2af0e679f --- /dev/null +++ b/atom/common/native_mate_converters/blink_converter.h @@ -0,0 +1,45 @@ +// Copyright (c) 2015 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_BLINK_CONVERTER_H_ +#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_BLINK_CONVERTER_H_ + +#include "native_mate/converter.h" + +namespace blink { +struct WebDeviceEmulationParams; +struct WebFloatPoint; +struct WebPoint; +struct WebSize; +} + +namespace mate { + +template<> +struct Converter { + static bool FromV8(v8::Isolate* isolate, v8::Local val, + blink::WebFloatPoint* out); +}; + +template<> +struct Converter { + static bool FromV8(v8::Isolate* isolate, v8::Local val, + blink::WebPoint* out); +}; + +template<> +struct Converter { + static bool FromV8(v8::Isolate* isolate, v8::Local val, + blink::WebSize* out); +}; + +template<> +struct Converter { + static bool FromV8(v8::Isolate* isolate, v8::Local val, + blink::WebDeviceEmulationParams* out); +}; + +} // namespace mate + +#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_BLINK_CONVERTER_H_ diff --git a/filenames.gypi b/filenames.gypi index 461c812753c9..ce679640334d 100644 --- a/filenames.gypi +++ b/filenames.gypi @@ -286,6 +286,8 @@ 'atom/common/linux/application_info.cc', 'atom/common/native_mate_converters/accelerator_converter.cc', 'atom/common/native_mate_converters/accelerator_converter.h', + 'atom/common/native_mate_converters/blink_converter.cc', + 'atom/common/native_mate_converters/blink_converter.h', 'atom/common/native_mate_converters/callback.h', 'atom/common/native_mate_converters/file_path_converter.h', 'atom/common/native_mate_converters/gfx_converter.cc',