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 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_UI_WEBUI_NTP_APP_LAUNCHER_HANDLER_H_ 6 #define CHROME_BROWSER_UI_WEBUI_NTP_APP_LAUNCHER_HANDLER_H_ 7 8 #include <set> 9 #include <string> 10 11 #include "apps/metrics_names.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/prefs/pref_change_registrar.h" 14 #include "chrome/browser/extensions/extension_uninstall_dialog.h" 15 #include "chrome/browser/favicon/favicon_service.h" 16 #include "chrome/browser/ui/extensions/extension_enable_flow_delegate.h" 17 #include "chrome/common/cancelable_task_tracker.h" 18 #include "chrome/common/extensions/extension.h" 19 #include "chrome/common/extensions/extension_constants.h" 20 #include "content/public/browser/notification_observer.h" 21 #include "content/public/browser/notification_registrar.h" 22 #include "content/public/browser/web_ui_message_handler.h" 23 #include "sync/api/string_ordinal.h" 24 25 class ExtensionEnableFlow; 26 class ExtensionService; 27 class PrefChangeRegistrar; 28 class Profile; 29 30 namespace chrome { 31 struct FaviconImageResult; 32 } 33 34 // The handler for Javascript messages related to the "apps" view. 35 class AppLauncherHandler : public content::WebUIMessageHandler, 36 public ExtensionUninstallDialog::Delegate, 37 public ExtensionEnableFlowDelegate, 38 public content::NotificationObserver { 39 public: 40 explicit AppLauncherHandler(ExtensionService* extension_service); 41 virtual ~AppLauncherHandler(); 42 43 // Populate a dictionary with the information from an extension. 44 static void CreateAppInfo( 45 const extensions::Extension* extension, 46 ExtensionService* service, 47 base::DictionaryValue* value); 48 49 // WebUIMessageHandler implementation. 50 virtual void RegisterMessages() OVERRIDE; 51 52 // content::NotificationObserver 53 virtual void Observe(int type, 54 const content::NotificationSource& source, 55 const content::NotificationDetails& details) OVERRIDE; 56 57 // Populate the given dictionary with all installed app info. 58 void FillAppDictionary(base::DictionaryValue* value); 59 60 // Create a dictionary value for the given extension. May return NULL, e.g. if 61 // the given extension is not an app. If non-NULL, the caller assumes 62 // ownership of the pointer. 63 base::DictionaryValue* GetAppInfo(const extensions::Extension* extension); 64 65 // Populate the given dictionary with the web store promo content. 66 void FillPromoDictionary(base::DictionaryValue* value); 67 68 // Handles the "launchApp" message with unused |args|. 69 void HandleGetApps(const base::ListValue* args); 70 71 // Handles the "launchApp" message with |args| containing [extension_id, 72 // source] with optional [url, disposition], |disposition| defaulting to 73 // CURRENT_TAB. 74 void HandleLaunchApp(const base::ListValue* args); 75 76 // Handles the "setLaunchType" message with args containing [extension_id, 77 // launch_type]. 78 void HandleSetLaunchType(const base::ListValue* args); 79 80 // Handles the "uninstallApp" message with |args| containing [extension_id] 81 // and an optional bool to not confirm the uninstall when true, defaults to 82 // false. 83 void HandleUninstallApp(const base::ListValue* args); 84 85 // Handles the "createAppShortcut" message with |args| containing 86 // [extension_id]. 87 void HandleCreateAppShortcut(const base::ListValue* args); 88 89 // Handles the "reorderApps" message with |args| containing [dragged_app_id, 90 // app_order]. 91 void HandleReorderApps(const base::ListValue* args); 92 93 // Handles the "setPageIndex" message with |args| containing [extension_id, 94 // page_index]. 95 void HandleSetPageIndex(const base::ListValue* args); 96 97 // Handles "saveAppPageName" message with |args| containing [name, 98 // page_index]. 99 void HandleSaveAppPageName(const base::ListValue* args); 100 101 // Handles "generateAppForLink" message with |args| containing [url, title, 102 // page_index]. 103 void HandleGenerateAppForLink(const base::ListValue* args); 104 105 // Other registered message callbacks with unused |args|. 106 void StopShowingAppLauncherPromo(const base::ListValue* args); 107 void OnLearnMore(const base::ListValue* args); 108 109 private: 110 struct AppInstallInfo { 111 AppInstallInfo(); 112 ~AppInstallInfo(); 113 114 bool is_bookmark_app; 115 string16 title; 116 GURL app_url; 117 syncer::StringOrdinal page_ordinal; 118 }; 119 120 // Reset some instance flags we use to track the currently uninstalling app. 121 void CleanupAfterUninstall(); 122 123 // Prompts the user to re-enable the app for |extension_id|. 124 void PromptToEnableApp(const std::string& extension_id); 125 126 // ExtensionUninstallDialog::Delegate: 127 virtual void ExtensionUninstallAccepted() OVERRIDE; 128 virtual void ExtensionUninstallCanceled() OVERRIDE; 129 130 // ExtensionEnableFlowDelegate: 131 virtual void ExtensionEnableFlowFinished() OVERRIDE; 132 virtual void ExtensionEnableFlowAborted(bool user_initiated) OVERRIDE; 133 134 // Returns the ExtensionUninstallDialog object for this class, creating it if 135 // needed. 136 ExtensionUninstallDialog* GetExtensionUninstallDialog(); 137 138 // Continuation for installing a bookmark app after favicon lookup. 139 void OnFaviconForApp(scoped_ptr<AppInstallInfo> install_info, 140 const chrome::FaviconImageResult& image_result); 141 142 // Sends |highlight_app_id_| to the js. 143 void SetAppToBeHighlighted(); 144 145 void OnExtensionPreferenceChanged(); 146 147 void OnLocalStatePreferenceChanged(); 148 149 // The apps are represented in the extensions model, which 150 // outlives us since it's owned by our containing profile. 151 ExtensionService* const extension_service_; 152 153 // We monitor changes to the extension system so that we can reload the apps 154 // when necessary. 155 content::NotificationRegistrar registrar_; 156 157 // Monitor extension preference changes so that the Web UI can be notified. 158 PrefChangeRegistrar extension_pref_change_registrar_; 159 160 // Monitor the local state pref to control the app launcher promo. 161 PrefChangeRegistrar local_state_pref_change_registrar_; 162 163 // Used to show confirmation UI for uninstalling extensions in incognito mode. 164 scoped_ptr<ExtensionUninstallDialog> extension_uninstall_dialog_; 165 166 // Used to show confirmation UI for enabling extensions. 167 scoped_ptr<ExtensionEnableFlow> extension_enable_flow_; 168 169 // The ids of apps to show on the NTP. 170 std::set<std::string> visible_apps_; 171 172 // The id of the extension we are prompting the user about (either enable or 173 // uninstall). 174 std::string extension_id_prompting_; 175 176 // When true, we ignore changes to the underlying data rather than immediately 177 // refreshing. This is useful when making many batch updates to avoid flicker. 178 bool ignore_changes_; 179 180 // When true, we have attempted to install a bookmark app, and are still 181 // waiting to hear about success or failure from the extensions system. 182 bool attempted_bookmark_app_install_; 183 184 // True if we have executed HandleGetApps() at least once. 185 bool has_loaded_apps_; 186 187 // The ID of the app to be highlighted on the NTP (i.e. shown on the page 188 // and pulsed). This is done for new installs. The actual higlighting occurs 189 // when the app is added to the page (via getAppsCallback or appAdded). 190 std::string highlight_app_id_; 191 192 // Used for favicon loading tasks. 193 CancelableTaskTracker cancelable_task_tracker_; 194 195 DISALLOW_COPY_AND_ASSIGN(AppLauncherHandler); 196 }; 197 198 #endif // CHROME_BROWSER_UI_WEBUI_NTP_APP_LAUNCHER_HANDLER_H_ 199