Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / ntp / app_launcher_handler.h
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 "base/task/cancelable_task_tracker.h"
15 #include "chrome/browser/extensions/extension_uninstall_dialog.h"
16 #include "chrome/browser/favicon/favicon_service.h"
17 #include "chrome/browser/ui/extensions/extension_enable_flow_delegate.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"
24
25 class ExtensionEnableFlow;
26 class ExtensionService;
27 class PrefChangeRegistrar;
28 class Profile;
29
30 namespace favicon_base {
31 struct FaviconImageResult;
32 }
33
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 {
40  public:
41   explicit AppLauncherHandler(ExtensionService* extension_service);
42   ~AppLauncherHandler() override;
43
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);
49
50   // WebUIMessageHandler implementation.
51   void RegisterMessages() override;
52
53   // content::NotificationObserver
54   void Observe(int type,
55                const content::NotificationSource& source,
56                const content::NotificationDetails& details) override;
57
58   // Populate the given dictionary with all installed app info.
59   void FillAppDictionary(base::DictionaryValue* value);
60
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);
65
66   // Populate the given dictionary with the web store promo content.
67   void FillPromoDictionary(base::DictionaryValue* value);
68
69   // Handles the "launchApp" message with unused |args|.
70   void HandleGetApps(const base::ListValue* args);
71
72   // Handles the "launchApp" message with |args| containing [extension_id,
73   // source] with optional [url, disposition], |disposition| defaulting to
74   // CURRENT_TAB.
75   void HandleLaunchApp(const base::ListValue* args);
76
77   // Handles the "setLaunchType" message with args containing [extension_id,
78   // launch_type].
79   void HandleSetLaunchType(const base::ListValue* args);
80
81   // Handles the "uninstallApp" message with |args| containing [extension_id]
82   // and an optional bool to not confirm the uninstall when true, defaults to
83   // false.
84   void HandleUninstallApp(const base::ListValue* args);
85
86   // Handles the "createAppShortcut" message with |args| containing
87   // [extension_id].
88   void HandleCreateAppShortcut(const base::ListValue* args);
89
90   // Handles the "reorderApps" message with |args| containing [dragged_app_id,
91   // app_order].
92   void HandleReorderApps(const base::ListValue* args);
93
94   // Handles the "setPageIndex" message with |args| containing [extension_id,
95   // page_index].
96   void HandleSetPageIndex(const base::ListValue* args);
97
98   // Handles "saveAppPageName" message with |args| containing [name,
99   // page_index].
100   void HandleSaveAppPageName(const base::ListValue* args);
101
102   // Handles "generateAppForLink" message with |args| containing [url, title,
103   // page_index].
104   void HandleGenerateAppForLink(const base::ListValue* args);
105
106   // Other registered message callbacks with unused |args|.
107   void StopShowingAppLauncherPromo(const base::ListValue* args);
108   void OnLearnMore(const base::ListValue* args);
109
110  private:
111   struct AppInstallInfo {
112     AppInstallInfo();
113     ~AppInstallInfo();
114
115     base::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   void ExtensionUninstallAccepted() override;
128   void ExtensionUninstallCanceled() override;
129
130   // ExtensionEnableFlowDelegate:
131   void ExtensionEnableFlowFinished() override;
132   void ExtensionEnableFlowAborted(bool user_initiated) override;
133
134   // Returns the ExtensionUninstallDialog object for this class, creating it if
135   // needed.
136   extensions::ExtensionUninstallDialog* GetExtensionUninstallDialog();
137
138   // Continuation for installing a bookmark app after favicon lookup.
139   void OnFaviconForApp(scoped_ptr<AppInstallInfo> install_info,
140                        const favicon_base::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<extensions::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   base::CancelableTaskTracker cancelable_task_tracker_;
194
195   DISALLOW_COPY_AND_ASSIGN(AppLauncherHandler);
196 };
197
198 #endif  // CHROME_BROWSER_UI_WEBUI_NTP_APP_LAUNCHER_HANDLER_H_