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.
5 #ifndef CHROME_BROWSER_UI_WEBUI_NTP_APP_LAUNCHER_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_NTP_APP_LAUNCHER_HANDLER_H_
11 #include "apps/metrics_names.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/prefs/pref_change_registrar.h"
14 #include "base/task/cancelable_task_tracker.h"
15 #include "chrome/browser/extensions/extension_uninstall_dialog.h"
16 #include "chrome/browser/ui/extensions/extension_enable_flow_delegate.h"
17 #include "chrome/common/extensions/extension_constants.h"
18 #include "components/favicon/core/favicon_service.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
21 #include "content/public/browser/web_ui_message_handler.h"
22 #include "extensions/browser/extension_registry_observer.h"
23 #include "extensions/common/extension.h"
24 #include "sync/api/string_ordinal.h"
26 class ExtensionEnableFlow
;
27 class ExtensionService
;
28 class PrefChangeRegistrar
;
31 namespace favicon_base
{
32 struct FaviconImageResult
;
35 // The handler for Javascript messages related to the "apps" view.
36 class AppLauncherHandler
37 : public content::WebUIMessageHandler
,
38 public extensions::ExtensionUninstallDialog::Delegate
,
39 public ExtensionEnableFlowDelegate
,
40 public content::NotificationObserver
,
41 public extensions::ExtensionRegistryObserver
{
43 explicit AppLauncherHandler(ExtensionService
* extension_service
);
44 ~AppLauncherHandler() override
;
46 // Populate a dictionary with the information from an extension.
47 static void CreateAppInfo(
48 const extensions::Extension
* extension
,
49 ExtensionService
* service
,
50 base::DictionaryValue
* value
);
52 // WebUIMessageHandler:
53 void RegisterMessages() override
;
55 // content::NotificationObserver:
56 void Observe(int type
,
57 const content::NotificationSource
& source
,
58 const content::NotificationDetails
& details
) override
;
60 // extensions::ExtensionRegistryObserver:
61 void OnExtensionLoaded(content::BrowserContext
* browser_context
,
62 const extensions::Extension
* extension
) override
;
63 void OnExtensionUnloaded(
64 content::BrowserContext
* browser_context
,
65 const extensions::Extension
* extension
,
66 extensions::UnloadedExtensionInfo::Reason reason
) override
;
67 void OnExtensionUninstalled(content::BrowserContext
* browser_context
,
68 const extensions::Extension
* extension
,
69 extensions::UninstallReason reason
) override
;
71 // Populate the given dictionary with all installed app info.
72 void FillAppDictionary(base::DictionaryValue
* value
);
74 // Create a dictionary value for the given extension. May return NULL, e.g. if
75 // the given extension is not an app. If non-NULL, the caller assumes
76 // ownership of the pointer.
77 base::DictionaryValue
* GetAppInfo(const extensions::Extension
* extension
);
79 // Populate the given dictionary with the web store promo content.
80 void FillPromoDictionary(base::DictionaryValue
* value
);
82 // Handles the "launchApp" message with unused |args|.
83 void HandleGetApps(const base::ListValue
* args
);
85 // Handles the "launchApp" message with |args| containing [extension_id,
86 // source] with optional [url, disposition], |disposition| defaulting to
88 void HandleLaunchApp(const base::ListValue
* args
);
90 // Handles the "setLaunchType" message with args containing [extension_id,
92 void HandleSetLaunchType(const base::ListValue
* args
);
94 // Handles the "uninstallApp" message with |args| containing [extension_id]
95 // and an optional bool to not confirm the uninstall when true, defaults to
97 void HandleUninstallApp(const base::ListValue
* args
);
99 // Handles the "createAppShortcut" message with |args| containing
101 void HandleCreateAppShortcut(const base::ListValue
* args
);
103 // Handles the "showAppInfo" message with |args| containing [extension_id].
104 void HandleShowAppInfo(const base::ListValue
* args
);
106 // Handles the "reorderApps" message with |args| containing [dragged_app_id,
108 void HandleReorderApps(const base::ListValue
* args
);
110 // Handles the "setPageIndex" message with |args| containing [extension_id,
112 void HandleSetPageIndex(const base::ListValue
* args
);
114 // Handles "saveAppPageName" message with |args| containing [name,
116 void HandleSaveAppPageName(const base::ListValue
* args
);
118 // Handles "generateAppForLink" message with |args| containing [url, title,
120 void HandleGenerateAppForLink(const base::ListValue
* args
);
122 // Other registered message callbacks with unused |args|.
123 void StopShowingAppLauncherPromo(const base::ListValue
* args
);
124 void OnLearnMore(const base::ListValue
* args
);
127 struct AppInstallInfo
{
131 base::string16 title
;
133 syncer::StringOrdinal page_ordinal
;
136 // Reset some instance flags we use to track the currently uninstalling app.
137 void CleanupAfterUninstall();
139 // Prompts the user to re-enable the app for |extension_id|.
140 void PromptToEnableApp(const std::string
& extension_id
);
142 // ExtensionUninstallDialog::Delegate:
143 void OnExtensionUninstallDialogClosed(bool did_start_uninstall
,
144 const base::string16
& error
) override
;
146 // ExtensionEnableFlowDelegate:
147 void ExtensionEnableFlowFinished() override
;
148 void ExtensionEnableFlowAborted(bool user_initiated
) override
;
150 // Returns the ExtensionUninstallDialog object for this class, creating it if
152 extensions::ExtensionUninstallDialog
* GetExtensionUninstallDialog();
154 // Continuation for installing a bookmark app after favicon lookup.
155 void OnFaviconForApp(scoped_ptr
<AppInstallInfo
> install_info
,
156 const favicon_base::FaviconImageResult
& image_result
);
158 // Sends |highlight_app_id_| to the js.
159 void SetAppToBeHighlighted();
161 void OnExtensionPreferenceChanged();
163 void OnLocalStatePreferenceChanged();
165 // Called when an app is removed (unloaded or uninstalled). Updates the UI.
166 void AppRemoved(const extensions::Extension
* extension
, bool is_uninstall
);
168 // True if the extension should be displayed.
169 bool ShouldShow(const extensions::Extension
* extension
) const;
171 // The apps are represented in the extensions model, which
172 // outlives us since it's owned by our containing profile.
173 ExtensionService
* const extension_service_
;
175 // We monitor changes to the extension system so that we can reload the apps
177 content::NotificationRegistrar registrar_
;
179 // Monitor extension preference changes so that the Web UI can be notified.
180 PrefChangeRegistrar extension_pref_change_registrar_
;
182 // Monitor the local state pref to control the app launcher promo.
183 PrefChangeRegistrar local_state_pref_change_registrar_
;
185 // Used to show confirmation UI for uninstalling extensions in incognito mode.
186 scoped_ptr
<extensions::ExtensionUninstallDialog
> extension_uninstall_dialog_
;
188 // Used to show confirmation UI for enabling extensions.
189 scoped_ptr
<ExtensionEnableFlow
> extension_enable_flow_
;
191 // The ids of apps to show on the NTP.
192 std::set
<std::string
> visible_apps_
;
194 // The id of the extension we are prompting the user about (either enable or
196 std::string extension_id_prompting_
;
198 // When true, we ignore changes to the underlying data rather than immediately
199 // refreshing. This is useful when making many batch updates to avoid flicker.
200 bool ignore_changes_
;
202 // When true, we have attempted to install a bookmark app, and are still
203 // waiting to hear about success or failure from the extensions system.
204 bool attempted_bookmark_app_install_
;
206 // True if we have executed HandleGetApps() at least once.
207 bool has_loaded_apps_
;
209 // The ID of the app to be highlighted on the NTP (i.e. shown on the page
210 // and pulsed). This is done for new installs. The actual higlighting occurs
211 // when the app is added to the page (via getAppsCallback or appAdded).
212 std::string highlight_app_id_
;
214 // Used for favicon loading tasks.
215 base::CancelableTaskTracker cancelable_task_tracker_
;
217 DISALLOW_COPY_AND_ASSIGN(AppLauncherHandler
);
220 #endif // CHROME_BROWSER_UI_WEBUI_NTP_APP_LAUNCHER_HANDLER_H_