extensions_ui.h revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
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_EXTENSIONS_EXTENSIONS_UI_H_
6#define CHROME_BROWSER_EXTENSIONS_EXTENSIONS_UI_H_
7#pragma once
8
9#include <string>
10#include <vector>
11
12#include "base/scoped_ptr.h"
13#include "chrome/browser/dom_ui/chrome_url_data_manager.h"
14#include "chrome/browser/dom_ui/web_ui.h"
15#include "chrome/browser/extensions/extension_install_ui.h"
16#include "chrome/browser/extensions/pack_extension_job.h"
17#include "chrome/browser/ui/shell_dialogs.h"
18#include "chrome/common/extensions/extension_resource.h"
19#include "chrome/common/notification_observer.h"
20#include "chrome/common/notification_registrar.h"
21#include "googleurl/src/gurl.h"
22
23class DictionaryValue;
24class Extension;
25class ExtensionService;
26class FilePath;
27class ListValue;
28class PrefService;
29class RenderProcessHost;
30class UserScript;
31
32// Information about a page running in an extension, for example a toolstrip,
33// a background page, or a tab contents.
34struct ExtensionPage {
35  ExtensionPage(const GURL& url, int render_process_id, int render_view_id,
36                bool incognito)
37    : url(url), render_process_id(render_process_id),
38      render_view_id(render_view_id), incognito(incognito) {}
39  GURL url;
40  int render_process_id;
41  int render_view_id;
42  bool incognito;
43};
44
45class ExtensionsUIHTMLSource : public ChromeURLDataManager::DataSource {
46 public:
47  ExtensionsUIHTMLSource();
48
49  // Called when the network layer has requested a resource underneath
50  // the path we registered.
51  virtual void StartDataRequest(const std::string& path,
52                                bool is_off_the_record,
53                                int request_id);
54  virtual std::string GetMimeType(const std::string&) const;
55
56 private:
57  ~ExtensionsUIHTMLSource() {}
58
59  DISALLOW_COPY_AND_ASSIGN(ExtensionsUIHTMLSource);
60};
61
62// The handler for JavaScript messages related to the "extensions" view.
63class ExtensionsDOMHandler
64    : public WebUIMessageHandler,
65      public NotificationObserver,
66      public PackExtensionJob::Client,
67      public SelectFileDialog::Listener,
68      public ExtensionInstallUI::Delegate {
69 public:
70
71  // Helper class that loads the icons for the extensions in the management UI.
72  // We do this with native code instead of just using chrome-extension:// URLs
73  // for two reasons:
74  //
75  // 1. We need to support the disabled extensions, too, and using URLs won't
76  //    work for them.
77  // 2. We want to desaturate the icons of the disabled extensions to make them
78  //    look disabled.
79  class IconLoader : public base::RefCountedThreadSafe<IconLoader> {
80   public:
81    explicit IconLoader(ExtensionsDOMHandler* handler);
82
83    // Load |icons|. Will call handler->OnIconsLoaded when complete. IconLoader
84    // takes ownership of both arguments.
85    void LoadIcons(std::vector<ExtensionResource>* icons,
86                   DictionaryValue* json);
87
88    // Cancel the load. IconLoader won't try to call back to the handler after
89    // this.
90    void Cancel();
91
92   private:
93    // Load the icons and call ReportResultOnUIThread when done. This method
94    // takes ownership of both arguments.
95    void LoadIconsOnFileThread(std::vector<ExtensionResource>* icons,
96                               DictionaryValue* json);
97
98    // Report back to the handler. This method takes ownership of |json|.
99    void ReportResultOnUIThread(DictionaryValue* json);
100
101    // The handler we will report back to.
102    ExtensionsDOMHandler* handler_;
103  };
104
105  explicit ExtensionsDOMHandler(ExtensionService* extension_service);
106  virtual ~ExtensionsDOMHandler();
107
108  // WebUIMessageHandler implementation.
109  virtual void RegisterMessages();
110
111  // Extension Detail JSON Struct for page. (static for ease of testing).
112  // Note: service can be NULL in unit tests.
113  static DictionaryValue* CreateExtensionDetailValue(
114      ExtensionService* service,
115      const Extension* extension,
116      const std::vector<ExtensionPage>& pages,
117      bool enabled,
118      bool terminated);
119
120  // ContentScript JSON Struct for page. (static for ease of testing).
121  static DictionaryValue* CreateContentScriptDetailValue(
122      const UserScript& script,
123      const FilePath& extension_path);
124
125  // ExtensionPackJob::Client
126  virtual void OnPackSuccess(const FilePath& crx_file,
127                             const FilePath& key_file);
128
129  virtual void OnPackFailure(const std::string& error);
130
131  // ExtensionInstallUI::Delegate implementation, used for receiving
132  // notification about uninstall confirmation dialog selections.
133  virtual void InstallUIProceed();
134  virtual void InstallUIAbort();
135
136 private:
137  // Callback for "requestExtensionsData" message.
138  void HandleRequestExtensionsData(const ListValue* args);
139
140  // Callback for "toggleDeveloperMode" message.
141  void HandleToggleDeveloperMode(const ListValue* args);
142
143  // Callback for "inspect" message.
144  void HandleInspectMessage(const ListValue* args);
145
146  // Callback for "reload" message.
147  void HandleReloadMessage(const ListValue* args);
148
149  // Callback for "enable" message.
150  void HandleEnableMessage(const ListValue* args);
151
152  // Callback for "enableIncognito" message.
153  void HandleEnableIncognitoMessage(const ListValue* args);
154
155  // Callback for "allowFileAcces" message.
156  void HandleAllowFileAccessMessage(const ListValue* args);
157
158  // Callback for "uninstall" message.
159  void HandleUninstallMessage(const ListValue* args);
160
161  // Callback for "options" message.
162  void HandleOptionsMessage(const ListValue* args);
163
164  // Callback for "showButton" message.
165  void HandleShowButtonMessage(const ListValue* args);
166
167  // Callback for "load" message.
168  void HandleLoadMessage(const ListValue* args);
169
170  // Callback for "pack" message.
171  void HandlePackMessage(const ListValue* args);
172
173  // Callback for "autoupdate" message.
174  void HandleAutoUpdateMessage(const ListValue* args);
175
176  // Utility for calling javascript window.alert in the page.
177  void ShowAlert(const std::string& message);
178
179  // Callback for "selectFilePath" message.
180  void HandleSelectFilePathMessage(const ListValue* args);
181
182  // Utility for callbacks that get an extension ID as the sole argument.
183  const Extension* GetExtension(const ListValue* args);
184
185  // Forces a UI update if appropriate after a notification is received.
186  void MaybeUpdateAfterNotification();
187
188  // SelectFileDialog::Listener
189  virtual void FileSelected(const FilePath& path,
190                            int index, void* params);
191  virtual void MultiFilesSelected(
192      const std::vector<FilePath>& files, void* params);
193  virtual void FileSelectionCanceled(void* params) {}
194
195  // NotificationObserver
196  virtual void Observe(NotificationType type,
197                       const NotificationSource& source,
198                       const NotificationDetails& details);
199
200  // Helper that lists the current active html pages for an extension.
201  std::vector<ExtensionPage> GetActivePagesForExtension(
202      const Extension* extension);
203  void GetActivePagesForExtensionProcess(
204      RenderProcessHost* process,
205      const Extension* extension,
206      std::vector<ExtensionPage> *result);
207
208  // Returns the best icon to display in the UI for an extension, or an empty
209  // ExtensionResource if no good icon exists.
210  ExtensionResource PickExtensionIcon(const Extension* extension);
211
212  // Loads the extension resources into the json data, then calls OnIconsLoaded.
213  // Takes ownership of |icons|.
214  // Called on the file thread.
215  void LoadExtensionIcons(std::vector<ExtensionResource>* icons,
216                          DictionaryValue* json_data);
217
218  // Takes ownership of |json_data| and tells HTML about it.
219  // Called on the UI thread.
220  void OnIconsLoaded(DictionaryValue* json_data);
221
222  // Returns the ExtensionInstallUI object for this class, creating it if
223  // needed.
224  ExtensionInstallUI* GetExtensionInstallUI();
225
226  // Our model.
227  scoped_refptr<ExtensionService> extensions_service_;
228
229  // Used to pick the directory when loading an extension.
230  scoped_refptr<SelectFileDialog> load_extension_dialog_;
231
232  // Used to package the extension.
233  scoped_refptr<PackExtensionJob> pack_job_;
234
235  // Used to load icons asynchronously on the file thread.
236  scoped_refptr<IconLoader> icon_loader_;
237
238  // Used to show confirmation UI for uninstalling/enabling extensions in
239  // incognito mode.
240  scoped_ptr<ExtensionInstallUI> install_ui_;
241
242  // The id of the extension we are prompting the user about.
243  std::string extension_id_prompting_;
244
245  // We monitor changes to the extension system so that we can reload when
246  // necessary.
247  NotificationRegistrar registrar_;
248
249  // If true, we will ignore notifications in ::Observe(). This is needed
250  // to prevent reloading the page when we were the cause of the
251  // notification.
252  bool ignore_notifications_;
253
254  // The page may be refreshed in response to a RENDER_VIEW_HOST_DELETED,
255  // but the iteration over RenderViewHosts will include the host because the
256  // notification is sent when it is in the process of being deleted (and before
257  // it is removed from the process). Keep a pointer to it so we can exclude
258  // it from the active views.
259  RenderViewHost* deleting_rvh_;
260
261  DISALLOW_COPY_AND_ASSIGN(ExtensionsDOMHandler);
262};
263
264class ExtensionsUI : public WebUI {
265 public:
266  explicit ExtensionsUI(TabContents* contents);
267
268  static RefCountedMemory* GetFaviconResourceBytes();
269
270  static void RegisterUserPrefs(PrefService* prefs);
271
272 private:
273  DISALLOW_COPY_AND_ASSIGN(ExtensionsUI);
274};
275
276#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSIONS_UI_H_
277