Chromium Code Reviews| 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 v8::Isolate* isolate = blink::mainThreadIsolate(); | |
| 28 v8::HandleScope handle_scope(isolate); | |
| 29 v8::Local<v8::Context> context = | |
| 30 render_frame()->GetWebFrame()->mainWorldScriptContext(); | |
| 31 if (context.IsEmpty()) | |
| 32 return; | |
| 33 v8::Context::Scope context_scope(context); | |
| 34 gin::Handle<GinWrapper> controller = gin::CreateHandle( | |
| 35 isolate, new GinWrapper(render_frame(), url_, web_restrictions_service_)); | |
| 36 if (controller.IsEmpty()) | |
| 37 return; | |
| 38 v8::Local<v8::Object> global = context->Global(); | |
| 39 global->Set(gin::StringToV8(isolate, "webRestrictions"), controller.ToV8()); | |
| 40 | |
| 41 // Once the gin wrapper has been installed we don't need to observe the | |
| 42 // render frame. Remove it so that the wrapper doesn't re-installed when | |
|
Bernhard Bauer
2016/05/19 10:00:15
Nit: "reinstalls itself" or "doesn't get reinstall
aberent
2016/05/19 15:44:03
Done (different wording).
| |
| 43 // something else is loaded into the frame. | |
| 44 delete this; | |
| 45 } | |
| 46 | |
| 47 gin::WrapperInfo GinWrapper::kWrapperInfo = {gin::kEmbedderNativeGin}; | |
| 48 | |
| 49 // static | |
| 50 void GinWrapper::InstallWhenFrameReady( | |
| 51 content::RenderFrame* render_frame, | |
| 52 const std::string& url, | |
| 53 const WebRestrictionsPtr& web_restrictions_service) { | |
| 54 new Loader(render_frame, url, web_restrictions_service); | |
| 55 } | |
| 56 | |
| 57 GinWrapper::GinWrapper(content::RenderFrame* render_frame, | |
| 58 const std::string& url, | |
| 59 const WebRestrictionsPtr& web_restrictions_service) | |
| 60 : url_(url), | |
| 61 web_restrictions_service_(web_restrictions_service), | |
| 62 weak_ptr_factory_(this) {} | |
| 63 | |
| 64 GinWrapper::~GinWrapper() {} | |
| 65 | |
| 66 bool GinWrapper::RequestPermission( | |
| 67 v8::Local<v8::Function> setRequestStatusCallback) { | |
| 68 setRequestStatusCallback_.Reset(blink::mainThreadIsolate(), | |
| 69 setRequestStatusCallback); | |
| 70 web_restrictions_service_->RequestPermission( | |
| 71 url_, base::Bind(&GinWrapper::OnAccessRequestAdded, | |
| 72 weak_ptr_factory_.GetWeakPtr())); | |
| 73 return true; | |
| 74 } | |
| 75 | |
| 76 void GinWrapper::OnAccessRequestAdded(bool success) { | |
| 77 if (setRequestStatusCallback_.IsEmpty()) { | |
| 78 return; | |
| 79 } | |
| 80 | |
| 81 v8::Isolate* isolate = blink::mainThreadIsolate(); | |
| 82 v8::Local<v8::Value> args = v8::Boolean::New(isolate, success); | |
| 83 v8::HandleScope handle_scope(isolate); | |
| 84 v8::Local<v8::Function> callback = | |
| 85 v8::Local<v8::Function>::New(isolate, setRequestStatusCallback_); | |
| 86 v8::Local<v8::Context> context = callback->CreationContext(); | |
| 87 if (context.IsEmpty()) | |
| 88 return; | |
| 89 | |
| 90 v8::Context::Scope context_scope(context); | |
| 91 v8::MicrotasksScope microtasks(isolate, | |
| 92 v8::MicrotasksScope::kDoNotRunMicrotasks); | |
| 93 | |
| 94 callback->Call(context->Global(), 1, &args); | |
| 95 } | |
| 96 | |
| 97 gin::ObjectTemplateBuilder GinWrapper::GetObjectTemplateBuilder( | |
| 98 v8::Isolate* isolate) { | |
| 99 return gin::Wrappable<GinWrapper>::GetObjectTemplateBuilder(isolate) | |
| 100 .SetMethod("requestPermission", &GinWrapper::RequestPermission); | |
| 101 } | |
| 102 | |
| 103 } // namespace web_restrictions | |
| OLD | NEW |