OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/ui/webui/ntp/shown_sections_handler.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/bind_helpers.h" | |
11 #include "base/command_line.h" | |
12 #include "base/string_number_conversions.h" | |
13 #include "base/values.h" | |
14 #include "chrome/browser/prefs/pref_service.h" | |
15 #include "chrome/browser/profiles/profile.h" | |
16 #include "chrome/common/chrome_notification_types.h" | |
17 #include "chrome/common/extensions/extension.h" | |
18 #include "chrome/common/pref_names.h" | |
19 #include "content/browser/user_metrics.h" | |
20 #include "content/public/browser/notification_details.h" | |
21 | |
22 namespace { | |
23 | |
24 // Will cause an UMA notification if the mode of the new tab page | |
25 // was changed to hide/show the most visited thumbnails. | |
26 // TODO(aa): Needs to be updated to match newest NTP - http://crbug.com/57440 | |
27 void NotifySectionDisabled(int new_mode, int old_mode, Profile *profile) { | |
28 // If the oldmode HAD either thumbs or lists visible. | |
29 bool old_had_it = (old_mode & THUMB) && !(old_mode & MENU_THUMB); | |
30 bool new_has_it = (new_mode & THUMB) && !(new_mode & MENU_THUMB); | |
31 | |
32 if (old_had_it && !new_has_it) { | |
33 UserMetrics::RecordAction( | |
34 UserMetricsAction("ShowSections_RecentSitesDisabled")); | |
35 } | |
36 | |
37 if (new_has_it && !old_had_it) { | |
38 UserMetrics::RecordAction( | |
39 UserMetricsAction("ShowSections_RecentSitesEnabled")); | |
40 } | |
41 } | |
42 | |
43 } // namespace | |
44 | |
45 // static | |
46 int ShownSectionsHandler::GetShownSections(PrefService* prefs) { | |
47 return prefs->GetInteger(prefs::kNTPShownSections); | |
48 } | |
49 | |
50 // static | |
51 void ShownSectionsHandler::SetShownSection(PrefService* prefs, | |
52 Section section) { | |
53 int shown_sections = GetShownSections(prefs); | |
54 shown_sections &= ~ALL_SECTIONS_MASK; | |
55 shown_sections |= section; | |
56 prefs->SetInteger(prefs::kNTPShownSections, shown_sections); | |
57 } | |
58 | |
59 ShownSectionsHandler::ShownSectionsHandler(PrefService* pref_service) | |
60 : pref_service_(pref_service) { | |
61 pref_registrar_.Init(pref_service); | |
62 pref_registrar_.Add(prefs::kNTPShownSections, this); | |
63 } | |
64 | |
65 void ShownSectionsHandler::RegisterMessages() { | |
66 web_ui_->RegisterMessageCallback("setShownSections", | |
67 base::Bind(&ShownSectionsHandler::HandleSetShownSections, | |
68 base::Unretained(this))); | |
69 } | |
70 | |
71 void ShownSectionsHandler::Observe( | |
72 int type, | |
73 const content::NotificationSource& source, | |
74 const content::NotificationDetails& details) { | |
75 if (type == chrome::NOTIFICATION_PREF_CHANGED) { | |
76 std::string* pref_name = content::Details<std::string>(details).ptr(); | |
77 DCHECK(*pref_name == prefs::kNTPShownSections); | |
78 int sections = pref_service_->GetInteger(prefs::kNTPShownSections); | |
79 base::FundamentalValue sections_value(sections); | |
80 web_ui_->CallJavascriptFunction("setShownSections", sections_value); | |
81 } else { | |
82 NOTREACHED(); | |
83 } | |
84 } | |
85 | |
86 void ShownSectionsHandler::HandleSetShownSections(const ListValue* args) { | |
87 double mode_double; | |
88 CHECK(args->GetDouble(0, &mode_double)); | |
89 int mode = static_cast<int>(mode_double); | |
90 int old_mode = pref_service_->GetInteger(prefs::kNTPShownSections); | |
91 | |
92 if (old_mode != mode) { | |
93 NotifySectionDisabled(mode, old_mode, Profile::FromWebUI(web_ui_)); | |
94 pref_service_->SetInteger(prefs::kNTPShownSections, mode); | |
95 } | |
96 } | |
97 | |
98 // static | |
99 void ShownSectionsHandler::RegisterUserPrefs(PrefService* pref_service) { | |
100 #if defined(OS_CHROMEOS) | |
101 // Default to have expanded APPS and all other sections are minimized. | |
102 pref_service->RegisterIntegerPref(prefs::kNTPShownSections, | |
103 APPS | MENU_THUMB | MENU_RECENT, | |
104 PrefService::UNSYNCABLE_PREF); | |
105 #else | |
106 pref_service->RegisterIntegerPref(prefs::kNTPShownSections, | |
107 THUMB, | |
108 PrefService::UNSYNCABLE_PREF); | |
109 #endif | |
110 } | |
111 | |
112 // static | |
113 void ShownSectionsHandler::MigrateUserPrefs(PrefService* pref_service, | |
114 int old_pref_version, | |
115 int new_pref_version) { | |
116 // Nothing to migrate for default kNTPShownSections value. | |
117 const PrefService::Preference* shown_sections_pref = | |
118 pref_service->FindPreference(prefs::kNTPShownSections); | |
119 if (!shown_sections_pref || shown_sections_pref->IsDefaultValue()) | |
120 return; | |
121 | |
122 bool changed = false; | |
123 int shown_sections = pref_service->GetInteger(prefs::kNTPShownSections); | |
124 | |
125 if (old_pref_version < 3) { | |
126 // In version 3, we went from being able to show multiple sections to being | |
127 // able to show only one expanded at a time. The only two expandable | |
128 // sections are APPS and THUMBS. | |
129 if (shown_sections & APPS) | |
130 shown_sections = APPS; | |
131 else | |
132 shown_sections = THUMB; | |
133 | |
134 changed = true; | |
135 } | |
136 | |
137 if (changed) | |
138 pref_service->SetInteger(prefs::kNTPShownSections, shown_sections); | |
139 } | |
140 | |
141 // static | |
142 void ShownSectionsHandler::OnExtensionInstalled(PrefService* prefs, | |
143 const Extension* extension) { | |
144 if (extension->is_app()) { | |
145 int mode = prefs->GetInteger(prefs::kNTPShownSections); | |
146 | |
147 // De-menu-mode the apps section. | |
148 mode &= ~MENU_APPS; | |
149 | |
150 // Hide any open sections. | |
151 mode &= ~ALL_SECTIONS_MASK; | |
152 | |
153 // Show the apps section. | |
154 mode |= APPS; | |
155 | |
156 prefs->SetInteger(prefs::kNTPShownSections, mode); | |
157 } | |
158 } | |
OLD | NEW |