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 "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_constants.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
;
31 struct FaviconImageResult
;
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
{
40 explicit AppLauncherHandler(ExtensionService
* extension_service
);
41 virtual ~AppLauncherHandler();
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
);
49 // WebUIMessageHandler implementation.
50 virtual void RegisterMessages() OVERRIDE
;
52 // content::NotificationObserver
53 virtual void Observe(int type
,
54 const content::NotificationSource
& source
,
55 const content::NotificationDetails
& details
) OVERRIDE
;
57 // Populate the given dictionary with all installed app info.
58 void FillAppDictionary(base::DictionaryValue
* value
);
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
);
65 // Populate the given dictionary with the web store promo content.
66 void FillPromoDictionary(base::DictionaryValue
* value
);
68 // Handles the "launchApp" message with unused |args|.
69 void HandleGetApps(const base::ListValue
* args
);
71 // Handles the "launchApp" message with |args| containing [extension_id,
72 // source] with optional [url, disposition], |disposition| defaulting to
74 void HandleLaunchApp(const base::ListValue
* args
);
76 // Handles the "setLaunchType" message with args containing [extension_id,
78 void HandleSetLaunchType(const base::ListValue
* args
);
80 // Handles the "uninstallApp" message with |args| containing [extension_id]
81 // and an optional bool to not confirm the uninstall when true, defaults to
83 void HandleUninstallApp(const base::ListValue
* args
);
85 // Handles the "createAppShortcut" message with |args| containing
87 void HandleCreateAppShortcut(const base::ListValue
* args
);
89 // Handles the "reorderApps" message with |args| containing [dragged_app_id,
91 void HandleReorderApps(const base::ListValue
* args
);
93 // Handles the "setPageIndex" message with |args| containing [extension_id,
95 void HandleSetPageIndex(const base::ListValue
* args
);
97 // Handles "saveAppPageName" message with |args| containing [name,
99 void HandleSaveAppPageName(const base::ListValue
* args
);
101 // Handles "generateAppForLink" message with |args| containing [url, title,
103 void HandleGenerateAppForLink(const base::ListValue
* args
);
105 // Other registered message callbacks with unused |args|.
106 void StopShowingAppLauncherPromo(const base::ListValue
* args
);
107 void OnLearnMore(const base::ListValue
* args
);
110 struct AppInstallInfo
{
114 bool is_bookmark_app
;
115 base::string16 title
;
117 syncer::StringOrdinal page_ordinal
;
120 // Reset some instance flags we use to track the currently uninstalling app.
121 void CleanupAfterUninstall();
123 // Prompts the user to re-enable the app for |extension_id|.
124 void PromptToEnableApp(const std::string
& extension_id
);
126 // ExtensionUninstallDialog::Delegate:
127 virtual void ExtensionUninstallAccepted() OVERRIDE
;
128 virtual void ExtensionUninstallCanceled() OVERRIDE
;
130 // ExtensionEnableFlowDelegate:
131 virtual void ExtensionEnableFlowFinished() OVERRIDE
;
132 virtual void ExtensionEnableFlowAborted(bool user_initiated
) OVERRIDE
;
134 // Returns the ExtensionUninstallDialog object for this class, creating it if
136 ExtensionUninstallDialog
* GetExtensionUninstallDialog();
138 // Continuation for installing a bookmark app after favicon lookup.
139 void OnFaviconForApp(scoped_ptr
<AppInstallInfo
> install_info
,
140 const chrome::FaviconImageResult
& image_result
);
142 // Sends |highlight_app_id_| to the js.
143 void SetAppToBeHighlighted();
145 void OnExtensionPreferenceChanged();
147 void OnLocalStatePreferenceChanged();
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_
;
153 // We monitor changes to the extension system so that we can reload the apps
155 content::NotificationRegistrar registrar_
;
157 // Monitor extension preference changes so that the Web UI can be notified.
158 PrefChangeRegistrar extension_pref_change_registrar_
;
160 // Monitor the local state pref to control the app launcher promo.
161 PrefChangeRegistrar local_state_pref_change_registrar_
;
163 // Used to show confirmation UI for uninstalling extensions in incognito mode.
164 scoped_ptr
<ExtensionUninstallDialog
> extension_uninstall_dialog_
;
166 // Used to show confirmation UI for enabling extensions.
167 scoped_ptr
<ExtensionEnableFlow
> extension_enable_flow_
;
169 // The ids of apps to show on the NTP.
170 std::set
<std::string
> visible_apps_
;
172 // The id of the extension we are prompting the user about (either enable or
174 std::string extension_id_prompting_
;
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_
;
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_
;
184 // True if we have executed HandleGetApps() at least once.
185 bool has_loaded_apps_
;
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_
;
192 // Used for favicon loading tasks.
193 CancelableTaskTracker cancelable_task_tracker_
;
195 DISALLOW_COPY_AND_ASSIGN(AppLauncherHandler
);
198 #endif // CHROME_BROWSER_UI_WEBUI_NTP_APP_LAUNCHER_HANDLER_H_