1f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// found in the LICENSE file.
4f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
5f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/android/new_tab_page_prefs.h"
6f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
7f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include <jni.h>
8f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
9f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/android/jni_string.h"
10f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/prefs/pref_service.h"
11f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/prefs/scoped_user_pref_update.h"
12f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/profiles/profile_android.h"
13f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/common/pref_names.h"
14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/pref_registry/pref_registry_syncable.h"
15f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "jni/NewTabPagePrefs_jni.h"
16f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
17f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)using base::android::ConvertJavaStringToUTF8;
18f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
19f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)static jlong Init(JNIEnv* env, jclass clazz, jobject profile) {
20f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  NewTabPagePrefs* new_tab_page_prefs =
21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      new NewTabPagePrefs(ProfileAndroid::FromProfileAndroid(profile));
22f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return reinterpret_cast<intptr_t>(new_tab_page_prefs);
23f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
24f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
25f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)NewTabPagePrefs::NewTabPagePrefs(Profile* profile)
26f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  : profile_(profile) {
27f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
29f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void NewTabPagePrefs::Destroy(JNIEnv* env, jobject obj) {
30f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  delete this;
31f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
32f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)NewTabPagePrefs::~NewTabPagePrefs() {
34f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
35f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)jboolean NewTabPagePrefs::GetCurrentlyOpenTabsCollapsed(JNIEnv* env,
375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                                       jobject obj) {
385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  PrefService* prefs = profile_->GetPrefs();
395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return prefs->GetBoolean(prefs::kNtpCollapsedCurrentlyOpenTabs);
405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
415f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
425f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void NewTabPagePrefs::SetCurrentlyOpenTabsCollapsed(JNIEnv* env,
435f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                                   jobject obj,
445f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                                   jboolean is_collapsed) {
455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  PrefService* prefs = profile_->GetPrefs();
465f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  prefs->SetBoolean(prefs::kNtpCollapsedCurrentlyOpenTabs, is_collapsed);
475f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
485f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
49f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)jboolean NewTabPagePrefs::GetSnapshotDocumentCollapsed(JNIEnv* env,
50f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                                       jobject obj) {
51f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return profile_->GetPrefs()->GetBoolean(prefs::kNtpCollapsedSnapshotDocument);
52f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
53f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
54f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void NewTabPagePrefs::SetSnapshotDocumentCollapsed(JNIEnv* env,
55f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                                   jobject obj,
56f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                                   jboolean is_collapsed) {
57f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  PrefService* prefs = profile_->GetPrefs();
58f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  prefs->SetBoolean(prefs::kNtpCollapsedSnapshotDocument, is_collapsed);
59f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
60f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
61f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)jboolean NewTabPagePrefs::GetRecentlyClosedTabsCollapsed(JNIEnv* env,
62f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                                         jobject obj) {
63f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return profile_->GetPrefs()->GetBoolean(
64f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      prefs::kNtpCollapsedRecentlyClosedTabs);
65f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
66f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
67f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void NewTabPagePrefs::SetRecentlyClosedTabsCollapsed(JNIEnv* env,
68f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                                     jobject obj,
69f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                                     jboolean is_collapsed) {
70f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  PrefService* prefs = profile_->GetPrefs();
71f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  prefs->SetBoolean(prefs::kNtpCollapsedRecentlyClosedTabs, is_collapsed);
72f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
73f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
74f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)jboolean NewTabPagePrefs::GetSyncPromoCollapsed(JNIEnv* env,
75f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                                       jobject obj) {
76f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return profile_->GetPrefs()->GetBoolean(prefs::kNtpCollapsedSyncPromo);
77f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
78f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
79f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void NewTabPagePrefs::SetSyncPromoCollapsed(JNIEnv* env,
80f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                            jobject obj,
81f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                            jboolean is_collapsed) {
82f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  PrefService* prefs = profile_->GetPrefs();
83f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  prefs->SetBoolean(prefs::kNtpCollapsedSyncPromo, is_collapsed);
84f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
85f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
86f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)jboolean NewTabPagePrefs::GetForeignSessionCollapsed(JNIEnv* env,
87f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                                     jobject obj,
88f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                                     jstring session_tag) {
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const base::DictionaryValue* dict = profile_->GetPrefs()->GetDictionary(
90f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      prefs::kNtpCollapsedForeignSessions);
91f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return dict && dict->HasKey(ConvertJavaStringToUTF8(env, session_tag));
92f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
93f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
94f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void NewTabPagePrefs::SetForeignSessionCollapsed(JNIEnv* env,
95f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                                 jobject obj,
96f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                                 jstring session_tag,
97f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                                 jboolean is_collapsed) {
98f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Store session tags for collapsed sessions in a preference so that the
99f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // collapsed state persists.
100f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  PrefService* prefs = profile_->GetPrefs();
101f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DictionaryPrefUpdate update(prefs, prefs::kNtpCollapsedForeignSessions);
102f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (is_collapsed)
103f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    update.Get()->SetBoolean(ConvertJavaStringToUTF8(env, session_tag), true);
104f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  else
105f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    update.Get()->Remove(ConvertJavaStringToUTF8(env, session_tag), NULL);
106f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
107f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
108f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// static
109f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void NewTabPagePrefs::RegisterProfilePrefs(
110f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    user_prefs::PrefRegistrySyncable* registry) {
111f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  registry->RegisterBooleanPref(
1125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      prefs::kNtpCollapsedCurrentlyOpenTabs,
1135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      false,
1145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
1155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  registry->RegisterBooleanPref(
116f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      prefs::kNtpCollapsedSnapshotDocument,
117f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      false,
118f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
119f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  registry->RegisterBooleanPref(
120f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      prefs::kNtpCollapsedRecentlyClosedTabs,
121f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      false,
122f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
123f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  registry->RegisterBooleanPref(
124f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      prefs::kNtpCollapsedSyncPromo,
125f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      false,
126f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
1275c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  registry->RegisterDictionaryPref(
1285c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      prefs::kNtpCollapsedForeignSessions,
1295c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
1305c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  registry->RegisterDictionaryPref(
1315c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      prefs::kNtpMostVisitedURLsBlacklist,
1325c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
133f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
134f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
135f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// static
136f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)bool NewTabPagePrefs::RegisterNewTabPagePrefs(JNIEnv* env) {
137f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return RegisterNativesImpl(env);
138f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
139