OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/supervised_user_error_page/gin_wrapper.h" |
| 6 |
| 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "content/public/renderer/render_frame.h" |
| 9 #include "gin/handle.h" |
| 10 #include "gin/object_template_builder.h" |
| 11 #include "third_party/WebKit/public/web/WebKit.h" |
| 12 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 13 |
| 14 using web_restrictions::mojom::WebRestrictionsPtr; |
| 15 |
| 16 namespace supervised_user_error_page { |
| 17 |
| 18 GinWrapper::Loader::Loader( |
| 19 content::RenderFrame* render_frame, |
| 20 const std::string& url, |
| 21 const web_restrictions::mojom::WebRestrictionsPtr& web_restrictions_service) |
| 22 : content::RenderFrameObserver(render_frame), |
| 23 url_(url), |
| 24 web_restrictions_service_(web_restrictions_service) {} |
| 25 |
| 26 void GinWrapper::Loader::DidClearWindowObject() { |
| 27 |
| 28 InstallGinWrapper(); |
| 29 |
| 30 // Once the gin wrapper has been installed we don't need to observe the |
| 31 // render frame. Delete the loader so that the wrapper isn't re-installed when |
| 32 // something else is loaded into the frame. |
| 33 delete this; |
| 34 } |
| 35 |
| 36 void GinWrapper::Loader::InstallGinWrapper() { |
| 37 v8::Isolate* isolate = blink::mainThreadIsolate(); |
| 38 v8::HandleScope handle_scope(isolate); |
| 39 v8::Local<v8::Context> context = |
| 40 render_frame()->GetWebFrame()->mainWorldScriptContext(); |
| 41 if (context.IsEmpty()) |
| 42 return; |
| 43 v8::Context::Scope context_scope(context); |
| 44 gin::Handle<GinWrapper> controller = gin::CreateHandle( |
| 45 isolate, new GinWrapper(render_frame(), url_, web_restrictions_service_)); |
| 46 if (controller.IsEmpty()) |
| 47 return; |
| 48 v8::Local<v8::Object> global = context->Global(); |
| 49 global->Set(gin::StringToV8(isolate, "webRestrictions"), controller.ToV8()); |
| 50 } |
| 51 |
| 52 void GinWrapper::Loader::OnDestruct() { |
| 53 delete this; |
| 54 } |
| 55 |
| 56 gin::WrapperInfo GinWrapper::kWrapperInfo = {gin::kEmbedderNativeGin}; |
| 57 |
| 58 // static |
| 59 void GinWrapper::InstallWhenFrameReady( |
| 60 content::RenderFrame* render_frame, |
| 61 const std::string& url, |
| 62 const WebRestrictionsPtr& web_restrictions_service) { |
| 63 new Loader(render_frame, url, web_restrictions_service); |
| 64 } |
| 65 |
| 66 GinWrapper::GinWrapper(content::RenderFrame* render_frame, |
| 67 const std::string& url, |
| 68 const WebRestrictionsPtr& web_restrictions_service) |
| 69 : url_(url), |
| 70 web_restrictions_service_(web_restrictions_service), |
| 71 weak_ptr_factory_(this) {} |
| 72 |
| 73 GinWrapper::~GinWrapper() {} |
| 74 |
| 75 bool GinWrapper::RequestPermission( |
| 76 v8::Local<v8::Function> setRequestStatusCallback) { |
| 77 setRequestStatusCallback_.Reset(blink::mainThreadIsolate(), |
| 78 setRequestStatusCallback); |
| 79 web_restrictions_service_->RequestPermission( |
| 80 url_, base::Bind(&GinWrapper::OnAccessRequestAdded, |
| 81 weak_ptr_factory_.GetWeakPtr())); |
| 82 return true; |
| 83 } |
| 84 |
| 85 void GinWrapper::OnAccessRequestAdded(bool success) { |
| 86 if (setRequestStatusCallback_.IsEmpty()) { |
| 87 return; |
| 88 } |
| 89 |
| 90 v8::Isolate* isolate = blink::mainThreadIsolate(); |
| 91 v8::Local<v8::Value> args = v8::Boolean::New(isolate, success); |
| 92 v8::HandleScope handle_scope(isolate); |
| 93 v8::Local<v8::Function> callback = |
| 94 v8::Local<v8::Function>::New(isolate, setRequestStatusCallback_); |
| 95 v8::Local<v8::Context> context = callback->CreationContext(); |
| 96 if (context.IsEmpty()) |
| 97 return; |
| 98 |
| 99 v8::Context::Scope context_scope(context); |
| 100 v8::MicrotasksScope microtasks(isolate, |
| 101 v8::MicrotasksScope::kDoNotRunMicrotasks); |
| 102 |
| 103 callback->Call(context->Global(), 1, &args); |
| 104 } |
| 105 |
| 106 gin::ObjectTemplateBuilder GinWrapper::GetObjectTemplateBuilder( |
| 107 v8::Isolate* isolate) { |
| 108 return gin::Wrappable<GinWrapper>::GetObjectTemplateBuilder(isolate) |
| 109 .SetMethod("requestPermission", &GinWrapper::RequestPermission); |
| 110 } |
| 111 |
| 112 } // namespace web_restrictions |
OLD | NEW |