summaryrefslogtreecommitdiffstats
path: root/chrome/browser/android/ntp/new_tab_page_prefs.cc
blob: dd1f3325aed12463554ebe4f00e5afea62e9b791 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/android/ntp/new_tab_page_prefs.h"

#include <jni.h>

#include "base/android/jni_string.h"
#include "chrome/browser/profiles/profile_android.h"
#include "chrome/common/pref_names.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "jni/NewTabPagePrefs_jni.h"

using base::android::ConvertJavaStringToUTF8;

static jlong Init(JNIEnv* env,
                  const JavaParamRef<jclass>& clazz,
                  const JavaParamRef<jobject>& profile) {
  NewTabPagePrefs* new_tab_page_prefs =
      new NewTabPagePrefs(ProfileAndroid::FromProfileAndroid(profile));
  return reinterpret_cast<intptr_t>(new_tab_page_prefs);
}

NewTabPagePrefs::NewTabPagePrefs(Profile* profile)
  : profile_(profile) {
}

void NewTabPagePrefs::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) {
  delete this;
}

NewTabPagePrefs::~NewTabPagePrefs() {
}

jboolean NewTabPagePrefs::GetCurrentlyOpenTabsCollapsed(
    JNIEnv* env,
    const JavaParamRef<jobject>& obj) {
  PrefService* prefs = profile_->GetPrefs();
  return prefs->GetBoolean(prefs::kNtpCollapsedCurrentlyOpenTabs);
}

void NewTabPagePrefs::SetCurrentlyOpenTabsCollapsed(
    JNIEnv* env,
    const JavaParamRef<jobject>& obj,
    jboolean is_collapsed) {
  PrefService* prefs = profile_->GetPrefs();
  prefs->SetBoolean(prefs::kNtpCollapsedCurrentlyOpenTabs, is_collapsed);
}

jboolean NewTabPagePrefs::GetSnapshotDocumentCollapsed(
    JNIEnv* env,
    const JavaParamRef<jobject>& obj) {
  return profile_->GetPrefs()->GetBoolean(prefs::kNtpCollapsedSnapshotDocument);
}

void NewTabPagePrefs::SetSnapshotDocumentCollapsed(
    JNIEnv* env,
    const JavaParamRef<jobject>& obj,
    jboolean is_collapsed) {
  PrefService* prefs = profile_->GetPrefs();
  prefs->SetBoolean(prefs::kNtpCollapsedSnapshotDocument, is_collapsed);
}

jboolean NewTabPagePrefs::GetRecentlyClosedTabsCollapsed(
    JNIEnv* env,
    const JavaParamRef<jobject>& obj) {
  return profile_->GetPrefs()->GetBoolean(
      prefs::kNtpCollapsedRecentlyClosedTabs);
}

void NewTabPagePrefs::SetRecentlyClosedTabsCollapsed(
    JNIEnv* env,
    const JavaParamRef<jobject>& obj,
    jboolean is_collapsed) {
  PrefService* prefs = profile_->GetPrefs();
  prefs->SetBoolean(prefs::kNtpCollapsedRecentlyClosedTabs, is_collapsed);
}

jboolean NewTabPagePrefs::GetSyncPromoCollapsed(
    JNIEnv* env,
    const JavaParamRef<jobject>& obj) {
  return profile_->GetPrefs()->GetBoolean(prefs::kNtpCollapsedSyncPromo);
}

void NewTabPagePrefs::SetSyncPromoCollapsed(JNIEnv* env,
                                            const JavaParamRef<jobject>& obj,
                                            jboolean is_collapsed) {
  PrefService* prefs = profile_->GetPrefs();
  prefs->SetBoolean(prefs::kNtpCollapsedSyncPromo, is_collapsed);
}

jboolean NewTabPagePrefs::GetForeignSessionCollapsed(
    JNIEnv* env,
    const JavaParamRef<jobject>& obj,
    const JavaParamRef<jstring>& session_tag) {
  const base::DictionaryValue* dict = profile_->GetPrefs()->GetDictionary(
      prefs::kNtpCollapsedForeignSessions);
  return dict && dict->HasKey(ConvertJavaStringToUTF8(env, session_tag));
}

void NewTabPagePrefs::SetForeignSessionCollapsed(
    JNIEnv* env,
    const JavaParamRef<jobject>& obj,
    const JavaParamRef<jstring>& session_tag,
    jboolean is_collapsed) {
  // Store session tags for collapsed sessions in a preference so that the
  // collapsed state persists.
  PrefService* prefs = profile_->GetPrefs();
  DictionaryPrefUpdate update(prefs, prefs::kNtpCollapsedForeignSessions);
  if (is_collapsed)
    update.Get()->SetBoolean(ConvertJavaStringToUTF8(env, session_tag), true);
  else
    update.Get()->Remove(ConvertJavaStringToUTF8(env, session_tag), NULL);
}

// static
void NewTabPagePrefs::RegisterProfilePrefs(
    user_prefs::PrefRegistrySyncable* registry) {
  registry->RegisterBooleanPref(prefs::kNtpCollapsedCurrentlyOpenTabs, false);
  registry->RegisterBooleanPref(prefs::kNtpCollapsedSnapshotDocument, false);
  registry->RegisterBooleanPref(prefs::kNtpCollapsedRecentlyClosedTabs, false);
  registry->RegisterBooleanPref(prefs::kNtpCollapsedSyncPromo, false);
  registry->RegisterDictionaryPref(prefs::kNtpCollapsedForeignSessions);
}

// static
bool NewTabPagePrefs::RegisterNewTabPagePrefs(JNIEnv* env) {
  return RegisterNativesImpl(env);
}