jumplist_win.h revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2006-2009 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_JUMPLIST_WIN_H_
6#define CHROME_BROWSER_JUMPLIST_WIN_H_
7
8#include <list>
9#include <string>
10#include <utility>
11#include <vector>
12
13#include "base/ref_counted.h"
14#include "chrome/browser/cancelable_request.h"
15#include "chrome/browser/history/history.h"
16#include "chrome/browser/sessions/tab_restore_service.h"
17
18class FilePath;
19class Profile;
20class PageUsageData;
21
22// Represents a class used for creating an IShellLink object by the utility
23// functions in this file.
24// This class consists of three strings and a integer.
25// * arguments (std::wstring)
26//   The arguments for the application.
27// * title (std::wstring)
28//   The string to be displayed in a JumpList.
29// * icon (std::wstring)
30//   The absolute path to an icon to be displayed in a JumpList.
31// * index (int)
32//   The icon index in the icon file. If an icon file consists of two or more
33//   icons, set this value to identify the icon. If an icon file consists of
34// one icon, this value is 0.
35// Even though an IShellLink also needs the absolute path to an application to
36// be executed, this class does not have any variables for it because our
37// utility functions always use "chrome.exe" as the application and we don't
38// need it.
39class ShellLinkItem : public base::RefCountedThreadSafe<ShellLinkItem> {
40 public:
41  ShellLinkItem() : index_(0), favicon_(false) {
42  }
43
44  const std::wstring& arguments() const { return arguments_; }
45  const std::wstring& title() const { return title_; }
46  const std::wstring& icon() const { return icon_; }
47  int index() const { return index_; }
48  scoped_refptr<RefCountedMemory> data() const { return data_; }
49
50  void SetArguments(const std::wstring& arguments) {
51    arguments_ = arguments;
52  }
53
54  void SetTitle(const std::wstring& title) {
55    title_ = title;
56  }
57
58  void SetIcon(const std::wstring& icon, int index, bool favicon) {
59    icon_ = icon;
60    index_ = index;
61    favicon_ = favicon;
62  }
63
64  void SetIconData(scoped_refptr<RefCountedMemory> data) {
65    data_ = data;
66  }
67
68 private:
69  friend class base::RefCountedThreadSafe<ShellLinkItem>;
70
71  ~ShellLinkItem() {}
72
73  std::wstring arguments_;
74  std::wstring title_;
75  std::wstring icon_;
76  scoped_refptr<RefCountedMemory> data_;
77  int index_;
78  bool favicon_;
79
80  DISALLOW_COPY_AND_ASSIGN(ShellLinkItem);
81};
82
83typedef std::vector<scoped_refptr<ShellLinkItem> > ShellLinkItemList;
84
85// A class which implements an application JumpList.
86// This class encapsulates operations required for updating an application
87// JumpList:
88// * Retrieving "Most Visited" pages from HistoryService;
89// * Retrieving strings from the application resource;
90// * Creatng COM objects used by JumpList from PageUsageData objects;
91// * Adding COM objects to JumpList, etc.
92//
93// This class also implements TabRestoreService::Observer. So, once we call
94// AddObserver() and register this class as an observer, it automatically
95// updates a JumpList when a tab is added or removed.
96//
97// Updating a JumpList requires some file operations and it is not good to
98// update it in a UI thread. To solve this problem, this class posts a
99// task when it actually updates a JumpList. (This task is implemented in an
100// anomynous namespace in "jumplist_win.cc".)
101class JumpList : public TabRestoreService::Observer {
102 public:
103  JumpList();
104  ~JumpList();
105
106  // Registers (or unregisters) this object as an observer.
107  // When the TabRestoreService object notifies the tab status is changed, this
108  // class automatically updates an application JumpList.
109  bool AddObserver(Profile* profile);
110  void RemoveObserver();
111
112  // Observer callback for TabRestoreService::Observer to notify when a tab is
113  // added or removed.
114  // This function sends a query that retrieves "Most Visited" pages to
115  // HistoryService. When the query finishes successfully, HistoryService call
116  // OnSegmentUsageAvailable().
117  virtual void TabRestoreServiceChanged(TabRestoreService* service);
118
119  // Observer callback to notice when our associated TabRestoreService
120  // is destroyed.
121  virtual void TabRestoreServiceDestroyed(TabRestoreService* service);
122
123  // Returns true if the custom JumpList is enabled.
124  // We use the custom JumpList when we satisfy the following conditions:
125  // * Chromium is running on Windows 7 and;
126  // * Chromium is lauched without a "--disable-custom-jumplist" option.
127  // TODO(hbono): to be enabled by default when we finalize the categories and
128  // items of our JumpList.
129  static bool Enabled();
130
131 protected:
132  // Creates a ShellLinkItem object from a tab (or a window) and add it to the
133  // given list.
134  // These functions are copied from the RecentlyClosedTabsHandler class for
135  // compatibility with the new-tab page.
136  bool AddTab(const TabRestoreService::Tab* tab,
137              ShellLinkItemList* list,
138              size_t max_items);
139  bool AddWindow(const TabRestoreService::Window* window,
140                 ShellLinkItemList* list,
141                 size_t max_items);
142
143  // Starts loading a fav icon for each URL in |icon_urls_|.
144  // This function just sends a query to HistoryService.
145  bool StartLoadingFavIcon();
146
147  // A callback function for HistoryService that notify when the "Most Visited"
148  // list is available.
149  // This function updates the ShellLinkItemList objects and send another query
150  // that retrieves a fav icon for each URL in the list.
151  void OnSegmentUsageAvailable(CancelableRequestProvider::Handle handle,
152                               std::vector<PageUsageData*>* data);
153
154  // a callback function for HistoryService that notify when a requested fav
155  // icon is available.
156  // To avoid file operations, this function just attaches the given data to
157  // a ShellLinkItem object.
158  // When finishing loading all fav icons, this function posts a task that
159  // decompresses collected fav icons and updates a JumpList.
160  void OnFavIconDataAvailable(HistoryService::Handle handle,
161                              bool know_favicon,
162                              scoped_refptr<RefCountedMemory> data,
163                              bool expired,
164                              GURL icon_url);
165
166 private:
167  // Our consumers for HistoryService.
168  CancelableRequestConsumer most_visited_consumer_;
169  CancelableRequestConsumer fav_icon_consumer_;
170
171  // The Profile object used for listening its events.
172  Profile* profile_;
173
174  // App id to associate with the jump list.
175  std::wstring app_id_;
176
177  // The directory which contains JumpList icons.
178  FilePath icon_dir_;
179
180  // Items in the "Most Visited" category of the application JumpList.
181  ShellLinkItemList most_visited_pages_;
182
183  // Items in the "Recently Closed" category of the application JumpList.
184  ShellLinkItemList recently_closed_pages_;
185
186  // A list of URLs we need to retrieve their fav icons.
187  typedef std::pair<std::string, scoped_refptr<ShellLinkItem> > URLPair;
188  std::list<URLPair> icon_urls_;
189};
190
191#endif  // CHROME_BROWSER_JUMPLIST_WIN_H_
192