Home | History | Annotate | Download | only in cocoa
      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_COCOA_TASK_MANAGER_MAC_H_
      6 #define CHROME_BROWSER_UI_COCOA_TASK_MANAGER_MAC_H_
      7 #pragma once
      8 
      9 #import <Cocoa/Cocoa.h>
     10 #include <vector>
     11 
     12 #import "base/mac/cocoa_protocols.h"
     13 #include "base/memory/scoped_nsobject.h"
     14 #include "chrome/browser/task_manager/task_manager.h"
     15 #include "chrome/browser/ui/cocoa/table_row_nsimage_cache.h"
     16 
     17 @class WindowSizeAutosaver;
     18 class SkBitmap;
     19 class TaskManagerMac;
     20 
     21 // This class is responsible for loading the task manager window and for
     22 // managing it.
     23 @interface TaskManagerWindowController :
     24   NSWindowController<NSTableViewDataSource,
     25                      NSTableViewDelegate> {
     26  @private
     27   IBOutlet NSTableView* tableView_;
     28   IBOutlet NSButton* endProcessButton_;
     29   TaskManagerMac* taskManagerObserver_;  // weak
     30   TaskManager* taskManager_;  // weak
     31   TaskManagerModel* model_;  // weak
     32   bool highlightBackgroundResources_;
     33 
     34   scoped_nsobject<WindowSizeAutosaver> size_saver_;
     35 
     36   // These contain a permutation of [0..|model_->ResourceCount() - 1|]. Used to
     37   // implement sorting.
     38   std::vector<int> viewToModelMap_;
     39   std::vector<int> modelToViewMap_;
     40 
     41   // Descriptor of the current sort column.
     42   scoped_nsobject<NSSortDescriptor> currentSortDescriptor_;
     43 
     44   // Color we use for background resources.
     45   scoped_nsobject<NSColor> backgroundResourceColor_;
     46 }
     47 
     48 // Creates and shows the task manager's window.
     49 - (id)initWithTaskManagerObserver:(TaskManagerMac*)taskManagerObserver
     50      highlightBackgroundResources:(bool)highlightBackgroundResources;
     51 
     52 // Refreshes all data in the task manager table.
     53 - (void)reloadData;
     54 
     55 // Callback for "Stats for nerds" link.
     56 - (IBAction)statsLinkClicked:(id)sender;
     57 
     58 // Callback for "End process" button.
     59 - (IBAction)killSelectedProcesses:(id)sender;
     60 
     61 // Callback for double clicks on the table.
     62 - (void)selectDoubleClickedTab:(id)sender;
     63 @end
     64 
     65 @interface TaskManagerWindowController (TestingAPI)
     66 - (NSTableView*)tableView;
     67 @end
     68 
     69 // This class listens to task changed events sent by chrome.
     70 class TaskManagerMac : public TaskManagerModelObserver,
     71                        public TableRowNSImageCache::Table {
     72  public:
     73   TaskManagerMac(TaskManager* task_manager, bool highlight_background);
     74   virtual ~TaskManagerMac();
     75 
     76   // TaskManagerModelObserver
     77   virtual void OnModelChanged();
     78   virtual void OnItemsChanged(int start, int length);
     79   virtual void OnItemsAdded(int start, int length);
     80   virtual void OnItemsRemoved(int start, int length);
     81 
     82   // Called by the cocoa window controller when its window closes and the
     83   // controller destroyed itself. Informs the model to stop updating.
     84   void WindowWasClosed();
     85 
     86   // TableRowNSImageCache::Table
     87   virtual int RowCount() const;
     88   virtual SkBitmap GetIcon(int r) const;
     89 
     90   // Creates the task manager if it doesn't exist; otherwise, it activates the
     91   // existing task manager window. Highlights background resources if
     92   // |highlight_background_resources| is true.
     93   static void Show(bool highlight_background_resources);
     94 
     95   // Returns the TaskManager observed by |this|.
     96   TaskManager* task_manager() { return task_manager_; }
     97 
     98   // Lazily converts the image at the given row and caches it in |icon_cache_|.
     99   NSImage* GetImageForRow(int row);
    100 
    101   // Returns the cocoa object. Used for testing.
    102   TaskManagerWindowController* cocoa_controller() { return window_controller_; }
    103 
    104   // Returns true if the resource at this location is a background resource.
    105   bool IsBackgroundRow(int row) const;
    106  private:
    107   // The task manager.
    108   TaskManager* const task_manager_;  // weak
    109 
    110   // Our model.
    111   TaskManagerModel* const model_;  // weak
    112 
    113   // Controller of our window, destroys itself when the task manager window
    114   // is closed.
    115   TaskManagerWindowController* window_controller_;  // weak
    116 
    117   // Caches favicons for all rows. Needs to be initalized after |model_|.
    118   TableRowNSImageCache icon_cache_;
    119 
    120   // If true, highlight background resources.
    121   bool highlight_background_resources_;
    122 
    123   // An open task manager window. There can only be one open at a time. This
    124   // is reset to NULL when the window is closed.
    125   static TaskManagerMac* instance_;
    126 
    127   DISALLOW_COPY_AND_ASSIGN(TaskManagerMac);
    128 };
    129 
    130 #endif  // CHROME_BROWSER_UI_COCOA_TASK_MANAGER_MAC_H_
    131