Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(108)

Side by Side Diff: chrome/browser/ui/webui/downloads_dom_handler.h

Issue 10854127: Rewrite DownloadsDOMHandler (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOM_HANDLER_H_ 5 #ifndef CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOM_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOM_HANDLER_H_ 6 #define CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOM_HANDLER_H_
7 7
8 #include <set>
8 #include <vector> 9 #include <vector>
9 10
10 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
11 #include "base/memory/weak_ptr.h" 12 #include "base/memory/weak_ptr.h"
12 #include "content/public/browser/download_item.h" 13 #include "content/public/browser/download_item.h"
13 #include "content/public/browser/download_manager.h" 14 #include "content/public/browser/download_manager.h"
14 #include "content/public/browser/web_ui_message_handler.h" 15 #include "content/public/browser/web_ui_message_handler.h"
15 16
16 namespace base { 17 namespace base {
17 class ListValue; 18 class ListValue;
18 } 19 }
19 20
21 namespace content {
22 class WebContents;
23 }
24
20 // The handler for Javascript messages related to the "downloads" view, 25 // The handler for Javascript messages related to the "downloads" view,
21 // also observes changes to the download manager. 26 // also observes changes to the download manager.
22 class DownloadsDOMHandler : public content::WebUIMessageHandler, 27 class DownloadsDOMHandler : public content::WebUIMessageHandler,
23 public content::DownloadManager::Observer, 28 public content::DownloadManager::Observer,
24 public content::DownloadItem::Observer { 29 public content::DownloadItem::Observer {
25 public: 30 public:
26 explicit DownloadsDOMHandler(content::DownloadManager* dlm); 31 explicit DownloadsDOMHandler(content::DownloadManager* dlm);
27 virtual ~DownloadsDOMHandler(); 32 virtual ~DownloadsDOMHandler();
28 33
29 void Init(); 34 void Init();
30 35
31 // WebUIMessageHandler implementation. 36 // WebUIMessageHandler implementation.
32 virtual void RegisterMessages() OVERRIDE; 37 virtual void RegisterMessages() OVERRIDE;
33 38
34 // content::DownloadItem::Observer interface 39 // content::DownloadItem::Observer interface
35 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE; 40 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE;
36 virtual void OnDownloadDestroyed(content::DownloadItem* download) OVERRIDE; 41 virtual void OnDownloadDestroyed(content::DownloadItem* download) OVERRIDE;
37 42
38 // content::DownloadManager::Observer interface 43 // content::DownloadManager::Observer interface
39 virtual void ModelChanged(content::DownloadManager* manager) OVERRIDE; 44 virtual void OnDownloadCreated(content::DownloadManager* manager,
45 content::DownloadItem* download) OVERRIDE;
40 virtual void ManagerGoingDown(content::DownloadManager* manager) OVERRIDE; 46 virtual void ManagerGoingDown(content::DownloadManager* manager) OVERRIDE;
41 47
42 // Callback for the "onPageLoaded" message. 48 // Callback for the "onPageLoaded" message.
43 void OnPageLoaded(const base::ListValue* args); 49 void OnPageLoaded(const base::ListValue* args);
44 50
45 // Callback for the "getDownloads" message. 51 // Callback for the "getDownloads" message.
46 void HandleGetDownloads(const base::ListValue* args); 52 void HandleGetDownloads(const base::ListValue* args);
47 53
48 // Callback for the "openFile" message - opens the file in the shell. 54 // Callback for the "openFile" message - opens the file in the shell.
49 void HandleOpenFile(const base::ListValue* args); 55 void HandleOpenFile(const base::ListValue* args);
(...skipping 22 matching lines...) Expand all
72 // Callback for the "cancel" message - cancels the download. 78 // Callback for the "cancel" message - cancels the download.
73 void HandleCancel(const base::ListValue* args); 79 void HandleCancel(const base::ListValue* args);
74 80
75 // Callback for the "clearAll" message - clears all the downloads. 81 // Callback for the "clearAll" message - clears all the downloads.
76 void HandleClearAll(const base::ListValue* args); 82 void HandleClearAll(const base::ListValue* args);
77 83
78 // Callback for the "openDownloadsFolder" message - opens the downloads 84 // Callback for the "openDownloadsFolder" message - opens the downloads
79 // folder. 85 // folder.
80 void HandleOpenDownloadsFolder(const base::ListValue* args); 86 void HandleOpenDownloadsFolder(const base::ListValue* args);
81 87
88 protected:
89 // These methods are for mocking so that most of this class does not actually
90 // depend on WebUI. The other methods that depend on WebUI are
91 // RegisterMessages() and HandleDrag().
92 virtual content::WebContents* GetWebUIWebContents();
93 virtual void CallDownloadsList(const base::ListValue& downloads);
94 virtual void CallDownloadUpdated(const base::ListValue& download);
95
82 private: 96 private:
83 class OriginalDownloadManagerObserver; 97 typedef std::set<content::DownloadItem*> DownloadSet;
84 98
85 // Send the current list of downloads to the page. 99 // Send the current list of downloads to the page.
Randy Smith (Not in Mondays) 2012/08/17 17:49:47 Could you document somewhere (here, observing_item
benjhayden 2012/08/17 20:54:15 Documented in OnDownloadCreated by the AddObserver
86 void SendCurrentDownloads(); 100 void SendCurrentDownloads();
87 101
102 // Returns all the items for both DownloadManagers matching |search_text_|.
103 void SearchDownloads(content::DownloadManager::DownloadVector* downloads);
104
88 // Clear all download items and their observers. 105 // Clear all download items and their observers.
89 void ClearDownloadItems(); 106 void ClearDownloadItems();
90 107
91 // Display a native prompt asking the user for confirmation after accepting 108 // Display a native prompt asking the user for confirmation after accepting
92 // the dangerous download specified by |dangerous|. The function returns 109 // the dangerous download specified by |dangerous|. The function returns
93 // immediately, and will invoke DangerPromptAccepted() asynchronously if the 110 // immediately, and will invoke DangerPromptAccepted() asynchronously if the
94 // user accepts the dangerous download. The native prompt will observe 111 // user accepts the dangerous download. The native prompt will observe
95 // |dangerous| until either the dialog is dismissed or |dangerous| is no 112 // |dangerous| until either the dialog is dismissed or |dangerous| is no
96 // longer an in-progress dangerous download. 113 // longer an in-progress dangerous download.
97 void ShowDangerPrompt(content::DownloadItem* dangerous); 114 void ShowDangerPrompt(content::DownloadItem* dangerous);
98 115
99 // Called when the user accepts a dangerous download via the 116 // Called when the user accepts a dangerous download via the
100 // DownloadDangerPrompt invoked via ShowDangerPrompt(). 117 // DownloadDangerPrompt invoked via ShowDangerPrompt().
101 void DangerPromptAccepted(int download_id); 118 void DangerPromptAccepted(int download_id);
102 119
103 // Return the download that corresponds to a given id.
104 content::DownloadItem* GetDownloadById(int id);
105
106 // Return the download that is referred to in a given value. 120 // Return the download that is referred to in a given value.
107 content::DownloadItem* GetDownloadByValue(const base::ListValue* args); 121 content::DownloadItem* GetDownloadByValue(const base::ListValue* args);
108 122
109 // Current search text. 123 // Current search text.
110 std::wstring search_text_; 124 string16 search_text_;
125
126 // Keep track of all items that we are observing so that we can be sure to
127 // RemoveObserver when we are destroyed.
128 DownloadSet observing_items_;
111 129
112 // Our model 130 // Our model
113 content::DownloadManager* download_manager_; 131 content::DownloadManager* download_manager_;
114 132
115 // If |download_manager_| belongs to an incognito profile than this 133 // If |download_manager_| belongs to an incognito profile then this
116 // is the DownloadManager for the original profile; otherwise, this is 134 // is the DownloadManager for the original profile; otherwise, this is
117 // NULL. 135 // NULL.
118 content::DownloadManager* original_profile_download_manager_; 136 content::DownloadManager* original_profile_download_manager_;
119 137
120 // True once the page has loaded the first time (it may load multiple times, 138 bool update_scheduled_;
121 // e.g. on reload).
122 bool initialized_;
123
124 // The current set of visible DownloadItems for this view received from the
125 // DownloadManager. DownloadManager owns the DownloadItems. The vector is
126 // kept in order, sorted by ascending start time.
127 // Note that when a download item is removed, the entry in the vector becomes
128 // null. This should only be a transient state, as a ModelChanged()
129 // notification should follow close on the heels of such a change.
130 typedef std::vector<content::DownloadItem*> OrderedDownloads;
131 OrderedDownloads download_items_;
132 139
133 base::WeakPtrFactory<DownloadsDOMHandler> weak_ptr_factory_; 140 base::WeakPtrFactory<DownloadsDOMHandler> weak_ptr_factory_;
134 141
135 DISALLOW_COPY_AND_ASSIGN(DownloadsDOMHandler); 142 DISALLOW_COPY_AND_ASSIGN(DownloadsDOMHandler);
136 }; 143 };
137 144
138 #endif // CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOM_HANDLER_H_ 145 #endif // CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOM_HANDLER_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/ui/webui/downloads_dom_handler.cc » ('j') | chrome/browser/ui/webui/downloads_dom_handler.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698