|
| 1 | +/* Copyright (c) 2021 The Brave Authors. All rights reserved. |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 4 | + * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 | + |
| 6 | +#include "brave/components/brave_ads/renderer/brave_ads_js_handler.h" |
| 7 | + |
| 8 | +#include <utility> |
| 9 | + |
| 10 | +#include "base/no_destructor.h" |
| 11 | +#include "content/public/renderer/render_frame.h" |
| 12 | +#include "gin/arguments.h" |
| 13 | +#include "gin/function_template.h" |
| 14 | +#include "third_party/blink/public/common/browser_interface_broker_proxy.h" |
| 15 | +#include "third_party/blink/public/common/thread_safe_browser_interface_broker_proxy.h" |
| 16 | +#include "third_party/blink/public/platform/web_string.h" |
| 17 | +#include "third_party/blink/public/web/blink.h" |
| 18 | +#include "third_party/blink/public/web/web_console_message.h" |
| 19 | +#include "third_party/blink/public/web/web_local_frame.h" |
| 20 | +#include "third_party/blink/public/web/web_script_source.h" |
| 21 | + |
| 22 | +namespace brave_ads { |
| 23 | + |
| 24 | +namespace { |
| 25 | + |
| 26 | +constexpr char kUserGestureRejectReason[] = |
| 27 | + "braveRequestAdsEnabled: API can only be initiated by a user gesture."; |
| 28 | + |
| 29 | +} // namespace |
| 30 | + |
| 31 | +BraveAdsJSHandler::BraveAdsJSHandler(content::RenderFrame* render_frame) |
| 32 | + : render_frame_(render_frame) {} |
| 33 | + |
| 34 | +BraveAdsJSHandler::~BraveAdsJSHandler() = default; |
| 35 | + |
| 36 | +void BraveAdsJSHandler::AddJavaScriptObjectToFrame( |
| 37 | + v8::Local<v8::Context> context) { |
| 38 | + v8::Isolate* isolate = blink::MainThreadIsolate(); |
| 39 | + v8::HandleScope handle_scope(isolate); |
| 40 | + if (context.IsEmpty()) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + v8::Context::Scope context_scope(context); |
| 45 | + |
| 46 | + BindFunctionsToObject(isolate, context); |
| 47 | +} |
| 48 | + |
| 49 | +void BraveAdsJSHandler::BindFunctionsToObject(v8::Isolate* isolate, |
| 50 | + v8::Local<v8::Context> context) { |
| 51 | + v8::Local<v8::Object> global = context->Global(); |
| 52 | + v8::Local<v8::Object> chrome_obj; |
| 53 | + v8::Local<v8::Value> chrome_value; |
| 54 | + if (!global->Get(context, gin::StringToV8(isolate, "chrome")) |
| 55 | + .ToLocal(&chrome_value) || |
| 56 | + !chrome_value->IsObject()) { |
| 57 | + chrome_obj = v8::Object::New(isolate); |
| 58 | + global->Set(context, gin::StringToSymbol(isolate, "chrome"), chrome_obj) |
| 59 | + .Check(); |
| 60 | + } else { |
| 61 | + chrome_obj = chrome_value->ToObject(context).ToLocalChecked(); |
| 62 | + } |
| 63 | + |
| 64 | + BindFunctionToObject( |
| 65 | + isolate, chrome_obj, "braveRequestAdsEnabled", |
| 66 | + base::BindRepeating(&BraveAdsJSHandler::RequestAdsEnabled, |
| 67 | + base::Unretained(this))); |
| 68 | +} |
| 69 | + |
| 70 | +template <typename Sig> |
| 71 | +void BraveAdsJSHandler::BindFunctionToObject( |
| 72 | + v8::Isolate* isolate, |
| 73 | + v8::Local<v8::Object> javascript_object, |
| 74 | + const std::string& name, |
| 75 | + const base::RepeatingCallback<Sig>& callback) { |
| 76 | + v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
| 77 | + javascript_object |
| 78 | + ->Set(context, gin::StringToSymbol(isolate, name), |
| 79 | + gin::CreateFunctionTemplate(isolate, callback) |
| 80 | + ->GetFunction(context) |
| 81 | + .ToLocalChecked()) |
| 82 | + .Check(); |
| 83 | +} |
| 84 | + |
| 85 | +bool BraveAdsJSHandler::EnsureConnected() { |
| 86 | + if (!brave_ads_.is_bound()) { |
| 87 | + render_frame_->GetBrowserInterfaceBroker()->GetInterface( |
| 88 | + brave_ads_.BindNewPipeAndPassReceiver()); |
| 89 | + brave_ads_.set_disconnect_handler(base::BindOnce( |
| 90 | + &BraveAdsJSHandler::OnRemoteDisconnect, base::Unretained(this))); |
| 91 | + } |
| 92 | + |
| 93 | + return brave_ads_.is_bound(); |
| 94 | +} |
| 95 | + |
| 96 | +void BraveAdsJSHandler::OnRemoteDisconnect() { |
| 97 | + brave_ads_.reset(); |
| 98 | + EnsureConnected(); |
| 99 | +} |
| 100 | + |
| 101 | +v8::Local<v8::Promise> BraveAdsJSHandler::RequestAdsEnabled( |
| 102 | + v8::Isolate* isolate) { |
| 103 | + if (!EnsureConnected()) { |
| 104 | + return v8::Local<v8::Promise>(); |
| 105 | + } |
| 106 | + |
| 107 | + v8::MaybeLocal<v8::Promise::Resolver> maybe_resolver = |
| 108 | + v8::Promise::Resolver::New(isolate->GetCurrentContext()); |
| 109 | + if (maybe_resolver.IsEmpty()) { |
| 110 | + return v8::Local<v8::Promise>(); |
| 111 | + } |
| 112 | + |
| 113 | + v8::Local<v8::Promise::Resolver> resolver = maybe_resolver.ToLocalChecked(); |
| 114 | + |
| 115 | + auto* web_frame = render_frame_->GetWebFrame(); |
| 116 | + DCHECK(web_frame); |
| 117 | + if (!web_frame->HasTransientUserActivation()) { |
| 118 | + v8::Local<v8::String> result = |
| 119 | + v8::String::NewFromUtf8(isolate, kUserGestureRejectReason) |
| 120 | + .ToLocalChecked(); |
| 121 | + ALLOW_UNUSED_LOCAL(resolver->Reject(isolate->GetCurrentContext(), result)); |
| 122 | + return resolver->GetPromise(); |
| 123 | + } |
| 124 | + |
| 125 | + auto promise_resolver = std::make_unique<v8::Global<v8::Promise::Resolver>>(); |
| 126 | + promise_resolver->Reset(isolate, resolver); |
| 127 | + auto context_old = std::make_unique<v8::Global<v8::Context>>( |
| 128 | + isolate, isolate->GetCurrentContext()); |
| 129 | + brave_ads_->RequestAdsEnabled(base::BindOnce( |
| 130 | + &BraveAdsJSHandler::OnRequestAdsEnabled, base::Unretained(this), |
| 131 | + std::move(promise_resolver), isolate, std::move(context_old))); |
| 132 | + |
| 133 | + return resolver->GetPromise(); |
| 134 | +} |
| 135 | + |
| 136 | +void BraveAdsJSHandler::OnRequestAdsEnabled( |
| 137 | + std::unique_ptr<v8::Global<v8::Promise::Resolver>> promise_resolver, |
| 138 | + v8::Isolate* isolate, |
| 139 | + std::unique_ptr<v8::Global<v8::Context>> context_old, |
| 140 | + bool response) { |
| 141 | + v8::HandleScope handle_scope(isolate); |
| 142 | + v8::Local<v8::Context> context = context_old->Get(isolate); |
| 143 | + v8::Context::Scope context_scope(context); |
| 144 | + v8::MicrotasksScope microtasks(isolate, |
| 145 | + v8::MicrotasksScope::kDoNotRunMicrotasks); |
| 146 | + |
| 147 | + v8::Local<v8::Promise::Resolver> resolver = promise_resolver->Get(isolate); |
| 148 | + v8::Local<v8::Boolean> result = v8::Boolean::New(isolate, response); |
| 149 | + |
| 150 | + ALLOW_UNUSED_LOCAL(resolver->Resolve(context, result)); |
| 151 | +} |
| 152 | + |
| 153 | +} // namespace brave_ads |
0 commit comments