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 "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "chrome/common/pref_names.h" | 8 #include "chrome/common/pref_names.h" |
9 #include "chrome/browser/prefs/pref_service.h" | 9 #include "chrome/browser/prefs/pref_service.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/common/chrome_notification_types.h" | 11 #include "chrome/common/chrome_notification_types.h" |
12 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
| 13 #include "content/browser/tab_contents/tab_contents.h" |
13 #include "content/common/notification_service.h" | 14 #include "content/common/notification_service.h" |
14 | 15 |
15 void NewTabPageHandler::RegisterMessages() { | 16 void NewTabPageHandler::RegisterMessages() { |
16 web_ui_->RegisterMessageCallback("closePromo", NewCallback( | 17 web_ui_->RegisterMessageCallback("closePromo", NewCallback( |
17 this, &NewTabPageHandler::HandleClosePromo)); | 18 this, &NewTabPageHandler::HandleClosePromo)); |
18 web_ui_->RegisterMessageCallback("pageSelected", NewCallback( | 19 web_ui_->RegisterMessageCallback("pageSelected", NewCallback( |
19 this, &NewTabPageHandler::HandlePageSelected)); | 20 this, &NewTabPageHandler::HandlePageSelected)); |
20 } | 21 } |
21 | 22 |
22 void NewTabPageHandler::HandleClosePromo(const ListValue* args) { | 23 void NewTabPageHandler::HandleClosePromo(const ListValue* args) { |
23 web_ui_->GetProfile()->GetPrefs()->SetBoolean(prefs::kNTPPromoClosed, true); | 24 Profile* profile = |
| 25 Profile::FromBrowserContext(web_ui_->tab_contents()->browser_context()); |
| 26 profile->GetPrefs()->SetBoolean(prefs::kNTPPromoClosed, true); |
24 NotificationService* service = NotificationService::current(); | 27 NotificationService* service = NotificationService::current(); |
25 service->Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, | 28 service->Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, |
26 Source<NewTabPageHandler>(this), | 29 Source<NewTabPageHandler>(this), |
27 NotificationService::NoDetails()); | 30 NotificationService::NoDetails()); |
28 } | 31 } |
29 | 32 |
30 void NewTabPageHandler::HandlePageSelected(const ListValue* args) { | 33 void NewTabPageHandler::HandlePageSelected(const ListValue* args) { |
31 double page_id_double; | 34 double page_id_double; |
32 CHECK(args->GetDouble(0, &page_id_double)); | 35 CHECK(args->GetDouble(0, &page_id_double)); |
33 int page_id = static_cast<int>(page_id_double); | 36 int page_id = static_cast<int>(page_id_double); |
34 | 37 |
35 double index_double; | 38 double index_double; |
36 CHECK(args->GetDouble(1, &index_double)); | 39 CHECK(args->GetDouble(1, &index_double)); |
37 int index = static_cast<int>(index_double); | 40 int index = static_cast<int>(index_double); |
38 | 41 |
39 PrefService* prefs = web_ui_->GetProfile()->GetPrefs(); | 42 Profile* profile = |
| 43 Profile::FromBrowserContext(web_ui_->tab_contents()->browser_context()); |
| 44 PrefService* prefs = profile->GetPrefs(); |
40 prefs->SetInteger(prefs::kNTPShownPage, page_id | index); | 45 prefs->SetInteger(prefs::kNTPShownPage, page_id | index); |
41 } | 46 } |
42 | 47 |
43 // static | 48 // static |
44 void NewTabPageHandler::RegisterUserPrefs(PrefService* prefs) { | 49 void NewTabPageHandler::RegisterUserPrefs(PrefService* prefs) { |
45 // TODO(estade): should be syncable. | 50 // TODO(estade): should be syncable. |
46 prefs->RegisterIntegerPref(prefs::kNTPShownPage, APPS_PAGE_ID, | 51 prefs->RegisterIntegerPref(prefs::kNTPShownPage, APPS_PAGE_ID, |
47 PrefService::UNSYNCABLE_PREF); | 52 PrefService::UNSYNCABLE_PREF); |
48 } | 53 } |
49 | 54 |
50 // static | 55 // static |
51 void NewTabPageHandler::GetLocalizedValues(Profile* profile, | 56 void NewTabPageHandler::GetLocalizedValues(Profile* profile, |
52 DictionaryValue* values) { | 57 DictionaryValue* values) { |
53 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kNewTabPage4)) | 58 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kNewTabPage4)) |
54 return; | 59 return; |
55 | 60 |
56 values->SetInteger("most_visited_page_id", MOST_VISITED_PAGE_ID); | 61 values->SetInteger("most_visited_page_id", MOST_VISITED_PAGE_ID); |
57 values->SetInteger("apps_page_id", APPS_PAGE_ID); | 62 values->SetInteger("apps_page_id", APPS_PAGE_ID); |
58 values->SetInteger("bookmarks_page_id", BOOKMARKS_PAGE_ID); | 63 values->SetInteger("bookmarks_page_id", BOOKMARKS_PAGE_ID); |
59 | 64 |
60 // TODO(estade) Should respect shown sections pref (i.e. migrate if it | 65 // TODO(estade) Should respect shown sections pref (i.e. migrate if it |
61 // exists). | 66 // exists). |
62 PrefService* prefs = profile->GetPrefs(); | 67 PrefService* prefs = profile->GetPrefs(); |
63 int shown_page = prefs->GetInteger(prefs::kNTPShownPage); | 68 int shown_page = prefs->GetInteger(prefs::kNTPShownPage); |
64 values->SetInteger("shown_page_type", shown_page & ~INDEX_MASK); | 69 values->SetInteger("shown_page_type", shown_page & ~INDEX_MASK); |
65 values->SetInteger("shown_page_index", shown_page & INDEX_MASK); | 70 values->SetInteger("shown_page_index", shown_page & INDEX_MASK); |
66 } | 71 } |
OLD | NEW |