OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/app_launcher_handler.h" | 5 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
13 #include "base/i18n/rtl.h" | 13 #include "base/i18n/rtl.h" |
14 #include "base/metrics/field_trial.h" | 14 #include "base/metrics/field_trial.h" |
15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
16 #include "base/string_number_conversions.h" | 16 #include "base/string_number_conversions.h" |
17 #include "base/string_split.h" | 17 #include "base/string_split.h" |
18 #include "base/string_util.h" | 18 #include "base/string_util.h" |
19 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
20 #include "base/values.h" | 20 #include "base/values.h" |
| 21 #include "chrome/browser/extensions/app_notification.h" |
21 #include "chrome/browser/extensions/app_notification_manager.h" | 22 #include "chrome/browser/extensions/app_notification_manager.h" |
22 #include "chrome/browser/extensions/apps_promo.h" | 23 #include "chrome/browser/extensions/apps_promo.h" |
23 #include "chrome/browser/extensions/crx_installer.h" | 24 #include "chrome/browser/extensions/crx_installer.h" |
24 #include "chrome/browser/extensions/extension_prefs.h" | 25 #include "chrome/browser/extensions/extension_prefs.h" |
25 #include "chrome/browser/extensions/extension_service.h" | 26 #include "chrome/browser/extensions/extension_service.h" |
26 #include "chrome/browser/extensions/extension_sorting.h" | 27 #include "chrome/browser/extensions/extension_sorting.h" |
27 #include "chrome/browser/extensions/extension_system.h" | 28 #include "chrome/browser/extensions/extension_system.h" |
28 #include "chrome/browser/prefs/pref_service.h" | 29 #include "chrome/browser/prefs/pref_service.h" |
29 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 30 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
30 #include "chrome/browser/profiles/profile.h" | 31 #include "chrome/browser/profiles/profile.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 ignore_changes_(false), | 87 ignore_changes_(false), |
87 attempted_bookmark_app_install_(false), | 88 attempted_bookmark_app_install_(false), |
88 has_loaded_apps_(false) { | 89 has_loaded_apps_(false) { |
89 } | 90 } |
90 | 91 |
91 AppLauncherHandler::~AppLauncherHandler() {} | 92 AppLauncherHandler::~AppLauncherHandler() {} |
92 | 93 |
93 // Serializes |notification| into a new DictionaryValue which the caller then | 94 // Serializes |notification| into a new DictionaryValue which the caller then |
94 // owns. | 95 // owns. |
95 static DictionaryValue* SerializeNotification( | 96 static DictionaryValue* SerializeNotification( |
96 const AppNotification& notification) { | 97 const extensions::AppNotification& notification) { |
97 DictionaryValue* dictionary = new DictionaryValue(); | 98 DictionaryValue* dictionary = new DictionaryValue(); |
98 dictionary->SetString("title", notification.title()); | 99 dictionary->SetString("title", notification.title()); |
99 dictionary->SetString("body", notification.body()); | 100 dictionary->SetString("body", notification.body()); |
100 if (!notification.link_url().is_empty()) { | 101 if (!notification.link_url().is_empty()) { |
101 dictionary->SetString("linkUrl", notification.link_url().spec()); | 102 dictionary->SetString("linkUrl", notification.link_url().spec()); |
102 dictionary->SetString("linkText", notification.link_text()); | 103 dictionary->SetString("linkText", notification.link_text()); |
103 } | 104 } |
104 return dictionary; | 105 return dictionary; |
105 } | 106 } |
106 | 107 |
107 void AppLauncherHandler::CreateAppInfo(const Extension* extension, | 108 void AppLauncherHandler::CreateAppInfo( |
108 const AppNotification* notification, | 109 const Extension* extension, |
109 ExtensionService* service, | 110 const extensions::AppNotification* notification, |
110 DictionaryValue* value) { | 111 ExtensionService* service, |
| 112 DictionaryValue* value) { |
111 value->Clear(); | 113 value->Clear(); |
112 | 114 |
113 // The Extension class 'helpfully' wraps bidi control characters that | 115 // The Extension class 'helpfully' wraps bidi control characters that |
114 // impede our ability to determine directionality. | 116 // impede our ability to determine directionality. |
115 string16 name = UTF8ToUTF16(extension->name()); | 117 string16 name = UTF8ToUTF16(extension->name()); |
116 base::i18n::UnadjustStringForLocaleDirection(&name); | 118 base::i18n::UnadjustStringForLocaleDirection(&name); |
117 NewTabUI::SetURLTitleAndDirection(value, name, extension->GetFullLaunchURL()); | 119 NewTabUI::SetURLTitleAndDirection(value, name, extension->GetFullLaunchURL()); |
118 | 120 |
119 bool enabled = service->IsExtensionEnabled(extension->id()) && | 121 bool enabled = service->IsExtensionEnabled(extension->id()) && |
120 !service->GetTerminatedExtension(extension->id()); | 122 !service->GetTerminatedExtension(extension->id()); |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 return; | 244 return; |
243 } | 245 } |
244 | 246 |
245 if (ignore_changes_ || !has_loaded_apps_) | 247 if (ignore_changes_ || !has_loaded_apps_) |
246 return; | 248 return; |
247 | 249 |
248 switch (type) { | 250 switch (type) { |
249 case chrome::NOTIFICATION_APP_NOTIFICATION_STATE_CHANGED: { | 251 case chrome::NOTIFICATION_APP_NOTIFICATION_STATE_CHANGED: { |
250 const std::string& id = | 252 const std::string& id = |
251 *content::Details<const std::string>(details).ptr(); | 253 *content::Details<const std::string>(details).ptr(); |
252 const AppNotification* notification = | 254 const extensions::AppNotification* notification = |
253 extension_service_->app_notification_manager()->GetLast(id); | 255 extension_service_->app_notification_manager()->GetLast(id); |
254 base::StringValue id_value(id); | 256 base::StringValue id_value(id); |
255 if (notification) { | 257 if (notification) { |
256 scoped_ptr<DictionaryValue> notification_value( | 258 scoped_ptr<DictionaryValue> notification_value( |
257 SerializeNotification(*notification)); | 259 SerializeNotification(*notification)); |
258 web_ui()->CallJavascriptFunction("ntp.appNotificationChanged", | 260 web_ui()->CallJavascriptFunction("ntp.appNotificationChanged", |
259 id_value, *notification_value.get()); | 261 id_value, *notification_value.get()); |
260 } else { | 262 } else { |
261 web_ui()->CallJavascriptFunction("ntp.appNotificationChanged", | 263 web_ui()->CallJavascriptFunction("ntp.appNotificationChanged", |
262 id_value); | 264 id_value); |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 l10n_util::GetStringUTF16(IDS_APP_DEFAULT_PAGE_NAME))); | 401 l10n_util::GetStringUTF16(IDS_APP_DEFAULT_PAGE_NAME))); |
400 dictionary->Set("appPageNames", | 402 dictionary->Set("appPageNames", |
401 static_cast<ListValue*>(list->DeepCopy())); | 403 static_cast<ListValue*>(list->DeepCopy())); |
402 } else { | 404 } else { |
403 dictionary->Set("appPageNames", | 405 dictionary->Set("appPageNames", |
404 static_cast<ListValue*>(app_page_names->DeepCopy())); | 406 static_cast<ListValue*>(app_page_names->DeepCopy())); |
405 } | 407 } |
406 } | 408 } |
407 | 409 |
408 DictionaryValue* AppLauncherHandler::GetAppInfo(const Extension* extension) { | 410 DictionaryValue* AppLauncherHandler::GetAppInfo(const Extension* extension) { |
409 AppNotificationManager* notification_manager = | 411 extensions::AppNotificationManager* notification_manager = |
410 extension_service_->app_notification_manager(); | 412 extension_service_->app_notification_manager(); |
411 DictionaryValue* app_info = new DictionaryValue(); | 413 DictionaryValue* app_info = new DictionaryValue(); |
412 // CreateAppInfo can change the extension prefs. | 414 // CreateAppInfo can change the extension prefs. |
413 AutoReset<bool> auto_reset(&ignore_changes_, true); | 415 AutoReset<bool> auto_reset(&ignore_changes_, true); |
414 CreateAppInfo(extension, | 416 CreateAppInfo(extension, |
415 notification_manager->GetLast(extension->id()), | 417 notification_manager->GetLast(extension->id()), |
416 extension_service_, | 418 extension_service_, |
417 app_info); | 419 app_info); |
418 return app_info; | 420 return app_info; |
419 } | 421 } |
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
766 std::string extension_id; | 768 std::string extension_id; |
767 CHECK(args->GetString(0, &extension_id)); | 769 CHECK(args->GetString(0, &extension_id)); |
768 | 770 |
769 const Extension* extension = extension_service_->GetExtensionById( | 771 const Extension* extension = extension_service_->GetExtensionById( |
770 extension_id, true); | 772 extension_id, true); |
771 if (!extension) | 773 if (!extension) |
772 return; | 774 return; |
773 | 775 |
774 UMA_HISTOGRAM_COUNTS("AppNotification.NTPNotificationClosed", 1); | 776 UMA_HISTOGRAM_COUNTS("AppNotification.NTPNotificationClosed", 1); |
775 | 777 |
776 AppNotificationManager* notification_manager = | 778 extensions::AppNotificationManager* notification_manager = |
777 extension_service_->app_notification_manager(); | 779 extension_service_->app_notification_manager(); |
778 notification_manager->ClearAll(extension_id); | 780 notification_manager->ClearAll(extension_id); |
779 } | 781 } |
780 | 782 |
781 void AppLauncherHandler::HandleSetNotificationsDisabled( | 783 void AppLauncherHandler::HandleSetNotificationsDisabled( |
782 const ListValue* args) { | 784 const ListValue* args) { |
783 std::string extension_id; | 785 std::string extension_id; |
784 bool disabled = false; | 786 bool disabled = false; |
785 CHECK(args->GetString(0, &extension_id)); | 787 CHECK(args->GetString(0, &extension_id)); |
786 CHECK(args->GetBoolean(1, &disabled)); | 788 CHECK(args->GetBoolean(1, &disabled)); |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
995 | 997 |
996 void AppLauncherHandler::UninstallDefaultApps() { | 998 void AppLauncherHandler::UninstallDefaultApps() { |
997 AppsPromo* apps_promo = extension_service_->apps_promo(); | 999 AppsPromo* apps_promo = extension_service_->apps_promo(); |
998 const extensions::ExtensionIdSet& app_ids = apps_promo->old_default_apps(); | 1000 const extensions::ExtensionIdSet& app_ids = apps_promo->old_default_apps(); |
999 for (extensions::ExtensionIdSet::const_iterator iter = app_ids.begin(); | 1001 for (extensions::ExtensionIdSet::const_iterator iter = app_ids.begin(); |
1000 iter != app_ids.end(); ++iter) { | 1002 iter != app_ids.end(); ++iter) { |
1001 if (extension_service_->GetExtensionById(*iter, true)) | 1003 if (extension_service_->GetExtensionById(*iter, true)) |
1002 extension_service_->UninstallExtension(*iter, false, NULL); | 1004 extension_service_->UninstallExtension(*iter, false, NULL); |
1003 } | 1005 } |
1004 } | 1006 } |
OLD | NEW |