Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / android / contextualsearch / contextual_search_tab_helper.cc
blob8dfc691d2837a19a6fa92453508837434001d59b
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.
5 #include "chrome/browser/android/contextualsearch/contextual_search_tab_helper.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/prefs/pref_change_registrar.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/profiles/profile_android.h"
12 #include "chrome/common/pref_names.h"
13 #include "jni/ContextualSearchTabHelper_jni.h"
16 ContextualSearchTabHelper::ContextualSearchTabHelper(JNIEnv* env,
17 jobject obj,
18 Profile* profile)
19 : weak_java_ref_(env, obj),
20 pref_change_registrar_(new PrefChangeRegistrar()),
21 weak_factory_(this) {
22 pref_change_registrar_->Init(profile->GetPrefs());
23 pref_change_registrar_->Add(
24 prefs::kContextualSearchEnabled,
25 base::Bind(&ContextualSearchTabHelper::OnContextualSearchPrefChanged,
26 weak_factory_.GetWeakPtr()));
29 ContextualSearchTabHelper::~ContextualSearchTabHelper() {
30 pref_change_registrar_->RemoveAll();
33 void ContextualSearchTabHelper::OnContextualSearchPrefChanged() {
34 JNIEnv* env = base::android::AttachCurrentThread();
35 ScopedJavaLocalRef<jobject> jobj = weak_java_ref_.get(env);
36 Java_ContextualSearchTabHelper_onContextualSearchPrefChanged(env, jobj.obj());
39 void ContextualSearchTabHelper::Destroy(JNIEnv* env, jobject obj) {
40 delete this;
43 static jlong Init(JNIEnv* env,
44 const JavaParamRef<jobject>& obj,
45 const JavaParamRef<jobject>& java_profile) {
46 Profile* profile = ProfileAndroid::FromProfileAndroid(java_profile);
47 CHECK(profile);
48 ContextualSearchTabHelper* tab = new ContextualSearchTabHelper(
49 env, obj, profile);
50 return reinterpret_cast<intptr_t>(tab);
53 bool RegisterContextualSearchTabHelper(JNIEnv* env) {
54 return RegisterNativesImpl(env);