new_tab_page_prefs.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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/new_tab_page_prefs.h"
6
7#include <jni.h>
8
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"
16
17using base::android::ConvertJavaStringToUTF8;
18
19static 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);
23}
24
25NewTabPagePrefs::NewTabPagePrefs(Profile* profile)
26  : profile_(profile) {
27}
28
29void NewTabPagePrefs::Destroy(JNIEnv* env, jobject obj) {
30  delete this;
31}
32
33NewTabPagePrefs::~NewTabPagePrefs() {
34}
35
36jboolean NewTabPagePrefs::GetCurrentlyOpenTabsCollapsed(JNIEnv* env,
37                                                       jobject obj) {
38  PrefService* prefs = profile_->GetPrefs();
39  return prefs->GetBoolean(prefs::kNtpCollapsedCurrentlyOpenTabs);
40}
41
42void NewTabPagePrefs::SetCurrentlyOpenTabsCollapsed(JNIEnv* env,
43                                                   jobject obj,
44                                                   jboolean is_collapsed) {
45  PrefService* prefs = profile_->GetPrefs();
46  prefs->SetBoolean(prefs::kNtpCollapsedCurrentlyOpenTabs, is_collapsed);
47}
48
49jboolean NewTabPagePrefs::GetSnapshotDocumentCollapsed(JNIEnv* env,
50                                                       jobject obj) {
51  return profile_->GetPrefs()->GetBoolean(prefs::kNtpCollapsedSnapshotDocument);
52}
53
54void NewTabPagePrefs::SetSnapshotDocumentCollapsed(JNIEnv* env,
55                                                   jobject obj,
56                                                   jboolean is_collapsed) {
57  PrefService* prefs = profile_->GetPrefs();
58  prefs->SetBoolean(prefs::kNtpCollapsedSnapshotDocument, is_collapsed);
59}
60
61jboolean NewTabPagePrefs::GetRecentlyClosedTabsCollapsed(JNIEnv* env,
62                                                         jobject obj) {
63  return profile_->GetPrefs()->GetBoolean(
64      prefs::kNtpCollapsedRecentlyClosedTabs);
65}
66
67void NewTabPagePrefs::SetRecentlyClosedTabsCollapsed(JNIEnv* env,
68                                                     jobject obj,
69                                                     jboolean is_collapsed) {
70  PrefService* prefs = profile_->GetPrefs();
71  prefs->SetBoolean(prefs::kNtpCollapsedRecentlyClosedTabs, is_collapsed);
72}
73
74jboolean NewTabPagePrefs::GetSyncPromoCollapsed(JNIEnv* env,
75                                                       jobject obj) {
76  return profile_->GetPrefs()->GetBoolean(prefs::kNtpCollapsedSyncPromo);
77}
78
79void NewTabPagePrefs::SetSyncPromoCollapsed(JNIEnv* env,
80                                            jobject obj,
81                                            jboolean is_collapsed) {
82  PrefService* prefs = profile_->GetPrefs();
83  prefs->SetBoolean(prefs::kNtpCollapsedSyncPromo, is_collapsed);
84}
85
86jboolean NewTabPagePrefs::GetForeignSessionCollapsed(JNIEnv* env,
87                                                     jobject obj,
88                                                     jstring session_tag) {
89  const base::DictionaryValue* dict = profile_->GetPrefs()->GetDictionary(
90      prefs::kNtpCollapsedForeignSessions);
91  return dict && dict->HasKey(ConvertJavaStringToUTF8(env, session_tag));
92}
93
94void NewTabPagePrefs::SetForeignSessionCollapsed(JNIEnv* env,
95                                                 jobject obj,
96                                                 jstring session_tag,
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);
102  if (is_collapsed)
103    update.Get()->SetBoolean(ConvertJavaStringToUTF8(env, session_tag), true);
104  else
105    update.Get()->Remove(ConvertJavaStringToUTF8(env, session_tag), NULL);
106}
107
108// static
109void NewTabPagePrefs::RegisterProfilePrefs(
110    user_prefs::PrefRegistrySyncable* registry) {
111  registry->RegisterBooleanPref(
112      prefs::kNtpCollapsedCurrentlyOpenTabs,
113      false,
114      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
115  registry->RegisterBooleanPref(
116      prefs::kNtpCollapsedSnapshotDocument,
117      false,
118      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
119  registry->RegisterBooleanPref(
120      prefs::kNtpCollapsedRecentlyClosedTabs,
121      false,
122      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
123  registry->RegisterBooleanPref(
124      prefs::kNtpCollapsedSyncPromo,
125      false,
126      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
127  registry->RegisterDictionaryPref(
128      prefs::kNtpCollapsedForeignSessions,
129      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
130  registry->RegisterDictionaryPref(
131      prefs::kNtpMostVisitedURLsBlacklist,
132      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
133}
134
135// static
136bool NewTabPagePrefs::RegisterNewTabPagePrefs(JNIEnv* env) {
137  return RegisterNativesImpl(env);
138}
139