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