OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/android/ntp/new_tab_page_prefs.h" | |
6 | |
7 #include <jni.h> | |
8 | |
9 #include "base/android/jni_string.h" | |
10 #include "chrome/browser/profiles/profile_android.h" | |
11 #include "chrome/common/pref_names.h" | |
12 #include "components/pref_registry/pref_registry_syncable.h" | |
13 #include "components/prefs/pref_service.h" | |
14 #include "components/prefs/scoped_user_pref_update.h" | |
15 #include "jni/NewTabPagePrefs_jni.h" | |
16 | |
17 using base::android::ConvertJavaStringToUTF8; | |
18 using base::android::JavaParamRef; | |
19 | |
20 static jlong Init(JNIEnv* env, | |
21 const JavaParamRef<jclass>& clazz, | |
22 const JavaParamRef<jobject>& profile) { | |
23 NewTabPagePrefs* new_tab_page_prefs = | |
24 new NewTabPagePrefs(ProfileAndroid::FromProfileAndroid(profile)); | |
25 return reinterpret_cast<intptr_t>(new_tab_page_prefs); | |
26 } | |
27 | |
28 NewTabPagePrefs::NewTabPagePrefs(Profile* profile) | |
29 : profile_(profile) { | |
30 } | |
31 | |
32 void NewTabPagePrefs::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { | |
33 delete this; | |
34 } | |
35 | |
36 NewTabPagePrefs::~NewTabPagePrefs() { | |
37 } | |
38 | |
39 jboolean NewTabPagePrefs::GetSnapshotDocumentCollapsed( | |
40 JNIEnv* env, | |
41 const JavaParamRef<jobject>& obj) { | |
42 return profile_->GetPrefs()->GetBoolean(prefs::kNtpCollapsedSnapshotDocument); | |
43 } | |
44 | |
45 void NewTabPagePrefs::SetSnapshotDocumentCollapsed( | |
46 JNIEnv* env, | |
47 const JavaParamRef<jobject>& obj, | |
48 jboolean is_collapsed) { | |
49 PrefService* prefs = profile_->GetPrefs(); | |
50 prefs->SetBoolean(prefs::kNtpCollapsedSnapshotDocument, is_collapsed); | |
51 } | |
52 | |
53 jboolean NewTabPagePrefs::GetRecentlyClosedTabsCollapsed( | |
54 JNIEnv* env, | |
55 const JavaParamRef<jobject>& obj) { | |
56 return profile_->GetPrefs()->GetBoolean( | |
57 prefs::kNtpCollapsedRecentlyClosedTabs); | |
58 } | |
59 | |
60 void NewTabPagePrefs::SetRecentlyClosedTabsCollapsed( | |
61 JNIEnv* env, | |
62 const JavaParamRef<jobject>& obj, | |
63 jboolean is_collapsed) { | |
64 PrefService* prefs = profile_->GetPrefs(); | |
65 prefs->SetBoolean(prefs::kNtpCollapsedRecentlyClosedTabs, is_collapsed); | |
66 } | |
67 | |
68 jboolean NewTabPagePrefs::GetSyncPromoCollapsed( | |
69 JNIEnv* env, | |
70 const JavaParamRef<jobject>& obj) { | |
71 return profile_->GetPrefs()->GetBoolean(prefs::kNtpCollapsedSyncPromo); | |
72 } | |
73 | |
74 void NewTabPagePrefs::SetSyncPromoCollapsed(JNIEnv* env, | |
75 const JavaParamRef<jobject>& obj, | |
76 jboolean is_collapsed) { | |
77 PrefService* prefs = profile_->GetPrefs(); | |
78 prefs->SetBoolean(prefs::kNtpCollapsedSyncPromo, is_collapsed); | |
79 } | |
80 | |
81 jboolean NewTabPagePrefs::GetForeignSessionCollapsed( | |
82 JNIEnv* env, | |
83 const JavaParamRef<jobject>& obj, | |
84 const JavaParamRef<jstring>& session_tag) { | |
85 const base::DictionaryValue* dict = profile_->GetPrefs()->GetDictionary( | |
86 prefs::kNtpCollapsedForeignSessions); | |
87 return dict && dict->HasKey(ConvertJavaStringToUTF8(env, session_tag)); | |
88 } | |
89 | |
90 void NewTabPagePrefs::SetForeignSessionCollapsed( | |
91 JNIEnv* env, | |
92 const JavaParamRef<jobject>& obj, | |
93 const JavaParamRef<jstring>& session_tag, | |
94 jboolean is_collapsed) { | |
95 // Store session tags for collapsed sessions in a preference so that the | |
96 // collapsed state persists. | |
97 PrefService* prefs = profile_->GetPrefs(); | |
98 DictionaryPrefUpdate update(prefs, prefs::kNtpCollapsedForeignSessions); | |
99 if (is_collapsed) | |
100 update.Get()->SetBoolean(ConvertJavaStringToUTF8(env, session_tag), true); | |
101 else | |
102 update.Get()->Remove(ConvertJavaStringToUTF8(env, session_tag), NULL); | |
103 } | |
104 | |
105 // static | |
106 void NewTabPagePrefs::RegisterProfilePrefs( | |
107 user_prefs::PrefRegistrySyncable* registry) { | |
108 registry->RegisterBooleanPref(prefs::kNtpCollapsedSnapshotDocument, false); | |
109 registry->RegisterBooleanPref(prefs::kNtpCollapsedRecentlyClosedTabs, false); | |
110 registry->RegisterBooleanPref(prefs::kNtpCollapsedSyncPromo, false); | |
111 registry->RegisterDictionaryPref(prefs::kNtpCollapsedForeignSessions); | |
112 } | |
113 | |
114 // static | |
115 bool NewTabPagePrefs::RegisterNewTabPagePrefs(JNIEnv* env) { | |
116 return RegisterNativesImpl(env); | |
117 } | |
OLD | NEW |