Fixing build: GetViewContainer changed name from under me. :)
[chromium-blink-merge.git] / chrome / browser / base_history_model.h
blob51a0aca0b0724fa19f91d5a2c0d4e0d9cf73850f
1 // Copyright (c) 2006-2008 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.
5 // Base class used by history view. BaseHistoryModel provides support for
6 // fetching thumbnails and favicons, but not the actual contents that are
7 // displayed.
9 #ifndef CHROME_BROWSER_BASE_HISTORY_MODEL_H__
10 #define CHROME_BROWSER_BASE_HISTORY_MODEL_H__
12 #include "base/basictypes.h"
13 #include "base/time.h"
14 #include "chrome/browser/history/history.h"
15 #include "chrome/common/mru_cache.h"
16 #include "SkBitmap.h"
18 class Profile;
20 // Defines functions that allow a view associated with this model to
21 // learn that it should relayout/paint itself.
22 class BaseHistoryModelObserver {
23 public:
24 // Called when the data in the model has changed.
25 // |result_set_changed| is true when the model item counts have changed,
26 // false when the change is just item metadata (like thumbnails or
27 // starredness).
28 virtual void ModelChanged(bool result_set_changed) = 0;
30 // Called when a long operation has begun, to allow the observer to show
31 // that work is pending.
32 virtual void ModelBeginWork() = 0;
34 // Called when a long operation has completed.
35 virtual void ModelEndWork() = 0;
38 class BaseHistoryModel {
39 public:
40 explicit BaseHistoryModel(Profile* profile);
41 virtual ~BaseHistoryModel();
43 // The observer, notified of changes to the model or when processing
44 // begins/ends.
45 void SetObserver(BaseHistoryModelObserver* observer);
46 BaseHistoryModelObserver* GetObserver() const;
48 // Returns the number of history items currently in the model.
49 virtual int GetItemCount() = 0;
51 // Returns the time of the visit with the given index.
52 virtual Time GetVisitTime(int index) = 0;
54 // Returns the title at the specified index.
55 virtual const std::wstring& GetTitle(int index) = 0;
57 // Returns the URL at the specified index.
58 virtual const GURL& GetURL(int index) = 0;
60 // Returns the id of the URL at the specified index.
61 virtual history::URLID GetURLID(int index) = 0;
63 // Returns true if the page at the specified index is starred.
64 virtual bool IsStarred(int index) = 0;
66 // Returns true if the entries are search results (this affects the UI shown).
67 virtual bool IsSearchResults() { return is_search_results_; }
69 // Returns the snippet at the specified index.
70 virtual const Snippet& GetSnippet(int index) = 0;
72 // Returns the favicon or thumbnail for the specified page. If the image is
73 // not available, NULL is returned. If the image has NOT been loaded, it is
74 // loaded and the observer is called back when done loading.
75 SkBitmap* GetThumbnail(int index);
76 SkBitmap* GetFavicon(int index);
78 // Sets the new value of the search text, and requeries if the new value
79 // is different from the previous value.
80 virtual void SetSearchText(const std::wstring& search_text) { }
82 // Returns the search text.
83 virtual const std::wstring& GetSearchText() const = 0;
85 // Change the starred state of a given index.
86 virtual void SetPageStarred(int index, bool state) = 0;
88 // The following methods are only invoked by HistoryView if the HistoryView
89 // is configured to show bookmark controls.
91 // Returns true if the item is shown on the bookmark bar.
92 virtual bool IsOnBookmarkBar(int index) { return false; }
94 // Returns the path of the item on the bookmark bar. This may return the
95 // empty string.
96 virtual std::wstring GetBookmarkBarPath(int index) { return std::wstring(); }
98 // Edits the specified entry. This is intended for bookmarks where the user
99 // can edit various properties of the bookmark.
100 virtual void Edit(int index) {}
102 // Removes the specified entry.
103 virtual void Remove(int index) {}
105 // Removes the specified range from the model. This should NOT update the
106 // backend, rather it should just update the model and notify listeners
107 // appropriately. This need only be implemented if you turn on delete controls
108 // in the hosting HistoryView.
109 virtual void RemoveFromModel(int start, int length) { NOTREACHED(); }
111 // Reloads the model.
112 virtual void Refresh() = 0;
114 Profile* profile() const { return profile_; }
116 // Number of months history requests should go back for.
117 static const int kHistoryScopeMonths;
119 protected:
120 // Invoke when you're about to schedule a request on the history service. If
121 // no requests have been made, the observer is notified of ModelBeginWork.
122 void AboutToScheduleRequest();
124 // Invoke from a callback from the history service. If no requests are pending
125 // on the history service the observer is notified of ModelEndWork.
126 void RequestCompleted();
128 #ifndef NDEBUG
129 // For debugging, meant to be wrapped in a DCHECK.
130 bool IsValidIndex(int index) {
131 return (index >= 0 && index < GetItemCount());
133 #endif
135 // The user profile associated with the page that this model feeds.
136 Profile* profile_;
138 // For history requests. This is used when requesting favicons and
139 // thumbnails, subclasses may also use this.
140 CancelableRequestConsumerT<history::URLID, 0> cancelable_consumer_;
142 // Notified of changes to the content, typically HistoryView.
143 BaseHistoryModelObserver* observer_;
145 // Whether the last returned result was a set of search results.
146 bool is_search_results_;
148 private:
149 // Enum of the types of image we cache. Used by GetImage.
150 enum ImageType {
151 THUMBNAIL,
152 FAVICON
155 typedef MRUCache<history::URLID, SkBitmap> CacheType;
157 // Returns the image at the specified index. If the image has not been loaded
158 // it is loaded.
159 SkBitmap* GetImage(ImageType type, int index);
161 // Called when a request for the thumbnail data is complete. Updates the
162 // thumnails_ cache and notifies the observer (assuming the image is valid).
163 void OnThumbnailDataAvailable(
164 HistoryService::Handle request_handle,
165 scoped_refptr<RefCountedBytes> data);
167 // Callback when a favicon is available. Updates the favicons_ cache and
168 // notifies the observer (assuming we can extract a valid image).
169 void OnFaviconDataAvailable(
170 HistoryService::Handle handle,
171 bool know_favicon,
172 scoped_refptr<RefCountedBytes> data,
173 bool expired,
174 GURL icon_url);
176 // Keeps track of thumbnails for pages
177 CacheType thumbnails_;
179 // Keeps track of favicons for pages
180 CacheType favicons_;
182 DISALLOW_EVIL_CONSTRUCTORS(BaseHistoryModel);
185 #endif // CHROME_BROWSER_BASE_HISTORY_MODEL_H__