Commit b6682f04 authored by Robbie McElrath's avatar Robbie McElrath Committed by Commit Bot

[WebLayer] Create Java BrowserContextHandle in //components/embedder_support.

This CL introduces a BrowserContextHandle interface that provides a
pointer to a native BrowserContext, and can be used to by components to
take a BrowserContext reference within Java code. Clank's Profile class
implements this interface, which allows us to pass a Profile to
components that need a BrowserContext. In the future, WebLayer's Profile
representation will also implement this interface.

This change was motivated by the componentization efforts underway as
part of the WebLayer project, several of which would benefit from a Java
representation of the BrowserContext.

Bug: 1058600
Change-Id: I65778791731c8d1f1529f06eab543774a60155ee
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2130674
Commit-Queue: Robbie McElrath <rmcelrath@chromium.org>
Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Reviewed-by: default avatarMihai Sardarescu <msarda@chromium.org>
Reviewed-by: default avatarBo <boliu@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Cr-Commit-Position: refs/heads/master@{#760540}
parent 3147e2a5
...@@ -12,6 +12,7 @@ android_library("java") { ...@@ -12,6 +12,7 @@ android_library("java") {
"//base:jni_java", "//base:jni_java",
"//chrome/browser/android/crypto:java", "//chrome/browser/android/crypto:java",
"//chrome/browser/preferences:java", "//chrome/browser/preferences:java",
"//components/embedder_support/android:browser_context_java",
"//content/public/android:content_java", "//content/public/android:content_java",
] ]
annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ] annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ]
......
...@@ -9,12 +9,13 @@ import androidx.annotation.VisibleForTesting; ...@@ -9,12 +9,13 @@ import androidx.annotation.VisibleForTesting;
import org.chromium.base.annotations.CalledByNative; import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.NativeMethods; import org.chromium.base.annotations.NativeMethods;
import org.chromium.chrome.browser.cookies.CookiesFetcher; import org.chromium.chrome.browser.cookies.CookiesFetcher;
import org.chromium.components.embedder_support.browser_context.BrowserContextHandle;
import org.chromium.content_public.browser.WebContents; import org.chromium.content_public.browser.WebContents;
/** /**
* Wrapper that allows passing a Profile reference around in the Java layer. * Wrapper that allows passing a Profile reference around in the Java layer.
*/ */
public class Profile { public class Profile implements BrowserContextHandle {
/** Whether this wrapper corresponds to an off the record Profile. */ /** Whether this wrapper corresponds to an off the record Profile. */
private final boolean mIsOffTheRecord; private final boolean mIsOffTheRecord;
...@@ -112,6 +113,11 @@ public class Profile { ...@@ -112,6 +113,11 @@ public class Profile {
return mNativeProfileAndroid != 0; return mNativeProfileAndroid != 0;
} }
@Override
public long getNativeBrowserContextPointer() {
return ProfileJni.get().getBrowserContextPointer(mNativeProfileAndroid);
}
@CalledByNative @CalledByNative
private static Profile create(long nativeProfileAndroid) { private static Profile create(long nativeProfileAndroid) {
return new Profile(nativeProfileAndroid); return new Profile(nativeProfileAndroid);
...@@ -145,5 +151,6 @@ public class Profile { ...@@ -145,5 +151,6 @@ public class Profile {
boolean isChild(long nativeProfileAndroid, Profile caller); boolean isChild(long nativeProfileAndroid, Profile caller);
void wipe(long nativeProfileAndroid, Profile caller); void wipe(long nativeProfileAndroid, Profile caller);
Object getProfileKey(long nativeProfileAndroid, Profile caller); Object getProfileKey(long nativeProfileAndroid, Profile caller);
long getBrowserContextPointer(long nativeProfileAndroid);
} }
} }
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "chrome/browser/profiles/profile_key.h" #include "chrome/browser/profiles/profile_key.h"
#include "chrome/browser/profiles/profile_key_android.h" #include "chrome/browser/profiles/profile_key_android.h"
#include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile_manager.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
using base::android::AttachCurrentThread; using base::android::AttachCurrentThread;
...@@ -122,6 +123,11 @@ void ProfileAndroid::Wipe(JNIEnv* env, const JavaParamRef<jobject>& obj) { ...@@ -122,6 +123,11 @@ void ProfileAndroid::Wipe(JNIEnv* env, const JavaParamRef<jobject>& obj) {
profile_->Wipe(); profile_->Wipe();
} }
jlong ProfileAndroid::GetBrowserContextPointer(JNIEnv* env) {
return reinterpret_cast<jlong>(
static_cast<content::BrowserContext*>(profile_));
}
// static // static
ScopedJavaLocalRef<jobject> JNI_Profile_GetLastUsedRegularProfile(JNIEnv* env) { ScopedJavaLocalRef<jobject> JNI_Profile_GetLastUsedRegularProfile(JNIEnv* env) {
return ProfileAndroid::GetLastUsedRegularProfile(env); return ProfileAndroid::GetLastUsedRegularProfile(env);
......
...@@ -61,6 +61,8 @@ class ProfileAndroid : public base::SupportsUserData::Data { ...@@ -61,6 +61,8 @@ class ProfileAndroid : public base::SupportsUserData::Data {
void Wipe(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj); void Wipe(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj);
jlong GetBrowserContextPointer(JNIEnv* env);
explicit ProfileAndroid(Profile* profile); explicit ProfileAndroid(Profile* profile);
~ProfileAndroid() override; ~ProfileAndroid() override;
......
...@@ -5,6 +5,27 @@ ...@@ -5,6 +5,27 @@
import("//build/config/android/rules.gni") import("//build/config/android/rules.gni")
import("//build/config/locales.gni") import("//build/config/locales.gni")
android_library("browser_context_java") {
sources = [ "java/src/org/chromium/components/embedder_support/browser_context/BrowserContextHandle.java" ]
deps = [ "//base:base_java" ]
}
generate_jni("browser_context_jni_headers") {
sources = [ "java/src/org/chromium/components/embedder_support/browser_context/BrowserContextHandle.java" ]
}
static_library("browser_context") {
sources = [
"browser_context/browser_context_handle.cc",
"browser_context/browser_context_handle.h",
]
deps = [
":browser_context_jni_headers",
"//base",
"//content/public/browser",
]
}
android_library("application_java") { android_library("application_java") {
deps = [ "//base:base_java" ] deps = [ "//base:base_java" ]
sources = [ sources = [
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/embedder_support/android/browser_context/browser_context_handle.h"
#include "base/android/jni_android.h"
#include "components/embedder_support/android/browser_context_jni_headers/BrowserContextHandle_jni.h"
#include "content/public/browser/browser_context.h"
using base::android::AttachCurrentThread;
using base::android::JavaRef;
namespace browser_context {
content::BrowserContext* BrowserContextFromJavaHandle(
const JavaRef<jobject>& jhandle) {
return reinterpret_cast<content::BrowserContext*>(
Java_BrowserContextHandle_getNativeBrowserContextPointer(
AttachCurrentThread(), jhandle));
}
} // namespace browser_context
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_EMBEDDER_SUPPORT_ANDROID_BROWSER_CONTEXT_BROWSER_CONTEXT_HANDLE_H_
#define COMPONENTS_EMBEDDER_SUPPORT_ANDROID_BROWSER_CONTEXT_BROWSER_CONTEXT_HANDLE_H_
#include <jni.h>
#include "base/android/scoped_java_ref.h"
namespace content {
class BrowserContext;
}
namespace browser_context {
// Returns a pointer to the native BrowserContext wrapped by the given Java
// BrowserContextHandle reference.
content::BrowserContext* BrowserContextFromJavaHandle(
const base::android::JavaRef<jobject>& jhandle);
} // namespace browser_context
#endif // COMPONENTS_EMBEDDER_SUPPORT_ANDROID_BROWSER_CONTEXT_BROWSER_CONTEXT_HANDLE_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.components.embedder_support.browser_context;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
/**
* An interface that provides access to a native BrowserContext.
*/
@JNINamespace("browser_context")
public interface BrowserContextHandle {
/** @return A pointer to the native BrowserContext that this object wraps. */
@CalledByNative
long getNativeBrowserContextPointer();
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment