Home | History | Annotate | Download | only in extensions
      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/memory/scoped_ptr.h"
     13 #include "chrome/browser/extensions/extension_uninstall_dialog.h"
     14 #include "chrome/browser/extensions/pack_extension_job.h"
     15 #include "chrome/browser/ui/shell_dialogs.h"
     16 #include "chrome/browser/ui/webui/chrome_url_data_manager.h"
     17 #include "chrome/common/extensions/extension_resource.h"
     18 #include "content/browser/webui/web_ui.h"
     19 #include "content/common/notification_observer.h"
     20 #include "content/common/notification_registrar.h"
     21 #include "googleurl/src/gurl.h"
     22 
     23 class DictionaryValue;
     24 class Extension;
     25 class ExtensionService;
     26 class FilePath;
     27 class ListValue;
     28 class PrefService;
     29 class RenderProcessHost;
     30 class UserScript;
     31 
     32 // Information about a page running in an extension, for example a toolstrip,
     33 // a background page, or a tab contents.
     34 struct 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 
     45 class 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_incognito,
     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.
     63 class ExtensionsDOMHandler : public WebUIMessageHandler,
     64                              public NotificationObserver,
     65                              public PackExtensionJob::Client,
     66                              public SelectFileDialog::Listener,
     67                              public ExtensionUninstallDialog::Delegate {
     68  public:
     69   explicit ExtensionsDOMHandler(ExtensionService* extension_service);
     70   virtual ~ExtensionsDOMHandler();
     71 
     72   // WebUIMessageHandler implementation.
     73   virtual void RegisterMessages();
     74 
     75   // Extension Detail JSON Struct for page. (static for ease of testing).
     76   // Note: service can be NULL in unit tests.
     77   static DictionaryValue* CreateExtensionDetailValue(
     78       ExtensionService* service,
     79       const Extension* extension,
     80       const std::vector<ExtensionPage>& pages,
     81       bool enabled,
     82       bool terminated);
     83 
     84   // ExtensionPackJob::Client
     85   virtual void OnPackSuccess(const FilePath& crx_file,
     86                              const FilePath& key_file);
     87 
     88   virtual void OnPackFailure(const std::string& error);
     89 
     90   // ExtensionUninstallDialog::Delegate:
     91   virtual void ExtensionDialogAccepted();
     92   virtual void ExtensionDialogCanceled();
     93 
     94  private:
     95   // Callback for "requestExtensionsData" message.
     96   void HandleRequestExtensionsData(const ListValue* args);
     97 
     98   // Callback for "toggleDeveloperMode" message.
     99   void HandleToggleDeveloperMode(const ListValue* args);
    100 
    101   // Callback for "inspect" message.
    102   void HandleInspectMessage(const ListValue* args);
    103 
    104   // Callback for "reload" message.
    105   void HandleReloadMessage(const ListValue* args);
    106 
    107   // Callback for "enable" message.
    108   void HandleEnableMessage(const ListValue* args);
    109 
    110   // Callback for "enableIncognito" message.
    111   void HandleEnableIncognitoMessage(const ListValue* args);
    112 
    113   // Callback for "allowFileAcces" message.
    114   void HandleAllowFileAccessMessage(const ListValue* args);
    115 
    116   // Callback for "uninstall" message.
    117   void HandleUninstallMessage(const ListValue* args);
    118 
    119   // Callback for "options" message.
    120   void HandleOptionsMessage(const ListValue* args);
    121 
    122   // Callback for "showButton" message.
    123   void HandleShowButtonMessage(const ListValue* args);
    124 
    125   // Callback for "load" message.
    126   void HandleLoadMessage(const ListValue* args);
    127 
    128   // Callback for "pack" message.
    129   void HandlePackMessage(const ListValue* args);
    130 
    131   // Callback for "autoupdate" message.
    132   void HandleAutoUpdateMessage(const ListValue* args);
    133 
    134   // Utility for calling javascript window.alert in the page.
    135   void ShowAlert(const std::string& message);
    136 
    137   // Callback for "selectFilePath" message.
    138   void HandleSelectFilePathMessage(const ListValue* args);
    139 
    140   // Utility for callbacks that get an extension ID as the sole argument.
    141   const Extension* GetExtension(const ListValue* args);
    142 
    143   // Forces a UI update if appropriate after a notification is received.
    144   void MaybeUpdateAfterNotification();
    145 
    146   // Register for notifications that we need to reload the page.
    147   void RegisterForNotifications();
    148 
    149   // SelectFileDialog::Listener
    150   virtual void FileSelected(const FilePath& path,
    151                             int index, void* params);
    152   virtual void MultiFilesSelected(
    153       const std::vector<FilePath>& files, void* params);
    154   virtual void FileSelectionCanceled(void* params) {}
    155 
    156   // NotificationObserver
    157   virtual void Observe(NotificationType type,
    158                        const NotificationSource& source,
    159                        const NotificationDetails& details);
    160 
    161   // Helper that lists the current active html pages for an extension.
    162   std::vector<ExtensionPage> GetActivePagesForExtension(
    163       const Extension* extension);
    164   void GetActivePagesForExtensionProcess(
    165       RenderProcessHost* process,
    166       const Extension* extension,
    167       std::vector<ExtensionPage> *result);
    168 
    169   // Returns the ExtensionUninstallDialog object for this class, creating it if
    170   // needed.
    171   ExtensionUninstallDialog* GetExtensionUninstallDialog();
    172 
    173   // Our model.
    174   scoped_refptr<ExtensionService> extensions_service_;
    175 
    176   // Used to pick the directory when loading an extension.
    177   scoped_refptr<SelectFileDialog> load_extension_dialog_;
    178 
    179   // Used to package the extension.
    180   scoped_refptr<PackExtensionJob> pack_job_;
    181 
    182   // Used to show confirmation UI for uninstalling extensions in incognito mode.
    183   scoped_ptr<ExtensionUninstallDialog> extension_uninstall_dialog_;
    184 
    185   // The id of the extension we are prompting the user about.
    186   std::string extension_id_prompting_;
    187 
    188   // We monitor changes to the extension system so that we can reload when
    189   // necessary.
    190   NotificationRegistrar registrar_;
    191 
    192   // If true, we will ignore notifications in ::Observe(). This is needed
    193   // to prevent reloading the page when we were the cause of the
    194   // notification.
    195   bool ignore_notifications_;
    196 
    197   // The page may be refreshed in response to a RENDER_VIEW_HOST_DELETED,
    198   // but the iteration over RenderViewHosts will include the host because the
    199   // notification is sent when it is in the process of being deleted (and before
    200   // it is removed from the process). Keep a pointer to it so we can exclude
    201   // it from the active views.
    202   RenderViewHost* deleting_rvh_;
    203 
    204   DISALLOW_COPY_AND_ASSIGN(ExtensionsDOMHandler);
    205 };
    206 
    207 class ExtensionsUI : public WebUI {
    208  public:
    209   explicit ExtensionsUI(TabContents* contents);
    210 
    211   static RefCountedMemory* GetFaviconResourceBytes();
    212 
    213   static void RegisterUserPrefs(PrefService* prefs);
    214 
    215  private:
    216   DISALLOW_COPY_AND_ASSIGN(ExtensionsUI);
    217 };
    218 
    219 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSIONS_UI_H_
    220