1// Copyright (c) 2012 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_TABLE_ROW_NSIMAGE_CACHE_H_
6#define CHROME_BROWSER_UI_COCOA_TABLE_ROW_NSIMAGE_CACHE_H_
7
8#import <Cocoa/Cocoa.h>
9
10#include "base/mac/scoped_nsobject.h"
11
12namespace gfx {
13class ImageSkia;
14}
15
16// There are several dialogs that display tabular data with one SkBitmap
17// per row. This class converts these SkBitmaps to NSImages on demand, and
18// caches the results.
19class TableRowNSImageCache {
20 public:
21  // Interface this cache expects for its table model.
22  class Table {
23   public:
24    // Returns the number of rows in the table.
25    virtual int RowCount() const = 0;
26
27    // Returns the icon of the |row|th row.
28    virtual gfx::ImageSkia GetIcon(int row) const = 0;
29
30   protected:
31    virtual ~Table() {}
32  };
33
34  // |model| must outlive the cache.
35  explicit TableRowNSImageCache(Table* model);
36  ~TableRowNSImageCache();
37
38  // Lazily converts the image at the given row and caches it in |icon_images_|.
39  NSImage* GetImageForRow(int row);
40
41  // Call these functions every time the table changes, to update the cache.
42  void OnModelChanged();
43  void OnItemsChanged(int start, int length);
44  void OnItemsAdded(int start, int length);
45  void OnItemsRemoved(int start, int length);
46
47 private:
48  // The table model we query for row count and icons.
49  Table* model_;  // weak
50
51  // Stores strong NSImage refs for icons. If an entry is NULL, it will be
52  // created in GetImageForRow().
53  base::scoped_nsobject<NSPointerArray> icon_images_;
54};
55
56#endif  // CHROME_BROWSER_UI_COCOA_TABLE_ROW_NSIMAGE_CACHE_H_
57