| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/ntp/new_tab_page_handler.h" | 5 #include "chrome/browser/ui/webui/ntp/new_tab_page_handler.h" |
| 6 | 6 |
| 7 #include "chrome/common/pref_names.h" | 7 #include "chrome/common/pref_names.h" |
| 8 #include "chrome/browser/prefs/pref_service.h" | 8 #include "chrome/browser/prefs/pref_service.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | 10 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
| 11 #include "chrome/common/chrome_notification_types.h" | 11 #include "chrome/common/chrome_notification_types.h" |
| 12 #include "content/common/notification_service.h" | 12 #include "content/common/notification_service.h" |
| 13 #include "grit/chromium_strings.h" |
| 14 #include "ui/base/l10n/l10n_util.h" |
| 15 |
| 16 static const int kIntroDisplayMax = 10; |
| 13 | 17 |
| 14 void NewTabPageHandler::RegisterMessages() { | 18 void NewTabPageHandler::RegisterMessages() { |
| 15 web_ui_->RegisterMessageCallback("closePromo", NewCallback( | 19 web_ui_->RegisterMessageCallback("closePromo", NewCallback( |
| 16 this, &NewTabPageHandler::HandleClosePromo)); | 20 this, &NewTabPageHandler::HandleClosePromo)); |
| 17 web_ui_->RegisterMessageCallback("pageSelected", NewCallback( | 21 web_ui_->RegisterMessageCallback("pageSelected", NewCallback( |
| 18 this, &NewTabPageHandler::HandlePageSelected)); | 22 this, &NewTabPageHandler::HandlePageSelected)); |
| 23 web_ui_->RegisterMessageCallback("navigationDotUsed", NewCallback( |
| 24 this, &NewTabPageHandler::HandleNavDotUsed)); |
| 25 web_ui_->RegisterMessageCallback("introMessageSeen", NewCallback( |
| 26 this, &NewTabPageHandler::HandleIntroMessageSeen)); |
| 19 } | 27 } |
| 20 | 28 |
| 21 void NewTabPageHandler::HandleClosePromo(const ListValue* args) { | 29 void NewTabPageHandler::HandleClosePromo(const ListValue* args) { |
| 22 web_ui_->GetProfile()->GetPrefs()->SetBoolean(prefs::kNTPPromoClosed, true); | 30 web_ui_->GetProfile()->GetPrefs()->SetBoolean(prefs::kNTPPromoClosed, true); |
| 23 NotificationService* service = NotificationService::current(); | 31 NotificationService* service = NotificationService::current(); |
| 24 service->Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, | 32 service->Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, |
| 25 Source<NewTabPageHandler>(this), | 33 Source<NewTabPageHandler>(this), |
| 26 NotificationService::NoDetails()); | 34 NotificationService::NoDetails()); |
| 27 } | 35 } |
| 28 | 36 |
| 29 void NewTabPageHandler::HandlePageSelected(const ListValue* args) { | 37 void NewTabPageHandler::HandlePageSelected(const ListValue* args) { |
| 30 double page_id_double; | 38 double page_id_double; |
| 31 CHECK(args->GetDouble(0, &page_id_double)); | 39 CHECK(args->GetDouble(0, &page_id_double)); |
| 32 int page_id = static_cast<int>(page_id_double); | 40 int page_id = static_cast<int>(page_id_double); |
| 33 | 41 |
| 34 double index_double; | 42 double index_double; |
| 35 CHECK(args->GetDouble(1, &index_double)); | 43 CHECK(args->GetDouble(1, &index_double)); |
| 36 int index = static_cast<int>(index_double); | 44 int index = static_cast<int>(index_double); |
| 37 | 45 |
| 38 PrefService* prefs = web_ui_->GetProfile()->GetPrefs(); | 46 PrefService* prefs = web_ui_->GetProfile()->GetPrefs(); |
| 39 prefs->SetInteger(prefs::kNTPShownPage, page_id | index); | 47 prefs->SetInteger(prefs::kNTPShownPage, page_id | index); |
| 40 } | 48 } |
| 41 | 49 |
| 50 void NewTabPageHandler::HandleNavDotUsed(const ListValue* args) { |
| 51 PrefService* prefs = web_ui_->GetProfile()->GetPrefs(); |
| 52 prefs->SetInteger(prefs::kNTP4IntroDisplayCount, kIntroDisplayMax + 1); |
| 53 } |
| 54 |
| 55 void NewTabPageHandler::HandleIntroMessageSeen(const ListValue* args) { |
| 56 PrefService* prefs = web_ui_->GetProfile()->GetPrefs(); |
| 57 int intro_displays = prefs->GetInteger(prefs::kNTP4IntroDisplayCount); |
| 58 prefs->SetInteger(prefs::kNTP4IntroDisplayCount, intro_displays + 1); |
| 59 } |
| 60 |
| 42 // static | 61 // static |
| 43 void NewTabPageHandler::RegisterUserPrefs(PrefService* prefs) { | 62 void NewTabPageHandler::RegisterUserPrefs(PrefService* prefs) { |
| 44 // TODO(estade): should be syncable. | 63 // TODO(estade): should be syncable. |
| 45 prefs->RegisterIntegerPref(prefs::kNTPShownPage, APPS_PAGE_ID, | 64 prefs->RegisterIntegerPref(prefs::kNTPShownPage, APPS_PAGE_ID, |
| 46 PrefService::UNSYNCABLE_PREF); | 65 PrefService::UNSYNCABLE_PREF); |
| 66 prefs->RegisterIntegerPref(prefs::kNTP4IntroDisplayCount, 0, |
| 67 PrefService::UNSYNCABLE_PREF); |
| 47 } | 68 } |
| 48 | 69 |
| 49 // static | 70 // static |
| 50 void NewTabPageHandler::GetLocalizedValues(Profile* profile, | 71 void NewTabPageHandler::GetLocalizedValues(Profile* profile, |
| 51 DictionaryValue* values) { | 72 DictionaryValue* values) { |
| 52 if (!NewTabUI::Ntp4Enabled()) | 73 if (!NewTabUI::Ntp4Enabled()) |
| 53 return; | 74 return; |
| 54 | 75 |
| 55 values->SetInteger("most_visited_page_id", MOST_VISITED_PAGE_ID); | 76 values->SetInteger("most_visited_page_id", MOST_VISITED_PAGE_ID); |
| 56 values->SetInteger("apps_page_id", APPS_PAGE_ID); | 77 values->SetInteger("apps_page_id", APPS_PAGE_ID); |
| 57 values->SetInteger("bookmarks_page_id", BOOKMARKS_PAGE_ID); | 78 values->SetInteger("bookmarks_page_id", BOOKMARKS_PAGE_ID); |
| 58 | 79 |
| 59 PrefService* prefs = profile->GetPrefs(); | 80 PrefService* prefs = profile->GetPrefs(); |
| 60 int shown_page = prefs->GetInteger(prefs::kNTPShownPage); | 81 int shown_page = prefs->GetInteger(prefs::kNTPShownPage); |
| 61 values->SetInteger("shown_page_type", shown_page & ~INDEX_MASK); | 82 values->SetInteger("shown_page_type", shown_page & ~INDEX_MASK); |
| 62 values->SetInteger("shown_page_index", shown_page & INDEX_MASK); | 83 values->SetInteger("shown_page_index", shown_page & INDEX_MASK); |
| 84 |
| 85 int intro_displays = prefs->GetInteger(prefs::kNTP4IntroDisplayCount); |
| 86 if (intro_displays <= kIntroDisplayMax) { |
| 87 values->SetString("ntp4_intro_message", |
| 88 l10n_util::GetStringUTF16(IDS_NTP4_INTRO_MESSAGE)); |
| 89 } |
| 63 } | 90 } |
| OLD | NEW |