Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 #include "chrome/browser/ui/webui/active_downloads_ui.h" | 5 #include "chrome/browser/ui/webui/active_downloads_ui.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 76 virtual std::string GetMimeType(const std::string&) const { | 76 virtual std::string GetMimeType(const std::string&) const { |
| 77 return "text/html"; | 77 return "text/html"; |
| 78 } | 78 } |
| 79 | 79 |
| 80 private: | 80 private: |
| 81 ~ActiveDownloadsUIHTMLSource() {} | 81 ~ActiveDownloadsUIHTMLSource() {} |
| 82 | 82 |
| 83 DISALLOW_COPY_AND_ASSIGN(ActiveDownloadsUIHTMLSource); | 83 DISALLOW_COPY_AND_ASSIGN(ActiveDownloadsUIHTMLSource); |
| 84 }; | 84 }; |
| 85 | 85 |
| 86 // The handler for Javascript messages related to the "active_downloads" view. | |
|
achuithb
2011/06/23 02:56:38
This code just moved out of the anonymous namespac
| |
| 87 class ActiveDownloadsHandler | |
| 88 : public WebUIMessageHandler, | |
| 89 public DownloadManager::Observer, | |
| 90 public DownloadItem::Observer { | |
| 91 public: | |
| 92 ActiveDownloadsHandler(); | |
| 93 virtual ~ActiveDownloadsHandler(); | |
| 94 | |
| 95 // Initialization after Attach. | |
| 96 void Init(); | |
| 97 | |
| 98 // WebUIMessageHandler implementation. | |
| 99 virtual WebUIMessageHandler* Attach(WebUI* web_ui); | |
| 100 virtual void RegisterMessages(); | |
| 101 | |
| 102 // DownloadItem::Observer interface. | |
| 103 virtual void OnDownloadUpdated(DownloadItem* item); | |
| 104 virtual void OnDownloadOpened(DownloadItem* item) { } | |
| 105 | |
| 106 // DownloadManager::Observer interface. | |
| 107 virtual void ModelChanged(); | |
| 108 | |
| 109 // WebUI Callbacks. | |
| 110 void HandleGetDownloads(const ListValue* args); | |
| 111 void HandlePauseToggleDownload(const ListValue* args); | |
| 112 void HandleCancelDownload(const ListValue* args); | |
| 113 void HandleAllowDownload(const ListValue* args); | |
| 114 void OpenNewFullWindow(const ListValue* args); | |
| 115 void PlayMediaFile(const ListValue* args); | |
| 116 | |
| 117 private: | |
| 118 // Downloads helpers. | |
| 119 DownloadItem* GetDownloadById(const ListValue* args); | |
| 120 void UpdateDownloadList(); | |
| 121 void SendDownloads(); | |
| 122 void AddDownload(DownloadItem* item); | |
| 123 bool SelectTab(const GURL& url); | |
| 124 | |
| 125 Profile* profile_; | |
| 126 TabContents* tab_contents_; | |
| 127 DownloadManager* download_manager_; | |
| 128 | |
| 129 typedef std::vector<DownloadItem*> DownloadList; | |
| 130 DownloadList active_downloads_; | |
| 131 DownloadList downloads_; | |
| 132 | |
| 133 DISALLOW_COPY_AND_ASSIGN(ActiveDownloadsHandler); | |
| 134 }; | |
| 135 | |
| 136 //////////////////////////////////////////////////////////////////////////////// | 86 //////////////////////////////////////////////////////////////////////////////// |
| 137 // | 87 // |
| 138 // ActiveDownloadsUIHTMLSource | 88 // ActiveDownloadsUIHTMLSource |
| 139 // | 89 // |
| 140 //////////////////////////////////////////////////////////////////////////////// | 90 //////////////////////////////////////////////////////////////////////////////// |
| 141 | 91 |
| 142 ActiveDownloadsUIHTMLSource::ActiveDownloadsUIHTMLSource() | 92 ActiveDownloadsUIHTMLSource::ActiveDownloadsUIHTMLSource() |
| 143 : DataSource(chrome::kChromeUIActiveDownloadsHost, MessageLoop::current()) { | 93 : DataSource(chrome::kChromeUIActiveDownloadsHost, MessageLoop::current()) { |
| 144 } | 94 } |
| 145 | 95 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 182 const std::string full_html = jstemplate_builder::GetI18nTemplateHtml( | 132 const std::string full_html = jstemplate_builder::GetI18nTemplateHtml( |
| 183 active_downloads_html, &localized_strings); | 133 active_downloads_html, &localized_strings); |
| 184 | 134 |
| 185 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); | 135 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); |
| 186 html_bytes->data.resize(full_html.size()); | 136 html_bytes->data.resize(full_html.size()); |
| 187 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); | 137 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); |
| 188 | 138 |
| 189 SendResponse(request_id, html_bytes); | 139 SendResponse(request_id, html_bytes); |
| 190 } | 140 } |
| 191 | 141 |
| 142 } // namespace | |
| 143 | |
| 192 //////////////////////////////////////////////////////////////////////////////// | 144 //////////////////////////////////////////////////////////////////////////////// |
| 193 // | 145 // |
| 194 // ActiveDownloadsHandler | 146 // ActiveDownloadsHandler |
| 195 // | 147 // |
| 196 //////////////////////////////////////////////////////////////////////////////// | 148 //////////////////////////////////////////////////////////////////////////////// |
| 197 | 149 |
| 150 // The handler for Javascript messages related to the "active_downloads" view. | |
| 151 class ActiveDownloadsHandler | |
| 152 : public WebUIMessageHandler, | |
| 153 public DownloadManager::Observer, | |
| 154 public DownloadItem::Observer { | |
| 155 public: | |
| 156 ActiveDownloadsHandler(); | |
| 157 virtual ~ActiveDownloadsHandler(); | |
| 158 | |
| 159 // Initialization after Attach. | |
| 160 void Init(); | |
| 161 | |
| 162 // WebUIMessageHandler implementation. | |
| 163 virtual WebUIMessageHandler* Attach(WebUI* web_ui); | |
| 164 virtual void RegisterMessages(); | |
| 165 | |
| 166 // DownloadItem::Observer interface. | |
| 167 virtual void OnDownloadUpdated(DownloadItem* item); | |
| 168 virtual void OnDownloadOpened(DownloadItem* item) { } | |
| 169 | |
| 170 // DownloadManager::Observer interface. | |
| 171 virtual void ModelChanged(); | |
| 172 | |
| 173 // WebUI Callbacks. | |
| 174 void HandleGetDownloads(const ListValue* args); | |
| 175 void HandlePauseToggleDownload(const ListValue* args); | |
| 176 void HandleCancelDownload(const ListValue* args); | |
| 177 void HandleAllowDownload(const ListValue* args); | |
| 178 void OpenNewFullWindow(const ListValue* args); | |
| 179 void PlayMediaFile(const ListValue* args); | |
| 180 | |
| 181 // For testing. | |
| 182 typedef std::vector<DownloadItem*> DownloadList; | |
| 183 const DownloadList& downloads() const { return downloads_; } | |
| 184 | |
| 185 private: | |
| 186 // Downloads helpers. | |
| 187 DownloadItem* GetDownloadById(const ListValue* args); | |
| 188 void UpdateDownloadList(); | |
| 189 void SendDownloads(); | |
| 190 void AddDownload(DownloadItem* item); | |
| 191 bool SelectTab(const GURL& url); | |
| 192 | |
| 193 Profile* profile_; | |
| 194 TabContents* tab_contents_; | |
| 195 DownloadManager* download_manager_; | |
| 196 | |
| 197 DownloadList active_downloads_; | |
| 198 DownloadList downloads_; | |
| 199 | |
| 200 DISALLOW_COPY_AND_ASSIGN(ActiveDownloadsHandler); | |
| 201 }; | |
| 202 | |
| 198 ActiveDownloadsHandler::ActiveDownloadsHandler() | 203 ActiveDownloadsHandler::ActiveDownloadsHandler() |
| 199 : profile_(NULL), | 204 : profile_(NULL), |
| 200 tab_contents_(NULL), | 205 tab_contents_(NULL), |
| 201 download_manager_(NULL) { | 206 download_manager_(NULL) { |
| 202 } | 207 } |
| 203 | 208 |
| 204 ActiveDownloadsHandler::~ActiveDownloadsHandler() { | 209 ActiveDownloadsHandler::~ActiveDownloadsHandler() { |
| 205 for (size_t i = 0; i < downloads_.size(); ++i) { | 210 for (size_t i = 0; i < downloads_.size(); ++i) { |
| 206 downloads_[i]->RemoveObserver(this); | 211 downloads_[i]->RemoveObserver(this); |
| 207 } | 212 } |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 358 active_downloads_.erase(ita); | 363 active_downloads_.erase(ita); |
| 359 SendDownloads(); | 364 SendDownloads(); |
| 360 } else { | 365 } else { |
| 361 const size_t id = it - downloads_.begin(); | 366 const size_t id = it - downloads_.begin(); |
| 362 scoped_ptr<DictionaryValue> result( | 367 scoped_ptr<DictionaryValue> result( |
| 363 download_util::CreateDownloadItemValue(item, id)); | 368 download_util::CreateDownloadItemValue(item, id)); |
| 364 web_ui_->CallJavascriptFunction("downloadUpdated", *result); | 369 web_ui_->CallJavascriptFunction("downloadUpdated", *result); |
| 365 } | 370 } |
| 366 } | 371 } |
| 367 | 372 |
| 368 } // namespace | |
| 369 | |
| 370 //////////////////////////////////////////////////////////////////////////////// | 373 //////////////////////////////////////////////////////////////////////////////// |
| 371 // | 374 // |
| 372 // ActiveDownloadsUI | 375 // ActiveDownloadsUI |
| 373 // | 376 // |
| 374 //////////////////////////////////////////////////////////////////////////////// | 377 //////////////////////////////////////////////////////////////////////////////// |
| 375 | 378 |
| 376 | 379 |
| 377 ActiveDownloadsUI::ActiveDownloadsUI(TabContents* contents) | 380 ActiveDownloadsUI::ActiveDownloadsUI(TabContents* contents) |
| 378 : HtmlDialogUI(contents) { | 381 : HtmlDialogUI(contents), |
| 379 ActiveDownloadsHandler* handler = new ActiveDownloadsHandler(); | 382 handler_(new ActiveDownloadsHandler()) { |
| 380 AddMessageHandler(handler->Attach(this)); | 383 AddMessageHandler(handler_->Attach(this)); |
| 381 handler->Init(); | 384 handler_->Init(); |
| 382 ActiveDownloadsUIHTMLSource* html_source = new ActiveDownloadsUIHTMLSource(); | 385 ActiveDownloadsUIHTMLSource* html_source = new ActiveDownloadsUIHTMLSource(); |
| 383 | 386 |
| 384 // Set up the chrome://active-downloads/ source. | 387 // Set up the chrome://active-downloads/ source. |
| 385 contents->profile()->GetChromeURLDataManager()->AddDataSource(html_source); | 388 contents->profile()->GetChromeURLDataManager()->AddDataSource(html_source); |
| 386 } | 389 } |
| 387 | 390 |
| 388 // static | 391 // static |
| 389 Browser* ActiveDownloadsUI::OpenPopup(Profile* profile) { | 392 Browser* ActiveDownloadsUI::OpenPopup(Profile* profile) { |
| 390 Browser* browser = GetPopup(profile); | 393 Browser* browser = GetPopup(profile); |
| 391 | 394 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 426 | 429 |
| 427 if (url.SchemeIs(chrome::kChromeUIScheme) && | 430 if (url.SchemeIs(chrome::kChromeUIScheme) && |
| 428 url.host() == chrome::kChromeUIActiveDownloadsHost) { | 431 url.host() == chrome::kChromeUIActiveDownloadsHost) { |
| 429 return (*it); | 432 return (*it); |
| 430 } | 433 } |
| 431 } | 434 } |
| 432 } | 435 } |
| 433 return NULL; | 436 return NULL; |
| 434 } | 437 } |
| 435 | 438 |
| 439 const ActiveDownloadsUI::DownloadList& ActiveDownloadsUI::GetDownloads() const { | |
| 440 return handler_->downloads(); | |
| 441 } | |
| OLD | NEW |