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/pref_registry/pref_registry_syncable.h"
15 #include "jni/NewTabPagePrefs_jni.h"
17 using base::android::ConvertJavaStringToUTF8
;
19 static jlong
Init(JNIEnv
* env
,
20 const JavaParamRef
<jclass
>& clazz
,
21 const JavaParamRef
<jobject
>& profile
) {
22 NewTabPagePrefs
* new_tab_page_prefs
=
23 new NewTabPagePrefs(ProfileAndroid::FromProfileAndroid(profile
));
24 return reinterpret_cast<intptr_t>(new_tab_page_prefs
);
27 NewTabPagePrefs::NewTabPagePrefs(Profile
* profile
)
31 void NewTabPagePrefs::Destroy(JNIEnv
* env
, jobject obj
) {
35 NewTabPagePrefs::~NewTabPagePrefs() {
38 jboolean
NewTabPagePrefs::GetCurrentlyOpenTabsCollapsed(JNIEnv
* env
,
40 PrefService
* prefs
= profile_
->GetPrefs();
41 return prefs
->GetBoolean(prefs::kNtpCollapsedCurrentlyOpenTabs
);
44 void NewTabPagePrefs::SetCurrentlyOpenTabsCollapsed(JNIEnv
* env
,
46 jboolean is_collapsed
) {
47 PrefService
* prefs
= profile_
->GetPrefs();
48 prefs
->SetBoolean(prefs::kNtpCollapsedCurrentlyOpenTabs
, is_collapsed
);
51 jboolean
NewTabPagePrefs::GetSnapshotDocumentCollapsed(JNIEnv
* env
,
53 return profile_
->GetPrefs()->GetBoolean(prefs::kNtpCollapsedSnapshotDocument
);
56 void NewTabPagePrefs::SetSnapshotDocumentCollapsed(JNIEnv
* env
,
58 jboolean is_collapsed
) {
59 PrefService
* prefs
= profile_
->GetPrefs();
60 prefs
->SetBoolean(prefs::kNtpCollapsedSnapshotDocument
, is_collapsed
);
63 jboolean
NewTabPagePrefs::GetRecentlyClosedTabsCollapsed(JNIEnv
* env
,
65 return profile_
->GetPrefs()->GetBoolean(
66 prefs::kNtpCollapsedRecentlyClosedTabs
);
69 void NewTabPagePrefs::SetRecentlyClosedTabsCollapsed(JNIEnv
* env
,
71 jboolean is_collapsed
) {
72 PrefService
* prefs
= profile_
->GetPrefs();
73 prefs
->SetBoolean(prefs::kNtpCollapsedRecentlyClosedTabs
, is_collapsed
);
76 jboolean
NewTabPagePrefs::GetSyncPromoCollapsed(JNIEnv
* env
,
78 return profile_
->GetPrefs()->GetBoolean(prefs::kNtpCollapsedSyncPromo
);
81 void NewTabPagePrefs::SetSyncPromoCollapsed(JNIEnv
* env
,
83 jboolean is_collapsed
) {
84 PrefService
* prefs
= profile_
->GetPrefs();
85 prefs
->SetBoolean(prefs::kNtpCollapsedSyncPromo
, is_collapsed
);
88 jboolean
NewTabPagePrefs::GetForeignSessionCollapsed(JNIEnv
* env
,
90 jstring session_tag
) {
91 const base::DictionaryValue
* dict
= profile_
->GetPrefs()->GetDictionary(
92 prefs::kNtpCollapsedForeignSessions
);
93 return dict
&& dict
->HasKey(ConvertJavaStringToUTF8(env
, session_tag
));
96 void NewTabPagePrefs::SetForeignSessionCollapsed(JNIEnv
* env
,
99 jboolean is_collapsed
) {
100 // Store session tags for collapsed sessions in a preference so that the
101 // collapsed state persists.
102 PrefService
* prefs
= profile_
->GetPrefs();
103 DictionaryPrefUpdate
update(prefs
, prefs::kNtpCollapsedForeignSessions
);
105 update
.Get()->SetBoolean(ConvertJavaStringToUTF8(env
, session_tag
), true);
107 update
.Get()->Remove(ConvertJavaStringToUTF8(env
, session_tag
), NULL
);
111 void NewTabPagePrefs::RegisterProfilePrefs(
112 user_prefs::PrefRegistrySyncable
* registry
) {
113 registry
->RegisterBooleanPref(prefs::kNtpCollapsedCurrentlyOpenTabs
, false);
114 registry
->RegisterBooleanPref(prefs::kNtpCollapsedSnapshotDocument
, false);
115 registry
->RegisterBooleanPref(prefs::kNtpCollapsedRecentlyClosedTabs
, false);
116 registry
->RegisterBooleanPref(prefs::kNtpCollapsedSyncPromo
, false);
117 registry
->RegisterDictionaryPref(prefs::kNtpCollapsedForeignSessions
);
121 bool NewTabPagePrefs::RegisterNewTabPagePrefs(JNIEnv
* env
) {
122 return RegisterNativesImpl(env
);