| 
									
										
										
										
											2014-10-31 11:17:05 -07:00
										 |  |  | // Copyright (c) 2014 GitHub, Inc.
 | 
					
						
							| 
									
										
										
										
											2014-10-24 18:24:12 +08:00
										 |  |  | // Use of this source code is governed by the MIT license that can be
 | 
					
						
							|  |  |  | // found in the LICENSE file.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | #include <memory>
 | 
					
						
							|  |  |  | #include <string>
 | 
					
						
							| 
									
										
										
										
											2019-01-24 08:18:54 -08:00
										 |  |  | #include <utility>
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | #include <vector>
 | 
					
						
							| 
									
										
										
										
											2019-01-24 08:18:54 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-12 14:36:49 -07:00
										 |  |  | #include "base/memory/memory_pressure_listener.h"
 | 
					
						
							| 
									
										
										
										
											2014-12-08 08:05:34 -08:00
										 |  |  | #include "content/public/renderer/render_frame.h"
 | 
					
						
							| 
									
										
										
										
											2018-08-16 15:57:40 -07:00
										 |  |  | #include "content/public/renderer/render_frame_observer.h"
 | 
					
						
							| 
									
										
										
										
											2017-11-24 12:04:20 +05:30
										 |  |  | #include "content/public/renderer/render_frame_visitor.h"
 | 
					
						
							| 
									
										
										
										
											2015-10-06 21:03:14 +05:30
										 |  |  | #include "content/public/renderer/render_view.h"
 | 
					
						
							| 
									
										
										
										
											2014-10-24 18:24:12 +08:00
										 |  |  | #include "native_mate/dictionary.h"
 | 
					
						
							|  |  |  | #include "native_mate/object_template_builder.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-03 18:18:23 -07:00
										 |  |  | #include "services/service_manager/public/cpp/interface_provider.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-19 13:46:59 -07:00
										 |  |  | #include "shell/common/api/api.mojom.h"
 | 
					
						
							|  |  |  | #include "shell/common/api/event_emitter_caller.h"
 | 
					
						
							|  |  |  | #include "shell/common/native_mate_converters/blink_converter.h"
 | 
					
						
							|  |  |  | #include "shell/common/native_mate_converters/callback.h"
 | 
					
						
							|  |  |  | #include "shell/common/native_mate_converters/gfx_converter.h"
 | 
					
						
							|  |  |  | #include "shell/common/native_mate_converters/string16_converter.h"
 | 
					
						
							|  |  |  | #include "shell/common/node_includes.h"
 | 
					
						
							|  |  |  | #include "shell/common/promise_util.h"
 | 
					
						
							|  |  |  | #include "shell/renderer/api/atom_api_spell_check_client.h"
 | 
					
						
							| 
									
										
										
										
											2018-07-20 18:08:18 +02:00
										 |  |  | #include "third_party/blink/public/platform/web_cache.h"
 | 
					
						
							| 
									
										
										
										
											2019-01-22 02:15:15 +05:30
										 |  |  | #include "third_party/blink/public/platform/web_isolated_world_info.h"
 | 
					
						
							| 
									
										
										
										
											2019-01-12 06:30:43 +05:30
										 |  |  | #include "third_party/blink/public/web/web_custom_element.h"
 | 
					
						
							| 
									
										
										
										
											2018-07-20 18:08:18 +02:00
										 |  |  | #include "third_party/blink/public/web/web_document.h"
 | 
					
						
							|  |  |  | #include "third_party/blink/public/web/web_element.h"
 | 
					
						
							|  |  |  | #include "third_party/blink/public/web/web_frame_widget.h"
 | 
					
						
							|  |  |  | #include "third_party/blink/public/web/web_ime_text_span.h"
 | 
					
						
							|  |  |  | #include "third_party/blink/public/web/web_input_method_controller.h"
 | 
					
						
							|  |  |  | #include "third_party/blink/public/web/web_local_frame.h"
 | 
					
						
							|  |  |  | #include "third_party/blink/public/web/web_script_execution_callback.h"
 | 
					
						
							|  |  |  | #include "third_party/blink/public/web/web_script_source.h"
 | 
					
						
							|  |  |  | #include "third_party/blink/public/web/web_view.h"
 | 
					
						
							| 
									
										
										
										
											2018-10-26 17:31:56 -07:00
										 |  |  | #include "url/url_util.h"
 | 
					
						
							| 
									
										
										
										
											2014-10-24 18:24:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-30 23:31:21 +02:00
										 |  |  | namespace mate { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <> | 
					
						
							|  |  |  | struct Converter<blink::WebLocalFrame::ScriptExecutionType> { | 
					
						
							|  |  |  |   static bool FromV8(v8::Isolate* isolate, | 
					
						
							| 
									
										
										
										
											2017-11-15 11:14:41 +01:00
										 |  |  |                      v8::Local<v8::Value> val, | 
					
						
							| 
									
										
										
										
											2017-08-30 23:31:21 +02:00
										 |  |  |                      blink::WebLocalFrame::ScriptExecutionType* out) { | 
					
						
							|  |  |  |     std::string execution_type; | 
					
						
							|  |  |  |     if (!ConvertFromV8(isolate, val, &execution_type)) | 
					
						
							|  |  |  |       return false; | 
					
						
							|  |  |  |     if (execution_type == "asynchronous") { | 
					
						
							|  |  |  |       *out = blink::WebLocalFrame::kAsynchronous; | 
					
						
							| 
									
										
										
										
											2018-04-17 21:55:30 -04:00
										 |  |  |     } else if (execution_type == "asynchronousBlockingOnload") { | 
					
						
							| 
									
										
										
										
											2017-08-30 23:31:21 +02:00
										 |  |  |       *out = blink::WebLocalFrame::kAsynchronousBlockingOnload; | 
					
						
							|  |  |  |     } else if (execution_type == "synchronous") { | 
					
						
							|  |  |  |       *out = blink::WebLocalFrame::kSynchronous; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-11-15 11:14:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-17 14:34:05 -07:00
										 |  |  | template <> | 
					
						
							|  |  |  | struct Converter<blink::WebDocument::CSSOrigin> { | 
					
						
							|  |  |  |   static bool FromV8(v8::Isolate* isolate, | 
					
						
							|  |  |  |                      v8::Local<v8::Value> val, | 
					
						
							|  |  |  |                      blink::WebDocument::CSSOrigin* out) { | 
					
						
							|  |  |  |     std::string css_origin; | 
					
						
							|  |  |  |     if (!ConvertFromV8(isolate, val, &css_origin)) | 
					
						
							|  |  |  |       return false; | 
					
						
							|  |  |  |     if (css_origin == "user") { | 
					
						
							|  |  |  |       *out = blink::WebDocument::CSSOrigin::kUserOrigin; | 
					
						
							|  |  |  |     } else if (css_origin == "author") { | 
					
						
							|  |  |  |       *out = blink::WebDocument::CSSOrigin::kAuthorOrigin; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-30 23:31:21 +02:00
										 |  |  | }  // namespace mate
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-19 14:23:04 -07:00
										 |  |  | namespace electron { | 
					
						
							| 
									
										
										
										
											2014-10-24 18:24:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace api { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 22:33:27 +05:30
										 |  |  | namespace { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-16 15:57:40 -07:00
										 |  |  | content::RenderFrame* GetRenderFrame(v8::Local<v8::Value> value) { | 
					
						
							|  |  |  |   v8::Local<v8::Context> context = | 
					
						
							|  |  |  |       v8::Local<v8::Object>::Cast(value)->CreationContext(); | 
					
						
							|  |  |  |   if (context.IsEmpty()) | 
					
						
							|  |  |  |     return nullptr; | 
					
						
							|  |  |  |   blink::WebLocalFrame* frame = blink::WebLocalFrame::FrameForContext(context); | 
					
						
							|  |  |  |   if (!frame) | 
					
						
							|  |  |  |     return nullptr; | 
					
						
							|  |  |  |   return content::RenderFrame::FromWebFrame(frame); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class RenderFrameStatus : public content::RenderFrameObserver { | 
					
						
							|  |  |  |  public: | 
					
						
							|  |  |  |   explicit RenderFrameStatus(content::RenderFrame* render_frame) | 
					
						
							|  |  |  |       : content::RenderFrameObserver(render_frame) {} | 
					
						
							|  |  |  |   ~RenderFrameStatus() final {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   bool is_ok() { return render_frame() != nullptr; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // RenderFrameObserver implementation.
 | 
					
						
							|  |  |  |   void OnDestruct() final {} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 22:33:27 +05:30
										 |  |  | class ScriptExecutionCallback : public blink::WebScriptExecutionCallback { | 
					
						
							|  |  |  |  public: | 
					
						
							| 
									
										
										
										
											2019-06-19 14:23:04 -07:00
										 |  |  |   explicit ScriptExecutionCallback(electron::util::Promise promise) | 
					
						
							| 
									
										
										
										
											2019-03-14 20:08:54 +01:00
										 |  |  |       : promise_(std::move(promise)) {} | 
					
						
							| 
									
										
										
										
											2016-07-10 13:21:42 +02:00
										 |  |  |   ~ScriptExecutionCallback() override {} | 
					
						
							| 
									
										
										
										
											2016-02-17 22:33:27 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-16 23:42:33 +03:00
										 |  |  |   void Completed( | 
					
						
							| 
									
										
										
										
											2016-02-17 22:33:27 +05:30
										 |  |  |       const blink::WebVector<v8::Local<v8::Value>>& result) override { | 
					
						
							| 
									
										
										
										
											2019-06-05 15:43:02 -07:00
										 |  |  |     if (!result.empty()) { | 
					
						
							|  |  |  |       if (!result[0].IsEmpty()) { | 
					
						
							|  |  |  |         // Right now only single results per frame is supported.
 | 
					
						
							|  |  |  |         promise_.Resolve(result[0]); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         promise_.RejectWithErrorMessage( | 
					
						
							|  |  |  |             "Script failed to execute, this normally means an error " | 
					
						
							| 
									
										
										
										
											2019-06-10 17:14:58 -07:00
										 |  |  |             "was thrown. Check the renderer console for the error."); | 
					
						
							| 
									
										
										
										
											2019-06-05 15:43:02 -07:00
										 |  |  |       } | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       promise_.RejectWithErrorMessage( | 
					
						
							|  |  |  |           "WebFrame was removed before script could run. This normally means " | 
					
						
							|  |  |  |           "the underlying frame was destroyed"); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-02-17 22:33:27 +05:30
										 |  |  |     delete this; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  private: | 
					
						
							| 
									
										
										
										
											2019-06-19 14:23:04 -07:00
										 |  |  |   electron::util::Promise promise_; | 
					
						
							| 
									
										
										
										
											2016-02-17 22:33:27 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |   DISALLOW_COPY_AND_ASSIGN(ScriptExecutionCallback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | class FrameSetSpellChecker : public content::RenderFrameVisitor { | 
					
						
							| 
									
										
										
										
											2017-11-24 12:04:20 +05:30
										 |  |  |  public: | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |   FrameSetSpellChecker(SpellCheckClient* spell_check_client, | 
					
						
							|  |  |  |                        content::RenderFrame* main_frame) | 
					
						
							|  |  |  |       : spell_check_client_(spell_check_client), main_frame_(main_frame) { | 
					
						
							|  |  |  |     content::RenderFrame::ForEach(this); | 
					
						
							|  |  |  |     main_frame->GetWebFrame()->SetSpellCheckPanelHostClient(spell_check_client); | 
					
						
							| 
									
										
										
										
											2017-11-24 12:04:20 +05:30
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-24 12:04:20 +05:30
										 |  |  |   bool Visit(content::RenderFrame* render_frame) override { | 
					
						
							| 
									
										
										
										
											2018-04-17 15:41:47 -07:00
										 |  |  |     auto* view = render_frame->GetRenderView(); | 
					
						
							| 
									
										
										
										
											2017-11-24 12:04:20 +05:30
										 |  |  |     if (view->GetMainRenderFrame() == main_frame_ || | 
					
						
							|  |  |  |         (render_frame->IsMainFrame() && render_frame == main_frame_)) { | 
					
						
							|  |  |  |       render_frame->GetWebFrame()->SetTextCheckClient(spell_check_client_); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  private: | 
					
						
							|  |  |  |   SpellCheckClient* spell_check_client_; | 
					
						
							|  |  |  |   content::RenderFrame* main_frame_; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |   DISALLOW_COPY_AND_ASSIGN(FrameSetSpellChecker); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-02-17 22:33:27 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | class SpellCheckerHolder : public content::RenderFrameObserver { | 
					
						
							| 
									
										
										
										
											2019-01-24 08:18:54 -08:00
										 |  |  |  public: | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |   // Find existing holder for the |render_frame|.
 | 
					
						
							|  |  |  |   static SpellCheckerHolder* FromRenderFrame( | 
					
						
							|  |  |  |       content::RenderFrame* render_frame) { | 
					
						
							|  |  |  |     for (auto* holder : instances_) { | 
					
						
							|  |  |  |       if (holder->render_frame() == render_frame) | 
					
						
							|  |  |  |         return holder; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return nullptr; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SpellCheckerHolder(content::RenderFrame* render_frame, | 
					
						
							|  |  |  |                      std::unique_ptr<SpellCheckClient> spell_check_client) | 
					
						
							| 
									
										
										
										
											2019-01-24 08:18:54 -08:00
										 |  |  |       : content::RenderFrameObserver(render_frame), | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |         spell_check_client_(std::move(spell_check_client)) { | 
					
						
							|  |  |  |     DCHECK(!FromRenderFrame(render_frame)); | 
					
						
							|  |  |  |     instances_.insert(this); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ~SpellCheckerHolder() final { instances_.erase(this); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void UnsetAndDestroy() { | 
					
						
							|  |  |  |     FrameSetSpellChecker set_spell_checker(nullptr, render_frame()); | 
					
						
							|  |  |  |     delete this; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-01-24 08:18:54 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // RenderFrameObserver implementation.
 | 
					
						
							|  |  |  |   void OnDestruct() final { | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |     // Since we delete this in WillReleaseScriptContext, this method is unlikely
 | 
					
						
							|  |  |  |     // to be called, but override anyway since I'm not sure if there are some
 | 
					
						
							|  |  |  |     // corner cases.
 | 
					
						
							|  |  |  |     //
 | 
					
						
							|  |  |  |     // Note that while there are two "delete this", it is totally fine as the
 | 
					
						
							|  |  |  |     // observer unsubscribes automatically in destructor and the other one won't
 | 
					
						
							|  |  |  |     // be called.
 | 
					
						
							|  |  |  |     //
 | 
					
						
							|  |  |  |     // Also note that we should not call UnsetAndDestroy here, as the render
 | 
					
						
							|  |  |  |     // frame is going to be destroyed.
 | 
					
						
							| 
									
										
										
										
											2019-01-24 08:18:54 -08:00
										 |  |  |     delete this; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |   void WillReleaseScriptContext(v8::Local<v8::Context> context, | 
					
						
							|  |  |  |                                 int world_id) final { | 
					
						
							|  |  |  |     // Unset spell checker when the script context is going to be released, as
 | 
					
						
							|  |  |  |     // the spell check implementation lives there.
 | 
					
						
							|  |  |  |     UnsetAndDestroy(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-24 08:18:54 -08:00
										 |  |  |  private: | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |   static std::set<SpellCheckerHolder*> instances_; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-24 08:18:54 -08:00
										 |  |  |   std::unique_ptr<SpellCheckClient> spell_check_client_; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | }  // namespace
 | 
					
						
							| 
									
										
										
										
											2014-10-24 18:24:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | // static
 | 
					
						
							|  |  |  | std::set<SpellCheckerHolder*> SpellCheckerHolder::instances_; | 
					
						
							| 
									
										
										
										
											2014-10-24 18:24:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | void SetName(v8::Local<v8::Value> window, const std::string& name) { | 
					
						
							|  |  |  |   GetRenderFrame(window)->GetWebFrame()->SetName( | 
					
						
							|  |  |  |       blink::WebString::FromUTF8(name)); | 
					
						
							| 
									
										
										
										
											2014-10-24 18:44:15 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-03 18:18:23 -07:00
										 |  |  | void SetZoomLevel(v8::Local<v8::Value> window, double level) { | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |   content::RenderFrame* render_frame = GetRenderFrame(window); | 
					
						
							| 
									
										
										
										
											2019-06-03 18:18:23 -07:00
										 |  |  |   mojom::ElectronBrowserPtr browser_ptr; | 
					
						
							|  |  |  |   render_frame->GetRemoteInterfaces()->GetInterface( | 
					
						
							|  |  |  |       mojo::MakeRequest(&browser_ptr)); | 
					
						
							|  |  |  |   browser_ptr->SetTemporaryZoomLevel(level); | 
					
						
							| 
									
										
										
										
											2014-10-24 18:24:12 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | double GetZoomLevel(v8::Local<v8::Value> window) { | 
					
						
							| 
									
										
										
										
											2017-01-31 14:55:48 +05:30
										 |  |  |   double result = 0.0; | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |   content::RenderFrame* render_frame = GetRenderFrame(window); | 
					
						
							| 
									
										
										
										
											2019-06-03 18:18:23 -07:00
										 |  |  |   mojom::ElectronBrowserPtr browser_ptr; | 
					
						
							|  |  |  |   render_frame->GetRemoteInterfaces()->GetInterface( | 
					
						
							|  |  |  |       mojo::MakeRequest(&browser_ptr)); | 
					
						
							|  |  |  |   browser_ptr->DoGetZoomLevel(&result); | 
					
						
							| 
									
										
										
										
											2017-01-30 22:36:50 +05:30
										 |  |  |   return result; | 
					
						
							| 
									
										
										
										
											2014-10-24 18:24:12 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-03 18:18:23 -07:00
										 |  |  | void SetZoomFactor(v8::Local<v8::Value> window, double factor) { | 
					
						
							|  |  |  |   SetZoomLevel(window, blink::WebView::ZoomFactorToZoomLevel(factor)); | 
					
						
							| 
									
										
										
										
											2014-10-24 18:24:12 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | double GetZoomFactor(v8::Local<v8::Value> window) { | 
					
						
							|  |  |  |   return blink::WebView::ZoomLevelToZoomFactor(GetZoomLevel(window)); | 
					
						
							| 
									
										
										
										
											2014-10-24 18:24:12 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | void SetVisualZoomLevelLimits(v8::Local<v8::Value> window, | 
					
						
							|  |  |  |                               double min_level, | 
					
						
							|  |  |  |                               double max_level) { | 
					
						
							|  |  |  |   blink::WebFrame* web_frame = GetRenderFrame(window)->GetWebFrame(); | 
					
						
							|  |  |  |   web_frame->View()->SetDefaultPageScaleLimits(min_level, max_level); | 
					
						
							|  |  |  |   web_frame->View()->SetIgnoreViewportTagScaleLimits(true); | 
					
						
							| 
									
										
										
										
											2015-08-27 22:08:25 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | void SetLayoutZoomLevelLimits(v8::Local<v8::Value> window, | 
					
						
							|  |  |  |                               double min_level, | 
					
						
							|  |  |  |                               double max_level) { | 
					
						
							|  |  |  |   blink::WebFrame* web_frame = GetRenderFrame(window)->GetWebFrame(); | 
					
						
							|  |  |  |   web_frame->View()->ZoomLimitsChanged(min_level, max_level); | 
					
						
							| 
									
										
										
										
											2016-11-21 14:59:27 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | void AllowGuestViewElementDefinition(v8::Isolate* isolate, | 
					
						
							|  |  |  |                                      v8::Local<v8::Value> window, | 
					
						
							|  |  |  |                                      v8::Local<v8::Object> context, | 
					
						
							|  |  |  |                                      v8::Local<v8::Function> register_cb) { | 
					
						
							|  |  |  |   v8::HandleScope handle_scope(isolate); | 
					
						
							| 
									
										
										
										
											2018-12-14 15:38:35 +09:00
										 |  |  |   v8::Context::Scope context_scope(context->CreationContext()); | 
					
						
							| 
									
										
										
										
											2019-01-12 06:30:43 +05:30
										 |  |  |   blink::WebCustomElement::EmbedderNamesAllowedScope embedder_names_scope; | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |   GetRenderFrame(context)->GetWebFrame()->RequestExecuteV8Function( | 
					
						
							|  |  |  |       context->CreationContext(), register_cb, v8::Null(isolate), 0, nullptr, | 
					
						
							|  |  |  |       nullptr); | 
					
						
							| 
									
										
										
										
											2014-10-24 18:24:12 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | int GetWebFrameId(v8::Local<v8::Value> window, | 
					
						
							|  |  |  |                   v8::Local<v8::Value> content_window) { | 
					
						
							| 
									
										
										
										
											2018-08-16 15:57:40 -07:00
										 |  |  |   // Get the WebLocalFrame before (possibly) executing any user-space JS while
 | 
					
						
							|  |  |  |   // getting the |params|. We track the status of the RenderFrame via an
 | 
					
						
							|  |  |  |   // observer in case it is deleted during user code execution.
 | 
					
						
							|  |  |  |   content::RenderFrame* render_frame = GetRenderFrame(content_window); | 
					
						
							|  |  |  |   RenderFrameStatus render_frame_status(render_frame); | 
					
						
							| 
									
										
										
										
											2015-05-29 13:47:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-16 15:57:40 -07:00
										 |  |  |   if (!render_frame_status.is_ok()) | 
					
						
							|  |  |  |     return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   blink::WebLocalFrame* frame = render_frame->GetWebFrame(); | 
					
						
							|  |  |  |   // Parent must exist.
 | 
					
						
							|  |  |  |   blink::WebFrame* parent_frame = frame->Parent(); | 
					
						
							|  |  |  |   DCHECK(parent_frame); | 
					
						
							|  |  |  |   DCHECK(parent_frame->IsWebLocalFrame()); | 
					
						
							| 
									
										
										
										
											2014-12-08 08:05:34 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-16 15:57:40 -07:00
										 |  |  |   return render_frame->GetRoutingID(); | 
					
						
							| 
									
										
										
										
											2016-09-08 10:01:01 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | void SetSpellCheckProvider(mate::Arguments* args, | 
					
						
							|  |  |  |                            v8::Local<v8::Value> window, | 
					
						
							|  |  |  |                            const std::string& language, | 
					
						
							|  |  |  |                            v8::Local<v8::Object> provider) { | 
					
						
							| 
									
										
										
										
											2019-01-09 11:17:05 -08:00
										 |  |  |   auto context = args->isolate()->GetCurrentContext(); | 
					
						
							|  |  |  |   if (!provider->Has(context, mate::StringToV8(args->isolate(), "spellCheck")) | 
					
						
							|  |  |  |            .ToChecked()) { | 
					
						
							| 
									
										
										
										
											2014-12-19 20:42:19 -08:00
										 |  |  |     args->ThrowError("\"spellCheck\" has to be defined"); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |   // Remove the old client.
 | 
					
						
							|  |  |  |   content::RenderFrame* render_frame = GetRenderFrame(window); | 
					
						
							|  |  |  |   auto* existing = SpellCheckerHolder::FromRenderFrame(render_frame); | 
					
						
							|  |  |  |   if (existing) | 
					
						
							|  |  |  |     existing->UnsetAndDestroy(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-24 12:04:20 +05:30
										 |  |  |   // Set spellchecker for all live frames in the same process or
 | 
					
						
							|  |  |  |   // in the sandbox mode for all live sub frames to this WebFrame.
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |   auto spell_check_client = | 
					
						
							|  |  |  |       std::make_unique<SpellCheckClient>(language, args->isolate(), provider); | 
					
						
							|  |  |  |   FrameSetSpellChecker spell_checker(spell_check_client.get(), render_frame); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Attach the spell checker to RenderFrame.
 | 
					
						
							|  |  |  |   new SpellCheckerHolder(render_frame, std::move(spell_check_client)); | 
					
						
							| 
									
										
										
										
											2014-12-18 13:54:01 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | void InsertText(v8::Local<v8::Value> window, const std::string& text) { | 
					
						
							|  |  |  |   blink::WebFrame* web_frame = GetRenderFrame(window)->GetWebFrame(); | 
					
						
							|  |  |  |   if (web_frame->IsWebLocalFrame()) { | 
					
						
							|  |  |  |     web_frame->ToWebLocalFrame() | 
					
						
							|  |  |  |         ->FrameWidget() | 
					
						
							|  |  |  |         ->GetActiveWebInputMethodController() | 
					
						
							|  |  |  |         ->CommitText(blink::WebString::FromUTF8(text), | 
					
						
							|  |  |  |                      blink::WebVector<blink::WebImeTextSpan>(), | 
					
						
							|  |  |  |                      blink::WebRange(), 0); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-01-13 11:21:16 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-17 14:34:05 -07:00
										 |  |  | base::string16 InsertCSS(v8::Local<v8::Value> window, | 
					
						
							|  |  |  |                          const std::string& css, | 
					
						
							|  |  |  |                          mate::Arguments* args) { | 
					
						
							|  |  |  |   blink::WebDocument::CSSOrigin css_origin = | 
					
						
							|  |  |  |       blink::WebDocument::CSSOrigin::kAuthorOrigin; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   mate::Dictionary options; | 
					
						
							|  |  |  |   if (args->GetNext(&options)) | 
					
						
							|  |  |  |     options.Get("cssOrigin", &css_origin); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |   blink::WebFrame* web_frame = GetRenderFrame(window)->GetWebFrame(); | 
					
						
							|  |  |  |   if (web_frame->IsWebLocalFrame()) { | 
					
						
							| 
									
										
										
										
											2019-06-17 17:39:36 +02:00
										 |  |  |     return web_frame->ToWebLocalFrame() | 
					
						
							|  |  |  |         ->GetDocument() | 
					
						
							| 
									
										
										
										
											2019-07-17 14:34:05 -07:00
										 |  |  |         .InsertStyleSheet(blink::WebString::FromUTF8(css), nullptr, css_origin) | 
					
						
							| 
									
										
										
										
											2019-06-17 17:39:36 +02:00
										 |  |  |         .Utf16(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return base::string16(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RemoveInsertedCSS(v8::Local<v8::Value> window, const base::string16& key) { | 
					
						
							|  |  |  |   blink::WebFrame* web_frame = GetRenderFrame(window)->GetWebFrame(); | 
					
						
							|  |  |  |   if (web_frame->IsWebLocalFrame()) { | 
					
						
							|  |  |  |     web_frame->ToWebLocalFrame()->GetDocument().RemoveInsertedStyleSheet( | 
					
						
							|  |  |  |         blink::WebString::FromUTF16(key)); | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-12-19 15:50:47 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-14 20:08:54 +01:00
										 |  |  | v8::Local<v8::Promise> ExecuteJavaScript(mate::Arguments* args, | 
					
						
							|  |  |  |                                          v8::Local<v8::Value> window, | 
					
						
							|  |  |  |                                          const base::string16& code) { | 
					
						
							|  |  |  |   v8::Isolate* isolate = args->isolate(); | 
					
						
							|  |  |  |   util::Promise promise(isolate); | 
					
						
							|  |  |  |   v8::Local<v8::Promise> handle = promise.GetHandle(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-13 12:17:56 +08:00
										 |  |  |   bool has_user_gesture = false; | 
					
						
							|  |  |  |   args->GetNext(&has_user_gesture); | 
					
						
							| 
									
										
										
										
											2019-03-14 20:08:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |   GetRenderFrame(window)->GetWebFrame()->RequestExecuteScriptAndReturnValue( | 
					
						
							| 
									
										
										
										
											2017-06-16 23:42:33 +03:00
										 |  |  |       blink::WebScriptSource(blink::WebString::FromUTF16(code)), | 
					
						
							| 
									
										
										
										
											2019-03-14 20:08:54 +01:00
										 |  |  |       has_user_gesture, new ScriptExecutionCallback(std::move(promise))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return handle; | 
					
						
							| 
									
										
										
										
											2016-01-13 12:11:46 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-14 20:08:54 +01:00
										 |  |  | v8::Local<v8::Promise> ExecuteJavaScriptInIsolatedWorld( | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |     mate::Arguments* args, | 
					
						
							|  |  |  |     v8::Local<v8::Value> window, | 
					
						
							| 
									
										
										
										
											2017-12-19 10:42:36 +09:00
										 |  |  |     int world_id, | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |     const std::vector<mate::Dictionary>& scripts) { | 
					
						
							| 
									
										
										
										
											2019-03-14 20:08:54 +01:00
										 |  |  |   v8::Isolate* isolate = args->isolate(); | 
					
						
							|  |  |  |   util::Promise promise(isolate); | 
					
						
							|  |  |  |   v8::Local<v8::Promise> handle = promise.GetHandle(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-30 23:31:21 +02:00
										 |  |  |   std::vector<blink::WebScriptSource> sources; | 
					
						
							| 
									
										
										
										
											2017-11-15 11:14:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   for (const auto& script : scripts) { | 
					
						
							|  |  |  |     base::string16 code; | 
					
						
							|  |  |  |     base::string16 url; | 
					
						
							|  |  |  |     int start_line = 1; | 
					
						
							|  |  |  |     script.Get("url", &url); | 
					
						
							|  |  |  |     script.Get("startLine", &start_line); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!script.Get("code", &code)) { | 
					
						
							| 
									
										
										
										
											2019-03-14 20:08:54 +01:00
										 |  |  |       promise.RejectWithErrorMessage("Invalid 'code'"); | 
					
						
							|  |  |  |       return handle; | 
					
						
							| 
									
										
										
										
											2017-11-15 11:14:41 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-17 21:55:30 -04:00
										 |  |  |     sources.emplace_back( | 
					
						
							|  |  |  |         blink::WebScriptSource(blink::WebString::FromUTF16(code), | 
					
						
							|  |  |  |                                blink::WebURL(GURL(url)), start_line)); | 
					
						
							| 
									
										
										
										
											2017-11-15 11:14:41 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-08-30 23:31:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   bool has_user_gesture = false; | 
					
						
							|  |  |  |   args->GetNext(&has_user_gesture); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   blink::WebLocalFrame::ScriptExecutionType scriptExecutionType = | 
					
						
							| 
									
										
										
										
											2017-12-19 10:42:36 +09:00
										 |  |  |       blink::WebLocalFrame::kSynchronous; | 
					
						
							| 
									
										
										
										
											2017-08-30 23:31:21 +02:00
										 |  |  |   args->GetNext(&scriptExecutionType); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-26 18:55:12 +09:00
										 |  |  |   // Debugging tip: if you see a crash stack trace beginning from this call,
 | 
					
						
							|  |  |  |   // then it is very likely that some exception happened when executing the
 | 
					
						
							|  |  |  |   // "content_script/init.js" script.
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |   GetRenderFrame(window)->GetWebFrame()->RequestExecuteScriptInIsolatedWorld( | 
					
						
							| 
									
										
										
										
											2017-08-30 23:31:21 +02:00
										 |  |  |       world_id, &sources.front(), sources.size(), has_user_gesture, | 
					
						
							| 
									
										
										
										
											2019-03-14 20:08:54 +01:00
										 |  |  |       scriptExecutionType, new ScriptExecutionCallback(std::move(promise))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return handle; | 
					
						
							| 
									
										
										
										
											2017-08-30 23:31:21 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-13 10:05:28 -08:00
										 |  |  | void SetIsolatedWorldInfo(v8::Local<v8::Value> window, | 
					
						
							|  |  |  |                           int world_id, | 
					
						
							|  |  |  |                           const mate::Dictionary& options, | 
					
						
							|  |  |  |                           mate::Arguments* args) { | 
					
						
							| 
									
										
										
										
											2019-01-22 02:15:15 +05:30
										 |  |  |   std::string origin_url, security_policy, name; | 
					
						
							|  |  |  |   options.Get("securityOrigin", &origin_url); | 
					
						
							|  |  |  |   options.Get("csp", &security_policy); | 
					
						
							| 
									
										
										
										
											2019-02-13 10:05:28 -08:00
										 |  |  |   options.Get("name", &name); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-22 02:15:15 +05:30
										 |  |  |   if (!security_policy.empty() && origin_url.empty()) { | 
					
						
							| 
									
										
										
										
											2019-02-13 10:05:28 -08:00
										 |  |  |     args->ThrowError( | 
					
						
							|  |  |  |         "If csp is specified, securityOrigin should also be specified"); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-22 02:15:15 +05:30
										 |  |  |   blink::WebIsolatedWorldInfo info; | 
					
						
							|  |  |  |   info.security_origin = blink::WebSecurityOrigin::CreateFromString( | 
					
						
							|  |  |  |       blink::WebString::FromUTF8(origin_url)); | 
					
						
							|  |  |  |   info.content_security_policy = blink::WebString::FromUTF8(security_policy); | 
					
						
							|  |  |  |   info.human_readable_name = blink::WebString::FromUTF8(name); | 
					
						
							|  |  |  |   GetRenderFrame(window)->GetWebFrame()->SetIsolatedWorldInfo(world_id, info); | 
					
						
							| 
									
										
										
										
											2019-02-13 10:05:28 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | blink::WebCache::ResourceTypeStats GetResourceUsage(v8::Isolate* isolate) { | 
					
						
							| 
									
										
										
										
											2016-05-12 13:56:46 -07:00
										 |  |  |   blink::WebCache::ResourceTypeStats stats; | 
					
						
							| 
									
										
										
										
											2017-06-16 23:42:33 +03:00
										 |  |  |   blink::WebCache::GetResourceTypeStats(&stats); | 
					
						
							| 
									
										
										
										
											2016-05-13 10:43:08 -07:00
										 |  |  |   return stats; | 
					
						
							| 
									
										
										
										
											2016-05-12 13:56:46 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | void ClearCache(v8::Isolate* isolate) { | 
					
						
							| 
									
										
										
										
											2016-05-12 14:36:49 -07:00
										 |  |  |   isolate->IdleNotificationDeadline(0.5); | 
					
						
							| 
									
										
										
										
											2017-06-16 23:42:33 +03:00
										 |  |  |   blink::WebCache::Clear(); | 
					
						
							| 
									
										
										
										
											2016-05-12 14:36:49 -07:00
										 |  |  |   base::MemoryPressureListener::NotifyMemoryPressure( | 
					
						
							| 
									
										
										
										
											2018-04-17 21:55:30 -04:00
										 |  |  |       base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); | 
					
						
							| 
									
										
										
										
											2016-05-12 14:36:49 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | v8::Local<v8::Value> FindFrameByRoutingId(v8::Isolate* isolate, | 
					
						
							|  |  |  |                                           v8::Local<v8::Value> window, | 
					
						
							|  |  |  |                                           int routing_id) { | 
					
						
							|  |  |  |   content::RenderFrame* render_frame = | 
					
						
							|  |  |  |       content::RenderFrame::FromRoutingID(routing_id); | 
					
						
							|  |  |  |   if (render_frame) | 
					
						
							|  |  |  |     return render_frame->GetWebFrame()->MainWorldScriptContext()->Global(); | 
					
						
							| 
									
										
										
										
											2018-01-09 14:41:40 -05:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |     return v8::Null(isolate); | 
					
						
							| 
									
										
										
										
											2018-01-09 14:41:40 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | v8::Local<v8::Value> GetOpener(v8::Isolate* isolate, | 
					
						
							|  |  |  |                                v8::Local<v8::Value> window) { | 
					
						
							|  |  |  |   blink::WebFrame* frame = GetRenderFrame(window)->GetWebFrame()->Opener(); | 
					
						
							| 
									
										
										
										
											2018-01-09 14:41:40 -05:00
										 |  |  |   if (frame && frame->IsWebLocalFrame()) | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |     return frame->ToWebLocalFrame()->MainWorldScriptContext()->Global(); | 
					
						
							| 
									
										
										
										
											2018-01-09 14:41:40 -05:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |     return v8::Null(isolate); | 
					
						
							| 
									
										
										
										
											2018-01-09 14:41:40 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | // Don't name it as GetParent, Windows has API with same name.
 | 
					
						
							|  |  |  | v8::Local<v8::Value> GetFrameParent(v8::Isolate* isolate, | 
					
						
							|  |  |  |                                     v8::Local<v8::Value> window) { | 
					
						
							|  |  |  |   blink::WebFrame* frame = GetRenderFrame(window)->GetWebFrame()->Parent(); | 
					
						
							| 
									
										
										
										
											2018-01-09 14:41:40 -05:00
										 |  |  |   if (frame && frame->IsWebLocalFrame()) | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |     return frame->ToWebLocalFrame()->MainWorldScriptContext()->Global(); | 
					
						
							| 
									
										
										
										
											2018-01-09 14:41:40 -05:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |     return v8::Null(isolate); | 
					
						
							| 
									
										
										
										
											2018-01-09 14:41:40 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | v8::Local<v8::Value> GetTop(v8::Isolate* isolate, v8::Local<v8::Value> window) { | 
					
						
							|  |  |  |   blink::WebFrame* frame = GetRenderFrame(window)->GetWebFrame()->Top(); | 
					
						
							| 
									
										
										
										
											2018-01-09 14:41:40 -05:00
										 |  |  |   if (frame && frame->IsWebLocalFrame()) | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |     return frame->ToWebLocalFrame()->MainWorldScriptContext()->Global(); | 
					
						
							| 
									
										
										
										
											2018-01-09 14:41:40 -05:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |     return v8::Null(isolate); | 
					
						
							| 
									
										
										
										
											2018-01-09 14:41:40 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | v8::Local<v8::Value> GetFirstChild(v8::Isolate* isolate, | 
					
						
							|  |  |  |                                    v8::Local<v8::Value> window) { | 
					
						
							|  |  |  |   blink::WebFrame* frame = GetRenderFrame(window)->GetWebFrame()->FirstChild(); | 
					
						
							| 
									
										
										
										
											2018-01-09 14:41:40 -05:00
										 |  |  |   if (frame && frame->IsWebLocalFrame()) | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |     return frame->ToWebLocalFrame()->MainWorldScriptContext()->Global(); | 
					
						
							| 
									
										
										
										
											2018-01-09 14:41:40 -05:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |     return v8::Null(isolate); | 
					
						
							| 
									
										
										
										
											2018-01-09 14:41:40 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | v8::Local<v8::Value> GetNextSibling(v8::Isolate* isolate, | 
					
						
							|  |  |  |                                     v8::Local<v8::Value> window) { | 
					
						
							|  |  |  |   blink::WebFrame* frame = GetRenderFrame(window)->GetWebFrame()->NextSibling(); | 
					
						
							|  |  |  |   if (frame && frame->IsWebLocalFrame()) | 
					
						
							|  |  |  |     return frame->ToWebLocalFrame()->MainWorldScriptContext()->Global(); | 
					
						
							| 
									
										
										
										
											2018-01-09 14:41:40 -05:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |     return v8::Null(isolate); | 
					
						
							| 
									
										
										
										
											2018-01-09 14:41:40 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | v8::Local<v8::Value> GetFrameForSelector(v8::Isolate* isolate, | 
					
						
							|  |  |  |                                          v8::Local<v8::Value> window, | 
					
						
							|  |  |  |                                          const std::string& selector) { | 
					
						
							|  |  |  |   blink::WebElement element = | 
					
						
							|  |  |  |       GetRenderFrame(window)->GetWebFrame()->GetDocument().QuerySelector( | 
					
						
							|  |  |  |           blink::WebString::FromUTF8(selector)); | 
					
						
							|  |  |  |   if (element.IsNull())  // not found
 | 
					
						
							|  |  |  |     return v8::Null(isolate); | 
					
						
							| 
									
										
										
										
											2018-01-09 14:41:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |   blink::WebFrame* frame = blink::WebFrame::FromFrameOwnerElement(element); | 
					
						
							|  |  |  |   if (frame && frame->IsWebLocalFrame()) | 
					
						
							|  |  |  |     return frame->ToWebLocalFrame()->MainWorldScriptContext()->Global(); | 
					
						
							| 
									
										
										
										
											2018-04-26 06:17:55 -04:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |     return v8::Null(isolate); | 
					
						
							| 
									
										
										
										
											2018-04-26 06:17:55 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | v8::Local<v8::Value> FindFrameByName(v8::Isolate* isolate, | 
					
						
							|  |  |  |                                      v8::Local<v8::Value> window, | 
					
						
							|  |  |  |                                      const std::string& name) { | 
					
						
							|  |  |  |   blink::WebFrame* frame = | 
					
						
							|  |  |  |       GetRenderFrame(window)->GetWebFrame()->FindFrameByName( | 
					
						
							|  |  |  |           blink::WebString::FromUTF8(name)); | 
					
						
							|  |  |  |   if (frame && frame->IsWebLocalFrame()) | 
					
						
							|  |  |  |     return frame->ToWebLocalFrame()->MainWorldScriptContext()->Global(); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     return v8::Null(isolate); | 
					
						
							| 
									
										
										
										
											2018-04-26 06:17:55 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | int GetRoutingId(v8::Local<v8::Value> window) { | 
					
						
							|  |  |  |   return GetRenderFrame(window)->GetRoutingID(); | 
					
						
							| 
									
										
										
										
											2014-10-24 18:24:12 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  // namespace api
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-19 14:23:04 -07:00
										 |  |  | }  // namespace electron
 | 
					
						
							| 
									
										
										
										
											2014-10-24 18:24:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-17 21:55:30 -04:00
										 |  |  | void Initialize(v8::Local<v8::Object> exports, | 
					
						
							|  |  |  |                 v8::Local<v8::Value> unused, | 
					
						
							|  |  |  |                 v8::Local<v8::Context> context, | 
					
						
							|  |  |  |                 void* priv) { | 
					
						
							| 
									
										
										
										
											2019-06-19 14:23:04 -07:00
										 |  |  |   using namespace electron::api;  // NOLINT(build/namespaces)
 | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-24 18:24:12 +08:00
										 |  |  |   v8::Isolate* isolate = context->GetIsolate(); | 
					
						
							|  |  |  |   mate::Dictionary dict(isolate, exports); | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |   dict.SetMethod("setName", &SetName); | 
					
						
							|  |  |  |   dict.SetMethod("setZoomLevel", &SetZoomLevel); | 
					
						
							|  |  |  |   dict.SetMethod("getZoomLevel", &GetZoomLevel); | 
					
						
							|  |  |  |   dict.SetMethod("setZoomFactor", &SetZoomFactor); | 
					
						
							|  |  |  |   dict.SetMethod("getZoomFactor", &GetZoomFactor); | 
					
						
							|  |  |  |   dict.SetMethod("setVisualZoomLevelLimits", &SetVisualZoomLevelLimits); | 
					
						
							|  |  |  |   dict.SetMethod("setLayoutZoomLevelLimits", &SetLayoutZoomLevelLimits); | 
					
						
							|  |  |  |   dict.SetMethod("allowGuestViewElementDefinition", | 
					
						
							|  |  |  |                  &AllowGuestViewElementDefinition); | 
					
						
							|  |  |  |   dict.SetMethod("getWebFrameId", &GetWebFrameId); | 
					
						
							|  |  |  |   dict.SetMethod("setSpellCheckProvider", &SetSpellCheckProvider); | 
					
						
							|  |  |  |   dict.SetMethod("insertText", &InsertText); | 
					
						
							|  |  |  |   dict.SetMethod("insertCSS", &InsertCSS); | 
					
						
							| 
									
										
										
										
											2019-06-17 17:39:36 +02:00
										 |  |  |   dict.SetMethod("removeInsertedCSS", &RemoveInsertedCSS); | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |   dict.SetMethod("executeJavaScript", &ExecuteJavaScript); | 
					
						
							|  |  |  |   dict.SetMethod("executeJavaScriptInIsolatedWorld", | 
					
						
							|  |  |  |                  &ExecuteJavaScriptInIsolatedWorld); | 
					
						
							| 
									
										
										
										
											2019-02-13 10:05:28 -08:00
										 |  |  |   dict.SetMethod("setIsolatedWorldInfo", &SetIsolatedWorldInfo); | 
					
						
							| 
									
										
										
										
											2019-02-09 06:38:31 +09:00
										 |  |  |   dict.SetMethod("getResourceUsage", &GetResourceUsage); | 
					
						
							|  |  |  |   dict.SetMethod("clearCache", &ClearCache); | 
					
						
							|  |  |  |   dict.SetMethod("_findFrameByRoutingId", &FindFrameByRoutingId); | 
					
						
							|  |  |  |   dict.SetMethod("_getFrameForSelector", &GetFrameForSelector); | 
					
						
							|  |  |  |   dict.SetMethod("_findFrameByName", &FindFrameByName); | 
					
						
							|  |  |  |   dict.SetMethod("_getOpener", &GetOpener); | 
					
						
							|  |  |  |   dict.SetMethod("_getParent", &GetFrameParent); | 
					
						
							|  |  |  |   dict.SetMethod("_getTop", &GetTop); | 
					
						
							|  |  |  |   dict.SetMethod("_getFirstChild", &GetFirstChild); | 
					
						
							|  |  |  |   dict.SetMethod("_getNextSibling", &GetNextSibling); | 
					
						
							|  |  |  |   dict.SetMethod("_getRoutingId", &GetRoutingId); | 
					
						
							| 
									
										
										
										
											2014-10-24 18:24:12 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  // namespace
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-08 23:59:52 +05:30
										 |  |  | NODE_LINKED_MODULE_CONTEXT_AWARE(atom_renderer_web_frame, Initialize) |