1// Copyright (c) 2011 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_APP_LAUNCHER_HANDLER_H_
6#define CHROME_BROWSER_UI_WEBUI_APP_LAUNCHER_HANDLER_H_
7#pragma once
8
9#include "base/memory/scoped_ptr.h"
10#include "chrome/browser/extensions/extension_install_ui.h"
11#include "chrome/browser/extensions/extension_uninstall_dialog.h"
12#include "chrome/browser/prefs/pref_change_registrar.h"
13#include "chrome/common/extensions/extension.h"
14#include "chrome/common/extensions/extension_constants.h"
15#include "content/browser/webui/web_ui.h"
16#include "content/common/notification_observer.h"
17#include "content/common/notification_registrar.h"
18
19class ExtensionPrefs;
20class ExtensionService;
21class NotificationRegistrar;
22class PrefChangeRegistrar;
23class Profile;
24
25namespace gfx {
26class Rect;
27}
28
29// The handler for Javascript messages related to the "apps" view.
30class AppLauncherHandler : public WebUIMessageHandler,
31                           public ExtensionUninstallDialog::Delegate,
32                           public ExtensionInstallUI::Delegate,
33                           public NotificationObserver {
34 public:
35  explicit AppLauncherHandler(ExtensionService* extension_service);
36  virtual ~AppLauncherHandler();
37
38  // Populate a dictionary with the information from an extension.
39  static void CreateAppInfo(const Extension* extension,
40                            ExtensionPrefs* extension_prefs,
41                            DictionaryValue* value);
42
43  // Callback for pings related to launching apps on the NTP.
44  static bool HandlePing(Profile* profile, const std::string& path);
45
46  // WebUIMessageHandler implementation.
47  virtual WebUIMessageHandler* Attach(WebUI* web_ui);
48  virtual void RegisterMessages();
49
50  // NotificationObserver
51  virtual void Observe(NotificationType type,
52                      const NotificationSource& source,
53                      const NotificationDetails& details);
54
55  // Populate the given dictionary with all installed app info.
56  void FillAppDictionary(DictionaryValue* value);
57
58  // Populate the given dictionary with the web store promo content.
59  void FillPromoDictionary(DictionaryValue* value);
60
61  // Callback for the "getApps" message.
62  void HandleGetApps(const ListValue* args);
63
64  // Callback for the "launchApp" message.
65  void HandleLaunchApp(const ListValue* args);
66
67  // Callback for the "setLaunchType" message.
68  void HandleSetLaunchType(const ListValue* args);
69
70  // Callback for the "uninstallApp" message.
71  void HandleUninstallApp(const ListValue* args);
72
73  // Callback for the "hideAppPromo" message.
74  void HandleHideAppsPromo(const ListValue* args);
75
76  // Callback for the "createAppShortcut" message.
77  void HandleCreateAppShortcut(const ListValue* args);
78
79  // Callback for the "reorderApps" message.
80  void HandleReorderApps(const ListValue* args);
81
82  // Callback for the "setPageIndex" message.
83  void HandleSetPageIndex(const ListValue* args);
84
85  // Callback for the "promoSeen" message.
86  void HandlePromoSeen(const ListValue* args);
87
88 private:
89  // Records a web store launch in the appropriate histograms. |promo_active|
90  // specifies if the web store promotion was active.
91  static void RecordWebStoreLaunch(bool promo_active);
92
93  // Records an app launch in the corresponding |bucket| of the app launch
94  // histogram. |promo_active| specifies if the web store promotion was active.
95  static void RecordAppLaunchByID(bool promo_active,
96                                  extension_misc::AppLaunchBucket bucket);
97
98  // Records an app launch in the corresponding |bucket| of the app launch
99  // histogram if the |escaped_url| corresponds to an installed app.
100  static void RecordAppLaunchByURL(Profile* profile,
101                                   std::string escaped_url,
102                                   extension_misc::AppLaunchBucket bucket);
103
104  // Prompts the user to re-enable the app for |extension_id|.
105  void PromptToEnableApp(const std::string& extension_id);
106
107  // ExtensionUninstallDialog::Delegate:
108  virtual void ExtensionDialogAccepted();
109  virtual void ExtensionDialogCanceled();
110
111  // ExtensionInstallUI::Delegate:
112  virtual void InstallUIProceed();
113  virtual void InstallUIAbort();
114
115  // Returns the ExtensionUninstallDialog object for this class, creating it if
116  // needed.
117  ExtensionUninstallDialog* GetExtensionUninstallDialog();
118
119  // Returns the ExtensionInstallUI object for this class, creating it if
120  // needed.
121  ExtensionInstallUI* GetExtensionInstallUI();
122
123  // Helper that uninstalls all the default apps.
124  void UninstallDefaultApps();
125
126  // The apps are represented in the extensions model.
127  scoped_refptr<ExtensionService> extensions_service_;
128
129  // We monitor changes to the extension system so that we can reload the apps
130  // when necessary.
131  NotificationRegistrar registrar_;
132
133  // Monitor extension preference changes so that the Web UI can be notified.
134  PrefChangeRegistrar pref_change_registrar_;
135
136  // Used to show confirmation UI for uninstalling extensions in incognito mode.
137  scoped_ptr<ExtensionUninstallDialog> extension_uninstall_dialog_;
138
139  // Used to show confirmation UI for enabling extensions in incognito mode.
140  scoped_ptr<ExtensionInstallUI> extension_install_ui_;
141
142  // The id of the extension we are prompting the user about.
143  std::string extension_id_prompting_;
144
145  // Whether the promo is currently being shown.
146  bool promo_active_;
147
148  // When true, we ignore changes to the underlying data rather than immediately
149  // refreshing. This is useful when making many batch updates to avoid flicker.
150  bool ignore_changes_;
151
152  DISALLOW_COPY_AND_ASSIGN(AppLauncherHandler);
153};
154
155#endif  // CHROME_BROWSER_UI_WEBUI_APP_LAUNCHER_HANDLER_H_
156