Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(178)

Unified Diff: components/dom_distiller/content/renderer/distiller_native_javascript.cc

Issue 1231083007: Expose distiller functions to JavaScript (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@refactor-content
Patch Set: cl format Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/dom_distiller/content/renderer/distiller_native_javascript.cc
diff --git a/components/dom_distiller/content/renderer/distiller_native_javascript.cc b/components/dom_distiller/content/renderer/distiller_native_javascript.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d35983fa0026b68a1b1d6ce02b3524c1c57d5854
--- /dev/null
+++ b/components/dom_distiller/content/renderer/distiller_native_javascript.cc
@@ -0,0 +1,79 @@
+// Copyright 2015 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 "components/dom_distiller/content/renderer/distiller_native_javascript.h"
+
+#include <string>
+
+#include "base/bind.h"
+#include "base/strings/utf_string_conversions.h"
+#include "components/dom_distiller/content/common/distiller_javascript_service.mojom.h"
+#include "content/public/common/service_registry.h"
+#include "content/public/renderer/render_frame.h"
+#include "gin/arguments.h"
+#include "gin/function_template.h"
+#include "third_party/WebKit/public/web/WebKit.h"
+#include "third_party/WebKit/public/web/WebLocalFrame.h"
+#include "v8/include/v8.h"
+
+using blink::WebLocalFrame;
+
+namespace dom_distiller {
+
+DistillerNativeJavaScript::DistillerNativeJavaScript(
+ content::RenderFrame* render_frame)
+ : render_frame_(render_frame) {}
+
+DistillerNativeJavaScript::~DistillerNativeJavaScript() {}
+
+void DistillerNativeJavaScript::AddJavaScriptObjectToFrame() {
+ v8::Isolate* isolate = blink::mainThreadIsolate();
+ v8::HandleScope handle_scope(isolate);
+ v8::Local<v8::Context> context =
+ render_frame_->GetWebFrame()->mainWorldScriptContext();
+ if (context.IsEmpty())
+ return;
+
+ v8::Context::Scope context_scope(context);
+
+ v8::Local<v8::Object> distiller_obj =
+ GetOrCreateDistillerObject(isolate, context->Global());
+
+ distiller_obj->Set(
+ gin::StringToSymbol(isolate, "echo"),
+ gin::CreateFunctionTemplate(
+ isolate, base::Bind(&DistillerNativeJavaScript::distillerEcho,
+ base::Unretained(this)))
+ ->GetFunction());
+}
+
+std::string DistillerNativeJavaScript::distillerEcho(
+ const std::string& message) {
+ if (!distiller_js_service_) {
+ render_frame_->GetServiceRegistry()->ConnectToRemoteService(
+ mojo::GetProxy(&distiller_js_service_));
+ }
+ // TODO(mdjones): It is possible and beneficial to have information
+ // returned from the browser process with these calls. The problem
+ // is waiting blocks this process.
+ distiller_js_service_->HandleDistillerEchoCall(message);
+
+ return message;
+}
+
+v8::Local<v8::Object> GetOrCreateDistillerObject(v8::Isolate* isolate,
+ v8::Local<v8::Object> global) {
+ v8::Local<v8::Object> distiller_obj;
+ v8::Local<v8::Value> distiller_value =
+ global->Get(gin::StringToV8(isolate, "distiller"));
+ if (distiller_value.IsEmpty() || !distiller_value->IsObject()) {
+ distiller_obj = v8::Object::New(isolate);
+ global->Set(gin::StringToSymbol(isolate, "distiller"), distiller_obj);
+ } else {
+ distiller_obj = v8::Local<v8::Object>::Cast(distiller_value);
+ }
+ return distiller_obj;
+}
+
+} // namespace dom_distiller

Powered by Google App Engine
This is Rietveld 408576698