1 // Copyright 2013 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/new_tab_page_prefs.h"
9 #include "base/android/jni_string.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/prefs/scoped_user_pref_update.h"
12 #include "chrome/browser/profiles/profile_android.h"
13 #include "chrome/common/pref_names.h"
14 #include "components/user_prefs/pref_registry_syncable.h"
15 #include "jni/NewTabPagePrefs_jni.h"
17 using base::android::ConvertJavaStringToUTF8
;
19 static jlong
Init(JNIEnv
* env
, jclass clazz
, jobject profile
) {
20 NewTabPagePrefs
* new_tab_page_prefs
=
21 new NewTabPagePrefs(ProfileAndroid::FromProfileAndroid(profile
));
22 return reinterpret_cast<intptr_t>(new_tab_page_prefs
);
25 NewTabPagePrefs::NewTabPagePrefs(Profile
* profile
)
29 void NewTabPagePrefs::Destroy(JNIEnv
* env
, jobject obj
) {
33 NewTabPagePrefs::~NewTabPagePrefs() {
36 jboolean
NewTabPagePrefs::GetSnapshotDocumentCollapsed(JNIEnv
* env
,
38 return profile_
->GetPrefs()->GetBoolean(prefs::kNtpCollapsedSnapshotDocument
);
41 void NewTabPagePrefs::SetSnapshotDocumentCollapsed(JNIEnv
* env
,
43 jboolean is_collapsed
) {
44 PrefService
* prefs
= profile_
->GetPrefs();
45 prefs
->SetBoolean(prefs::kNtpCollapsedSnapshotDocument
, is_collapsed
);
48 jboolean
NewTabPagePrefs::GetRecentlyClosedTabsCollapsed(JNIEnv
* env
,
50 return profile_
->GetPrefs()->GetBoolean(
51 prefs::kNtpCollapsedRecentlyClosedTabs
);
54 void NewTabPagePrefs::SetRecentlyClosedTabsCollapsed(JNIEnv
* env
,
56 jboolean is_collapsed
) {
57 PrefService
* prefs
= profile_
->GetPrefs();
58 prefs
->SetBoolean(prefs::kNtpCollapsedRecentlyClosedTabs
, is_collapsed
);
61 jboolean
NewTabPagePrefs::GetSyncPromoCollapsed(JNIEnv
* env
,
63 return profile_
->GetPrefs()->GetBoolean(prefs::kNtpCollapsedSyncPromo
);
66 void NewTabPagePrefs::SetSyncPromoCollapsed(JNIEnv
* env
,
68 jboolean is_collapsed
) {
69 PrefService
* prefs
= profile_
->GetPrefs();
70 prefs
->SetBoolean(prefs::kNtpCollapsedSyncPromo
, is_collapsed
);
73 jboolean
NewTabPagePrefs::GetForeignSessionCollapsed(JNIEnv
* env
,
75 jstring session_tag
) {
76 const base::DictionaryValue
* dict
= profile_
->GetPrefs()->GetDictionary(
77 prefs::kNtpCollapsedForeignSessions
);
78 return dict
&& dict
->HasKey(ConvertJavaStringToUTF8(env
, session_tag
));
81 void NewTabPagePrefs::SetForeignSessionCollapsed(JNIEnv
* env
,
84 jboolean is_collapsed
) {
85 // Store session tags for collapsed sessions in a preference so that the
86 // collapsed state persists.
87 PrefService
* prefs
= profile_
->GetPrefs();
88 DictionaryPrefUpdate
update(prefs
, prefs::kNtpCollapsedForeignSessions
);
90 update
.Get()->SetBoolean(ConvertJavaStringToUTF8(env
, session_tag
), true);
92 update
.Get()->Remove(ConvertJavaStringToUTF8(env
, session_tag
), NULL
);
96 void NewTabPagePrefs::RegisterProfilePrefs(
97 user_prefs::PrefRegistrySyncable
* registry
) {
98 registry
->RegisterBooleanPref(
99 prefs::kNtpCollapsedSnapshotDocument
,
101 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
102 registry
->RegisterBooleanPref(
103 prefs::kNtpCollapsedRecentlyClosedTabs
,
105 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
106 registry
->RegisterBooleanPref(
107 prefs::kNtpCollapsedSyncPromo
,
109 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
113 bool NewTabPagePrefs::RegisterNewTabPagePrefs(JNIEnv
* env
) {
114 return RegisterNativesImpl(env
);