Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moves backup results function to service workers #8493

Merged
merged 4 commits into from
Apr 13, 2021

Conversation

SergeyZhukovsky
Copy link
Member

Resolves brave/brave-browser#15217

Submitter Checklist:

  • I confirm that no security/privacy review is needed, or that I have requested one
  • There is a ticket for my issue
  • Used Github auto-closing keywords in the PR description above
  • Wrote a good PR/commit description
  • Added appropriate labels (QA/Yes or QA/No; release-notes/include or release-notes/exclude; OS/...) to the associated issue
  • Checked the PR locally: npm run test -- brave_browser_tests, npm run test -- brave_unit_tests, npm run lint, npm run gn_check, npm run tslint
  • Ran git rebase master (if needed)

Reviewer Checklist:

  • A security review is not needed, or a link to one is included in the PR description
  • New files have MPL-2.0 license header
  • Adequate test coverage exists to prevent regressions
  • Major classes, functions and non-trivial code blocks are well-commented
  • Changes in component dependencies are properly reflected in gn
  • Code follows the style guide
  • Test plan is specified in PR before merging

After-merge Checklist:

Test Plan:

const GURL& script_url);

private:
base::ThreadLocalPointer<std::vector<std::unique_ptr<BraveSearchJSHandler>>>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this a thread local?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a comment explaining that WillEvaluateServiceWorkerOnWorkerThread and WillDestroyServiceWorkerContextOnWorkerThread are called from the worker thread so this makes accessing the list thread safe?

int64_t service_worker_version_id,
const GURL& service_worker_scope,
const GURL& script_url) {
brave_search::BraveSearchSWHolder::GetInstance()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why isn't this class just owned by BraveContentRendererClient?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed it to be a member

void PopulateBinderMap(ServiceWorkerHost* host, mojo::BinderMap* map) {
DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
PopulateServiceWorkerBinders(host, map);
+ PopulateServiceWorkerBindersBrave(host, map);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not an extensible patch, it should BRAVE_POPULATE_BINDER_MAP


class BraveSearchJSHandler;

class BraveSearchSWHolder {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this name is a bit confusing to me, maybe BraveSearchServiceWorkerHandler? Using JS is ok because that's used more commonly than Javascript, but SW is not immediately obvious and in general abbreviations should be avoided unless it's to follow existing naming conventions, but in this case the convention is to use ServiceWorker

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will rename it in a follow up with tests

@@ -1,29 +1,27 @@
# common includes which can help minimize patches for
# src/third_party/blink/renderer/core/BUILD.gn
brave_blink_renderer_core_visibility = [
"//brave/third_party/blink/renderer/*"
"//brave/third_party/blink/renderer/*",
"//brave/components/brave_search/renderer:renderer",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this being added to blink visibility? Is this maybe leftover from previous patching?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redid the mojo and removed it from here

@@ -6,7 +6,7 @@
#include "brave/renderer/brave_content_renderer_client.h"

#include "base/feature_list.h"
#include "brave/components/brave_search/renderer/brave_search_render_frame_observer.h"
#include "brave/components/brave_search/renderer/brave_search_sw_holder.h"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing a dep for this in brave_chrome_renderer_public_deps

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

brave_chrome_renderer_public_deps = [
"//brave/components/brave_search/renderer",

if (observer)
observer->RunScriptsAtDocumentStart();

ChromeContentRendererClient::RunScriptsAtDocumentStart(render_frame);
}

void BraveContentRendererClient::WillEvaluateServiceWorkerOnWorkerThread(
blink::WebServiceWorkerContextProxy* context_proxy,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing includes for this and GURL which are both forward decls in the header

v8::Local<v8::Context> context) {
v8::Isolate* isolate = blink::MainThreadIsolate();
v8::Local<v8::Context> BraveSearchJSHandler::Context() {
return v8::Local<v8::Context>::New(isolate_, *context_);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could potentially be null if it is called before AddJavaScriptObject. You can pass the v8::Context into the BraveSearchJSHandler constructor because you have it in WillEvaluateServiceWorkerOnWorkerThread

v8::Local<v8::Object> javascript_object,
const std::string& name,
const base::RepeatingCallback<Sig>& callback) {
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you already have the context in context_

return;

std::unique_ptr<BraveSearchJSHandler> js_handler(new BraveSearchJSHandler());
js_handler->AddJavaScriptObject(v8_context);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just pass the v8_context into the BraveSearchJSHandler as mentioned above

v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = context_old->Get(isolate);
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context = context_->Get(isolate_);
v8::Context::Scope context_scope(context);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you also want v8::MicrotasksScope microtasks(isolate(), v8::MicrotasksScope::kDoNotRunMicrotasks); here.

// Brave-specific: allows the embedder to modify the referrer string
// according to user preferences.
#define BRAVE_CONTENT_BROWSER_CLIENT_H \
virtual void MaybeHideReferrer( \
BrowserContext* browser_context, const GURL& request_url, \
const GURL& document_url, blink::mojom::ReferrerPtr* referrer) {} \
virtual std::string GetEffectiveUserAgent(BrowserContext* browser_context, \
const GURL& url);
const GURL& url); \
virtual void RegisterBrowserInterfaceBindersForHost( \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't needed anymore

const std::string& response);

content::RenderFrame* render_frame_;
blink::ThreadSafeBrowserInterfaceBrokerProxy* broker_;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// not owned

@@ -46,6 +47,8 @@ void BraveContentRendererClient::RenderThreadStarted() {

brave_observer_ = std::make_unique<BraveRenderThreadObserver>();
content::RenderThread::Get()->AddObserver(brave_observer_.get());
brave_search_sw_holder_.InitBrowserInterfaceBrokerProxy(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I was thinking this would be a unique_ptr and you would initialize it here with browser_interface_broker_, but this is ok too. I would, however, change the name because Init has special meaning for mojo interface so maybe just set_browser_interface_broker?

int64_t service_worker_version_id,
const GURL& service_worker_scope,
const GURL& script_url) {
if (!service_worker_scope.is_valid() ||
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should also DCHECK broker_ here if we're not initializing this object with it

@@ -1,29 +1,25 @@
# common includes which can help minimize patches for
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can probably just drop these changes? Also fine if you leave them I guess, but you don't have to fix the lint errors if you don't touch it :)

@SergeyZhukovsky SergeyZhukovsky merged commit 5690893 into master Apr 13, 2021
@SergeyZhukovsky SergeyZhukovsky deleted the backup_results_sw branch April 13, 2021 14:06
@SergeyZhukovsky SergeyZhukovsky added this to the 1.25.x - Nightly milestone Apr 13, 2021
bbondy pushed a commit that referenced this pull request Apr 16, 2021
Moves backup results function to service workers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants