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/common/extension.h"
23 #include "sync/api/string_ordinal.h"
25 class ExtensionEnableFlow
;
26 class ExtensionService
;
27 class PrefChangeRegistrar
;
30 namespace favicon_base
{
31 struct FaviconImageResult
;
34 // The handler for Javascript messages related to the "apps" view.
35 class AppLauncherHandler
36 : public content::WebUIMessageHandler
,
37 public extensions::ExtensionUninstallDialog::Delegate
,
38 public ExtensionEnableFlowDelegate
,
39 public content::NotificationObserver
{
41 explicit AppLauncherHandler(ExtensionService
* extension_service
);
42 ~AppLauncherHandler() override
;
44 // Populate a dictionary with the information from an extension.
45 static void CreateAppInfo(
46 const extensions::Extension
* extension
,
47 ExtensionService
* service
,
48 base::DictionaryValue
* value
);
50 // WebUIMessageHandler implementation.
51 void RegisterMessages() override
;
53 // content::NotificationObserver
54 void Observe(int type
,
55 const content::NotificationSource
& source
,
56 const content::NotificationDetails
& details
) override
;
58 // Populate the given dictionary with all installed app info.
59 void FillAppDictionary(base::DictionaryValue
* value
);
61 // Create a dictionary value for the given extension. May return NULL, e.g. if
62 // the given extension is not an app. If non-NULL, the caller assumes
63 // ownership of the pointer.
64 base::DictionaryValue
* GetAppInfo(const extensions::Extension
* extension
);
66 // Populate the given dictionary with the web store promo content.
67 void FillPromoDictionary(base::DictionaryValue
* value
);
69 // Handles the "launchApp" message with unused |args|.
70 void HandleGetApps(const base::ListValue
* args
);
72 // Handles the "launchApp" message with |args| containing [extension_id,
73 // source] with optional [url, disposition], |disposition| defaulting to
75 void HandleLaunchApp(const base::ListValue
* args
);
77 // Handles the "setLaunchType" message with args containing [extension_id,
79 void HandleSetLaunchType(const base::ListValue
* args
);
81 // Handles the "uninstallApp" message with |args| containing [extension_id]
82 // and an optional bool to not confirm the uninstall when true, defaults to
84 void HandleUninstallApp(const base::ListValue
* args
);
86 // Handles the "createAppShortcut" message with |args| containing
88 void HandleCreateAppShortcut(const base::ListValue
* args
);
90 // Handles the "showAppInfo" message with |args| containing [extension_id].
91 void HandleShowAppInfo(const base::ListValue
* args
);
93 // Handles the "reorderApps" message with |args| containing [dragged_app_id,
95 void HandleReorderApps(const base::ListValue
* args
);
97 // Handles the "setPageIndex" message with |args| containing [extension_id,
99 void HandleSetPageIndex(const base::ListValue
* args
);
101 // Handles "saveAppPageName" message with |args| containing [name,
103 void HandleSaveAppPageName(const base::ListValue
* args
);
105 // Handles "generateAppForLink" message with |args| containing [url, title,
107 void HandleGenerateAppForLink(const base::ListValue
* args
);
109 // Other registered message callbacks with unused |args|.
110 void StopShowingAppLauncherPromo(const base::ListValue
* args
);
111 void OnLearnMore(const base::ListValue
* args
);
114 struct AppInstallInfo
{
118 base::string16 title
;
120 syncer::StringOrdinal page_ordinal
;
123 // Reset some instance flags we use to track the currently uninstalling app.
124 void CleanupAfterUninstall();
126 // Prompts the user to re-enable the app for |extension_id|.
127 void PromptToEnableApp(const std::string
& extension_id
);
129 // ExtensionUninstallDialog::Delegate:
130 void ExtensionUninstallAccepted() override
;
131 void ExtensionUninstallCanceled() override
;
133 // ExtensionEnableFlowDelegate:
134 void ExtensionEnableFlowFinished() override
;
135 void ExtensionEnableFlowAborted(bool user_initiated
) override
;
137 // Returns the ExtensionUninstallDialog object for this class, creating it if
139 extensions::ExtensionUninstallDialog
* GetExtensionUninstallDialog();
141 // Continuation for installing a bookmark app after favicon lookup.
142 void OnFaviconForApp(scoped_ptr
<AppInstallInfo
> install_info
,
143 const favicon_base::FaviconImageResult
& image_result
);
145 // Sends |highlight_app_id_| to the js.
146 void SetAppToBeHighlighted();
148 void OnExtensionPreferenceChanged();
150 void OnLocalStatePreferenceChanged();
152 // The apps are represented in the extensions model, which
153 // outlives us since it's owned by our containing profile.
154 ExtensionService
* const extension_service_
;
156 // We monitor changes to the extension system so that we can reload the apps
158 content::NotificationRegistrar registrar_
;
160 // Monitor extension preference changes so that the Web UI can be notified.
161 PrefChangeRegistrar extension_pref_change_registrar_
;
163 // Monitor the local state pref to control the app launcher promo.
164 PrefChangeRegistrar local_state_pref_change_registrar_
;
166 // Used to show confirmation UI for uninstalling extensions in incognito mode.
167 scoped_ptr
<extensions::ExtensionUninstallDialog
> extension_uninstall_dialog_
;
169 // Used to show confirmation UI for enabling extensions.
170 scoped_ptr
<ExtensionEnableFlow
> extension_enable_flow_
;
172 // The ids of apps to show on the NTP.
173 std::set
<std::string
> visible_apps_
;
175 // The id of the extension we are prompting the user about (either enable or
177 std::string extension_id_prompting_
;
179 // When true, we ignore changes to the underlying data rather than immediately
180 // refreshing. This is useful when making many batch updates to avoid flicker.
181 bool ignore_changes_
;
183 // When true, we have attempted to install a bookmark app, and are still
184 // waiting to hear about success or failure from the extensions system.
185 bool attempted_bookmark_app_install_
;
187 // True if we have executed HandleGetApps() at least once.
188 bool has_loaded_apps_
;
190 // The ID of the app to be highlighted on the NTP (i.e. shown on the page
191 // and pulsed). This is done for new installs. The actual higlighting occurs
192 // when the app is added to the page (via getAppsCallback or appAdded).
193 std::string highlight_app_id_
;
195 // Used for favicon loading tasks.
196 base::CancelableTaskTracker cancelable_task_tracker_
;
198 DISALLOW_COPY_AND_ASSIGN(AppLauncherHandler
);
201 #endif // CHROME_BROWSER_UI_WEBUI_NTP_APP_LAUNCHER_HANDLER_H_