| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/dom_ui/shown_sections_handler.h" | 5 #include "chrome/browser/dom_ui/shown_sections_handler.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/metrics/user_metrics.h" | 11 #include "chrome/browser/metrics/user_metrics.h" |
| 12 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
| 13 #include "chrome/browser/profile.h" | 13 #include "chrome/browser/profile.h" |
| 14 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/common/extensions/extension.h" | 15 #include "chrome/common/extensions/extension.h" |
| 16 #include "chrome/common/notification_details.h" | 16 #include "chrome/common/notification_details.h" |
| 17 #include "chrome/common/notification_source.h" | |
| 18 #include "chrome/common/notification_type.h" | 17 #include "chrome/common/notification_type.h" |
| 19 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
| 20 | 19 |
| 21 namespace { | 20 namespace { |
| 22 | 21 |
| 23 // Will cause an UMA notification if the mode of the new tab page | 22 // Will cause an UMA notification if the mode of the new tab page |
| 24 // was changed to hide/show the most visited thumbnails. | 23 // was changed to hide/show the most visited thumbnails. |
| 25 // TODO(aa): Needs to be updated to match newest NTP - http://crbug.com/57440 | 24 // TODO(aa): Needs to be updated to match newest NTP - http://crbug.com/57440 |
| 26 void NotifySectionDisabled(int new_mode, int old_mode, Profile *profile) { | 25 void NotifySectionDisabled(int new_mode, int old_mode, Profile *profile) { |
| 27 // If the oldmode HAD either thumbs or lists visible. | 26 // If the oldmode HAD either thumbs or lists visible. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 48 return prefs->GetInteger(prefs::kNTPShownSections); | 47 return prefs->GetInteger(prefs::kNTPShownSections); |
| 49 } | 48 } |
| 50 | 49 |
| 51 ShownSectionsHandler::ShownSectionsHandler(PrefService* pref_service) | 50 ShownSectionsHandler::ShownSectionsHandler(PrefService* pref_service) |
| 52 : pref_service_(pref_service) { | 51 : pref_service_(pref_service) { |
| 53 pref_registrar_.Init(pref_service); | 52 pref_registrar_.Init(pref_service); |
| 54 pref_registrar_.Add(prefs::kNTPShownSections, this); | 53 pref_registrar_.Add(prefs::kNTPShownSections, this); |
| 55 } | 54 } |
| 56 | 55 |
| 57 void ShownSectionsHandler::RegisterMessages() { | 56 void ShownSectionsHandler::RegisterMessages() { |
| 58 notification_registrar_.Add(this, NotificationType::EXTENSION_INSTALLED, | |
| 59 Source<Profile>(dom_ui_->GetProfile())); | |
| 60 | |
| 61 dom_ui_->RegisterMessageCallback("getShownSections", | 57 dom_ui_->RegisterMessageCallback("getShownSections", |
| 62 NewCallback(this, &ShownSectionsHandler::HandleGetShownSections)); | 58 NewCallback(this, &ShownSectionsHandler::HandleGetShownSections)); |
| 63 dom_ui_->RegisterMessageCallback("setShownSections", | 59 dom_ui_->RegisterMessageCallback("setShownSections", |
| 64 NewCallback(this, &ShownSectionsHandler::HandleSetShownSections)); | 60 NewCallback(this, &ShownSectionsHandler::HandleSetShownSections)); |
| 65 } | 61 } |
| 66 | 62 |
| 67 void ShownSectionsHandler::Observe(NotificationType type, | 63 void ShownSectionsHandler::Observe(NotificationType type, |
| 68 const NotificationSource& source, | 64 const NotificationSource& source, |
| 69 const NotificationDetails& details) { | 65 const NotificationDetails& details) { |
| 70 if (type == NotificationType::PREF_CHANGED) { | 66 if (type == NotificationType::PREF_CHANGED) { |
| 71 std::string* pref_name = Details<std::string>(details).ptr(); | 67 std::string* pref_name = Details<std::string>(details).ptr(); |
| 72 DCHECK(*pref_name == prefs::kNTPShownSections); | 68 DCHECK(*pref_name == prefs::kNTPShownSections); |
| 73 int sections = pref_service_->GetInteger(prefs::kNTPShownSections); | 69 int sections = pref_service_->GetInteger(prefs::kNTPShownSections); |
| 74 FundamentalValue sections_value(sections); | 70 FundamentalValue sections_value(sections); |
| 75 dom_ui_->CallJavascriptFunction(L"setShownSections", sections_value); | 71 dom_ui_->CallJavascriptFunction(L"setShownSections", sections_value); |
| 76 } else if (type == NotificationType::EXTENSION_INSTALLED) { | |
| 77 if (Details<const Extension>(details).ptr()->is_app()) { | |
| 78 int mode = pref_service_->GetInteger(prefs::kNTPShownSections); | |
| 79 | |
| 80 // De-minimize the apps section. | |
| 81 mode &= ~MINIMIZED_APPS; | |
| 82 | |
| 83 // Hide any open sections. | |
| 84 mode &= ~ALL_SECTIONS_MASK; | |
| 85 | |
| 86 // Show the apps section. | |
| 87 mode |= APPS; | |
| 88 | |
| 89 pref_service_->SetInteger(prefs::kNTPShownSections, mode); | |
| 90 } | |
| 91 } else { | 72 } else { |
| 92 NOTREACHED(); | 73 NOTREACHED(); |
| 93 } | 74 } |
| 94 } | 75 } |
| 95 | 76 |
| 96 void ShownSectionsHandler::HandleGetShownSections(const ListValue* args) { | 77 void ShownSectionsHandler::HandleGetShownSections(const ListValue* args) { |
| 97 int sections = GetShownSections(pref_service_); | 78 int sections = GetShownSections(pref_service_); |
| 98 FundamentalValue sections_value(sections); | 79 FundamentalValue sections_value(sections); |
| 99 dom_ui_->CallJavascriptFunction(L"onShownSections", sections_value); | 80 dom_ui_->CallJavascriptFunction(L"onShownSections", sections_value); |
| 100 } | 81 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 130 shown_sections = APPS; | 111 shown_sections = APPS; |
| 131 else | 112 else |
| 132 shown_sections = THUMB; | 113 shown_sections = THUMB; |
| 133 | 114 |
| 134 changed = true; | 115 changed = true; |
| 135 } | 116 } |
| 136 | 117 |
| 137 if (changed) | 118 if (changed) |
| 138 pref_service->SetInteger(prefs::kNTPShownSections, shown_sections); | 119 pref_service->SetInteger(prefs::kNTPShownSections, shown_sections); |
| 139 } | 120 } |
| 121 |
| 122 // static |
| 123 void ShownSectionsHandler::OnExtensionInstalled(PrefService* prefs, |
| 124 const Extension* extension) { |
| 125 if (extension->is_app()) { |
| 126 int mode = prefs->GetInteger(prefs::kNTPShownSections); |
| 127 |
| 128 // De-minimize the apps section. |
| 129 mode &= ~MINIMIZED_APPS; |
| 130 |
| 131 // Hide any open sections. |
| 132 mode &= ~ALL_SECTIONS_MASK; |
| 133 |
| 134 // Show the apps section. |
| 135 mode |= APPS; |
| 136 |
| 137 prefs->SetInteger(prefs::kNTPShownSections, mode); |
| 138 } |
| 139 } |
| OLD | NEW |