ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / android / new_tab_page_prefs.cc
blob0bb054d95ac48d80cd1c555a309ed6c0c7042ffb
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"
7 #include <jni.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)
26 : profile_(profile) {
29 void NewTabPagePrefs::Destroy(JNIEnv* env, jobject obj) {
30 delete this;
33 NewTabPagePrefs::~NewTabPagePrefs() {
36 jboolean NewTabPagePrefs::GetCurrentlyOpenTabsCollapsed(JNIEnv* env,
37 jobject obj) {
38 PrefService* prefs = profile_->GetPrefs();
39 return prefs->GetBoolean(prefs::kNtpCollapsedCurrentlyOpenTabs);
42 void NewTabPagePrefs::SetCurrentlyOpenTabsCollapsed(JNIEnv* env,
43 jobject obj,
44 jboolean is_collapsed) {
45 PrefService* prefs = profile_->GetPrefs();
46 prefs->SetBoolean(prefs::kNtpCollapsedCurrentlyOpenTabs, is_collapsed);
49 jboolean NewTabPagePrefs::GetSnapshotDocumentCollapsed(JNIEnv* env,
50 jobject obj) {
51 return profile_->GetPrefs()->GetBoolean(prefs::kNtpCollapsedSnapshotDocument);
54 void NewTabPagePrefs::SetSnapshotDocumentCollapsed(JNIEnv* env,
55 jobject obj,
56 jboolean is_collapsed) {
57 PrefService* prefs = profile_->GetPrefs();
58 prefs->SetBoolean(prefs::kNtpCollapsedSnapshotDocument, is_collapsed);
61 jboolean NewTabPagePrefs::GetRecentlyClosedTabsCollapsed(JNIEnv* env,
62 jobject obj) {
63 return profile_->GetPrefs()->GetBoolean(
64 prefs::kNtpCollapsedRecentlyClosedTabs);
67 void NewTabPagePrefs::SetRecentlyClosedTabsCollapsed(JNIEnv* env,
68 jobject obj,
69 jboolean is_collapsed) {
70 PrefService* prefs = profile_->GetPrefs();
71 prefs->SetBoolean(prefs::kNtpCollapsedRecentlyClosedTabs, is_collapsed);
74 jboolean NewTabPagePrefs::GetSyncPromoCollapsed(JNIEnv* env,
75 jobject obj) {
76 return profile_->GetPrefs()->GetBoolean(prefs::kNtpCollapsedSyncPromo);
79 void NewTabPagePrefs::SetSyncPromoCollapsed(JNIEnv* env,
80 jobject obj,
81 jboolean is_collapsed) {
82 PrefService* prefs = profile_->GetPrefs();
83 prefs->SetBoolean(prefs::kNtpCollapsedSyncPromo, is_collapsed);
86 jboolean 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));
94 void 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);
108 // static
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);
118 // static
119 bool NewTabPagePrefs::RegisterNewTabPagePrefs(JNIEnv* env) {
120 return RegisterNativesImpl(env);