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
,
19 : weak_java_ref_(env
, obj
),
20 pref_change_registrar_(new PrefChangeRegistrar()),
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
) {
43 static jlong
Init(JNIEnv
* env
, jobject obj
, jobject java_profile
) {
44 Profile
* profile
= ProfileAndroid::FromProfileAndroid(java_profile
);
46 ContextualSearchTabHelper
* tab
= new ContextualSearchTabHelper(
48 return reinterpret_cast<intptr_t>(tab
);
51 bool RegisterContextualSearchTabHelper(JNIEnv
* env
) {
52 return RegisterNativesImpl(env
);