Commit f9ef8089 authored by Roger McFarlane's avatar Roger McFarlane Committed by Commit Bot

Plumb account name and experiment ids through xsurface

Change-Id: Idbefd3b8758806d8fd3f93b9ef142a589d210069
Bug: 1073600
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2228449
Commit-Queue: Roger McFarlane <rogerm@chromium.org>
Reviewed-by: default avatarAlexei Svitkine <asvitkine@chromium.org>
Reviewed-by: default avatarJustin DeWitt <dewittj@chromium.org>
Reviewed-by: default avatarDan H <harringtond@chromium.org>
Cr-Commit-Position: refs/heads/master@{#777945}
parent 62813886
......@@ -21,6 +21,7 @@ import org.chromium.base.annotations.NativeMethods;
import org.chromium.chrome.browser.AppHooks;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.native_page.NativePageNavigationDelegate;
import org.chromium.chrome.browser.signin.IdentityServicesProvider;
import org.chromium.chrome.browser.suggestions.SuggestionsConfig;
import org.chromium.chrome.browser.tab.EmptyTabObserver;
import org.chromium.chrome.browser.tab.Tab;
......@@ -39,6 +40,8 @@ import org.chromium.components.feed.proto.FeedUiProto.Slice.SliceDataCase;
import org.chromium.components.feed.proto.FeedUiProto.StreamUpdate;
import org.chromium.components.feed.proto.FeedUiProto.StreamUpdate.SliceUpdate;
import org.chromium.components.feed.proto.FeedUiProto.StreamUpdate.SliceUpdate.UpdateCase;
import org.chromium.components.signin.base.CoreAccountInfo;
import org.chromium.components.signin.identitymanager.ConsentLevel;
import org.chromium.content_public.browser.LoadUrlParams;
import org.chromium.content_public.common.Referrer;
import org.chromium.network.mojom.ReferrerPolicy;
......@@ -80,17 +83,40 @@ public class FeedStreamSurface implements SurfaceActionsHandler, FeedActionsHand
public static ProcessScope xSurfaceProcessScope() {
if (sXSurfaceProcessScope == null) {
sXSurfaceProcessScope =
AppHooks.get().getExternalSurfaceProcessScope(new SurfaceDependencyProvider() {
@Override
public Context getContext() {
return ContextUtils.getApplicationContext();
}
});
sXSurfaceProcessScope = AppHooks.get().getExternalSurfaceProcessScope(
new FeedSurfaceDependencyProvider());
}
return sXSurfaceProcessScope;
}
/**
* Provides logging and context for all surfaces.
*
* TODO(rogerm): Find a more global home for this.
* TODO(rogerm): implement getClientInstanceId.
*/
private static class FeedSurfaceDependencyProvider implements SurfaceDependencyProvider {
FeedSurfaceDependencyProvider() {}
@Override
public Context getContext() {
return ContextUtils.getApplicationContext();
}
@Override
public String getAccountName() {
CoreAccountInfo primaryAccount =
IdentityServicesProvider.get().getIdentityManager().getPrimaryAccountInfo(
ConsentLevel.NOT_REQUIRED);
return primaryAccount == null ? "" : primaryAccount.getEmail();
}
@Override
public int[] getExperimentIds() {
return FeedStreamSurfaceJni.get().getExperimentIds();
}
}
/**
* A {@link TabObserver} that observes navigation related events that originate from Feed
* interactions. Calls reportPageLoaded when navigation completes.
......@@ -144,7 +170,7 @@ public class FeedStreamSurface implements SurfaceActionsHandler, FeedActionsHand
ProcessScope processScope = xSurfaceProcessScope();
if (processScope != null) {
mSurfaceScope = xSurfaceProcessScope().obtainSurfaceScope(mActivity);
mSurfaceScope = processScope.obtainSurfaceScope(mActivity);
} else {
mSurfaceScope = null;
}
......@@ -424,6 +450,7 @@ public class FeedStreamSurface implements SurfaceActionsHandler, FeedActionsHand
@NativeMethods
interface Natives {
long init(FeedStreamSurface caller);
int[] getExperimentIds();
// TODO(jianli): Call this function at the appropriate time.
void reportSliceViewed(
long nativeFeedStreamSurface, FeedStreamSurface caller, String sliceId);
......
......@@ -18,6 +18,7 @@
#include "components/feed/core/proto/v2/ui.pb.h"
#include "components/feed/core/v2/public/feed_service.h"
#include "components/feed/core/v2/public/feed_stream_api.h"
#include "components/variations/variations_http_header_provider.h"
using base::android::JavaParamRef;
using base::android::JavaRef;
......@@ -31,6 +32,17 @@ static jlong JNI_FeedStreamSurface_Init(JNIEnv* env,
return reinterpret_cast<intptr_t>(new FeedStreamSurface(j_this));
}
static base::android::ScopedJavaLocalRef<jintArray>
JNI_FeedStreamSurface_GetExperimentIds(JNIEnv* env) {
auto* variations_http_header_provider =
variations::VariationsHttpHeaderProvider::GetInstance();
DCHECK(variations_http_header_provider != nullptr);
return base::android::ToJavaIntArray(
env, variations_http_header_provider
->GetVariationsVectorForWebPropertiesKeys());
}
FeedStreamSurface::FeedStreamSurface(const JavaRef<jobject>& j_this)
: feed_stream_api_(nullptr) {
java_ref_.Reset(j_this);
......
......@@ -18,6 +18,21 @@ public interface SurfaceDependencyProvider {
return null;
}
/** Returns the account name of the signed-in user, or the empty string. */
default String getAccountName() {
return "";
}
/** Returns the client instance id for this chrome. */
default String getClientInstanceId() {
return "";
}
/** Returns the collection of currently active experiment ids. */
default int[] getExperimentIds() {
return new int[0];
}
/** @see {Log.e} */
default void logError(String tag, String messageTemplate, Object... args) {}
......
......@@ -6,6 +6,7 @@
#include <stddef.h>
#include <algorithm>
#include <set>
#include <string>
#include <vector>
......@@ -85,6 +86,11 @@ std::string VariationsHttpHeaderProvider::GetVariationsString() {
std::vector<VariationID> VariationsHttpHeaderProvider::GetVariationsVector(
IDCollectionKey key) {
return GetVariationsVector(std::set<IDCollectionKey>{key});
}
std::vector<VariationID> VariationsHttpHeaderProvider::GetVariationsVector(
const std::set<IDCollectionKey>& keys) {
InitVariationIDsCacheIfNeeded();
// Get all the active variation ids while holding the lock.
......@@ -94,17 +100,29 @@ std::vector<VariationID> VariationsHttpHeaderProvider::GetVariationsVector(
all_variation_ids = GetAllVariationIds();
}
// Copy the requested variations to the output vector. Note that the ids will
// be in sorted order because they're coming from a std::set.
// Copy the requested variations to the output vector.
std::vector<VariationID> result;
result.reserve(all_variation_ids.size());
for (const VariationIDEntry& entry : all_variation_ids) {
if (entry.second == key)
if (keys.find(entry.second) != keys.end())
result.push_back(entry.first);
}
// Make sure each enry is unique. As a side-effect, the output will be sorted.
std::sort(result.begin(), result.end());
result.erase(std::unique(result.begin(), result.end()), result.end());
return result;
}
std::vector<VariationID>
VariationsHttpHeaderProvider::GetVariationsVectorForWebPropertiesKeys() {
const std::set<IDCollectionKey> web_properties_keys{
variations::GOOGLE_WEB_PROPERTIES,
variations::GOOGLE_WEB_PROPERTIES_SIGNED_IN,
variations::GOOGLE_WEB_PROPERTIES_TRIGGER};
return GetVariationsVector(web_properties_keys);
}
VariationsHttpHeaderProvider::ForceIdsResult
VariationsHttpHeaderProvider::ForceVariationIds(
const std::vector<std::string>& variation_ids,
......
......@@ -64,10 +64,20 @@ class VariationsHttpHeaderProvider : public base::FieldTrialList::Observer,
// apps.
std::string GetGoogleAppVariationsString();
// Returns the collection of of variation ids matching the given |key|. Each
// Returns the collection of variation ids matching the given |key|. Each
// entry in the returned vector will be unique.
std::vector<VariationID> GetVariationsVector(IDCollectionKey key);
// Returns the collection of variation ids matching any of the given
// |keys|. Each entry in the returned vector will be unique.
// TODO(rogerm): rename/move to private impl function.
std::vector<VariationID> GetVariationsVector(
const std::set<IDCollectionKey>& key);
// Returns the collection of variations ids for all Google Web Properties
// related keys.
std::vector<VariationID> GetVariationsVectorForWebPropertiesKeys();
// Result of ForceVariationIds() call.
enum class ForceIdsResult {
SUCCESS,
......@@ -120,7 +130,11 @@ class VariationsHttpHeaderProvider : public base::FieldTrialList::Observer,
FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest,
GetVariationsString);
FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest,
GetVariationsVector);
GetVariationsVectorByKey);
FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest,
GetVariationsVectorByKeySet);
FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest,
GetVariationsVectorForWebPropertiesKeys);
VariationsHttpHeaderProvider();
~VariationsHttpHeaderProvider() override;
......
......@@ -241,7 +241,7 @@ TEST_F(VariationsHttpHeaderProviderTest, GetVariationsString) {
EXPECT_EQ(" 100 123 124 200 ", provider.GetVariationsString());
}
TEST_F(VariationsHttpHeaderProviderTest, GetVariationsVector) {
TEST_F(VariationsHttpHeaderProviderTest, GetVariationsVectorByKey) {
base::test::SingleThreadTaskEnvironment task_environment;
CreateTrialAndAssociateId("t1", "g1", GOOGLE_WEB_PROPERTIES, 121);
CreateTrialAndAssociateId("t2", "g2", GOOGLE_WEB_PROPERTIES, 122);
......@@ -263,4 +263,41 @@ TEST_F(VariationsHttpHeaderProviderTest, GetVariationsVector) {
provider.GetVariationsVector(GOOGLE_APP));
}
TEST_F(VariationsHttpHeaderProviderTest, GetVariationsVectorByKeySet) {
base::test::SingleThreadTaskEnvironment task_environment;
CreateTrialAndAssociateId("t1", "g1", GOOGLE_WEB_PROPERTIES, 121);
CreateTrialAndAssociateId("t2", "g2", GOOGLE_WEB_PROPERTIES, 122);
CreateTrialAndAssociateId("t3", "g3", GOOGLE_WEB_PROPERTIES_TRIGGER, 123);
CreateTrialAndAssociateId("t4", "g4", GOOGLE_WEB_PROPERTIES_TRIGGER, 124);
CreateTrialAndAssociateId("t5", "g5", GOOGLE_WEB_PROPERTIES_SIGNED_IN, 125);
CreateTrialAndAssociateId("t6", "g6", GOOGLE_WEB_PROPERTIES_SIGNED_IN,
124); // Note: Duplicate.
CreateTrialAndAssociateId("t7", "g7", GOOGLE_APP, 126);
VariationsHttpHeaderProvider provider;
provider.ForceVariationIds({"100", "200", "t101"}, "");
EXPECT_EQ((std::vector<VariationID>{100, 101, 121, 122, 123, 124, 200}),
provider.GetVariationsVector(
{GOOGLE_WEB_PROPERTIES, GOOGLE_WEB_PROPERTIES_TRIGGER}));
EXPECT_EQ((std::vector<VariationID>{101, 123, 124, 125}),
provider.GetVariationsVector({GOOGLE_WEB_PROPERTIES_SIGNED_IN,
GOOGLE_WEB_PROPERTIES_TRIGGER}));
EXPECT_EQ((std::vector<VariationID>{124, 125, 126}),
provider.GetVariationsVector(
{GOOGLE_APP, GOOGLE_WEB_PROPERTIES_SIGNED_IN}));
}
TEST_F(VariationsHttpHeaderProviderTest,
GetVariationsVectorForWebPropertiesKeys) {
CreateTrialAndAssociateId("t1", "g1", GOOGLE_WEB_PROPERTIES, 121);
CreateTrialAndAssociateId("t2", "g2", GOOGLE_WEB_PROPERTIES_TRIGGER, 122);
CreateTrialAndAssociateId("t3", "g3", GOOGLE_WEB_PROPERTIES_SIGNED_IN, 123);
CreateTrialAndAssociateId("t4", "g4", GOOGLE_APP, 124); // Will be excluded.
VariationsHttpHeaderProvider provider;
provider.ForceVariationIds({"100", "t101"}, "");
EXPECT_EQ((std::vector<VariationID>{100, 101, 121, 122, 123}),
provider.GetVariationsVectorForWebPropertiesKeys());
}
} // namespace variations
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