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
, 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::GetCurrentlyOpenTabsCollapsed(JNIEnv
* env
,
38 PrefService
* prefs
= profile_
->GetPrefs();
39 return prefs
->GetBoolean(prefs::kNtpCollapsedCurrentlyOpenTabs
);
42 void NewTabPagePrefs::SetCurrentlyOpenTabsCollapsed(JNIEnv
* env
,
44 jboolean is_collapsed
) {
45 PrefService
* prefs
= profile_
->GetPrefs();
46 prefs
->SetBoolean(prefs::kNtpCollapsedCurrentlyOpenTabs
, is_collapsed
);
49 jboolean
NewTabPagePrefs::GetSnapshotDocumentCollapsed(JNIEnv
* env
,
51 return profile_
->GetPrefs()->GetBoolean(prefs::kNtpCollapsedSnapshotDocument
);
54 void NewTabPagePrefs::SetSnapshotDocumentCollapsed(JNIEnv
* env
,
56 jboolean is_collapsed
) {
57 PrefService
* prefs
= profile_
->GetPrefs();
58 prefs
->SetBoolean(prefs::kNtpCollapsedSnapshotDocument
, is_collapsed
);
61 jboolean
NewTabPagePrefs::GetRecentlyClosedTabsCollapsed(JNIEnv
* env
,
63 return profile_
->GetPrefs()->GetBoolean(
64 prefs::kNtpCollapsedRecentlyClosedTabs
);
67 void NewTabPagePrefs::SetRecentlyClosedTabsCollapsed(JNIEnv
* env
,
69 jboolean is_collapsed
) {
70 PrefService
* prefs
= profile_
->GetPrefs();
71 prefs
->SetBoolean(prefs::kNtpCollapsedRecentlyClosedTabs
, is_collapsed
);
74 jboolean
NewTabPagePrefs::GetSyncPromoCollapsed(JNIEnv
* env
,
76 return profile_
->GetPrefs()->GetBoolean(prefs::kNtpCollapsedSyncPromo
);
79 void NewTabPagePrefs::SetSyncPromoCollapsed(JNIEnv
* env
,
81 jboolean is_collapsed
) {
82 PrefService
* prefs
= profile_
->GetPrefs();
83 prefs
->SetBoolean(prefs::kNtpCollapsedSyncPromo
, is_collapsed
);
86 jboolean
NewTabPagePrefs::GetForeignSessionCollapsed(JNIEnv
* env
,
88 jstring session_tag
) {
89 const base::DictionaryValue
* dict
= profile_
->GetPrefs()->GetDictionary(
90 prefs::kNtpCollapsedForeignSessions
);
91 return dict
&& dict
->HasKey(ConvertJavaStringToUTF8(env
, session_tag
));
94 void NewTabPagePrefs::SetForeignSessionCollapsed(JNIEnv
* env
,
97 jboolean is_collapsed
) {
98 // Store session tags for collapsed sessions in a preference so that the
99 // collapsed state persists.
100 PrefService
* prefs
= profile_
->GetPrefs();
101 DictionaryPrefUpdate
update(prefs
, prefs::kNtpCollapsedForeignSessions
);
103 update
.Get()->SetBoolean(ConvertJavaStringToUTF8(env
, session_tag
), true);
105 update
.Get()->Remove(ConvertJavaStringToUTF8(env
, session_tag
), NULL
);
109 void NewTabPagePrefs::RegisterProfilePrefs(
110 user_prefs::PrefRegistrySyncable
* registry
) {
111 registry
->RegisterBooleanPref(prefs::kNtpCollapsedCurrentlyOpenTabs
, false);
112 registry
->RegisterBooleanPref(prefs::kNtpCollapsedSnapshotDocument
, false);
113 registry
->RegisterBooleanPref(prefs::kNtpCollapsedRecentlyClosedTabs
, false);
114 registry
->RegisterBooleanPref(prefs::kNtpCollapsedSyncPromo
, false);
115 registry
->RegisterDictionaryPref(prefs::kNtpCollapsedForeignSessions
);
119 bool NewTabPagePrefs::RegisterNewTabPagePrefs(JNIEnv
* env
) {
120 return RegisterNativesImpl(env
);