2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2014 GitHub, Inc.
|
2014-10-24 10:24:12 +00:00
|
|
|
// 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_frame.h"
|
|
|
|
|
2015-10-06 15:33:14 +00:00
|
|
|
#include "atom/common/api/api_messages.h"
|
2015-08-07 10:10:19 +00:00
|
|
|
#include "atom/common/native_mate_converters/callback.h"
|
2015-05-29 05:47:09 +00:00
|
|
|
#include "atom/common/native_mate_converters/gfx_converter.h"
|
2014-10-24 10:24:12 +00:00
|
|
|
#include "atom/common/native_mate_converters/string16_converter.h"
|
2014-12-18 21:54:01 +00:00
|
|
|
#include "atom/renderer/api/atom_api_spell_check_client.h"
|
2014-12-08 16:05:34 +00:00
|
|
|
#include "content/public/renderer/render_frame.h"
|
2015-10-06 15:33:14 +00:00
|
|
|
#include "content/public/renderer/render_view.h"
|
2014-10-24 10:24:12 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
|
|
|
#include "native_mate/object_template_builder.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebDocument.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebLocalFrame.h"
|
2016-01-13 04:11:46 +00:00
|
|
|
#include "third_party/WebKit/public/web/WebScopedUserGesture.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebScriptSource.h"
|
2015-07-29 11:20:50 +00:00
|
|
|
#include "third_party/WebKit/public/web/WebSecurityPolicy.h"
|
2014-10-24 10:24:12 +00:00
|
|
|
#include "third_party/WebKit/public/web/WebView.h"
|
|
|
|
|
|
|
|
#include "atom/common/node_includes.h"
|
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
|
|
|
WebFrame::WebFrame()
|
|
|
|
: web_frame_(blink::WebLocalFrame::frameForCurrentContext()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
WebFrame::~WebFrame() {
|
|
|
|
}
|
|
|
|
|
2014-10-24 10:44:15 +00:00
|
|
|
void WebFrame::SetName(const std::string& name) {
|
|
|
|
web_frame_->setName(blink::WebString::fromUTF8(name));
|
|
|
|
}
|
|
|
|
|
2014-10-24 10:24:12 +00:00
|
|
|
double WebFrame::SetZoomLevel(double level) {
|
2015-10-06 15:33:14 +00:00
|
|
|
auto render_view = content::RenderView::FromWebView(web_frame_->view());
|
|
|
|
// Notify guests if any for zoom level change.
|
|
|
|
render_view->Send(
|
|
|
|
new AtomViewHostMsg_ZoomLevelChanged(MSG_ROUTING_NONE, level));
|
2014-10-24 10:24:12 +00:00
|
|
|
return web_frame_->view()->setZoomLevel(level);
|
|
|
|
}
|
|
|
|
|
|
|
|
double WebFrame::GetZoomLevel() const {
|
|
|
|
return web_frame_->view()->zoomLevel();
|
|
|
|
}
|
|
|
|
|
|
|
|
double WebFrame::SetZoomFactor(double factor) {
|
|
|
|
return blink::WebView::zoomLevelToZoomFactor(SetZoomLevel(
|
|
|
|
blink::WebView::zoomFactorToZoomLevel(factor)));
|
|
|
|
}
|
|
|
|
|
|
|
|
double WebFrame::GetZoomFactor() const {
|
|
|
|
return blink::WebView::zoomLevelToZoomFactor(GetZoomLevel());
|
|
|
|
}
|
|
|
|
|
2015-08-27 14:08:25 +00:00
|
|
|
void WebFrame::SetZoomLevelLimits(double min_level, double max_level) {
|
|
|
|
web_frame_->view()->setDefaultPageScaleLimits(min_level, max_level);
|
|
|
|
}
|
|
|
|
|
2015-05-22 11:11:22 +00:00
|
|
|
v8::Local<v8::Value> WebFrame::RegisterEmbedderCustomElement(
|
|
|
|
const base::string16& name, v8::Local<v8::Object> options) {
|
2014-10-24 10:24:12 +00:00
|
|
|
blink::WebExceptionCode c = 0;
|
|
|
|
return web_frame_->document().registerEmbedderCustomElement(name, options, c);
|
|
|
|
}
|
|
|
|
|
2015-05-29 05:47:09 +00:00
|
|
|
void WebFrame::RegisterElementResizeCallback(
|
|
|
|
int element_instance_id,
|
|
|
|
const GuestViewContainer::ResizeCallback& callback) {
|
|
|
|
auto guest_view_container = GuestViewContainer::FromID(element_instance_id);
|
|
|
|
if (guest_view_container)
|
|
|
|
guest_view_container->RegisterElementResizeCallback(callback);
|
|
|
|
}
|
|
|
|
|
2014-12-08 16:05:34 +00:00
|
|
|
void WebFrame::AttachGuest(int id) {
|
|
|
|
content::RenderFrame::FromWebFrame(web_frame_)->AttachGuest(id);
|
|
|
|
}
|
|
|
|
|
2014-12-20 04:42:19 +00:00
|
|
|
void WebFrame::SetSpellCheckProvider(mate::Arguments* args,
|
|
|
|
const std::string& language,
|
2014-12-20 05:01:47 +00:00
|
|
|
bool auto_spell_correct_turned_on,
|
2015-05-22 11:11:22 +00:00
|
|
|
v8::Local<v8::Object> provider) {
|
2014-12-20 05:01:47 +00:00
|
|
|
if (!provider->Has(mate::StringToV8(args->isolate(), "spellCheck"))) {
|
2014-12-20 04:42:19 +00:00
|
|
|
args->ThrowError("\"spellCheck\" has to be defined");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-20 05:01:47 +00:00
|
|
|
spell_check_client_.reset(new SpellCheckClient(
|
|
|
|
language, auto_spell_correct_turned_on, args->isolate(), provider));
|
2014-12-19 00:44:38 +00:00
|
|
|
web_frame_->view()->setSpellCheckClient(spell_check_client_.get());
|
2014-12-18 21:54:01 +00:00
|
|
|
}
|
|
|
|
|
2015-07-29 11:20:50 +00:00
|
|
|
void WebFrame::RegisterURLSchemeAsSecure(const std::string& scheme) {
|
|
|
|
// Register scheme to secure list (https, wss, data).
|
|
|
|
blink::WebSecurityPolicy::registerURLSchemeAsSecure(
|
|
|
|
blink::WebString::fromUTF8(scheme));
|
|
|
|
}
|
|
|
|
|
2015-11-13 08:03:40 +00:00
|
|
|
void WebFrame::RegisterURLSchemeAsBypassingCSP(const std::string& scheme) {
|
2015-07-29 11:20:50 +00:00
|
|
|
// Register scheme to bypass pages's Content Security Policy.
|
|
|
|
blink::WebSecurityPolicy::registerURLSchemeAsBypassingContentSecurityPolicy(
|
|
|
|
blink::WebString::fromUTF8(scheme));
|
|
|
|
}
|
|
|
|
|
2015-09-21 17:29:59 +00:00
|
|
|
void WebFrame::RegisterURLSchemeAsPrivileged(const std::string& scheme) {
|
|
|
|
// Register scheme to privileged list (https, wss, data, chrome-extension)
|
|
|
|
blink::WebString privileged_scheme(blink::WebString::fromUTF8(scheme));
|
|
|
|
blink::WebSecurityPolicy::registerURLSchemeAsSecure(privileged_scheme);
|
|
|
|
blink::WebSecurityPolicy::registerURLSchemeAsBypassingContentSecurityPolicy(
|
|
|
|
privileged_scheme);
|
|
|
|
blink::WebSecurityPolicy::registerURLSchemeAsAllowingServiceWorkers(
|
|
|
|
privileged_scheme);
|
2015-12-29 10:26:19 +00:00
|
|
|
blink::WebSecurityPolicy::registerURLSchemeAsSupportingFetchAPI(
|
|
|
|
privileged_scheme);
|
2015-09-21 17:29:59 +00:00
|
|
|
}
|
|
|
|
|
2016-01-13 03:21:16 +00:00
|
|
|
void WebFrame::InsertText(const std::string& text) {
|
|
|
|
web_frame_->insertText(blink::WebString::fromUTF8(text));
|
|
|
|
}
|
|
|
|
|
2016-01-13 04:17:56 +00:00
|
|
|
void WebFrame::ExecuteJavaScript(const base::string16& code,
|
|
|
|
mate::Arguments* args) {
|
|
|
|
bool has_user_gesture = false;
|
|
|
|
args->GetNext(&has_user_gesture);
|
2016-01-13 04:11:46 +00:00
|
|
|
scoped_ptr<blink::WebScopedUserGesture> gesture(
|
2016-01-13 04:17:56 +00:00
|
|
|
has_user_gesture ? new blink::WebScopedUserGesture : nullptr);
|
2016-01-13 04:11:46 +00:00
|
|
|
web_frame_->executeScriptAndReturnValue(blink::WebScriptSource(code));
|
|
|
|
}
|
|
|
|
|
2014-10-24 10:24:12 +00:00
|
|
|
mate::ObjectTemplateBuilder WebFrame::GetObjectTemplateBuilder(
|
|
|
|
v8::Isolate* isolate) {
|
|
|
|
return mate::ObjectTemplateBuilder(isolate)
|
2014-10-24 10:44:15 +00:00
|
|
|
.SetMethod("setName", &WebFrame::SetName)
|
2014-10-24 10:24:12 +00:00
|
|
|
.SetMethod("setZoomLevel", &WebFrame::SetZoomLevel)
|
|
|
|
.SetMethod("getZoomLevel", &WebFrame::GetZoomLevel)
|
|
|
|
.SetMethod("setZoomFactor", &WebFrame::SetZoomFactor)
|
|
|
|
.SetMethod("getZoomFactor", &WebFrame::GetZoomFactor)
|
2015-08-27 14:08:25 +00:00
|
|
|
.SetMethod("setZoomLevelLimits", &WebFrame::SetZoomLevelLimits)
|
2014-10-24 10:24:12 +00:00
|
|
|
.SetMethod("registerEmbedderCustomElement",
|
2014-12-08 16:05:34 +00:00
|
|
|
&WebFrame::RegisterEmbedderCustomElement)
|
2015-05-29 05:47:09 +00:00
|
|
|
.SetMethod("registerElementResizeCallback",
|
|
|
|
&WebFrame::RegisterElementResizeCallback)
|
2014-12-18 21:54:01 +00:00
|
|
|
.SetMethod("attachGuest", &WebFrame::AttachGuest)
|
2015-01-08 20:37:41 +00:00
|
|
|
.SetMethod("setSpellCheckProvider", &WebFrame::SetSpellCheckProvider)
|
2015-11-13 08:03:40 +00:00
|
|
|
.SetMethod("registerURLSchemeAsSecure",
|
2015-07-29 11:20:50 +00:00
|
|
|
&WebFrame::RegisterURLSchemeAsSecure)
|
2015-11-13 08:03:40 +00:00
|
|
|
.SetMethod("registerURLSchemeAsBypassingCSP",
|
|
|
|
&WebFrame::RegisterURLSchemeAsBypassingCSP)
|
|
|
|
.SetMethod("registerURLSchemeAsPrivileged",
|
2016-01-13 03:21:16 +00:00
|
|
|
&WebFrame::RegisterURLSchemeAsPrivileged)
|
2016-01-13 04:11:46 +00:00
|
|
|
.SetMethod("insertText", &WebFrame::InsertText)
|
|
|
|
.SetMethod("executeJavaScript", &WebFrame::ExecuteJavaScript);
|
2014-10-24 10:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
mate::Handle<WebFrame> WebFrame::Create(v8::Isolate* isolate) {
|
|
|
|
return CreateHandle(isolate, new WebFrame);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-05-22 11:11:22 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context, void* priv) {
|
2014-10-24 10:24:12 +00:00
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
|
|
|
mate::Dictionary dict(isolate, exports);
|
|
|
|
dict.Set("webFrame", atom::api::WebFrame::Create(isolate));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_renderer_web_frame, Initialize)
|