From accfabf569b74560b7f337f1846c50e23b1aaf15 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 16 Jun 2014 14:27:57 +0800 Subject: [PATCH] Add web-view module. --- atom.gyp | 3 ++ atom/common/api/atom_extensions.h | 1 + atom/renderer/api/atom_api_web_view.cc | 69 ++++++++++++++++++++++++++ atom/renderer/api/atom_api_web_view.h | 43 ++++++++++++++++ atom/renderer/api/lib/web-view.coffee | 1 + 5 files changed, 117 insertions(+) create mode 100644 atom/renderer/api/atom_api_web_view.cc create mode 100644 atom/renderer/api/atom_api_web_view.h create mode 100644 atom/renderer/api/lib/web-view.coffee diff --git a/atom.gyp b/atom.gyp index e3075b4d985f..df92b76726a6 100644 --- a/atom.gyp +++ b/atom.gyp @@ -41,6 +41,7 @@ 'atom/renderer/lib/override.coffee', 'atom/renderer/api/lib/ipc.coffee', 'atom/renderer/api/lib/remote.coffee', + 'atom/renderer/api/lib/web-view.coffee', ], 'lib_sources': [ 'atom/app/atom_main_delegate.cc', @@ -219,6 +220,8 @@ 'atom/renderer/api/atom_api_renderer_ipc.cc', 'atom/renderer/api/atom_renderer_bindings.cc', 'atom/renderer/api/atom_renderer_bindings.h', + 'atom/renderer/api/atom_api_web_view.cc', + 'atom/renderer/api/atom_api_web_view.h', 'atom/renderer/atom_render_view_observer.cc', 'atom/renderer/atom_render_view_observer.h', 'atom/renderer/atom_renderer_client.cc', diff --git a/atom/common/api/atom_extensions.h b/atom/common/api/atom_extensions.h index 39a4159c555e..e8fefac78433 100644 --- a/atom/common/api/atom_extensions.h +++ b/atom/common/api/atom_extensions.h @@ -21,6 +21,7 @@ NODE_EXT_LIST_ITEM(atom_browser_window) // Module names start with `atom_renderer_` can only be used by renderer // process. NODE_EXT_LIST_ITEM(atom_renderer_ipc) +NODE_EXT_LIST_ITEM(atom_renderer_web_view) // Module names start with `atom_common_` can be used by both browser and // renderer processes. diff --git a/atom/renderer/api/atom_api_web_view.cc b/atom/renderer/api/atom_api_web_view.cc new file mode 100644 index 000000000000..fda2e25af6ed --- /dev/null +++ b/atom/renderer/api/atom_api_web_view.cc @@ -0,0 +1,69 @@ +// Copyright (c) 2014 GitHub, Inc. All rights reserved. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#include "atom/renderer/api/atom_api_web_view.h" + +#include "native_mate/dictionary.h" +#include "native_mate/object_template_builder.h" +#include "third_party/WebKit/public/web/WebFrame.h" +#include "third_party/WebKit/public/web/WebView.h" + +#include "atom/common/node_includes.h" + +namespace atom { + +namespace api { + +namespace { + +WebKit::WebView* GetCurrentWebView() { + WebKit::WebFrame* frame = WebKit::WebFrame::frameForCurrentContext(); + if (!frame) + return NULL; + return frame->view(); +} + +} // namespace + +WebView::WebView() : web_view_(GetCurrentWebView()) { +} + +WebView::~WebView() { +} + +void WebView::SetZoomLevel(double level) { + web_view_->setZoomLevel(level); +} + +double WebView::GetZoomLevel() const { + return web_view_->zoomLevel(); +} + +mate::ObjectTemplateBuilder WebView::GetObjectTemplateBuilder( + v8::Isolate* isolate) { + return mate::ObjectTemplateBuilder(isolate) + .SetMethod("setZoomLevel", &WebView::SetZoomLevel) + .SetMethod("getZoomLevel", &WebView::GetZoomLevel); +} + +// static +mate::Handle WebView::Create(v8::Isolate* isolate) { + return CreateHandle(isolate, new WebView); +} + +} // namespace api + +} // namespace atom + +namespace { + +void Initialize(v8::Handle exports) { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + mate::Dictionary dict(isolate, exports); + dict.Set("webView", atom::api::WebView::Create(isolate)); +} + +} // namespace + +NODE_MODULE(atom_renderer_web_view, Initialize) diff --git a/atom/renderer/api/atom_api_web_view.h b/atom/renderer/api/atom_api_web_view.h new file mode 100644 index 000000000000..bc4898d08618 --- /dev/null +++ b/atom/renderer/api/atom_api_web_view.h @@ -0,0 +1,43 @@ +// Copyright (c) 2014 GitHub, Inc. All rights reserved. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef ATOM_RENDERER_API_ATOM_API_WEB_VIEW_H_ +#define ATOM_RENDERER_API_ATOM_API_WEB_VIEW_H_ + +#include "native_mate/handle.h" +#include "native_mate/wrappable.h" + +namespace WebKit { +class WebView; +} + +namespace atom { + +namespace api { + +class WebView : public mate::Wrappable { + public: + static mate::Handle Create(v8::Isolate* isolate); + + private: + WebView(); + virtual ~WebView(); + + void SetZoomLevel(double level); + double GetZoomLevel() const; + + // mate::Wrappable: + virtual mate::ObjectTemplateBuilder GetObjectTemplateBuilder( + v8::Isolate* isolate); + + WebKit::WebView* web_view_; + + DISALLOW_COPY_AND_ASSIGN(WebView); +}; + +} // namespace api + +} // namespace atom + +#endif // ATOM_RENDERER_API_ATOM_API_WEB_VIEW_H_ diff --git a/atom/renderer/api/lib/web-view.coffee b/atom/renderer/api/lib/web-view.coffee new file mode 100644 index 000000000000..a02a16663abf --- /dev/null +++ b/atom/renderer/api/lib/web-view.coffee @@ -0,0 +1 @@ +module.exports = process.atomBinding('web_view').webView