| 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/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" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 } // namespace | 66 } // namespace |
| 67 | 67 |
| 68 AppLauncherHandler::AppLauncherHandler(ExtensionService* extension_service) | 68 AppLauncherHandler::AppLauncherHandler(ExtensionService* extension_service) |
| 69 : extensions_service_(extension_service), | 69 : extensions_service_(extension_service), |
| 70 promo_active_(false), | 70 promo_active_(false), |
| 71 ignore_changes_(false) { | 71 ignore_changes_(false) { |
| 72 } | 72 } |
| 73 | 73 |
| 74 AppLauncherHandler::~AppLauncherHandler() {} | 74 AppLauncherHandler::~AppLauncherHandler() {} |
| 75 | 75 |
| 76 // Serializes |notification| into a new DictionaryValue which the caller then |
| 77 // owns. |
| 78 static DictionaryValue* SerializeNotification( |
| 79 const AppNotification& notification) { |
| 80 DictionaryValue* dictionary = new DictionaryValue(); |
| 81 dictionary->SetString("title", notification.title); |
| 82 dictionary->SetString("body", notification.body); |
| 83 if (!notification.linkUrl.is_empty()) { |
| 84 dictionary->SetString("linkUrl", notification.linkUrl.spec()); |
| 85 dictionary->SetString("linkText", notification.linkText); |
| 86 } |
| 87 return dictionary; |
| 88 } |
| 89 |
| 76 // static | 90 // static |
| 77 void AppLauncherHandler::CreateAppInfo(const Extension* extension, | 91 void AppLauncherHandler::CreateAppInfo(const Extension* extension, |
| 92 const AppNotification* notification, |
| 78 ExtensionPrefs* prefs, | 93 ExtensionPrefs* prefs, |
| 79 DictionaryValue* value) { | 94 DictionaryValue* value) { |
| 80 bool enabled = | 95 bool enabled = |
| 81 prefs->GetExtensionState(extension->id()) != Extension::DISABLED; | 96 prefs->GetExtensionState(extension->id()) != Extension::DISABLED; |
| 82 GURL icon_big = | 97 GURL icon_big = |
| 83 ExtensionIconSource::GetIconURL(extension, | 98 ExtensionIconSource::GetIconURL(extension, |
| 84 Extension::EXTENSION_ICON_LARGE, | 99 Extension::EXTENSION_ICON_LARGE, |
| 85 ExtensionIconSet::MATCH_EXACTLY, | 100 ExtensionIconSet::MATCH_EXACTLY, |
| 86 !enabled); | 101 !enabled); |
| 87 GURL icon_small = | 102 GURL icon_small = |
| (...skipping 14 matching lines...) Expand all Loading... |
| 102 Extension::UserMayDisable(extension->location())); | 117 Extension::UserMayDisable(extension->location())); |
| 103 value->SetString("icon_big", icon_big.spec()); | 118 value->SetString("icon_big", icon_big.spec()); |
| 104 value->SetString("icon_small", icon_small.spec()); | 119 value->SetString("icon_small", icon_small.spec()); |
| 105 value->SetInteger("launch_container", extension->launch_container()); | 120 value->SetInteger("launch_container", extension->launch_container()); |
| 106 value->SetInteger("launch_type", | 121 value->SetInteger("launch_type", |
| 107 prefs->GetLaunchType(extension->id(), | 122 prefs->GetLaunchType(extension->id(), |
| 108 ExtensionPrefs::LAUNCH_DEFAULT)); | 123 ExtensionPrefs::LAUNCH_DEFAULT)); |
| 109 value->SetBoolean("is_component", | 124 value->SetBoolean("is_component", |
| 110 extension->location() == Extension::COMPONENT); | 125 extension->location() == Extension::COMPONENT); |
| 111 | 126 |
| 127 if (notification) |
| 128 value->Set("notification", SerializeNotification(*notification)); |
| 129 |
| 112 int app_launch_index = prefs->GetAppLaunchIndex(extension->id()); | 130 int app_launch_index = prefs->GetAppLaunchIndex(extension->id()); |
| 113 if (app_launch_index == -1) { | 131 if (app_launch_index == -1) { |
| 114 // Make sure every app has a launch index (some predate the launch index). | 132 // Make sure every app has a launch index (some predate the launch index). |
| 115 app_launch_index = prefs->GetNextAppLaunchIndex(); | 133 app_launch_index = prefs->GetNextAppLaunchIndex(); |
| 116 prefs->SetAppLaunchIndex(extension->id(), app_launch_index); | 134 prefs->SetAppLaunchIndex(extension->id(), app_launch_index); |
| 117 } | 135 } |
| 118 value->SetInteger("app_launch_index", app_launch_index); | 136 value->SetInteger("app_launch_index", app_launch_index); |
| 119 | 137 |
| 120 int page_index = prefs->GetPageIndex(extension->id()); | 138 int page_index = prefs->GetPageIndex(extension->id()); |
| 121 if (page_index >= 0) { | 139 if (page_index >= 0) { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 NewCallback(this, &AppLauncherHandler::HandlePromoSeen)); | 209 NewCallback(this, &AppLauncherHandler::HandlePromoSeen)); |
| 192 } | 210 } |
| 193 | 211 |
| 194 void AppLauncherHandler::Observe(NotificationType type, | 212 void AppLauncherHandler::Observe(NotificationType type, |
| 195 const NotificationSource& source, | 213 const NotificationSource& source, |
| 196 const NotificationDetails& details) { | 214 const NotificationDetails& details) { |
| 197 if (ignore_changes_) | 215 if (ignore_changes_) |
| 198 return; | 216 return; |
| 199 | 217 |
| 200 switch (type.value) { | 218 switch (type.value) { |
| 219 case NotificationType::APP_NOTIFICATION_STATE_CHANGED: { |
| 220 const std::string& id = *Details<const std::string>(details).ptr(); |
| 221 const AppNotification* notification = |
| 222 extensions_service_->app_notification_manager()->GetLast(id); |
| 223 ListValue args; |
| 224 args.Append(new StringValue(id)); |
| 225 if (notification) |
| 226 args.Append(SerializeNotification(*notification)); |
| 227 web_ui_->CallJavascriptFunction("appsNotificationChangeCallback", args); |
| 228 break; |
| 229 } |
| 201 case NotificationType::EXTENSION_LOADED: | 230 case NotificationType::EXTENSION_LOADED: |
| 202 case NotificationType::EXTENSION_UNLOADED: | 231 case NotificationType::EXTENSION_UNLOADED: |
| 203 case NotificationType::EXTENSION_LAUNCHER_REORDERED: | 232 case NotificationType::EXTENSION_LAUNCHER_REORDERED: |
| 204 // The promo may not load until a couple seconds after the first NTP view, | 233 // The promo may not load until a couple seconds after the first NTP view, |
| 205 // so we listen for the load notification and notify the NTP when ready. | 234 // so we listen for the load notification and notify the NTP when ready. |
| 206 case NotificationType::WEB_STORE_PROMO_LOADED: | 235 case NotificationType::WEB_STORE_PROMO_LOADED: |
| 207 if (web_ui_->tab_contents()) | 236 if (web_ui_->tab_contents()) |
| 208 HandleGetApps(NULL); | 237 HandleGetApps(NULL); |
| 209 break; | 238 break; |
| 210 case NotificationType::PREF_CHANGED: { | 239 case NotificationType::PREF_CHANGED: { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 221 } | 250 } |
| 222 } | 251 } |
| 223 | 252 |
| 224 void AppLauncherHandler::FillAppDictionary(DictionaryValue* dictionary) { | 253 void AppLauncherHandler::FillAppDictionary(DictionaryValue* dictionary) { |
| 225 ListValue* list = new ListValue(); | 254 ListValue* list = new ListValue(); |
| 226 const ExtensionList* extensions = extensions_service_->extensions(); | 255 const ExtensionList* extensions = extensions_service_->extensions(); |
| 227 ExtensionList::const_iterator it; | 256 ExtensionList::const_iterator it; |
| 228 for (it = extensions->begin(); it != extensions->end(); ++it) { | 257 for (it = extensions->begin(); it != extensions->end(); ++it) { |
| 229 // Don't include the WebStore. | 258 // Don't include the WebStore. |
| 230 // The WebStore launcher gets special treatment in ntp/apps.js. | 259 // The WebStore launcher gets special treatment in ntp/apps.js. |
| 231 if ((*it)->is_app() && | 260 const Extension* extension = *it; |
| 232 (*it)->id() != extension_misc::kWebStoreAppId) { | 261 if (extension->is_app() && |
| 262 extension->id() != extension_misc::kWebStoreAppId) { |
| 233 DictionaryValue* app_info = new DictionaryValue(); | 263 DictionaryValue* app_info = new DictionaryValue(); |
| 234 CreateAppInfo(*it, extensions_service_->extension_prefs(), app_info); | 264 AppNotificationManager* notification_manager = |
| 265 extensions_service_->app_notification_manager(); |
| 266 CreateAppInfo(extension, |
| 267 notification_manager->GetLast(extension->id()), |
| 268 extensions_service_->extension_prefs(), |
| 269 app_info); |
| 235 list->Append(app_info); | 270 list->Append(app_info); |
| 236 } | 271 } |
| 237 } | 272 } |
| 238 | 273 |
| 239 extensions = extensions_service_->disabled_extensions(); | 274 extensions = extensions_service_->disabled_extensions(); |
| 240 for (it = extensions->begin(); it != extensions->end(); ++it) { | 275 for (it = extensions->begin(); it != extensions->end(); ++it) { |
| 241 if ((*it)->is_app() && | 276 if ((*it)->is_app() && |
| 242 (*it)->id() != extension_misc::kWebStoreAppId) { | 277 (*it)->id() != extension_misc::kWebStoreAppId) { |
| 243 DictionaryValue* app_info = new DictionaryValue(); | 278 DictionaryValue* app_info = new DictionaryValue(); |
| 244 CreateAppInfo(*it, extensions_service_->extension_prefs(), app_info); | 279 CreateAppInfo(*it, |
| 280 NULL, |
| 281 extensions_service_->extension_prefs(), |
| 282 app_info); |
| 245 list->Append(app_info); | 283 list->Append(app_info); |
| 246 } | 284 } |
| 247 } | 285 } |
| 248 | 286 |
| 249 dictionary->Set("apps", list); | 287 dictionary->Set("apps", list); |
| 250 | 288 |
| 251 #if defined(OS_MACOSX) | 289 #if defined(OS_MACOSX) |
| 252 // App windows are not yet implemented on mac. | 290 // App windows are not yet implemented on mac. |
| 253 dictionary->SetBoolean("disableAppWindowLaunch", true); | 291 dictionary->SetBoolean("disableAppWindowLaunch", true); |
| 254 dictionary->SetBoolean("disableCreateAppShortcut", true); | 292 dictionary->SetBoolean("disableCreateAppShortcut", true); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 ignore_changes_ = false; | 345 ignore_changes_ = false; |
| 308 ShownSectionsHandler::SetShownSection(prefs, THUMB); | 346 ShownSectionsHandler::SetShownSection(prefs, THUMB); |
| 309 } | 347 } |
| 310 | 348 |
| 311 FillAppDictionary(&dictionary); | 349 FillAppDictionary(&dictionary); |
| 312 web_ui_->CallJavascriptFunction("getAppsCallback", dictionary); | 350 web_ui_->CallJavascriptFunction("getAppsCallback", dictionary); |
| 313 | 351 |
| 314 // First time we get here we set up the observer so that we can tell update | 352 // First time we get here we set up the observer so that we can tell update |
| 315 // the apps as they change. | 353 // the apps as they change. |
| 316 if (registrar_.IsEmpty()) { | 354 if (registrar_.IsEmpty()) { |
| 355 registrar_.Add(this, NotificationType::APP_NOTIFICATION_STATE_CHANGED, |
| 356 NotificationService::AllSources()); |
| 317 registrar_.Add(this, NotificationType::EXTENSION_LOADED, | 357 registrar_.Add(this, NotificationType::EXTENSION_LOADED, |
| 318 NotificationService::AllSources()); | 358 NotificationService::AllSources()); |
| 319 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, | 359 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, |
| 320 NotificationService::AllSources()); | 360 NotificationService::AllSources()); |
| 321 registrar_.Add(this, NotificationType::EXTENSION_LAUNCHER_REORDERED, | 361 registrar_.Add(this, NotificationType::EXTENSION_LAUNCHER_REORDERED, |
| 322 NotificationService::AllSources()); | 362 NotificationService::AllSources()); |
| 323 registrar_.Add(this, NotificationType::WEB_STORE_PROMO_LOADED, | 363 registrar_.Add(this, NotificationType::WEB_STORE_PROMO_LOADED, |
| 324 NotificationService::AllSources()); | 364 NotificationService::AllSources()); |
| 325 } | 365 } |
| 326 if (pref_change_registrar_.IsEmpty()) { | 366 if (pref_change_registrar_.IsEmpty()) { |
| (...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 652 | 692 |
| 653 void AppLauncherHandler::UninstallDefaultApps() { | 693 void AppLauncherHandler::UninstallDefaultApps() { |
| 654 AppsPromo* apps_promo = extensions_service_->apps_promo(); | 694 AppsPromo* apps_promo = extensions_service_->apps_promo(); |
| 655 const ExtensionIdSet& app_ids = apps_promo->old_default_apps(); | 695 const ExtensionIdSet& app_ids = apps_promo->old_default_apps(); |
| 656 for (ExtensionIdSet::const_iterator iter = app_ids.begin(); | 696 for (ExtensionIdSet::const_iterator iter = app_ids.begin(); |
| 657 iter != app_ids.end(); ++iter) { | 697 iter != app_ids.end(); ++iter) { |
| 658 if (extensions_service_->GetExtensionById(*iter, true)) | 698 if (extensions_service_->GetExtensionById(*iter, true)) |
| 659 extensions_service_->UninstallExtension(*iter, false, NULL); | 699 extensions_service_->UninstallExtension(*iter, false, NULL); |
| 660 } | 700 } |
| 661 } | 701 } |
| OLD | NEW |