Home | History | Annotate | Download | only in gtk
      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_GTK_TASK_MANAGER_GTK_H_
      6 #define CHROME_BROWSER_UI_GTK_TASK_MANAGER_GTK_H_
      7 #pragma once
      8 
      9 #include <gtk/gtk.h>
     10 
     11 #include <string>
     12 
     13 #include "base/memory/scoped_ptr.h"
     14 #include "chrome/browser/task_manager/task_manager.h"
     15 #include "grit/generated_resources.h"
     16 #include "ui/base/gtk/gtk_signal.h"
     17 
     18 namespace gfx {
     19 class Point;
     20 }
     21 
     22 class TaskManagerGtk : public TaskManagerModelObserver {
     23  public:
     24   explicit TaskManagerGtk(bool highlight_background_resources);
     25   virtual ~TaskManagerGtk();
     26 
     27   // TaskManagerModelObserver
     28   virtual void OnModelChanged();
     29   virtual void OnItemsChanged(int start, int length);
     30   virtual void OnItemsAdded(int start, int length);
     31   virtual void OnItemsRemoved(int start, int length);
     32 
     33   // Closes the task manager window.
     34   void Close();
     35 
     36   // Creates the task manager if it doesn't exist; otherwise, it activates the
     37   // existing task manager window. If |highlight_background_resources| is true,
     38   // background resources are rendered with a yellow highlight (for the
     39   // "View Background Pages" menu item).
     40   static void Show(bool highlight_background_resources);
     41 
     42  private:
     43   class ContextMenuController;
     44   friend class ContextMenuController;
     45 
     46   // Initializes the task manager dialog.
     47   void Init();
     48 
     49   // Set |dialog_|'s initial size, using its previous size if that was saved.
     50   void SetInitialDialogSize();
     51 
     52   // Connects the ctrl-w accelerator to the dialog.
     53   void ConnectAccelerators();
     54 
     55   // Sets up the treeview widget.
     56   void CreateTaskManagerTreeview();
     57 
     58   // Returns the model data for a given |row| and |col_id|.
     59   std::string GetModelText(int row, int col_id);
     60 
     61   // Retrieves the resource icon from the model for |row|.
     62   GdkPixbuf* GetModelIcon(int row);
     63 
     64   // Sets the treeview row data.  |row| is an index into the model and |iter|
     65   // is the current position in the treeview.
     66   void SetRowDataFromModel(int row, GtkTreeIter* iter);
     67 
     68   // Queries the treeview for the selected rows, and kills those processes.
     69   void KillSelectedProcesses();
     70 
     71   // Opens the context menu used to select the task manager columns.
     72   void ShowContextMenu(const gfx::Point& point, guint32 event_time);
     73 
     74   // Opens about:memory in a new foreground tab.
     75   void OnLinkActivated();
     76 
     77   // Compare implementation used for sorting columns.
     78   gint CompareImpl(GtkTreeModel* tree_model, GtkTreeIter* a,
     79                    GtkTreeIter* b, int id);
     80 
     81   // Response signal handler that notifies us of dialog destruction.
     82   CHROMEGTK_CALLBACK_0(TaskManagerGtk, void, OnDestroy);
     83 
     84   // Response signal handler that notifies us of dialog responses.
     85   CHROMEGTK_CALLBACK_1(TaskManagerGtk, void, OnResponse, int);
     86 
     87   // Realize signal handler to set the page column's initial size.
     88   CHROMEG_CALLBACK_0(TaskManagerGtk, void, OnTreeViewRealize, GtkTreeView*);
     89 
     90   // Changed signal handler that is sent when the treeview selection changes.
     91   CHROMEG_CALLBACK_0(TaskManagerGtk, void, OnSelectionChanged,
     92                      GtkTreeSelection*);
     93 
     94   // row-activated handler that foregrounds a process on activation (e.g.,
     95   // double-click).
     96   CHROMEGTK_CALLBACK_2(TaskManagerGtk, void, OnRowActivated,
     97                        GtkTreePath*, GtkTreeViewColumn*);
     98 
     99   // button-event handler that opens the right-click context menu.
    100   // Note: GTK does menu on mouse-up while views does menu on mouse-down;
    101   // this handler is used for both.
    102   CHROMEGTK_CALLBACK_1(TaskManagerGtk, gboolean, OnButtonEvent,
    103                        GdkEventButton*);
    104 
    105   // Handles an accelerator being pressed.
    106   CHROMEG_CALLBACK_3(TaskManagerGtk, gboolean, OnGtkAccelerator,
    107                      GtkAccelGroup*, GObject*, guint, GdkModifierType);
    108 
    109   // Page sorting callback.
    110   static gint ComparePage(GtkTreeModel* model, GtkTreeIter* a,
    111                           GtkTreeIter* b, gpointer task_manager) {
    112     return reinterpret_cast<TaskManagerGtk*>(task_manager)->
    113         CompareImpl(model, a, b, IDS_TASK_MANAGER_PAGE_COLUMN);
    114   }
    115 
    116   // Shared memory sorting callback.
    117   static gint CompareSharedMemory(GtkTreeModel* model, GtkTreeIter* a,
    118                                   GtkTreeIter* b, gpointer task_manager) {
    119     return reinterpret_cast<TaskManagerGtk*>(task_manager)->
    120         CompareImpl(model, a, b, IDS_TASK_MANAGER_SHARED_MEM_COLUMN);
    121   }
    122 
    123   // Private memory sorting callback.
    124   static gint ComparePrivateMemory(GtkTreeModel* model, GtkTreeIter* a,
    125                                    GtkTreeIter* b, gpointer task_manager) {
    126     return reinterpret_cast<TaskManagerGtk*>(task_manager)->
    127         CompareImpl(model, a, b, IDS_TASK_MANAGER_PRIVATE_MEM_COLUMN);
    128   }
    129 
    130   // Javascript memory sorting callback.
    131   static gint CompareV8Memory(GtkTreeModel* model, GtkTreeIter* a,
    132                               GtkTreeIter* b, gpointer task_manager) {
    133     return reinterpret_cast<TaskManagerGtk*>(task_manager)->
    134         CompareImpl(model, a, b,
    135                     IDS_TASK_MANAGER_JAVASCRIPT_MEMORY_ALLOCATED_COLUMN);
    136   }
    137 
    138   // CPU sorting callback.
    139   static gint CompareCPU(GtkTreeModel* model, GtkTreeIter* a,
    140                          GtkTreeIter* b, gpointer task_manager) {
    141     return reinterpret_cast<TaskManagerGtk*>(task_manager)->
    142         CompareImpl(model, a, b, IDS_TASK_MANAGER_CPU_COLUMN);
    143   }
    144 
    145   // Network sorting callback.
    146   static gint CompareNetwork(GtkTreeModel* model, GtkTreeIter* a,
    147                              GtkTreeIter* b, gpointer task_manager) {
    148     return reinterpret_cast<TaskManagerGtk*>(task_manager)->
    149         CompareImpl(model, a, b, IDS_TASK_MANAGER_NET_COLUMN);
    150   }
    151 
    152   // Process ID sorting callback.
    153   static gint CompareProcessID(GtkTreeModel* model, GtkTreeIter* a,
    154                                GtkTreeIter* b, gpointer task_manager) {
    155     return reinterpret_cast<TaskManagerGtk*>(task_manager)->
    156         CompareImpl(model, a, b, IDS_TASK_MANAGER_PROCESS_ID_COLUMN);
    157   }
    158 
    159   // WebCore Image Cache sorting callback.
    160   static gint CompareWebCoreImageCache(GtkTreeModel* model, GtkTreeIter* a,
    161                                        GtkTreeIter* b, gpointer task_manager) {
    162     return reinterpret_cast<TaskManagerGtk*>(task_manager)->
    163         CompareImpl(model, a, b, IDS_TASK_MANAGER_WEBCORE_IMAGE_CACHE_COLUMN);
    164   }
    165 
    166   // WebCore Scripts Cache sorting callback.
    167   static gint CompareWebCoreScriptsCache(GtkTreeModel* model, GtkTreeIter* a,
    168                                          GtkTreeIter* b,
    169                                          gpointer task_manager) {
    170     return reinterpret_cast<TaskManagerGtk*>(task_manager)->
    171         CompareImpl(model, a, b, IDS_TASK_MANAGER_WEBCORE_SCRIPTS_CACHE_COLUMN);
    172   }
    173 
    174   // WebCore CSS Cache sorting callback.
    175   static gint CompareWebCoreCssCache(GtkTreeModel* model, GtkTreeIter* a,
    176                                      GtkTreeIter* b, gpointer task_manager) {
    177     return reinterpret_cast<TaskManagerGtk*>(task_manager)->
    178         CompareImpl(model, a, b, IDS_TASK_MANAGER_WEBCORE_CSS_CACHE_COLUMN);
    179   }
    180 
    181   // Sqlite memory sorting callback.
    182   static gint CompareSqliteMemoryUsed(GtkTreeModel* model, GtkTreeIter* a,
    183                                       GtkTreeIter* b, gpointer task_manager) {
    184     return reinterpret_cast<TaskManagerGtk*>(task_manager)->
    185         CompareImpl(model, a, b, IDS_TASK_MANAGER_SQLITE_MEMORY_USED_COLUMN);
    186   }
    187 
    188   // Goats Teleported sorting callback.
    189   static gint CompareGoatsTeleported(GtkTreeModel* model, GtkTreeIter* a,
    190                                      GtkTreeIter* b, gpointer task_manager) {
    191     return reinterpret_cast<TaskManagerGtk*>(task_manager)->
    192         CompareImpl(model, a, b, IDS_TASK_MANAGER_GOATS_TELEPORTED_COLUMN);
    193   }
    194 
    195   // The task manager.
    196   TaskManager* task_manager_;
    197 
    198   // Our model.
    199   TaskManagerModel* model_;
    200 
    201   // The task manager dialog window.
    202   GtkWidget* dialog_;
    203 
    204   // The treeview that contains the process list.
    205   GtkWidget* treeview_;
    206 
    207   // The list of processes.
    208   GtkListStore* process_list_;
    209   GtkTreeModel* process_list_sort_;
    210 
    211   // The number of processes in |process_list_|.
    212   int process_count_;
    213 
    214   // The id of the |dialog_| destroy signal handler.
    215   gulong destroy_handler_id_;
    216 
    217   // The context menu controller.
    218   scoped_ptr<ContextMenuController> menu_controller_;
    219 
    220   GtkAccelGroup* accel_group_;
    221 
    222   // An open task manager window. There can only be one open at a time. This
    223   // is reset to NULL when the window is closed.
    224   static TaskManagerGtk* instance_;
    225 
    226   // We edit the selection in the OnSelectionChanged handler, and we use this
    227   // variable to prevent ourselves from handling further changes that we
    228   // ourselves caused.
    229   bool ignore_selection_changed_;
    230 
    231   // If true, background resources are rendered with a yellow highlight.
    232   bool highlight_background_resources_;
    233 
    234   DISALLOW_COPY_AND_ASSIGN(TaskManagerGtk);
    235 };
    236 
    237 #endif  // CHROME_BROWSER_UI_GTK_TASK_MANAGER_GTK_H_
    238