new_tab_page_prefs.cc revision f2477e01787aa58f445919b809d89e252beef54f
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/user_prefs/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::GetSnapshotDocumentCollapsed(JNIEnv* env,
37                                                       jobject obj) {
38  return profile_->GetPrefs()->GetBoolean(prefs::kNtpCollapsedSnapshotDocument);
39}
40
41void NewTabPagePrefs::SetSnapshotDocumentCollapsed(JNIEnv* env,
42                                                   jobject obj,
43                                                   jboolean is_collapsed) {
44  PrefService* prefs = profile_->GetPrefs();
45  prefs->SetBoolean(prefs::kNtpCollapsedSnapshotDocument, is_collapsed);
46}
47
48jboolean NewTabPagePrefs::GetRecentlyClosedTabsCollapsed(JNIEnv* env,
49                                                         jobject obj) {
50  return profile_->GetPrefs()->GetBoolean(
51      prefs::kNtpCollapsedRecentlyClosedTabs);
52}
53
54void NewTabPagePrefs::SetRecentlyClosedTabsCollapsed(JNIEnv* env,
55                                                     jobject obj,
56                                                     jboolean is_collapsed) {
57  PrefService* prefs = profile_->GetPrefs();
58  prefs->SetBoolean(prefs::kNtpCollapsedRecentlyClosedTabs, is_collapsed);
59}
60
61jboolean NewTabPagePrefs::GetSyncPromoCollapsed(JNIEnv* env,
62                                                       jobject obj) {
63  return profile_->GetPrefs()->GetBoolean(prefs::kNtpCollapsedSyncPromo);
64}
65
66void NewTabPagePrefs::SetSyncPromoCollapsed(JNIEnv* env,
67                                            jobject obj,
68                                            jboolean is_collapsed) {
69  PrefService* prefs = profile_->GetPrefs();
70  prefs->SetBoolean(prefs::kNtpCollapsedSyncPromo, is_collapsed);
71}
72
73jboolean NewTabPagePrefs::GetForeignSessionCollapsed(JNIEnv* env,
74                                                     jobject obj,
75                                                     jstring session_tag) {
76  const DictionaryValue* dict = profile_->GetPrefs()->GetDictionary(
77      prefs::kNtpCollapsedForeignSessions);
78  return dict && dict->HasKey(ConvertJavaStringToUTF8(env, session_tag));
79}
80
81void NewTabPagePrefs::SetForeignSessionCollapsed(JNIEnv* env,
82                                                 jobject obj,
83                                                 jstring session_tag,
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);
89  if (is_collapsed)
90    update.Get()->SetBoolean(ConvertJavaStringToUTF8(env, session_tag), true);
91  else
92    update.Get()->Remove(ConvertJavaStringToUTF8(env, session_tag), NULL);
93}
94
95// static
96void NewTabPagePrefs::RegisterProfilePrefs(
97    user_prefs::PrefRegistrySyncable* registry) {
98  registry->RegisterBooleanPref(
99      prefs::kNtpCollapsedSnapshotDocument,
100      false,
101      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
102  registry->RegisterBooleanPref(
103      prefs::kNtpCollapsedRecentlyClosedTabs,
104      false,
105      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
106  registry->RegisterBooleanPref(
107      prefs::kNtpCollapsedSyncPromo,
108      false,
109      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
110}
111
112// static
113bool NewTabPagePrefs::RegisterNewTabPagePrefs(JNIEnv* env) {
114  return RegisterNativesImpl(env);
115}
116