| 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 24 matching lines...) Expand all Loading... |
| 35 #include "chrome/browser/tabs/tab_strip_model.h" | 35 #include "chrome/browser/tabs/tab_strip_model.h" |
| 36 #include "chrome/browser/ui/browser.h" | 36 #include "chrome/browser/ui/browser.h" |
| 37 #include "chrome/browser/ui/browser_list.h" | 37 #include "chrome/browser/ui/browser_list.h" |
| 38 #include "chrome/browser/ui/browser_window.h" | 38 #include "chrome/browser/ui/browser_window.h" |
| 39 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 39 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 40 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" | 40 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
| 41 #include "chrome/browser/ui/webui/fileicon_source_chromeos.h" | 41 #include "chrome/browser/ui/webui/fileicon_source_chromeos.h" |
| 42 #include "chrome/common/chrome_paths.h" | 42 #include "chrome/common/chrome_paths.h" |
| 43 #include "chrome/common/chrome_switches.h" | 43 #include "chrome/common/chrome_switches.h" |
| 44 #include "chrome/common/url_constants.h" | 44 #include "chrome/common/url_constants.h" |
| 45 #include "content/browser/tab_contents/tab_contents.h" | 45 #include "content/browser/renderer_host/render_view_host.h" |
| 46 #include "content/browser/renderer_host/render_view_host_delegate.h" |
| 46 #include "content/public/browser/download_item.h" | 47 #include "content/public/browser/download_item.h" |
| 47 #include "content/public/browser/download_manager.h" | 48 #include "content/public/browser/download_manager.h" |
| 49 #include "content/public/browser/web_contents.h" |
| 48 #include "grit/browser_resources.h" | 50 #include "grit/browser_resources.h" |
| 49 #include "grit/chromium_strings.h" | 51 #include "grit/chromium_strings.h" |
| 50 #include "grit/generated_resources.h" | 52 #include "grit/generated_resources.h" |
| 51 #include "grit/locale_settings.h" | 53 #include "grit/locale_settings.h" |
| 52 #include "net/base/escape.h" | 54 #include "net/base/escape.h" |
| 53 #include "net/url_request/url_request_file_job.h" | 55 #include "net/url_request/url_request_file_job.h" |
| 54 #include "ui/base/resource/resource_bundle.h" | 56 #include "ui/base/resource/resource_bundle.h" |
| 55 | 57 |
| 58 using content::WebContents; |
| 59 |
| 56 namespace { | 60 namespace { |
| 57 | 61 |
| 58 static const int kPopupLeft = 0; | 62 static const int kPopupLeft = 0; |
| 59 static const int kPopupTop = 0; | 63 static const int kPopupTop = 0; |
| 60 static const int kPopupWidth = 250; | 64 static const int kPopupWidth = 250; |
| 61 // Minimum height of window must be 100, so kPopupHeight has space for | 65 // Minimum height of window must be 100, so kPopupHeight has space for |
| 62 // 2 download rows of 36 px and 'Show all files' link which is 29px. | 66 // 2 download rows of 36 px and 'Show all files' link which is 29px. |
| 63 static const int kPopupHeight = 36 * 2 + 29; | 67 static const int kPopupHeight = 36 * 2 + 29; |
| 64 | 68 |
| 65 static const char kPropertyPath[] = "path"; | 69 static const char kPropertyPath[] = "path"; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 | 150 |
| 147 private: | 151 private: |
| 148 // Downloads helpers. | 152 // Downloads helpers. |
| 149 DownloadItem* GetDownloadById(const ListValue* args); | 153 DownloadItem* GetDownloadById(const ListValue* args); |
| 150 void UpdateDownloadList(); | 154 void UpdateDownloadList(); |
| 151 void SendDownloads(); | 155 void SendDownloads(); |
| 152 void AddDownload(DownloadItem* item); | 156 void AddDownload(DownloadItem* item); |
| 153 bool SelectTab(const GURL& url); | 157 bool SelectTab(const GURL& url); |
| 154 | 158 |
| 155 Profile* profile_; | 159 Profile* profile_; |
| 156 TabContents* tab_contents_; | |
| 157 DownloadManager* download_manager_; | 160 DownloadManager* download_manager_; |
| 158 | 161 |
| 159 DownloadList active_downloads_; | 162 DownloadList active_downloads_; |
| 160 DownloadList downloads_; | 163 DownloadList downloads_; |
| 161 | 164 |
| 162 DISALLOW_COPY_AND_ASSIGN(ActiveDownloadsHandler); | 165 DISALLOW_COPY_AND_ASSIGN(ActiveDownloadsHandler); |
| 163 }; | 166 }; |
| 164 | 167 |
| 165 ActiveDownloadsHandler::ActiveDownloadsHandler() | 168 ActiveDownloadsHandler::ActiveDownloadsHandler() |
| 166 : profile_(NULL), | 169 : profile_(NULL), |
| 167 tab_contents_(NULL), | |
| 168 download_manager_(NULL) { | 170 download_manager_(NULL) { |
| 169 } | 171 } |
| 170 | 172 |
| 171 ActiveDownloadsHandler::~ActiveDownloadsHandler() { | 173 ActiveDownloadsHandler::~ActiveDownloadsHandler() { |
| 172 for (size_t i = 0; i < downloads_.size(); ++i) { | 174 for (size_t i = 0; i < downloads_.size(); ++i) { |
| 173 downloads_[i]->RemoveObserver(this); | 175 downloads_[i]->RemoveObserver(this); |
| 174 } | 176 } |
| 175 download_manager_->RemoveObserver(this); | 177 download_manager_->RemoveObserver(this); |
| 176 } | 178 } |
| 177 | 179 |
| 178 void ActiveDownloadsHandler::RegisterMessages() { | 180 void ActiveDownloadsHandler::RegisterMessages() { |
| 179 profile_ = Profile::FromWebUI(web_ui()); | 181 profile_ = Profile::FromWebUI(web_ui()); |
| 180 profile_->GetChromeURLDataManager()->AddDataSource(new FileIconSourceCros()); | 182 profile_->GetChromeURLDataManager()->AddDataSource(new FileIconSourceCros()); |
| 181 tab_contents_ = web_ui()->tab_contents(); | |
| 182 | 183 |
| 183 web_ui()->RegisterMessageCallback("getDownloads", | 184 web_ui()->RegisterMessageCallback("getDownloads", |
| 184 base::Bind(&ActiveDownloadsHandler::HandleGetDownloads, | 185 base::Bind(&ActiveDownloadsHandler::HandleGetDownloads, |
| 185 base::Unretained(this))); | 186 base::Unretained(this))); |
| 186 web_ui()->RegisterMessageCallback("pauseToggleDownload", | 187 web_ui()->RegisterMessageCallback("pauseToggleDownload", |
| 187 base::Bind(&ActiveDownloadsHandler::HandlePauseToggleDownload, | 188 base::Bind(&ActiveDownloadsHandler::HandlePauseToggleDownload, |
| 188 base::Unretained(this))); | 189 base::Unretained(this))); |
| 189 web_ui()->RegisterMessageCallback("allowDownload", | 190 web_ui()->RegisterMessageCallback("allowDownload", |
| 190 base::Bind(&ActiveDownloadsHandler::HandleAllowDownload, | 191 base::Bind(&ActiveDownloadsHandler::HandleAllowDownload, |
| 191 base::Unretained(this))); | 192 base::Unretained(this))); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 204 | 205 |
| 205 download_manager_ = | 206 download_manager_ = |
| 206 DownloadServiceFactory::GetForProfile(profile_)->GetDownloadManager(); | 207 DownloadServiceFactory::GetForProfile(profile_)->GetDownloadManager(); |
| 207 download_manager_->AddObserver(this); | 208 download_manager_->AddObserver(this); |
| 208 } | 209 } |
| 209 | 210 |
| 210 void ActiveDownloadsHandler::PlayMediaFile(const ListValue* args) { | 211 void ActiveDownloadsHandler::PlayMediaFile(const ListValue* args) { |
| 211 FilePath file_path(UTF16ToUTF8(ExtractStringValue(args))); | 212 FilePath file_path(UTF16ToUTF8(ExtractStringValue(args))); |
| 212 | 213 |
| 213 Browser* browser = Browser::GetBrowserForController( | 214 Browser* browser = Browser::GetBrowserForController( |
| 214 &tab_contents_->GetController(), NULL); | 215 &web_ui()->web_contents()->GetController(), NULL); |
| 215 MediaPlayer* mediaplayer = MediaPlayer::GetInstance(); | 216 MediaPlayer* mediaplayer = MediaPlayer::GetInstance(); |
| 216 mediaplayer->PopupMediaPlayer(browser); | 217 mediaplayer->PopupMediaPlayer(browser); |
| 217 mediaplayer->ForcePlayMediaFile(profile_, file_path); | 218 mediaplayer->ForcePlayMediaFile(profile_, file_path); |
| 218 } | 219 } |
| 219 | 220 |
| 220 DownloadItem* ActiveDownloadsHandler::GetDownloadById( | 221 DownloadItem* ActiveDownloadsHandler::GetDownloadById( |
| 221 const ListValue* args) { | 222 const ListValue* args) { |
| 222 int i; | 223 int i; |
| 223 if (!ExtractIntegerValue(args, &i)) | 224 if (!ExtractIntegerValue(args, &i)) |
| 224 return NULL; | 225 return NULL; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 247 } | 248 } |
| 248 } | 249 } |
| 249 | 250 |
| 250 void ActiveDownloadsHandler::HandleShowAllFiles(const ListValue* args) { | 251 void ActiveDownloadsHandler::HandleShowAllFiles(const ListValue* args) { |
| 251 file_manager_util::ViewFolder( | 252 file_manager_util::ViewFolder( |
| 252 DownloadPrefs::FromDownloadManager(download_manager_)->download_path()); | 253 DownloadPrefs::FromDownloadManager(download_manager_)->download_path()); |
| 253 } | 254 } |
| 254 | 255 |
| 255 bool ActiveDownloadsHandler::SelectTab(const GURL& url) { | 256 bool ActiveDownloadsHandler::SelectTab(const GURL& url) { |
| 256 for (TabContentsIterator it; !it.done(); ++it) { | 257 for (TabContentsIterator it; !it.done(); ++it) { |
| 257 TabContents* tab_contents = it->tab_contents(); | 258 WebContents* web_contents = it->web_contents(); |
| 258 if (tab_contents->GetURL() == url) { | 259 if (web_contents->GetURL() == url) { |
| 259 static_cast<RenderViewHostDelegate*>(tab_contents)->Activate(); | 260 web_contents->GetRenderViewHost()->delegate()->Activate(); |
| 260 return true; | 261 return true; |
| 261 } | 262 } |
| 262 } | 263 } |
| 263 return false; | 264 return false; |
| 264 } | 265 } |
| 265 | 266 |
| 266 void ActiveDownloadsHandler::OpenNewFullWindow(const ListValue* args) { | 267 void ActiveDownloadsHandler::OpenNewFullWindow(const ListValue* args) { |
| 267 std::string url = UTF16ToUTF8(ExtractStringValue(args)); | 268 std::string url = UTF16ToUTF8(ExtractStringValue(args)); |
| 268 | 269 |
| 269 if (SelectTab(GURL(url))) | 270 if (SelectTab(GURL(url))) |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 } | 347 } |
| 347 } | 348 } |
| 348 | 349 |
| 349 //////////////////////////////////////////////////////////////////////////////// | 350 //////////////////////////////////////////////////////////////////////////////// |
| 350 // | 351 // |
| 351 // ActiveDownloadsUI | 352 // ActiveDownloadsUI |
| 352 // | 353 // |
| 353 //////////////////////////////////////////////////////////////////////////////// | 354 //////////////////////////////////////////////////////////////////////////////// |
| 354 | 355 |
| 355 | 356 |
| 356 ActiveDownloadsUI::ActiveDownloadsUI(TabContents* contents) | 357 ActiveDownloadsUI::ActiveDownloadsUI(WebContents* contents) |
| 357 : HtmlDialogUI(contents), | 358 : HtmlDialogUI(contents), |
| 358 handler_(new ActiveDownloadsHandler()) { | 359 handler_(new ActiveDownloadsHandler()) { |
| 359 AddMessageHandler(handler_); | 360 AddMessageHandler(handler_); |
| 360 | 361 |
| 361 // Set up the chrome://active-downloads/ source. | 362 // Set up the chrome://active-downloads/ source. |
| 362 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 363 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
| 363 profile->GetChromeURLDataManager()->AddDataSource( | 364 profile->GetChromeURLDataManager()->AddDataSource( |
| 364 CreateActiveDownloadsUIHTMLSource()); | 365 CreateActiveDownloadsUIHTMLSource()); |
| 365 } | 366 } |
| 366 | 367 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 browser->window()->Show(); | 405 browser->window()->Show(); |
| 405 return browser; | 406 return browser; |
| 406 } | 407 } |
| 407 | 408 |
| 408 // static | 409 // static |
| 409 Browser* ActiveDownloadsUI::GetPopup() { | 410 Browser* ActiveDownloadsUI::GetPopup() { |
| 410 for (BrowserList::const_iterator it = BrowserList::begin(); | 411 for (BrowserList::const_iterator it = BrowserList::begin(); |
| 411 it != BrowserList::end(); | 412 it != BrowserList::end(); |
| 412 ++it) { | 413 ++it) { |
| 413 if ((*it)->is_type_panel() && (*it)->is_app()) { | 414 if ((*it)->is_type_panel() && (*it)->is_app()) { |
| 414 TabContents* tab_contents = (*it)->GetSelectedTabContents(); | 415 WebContents* web_contents = |
| 415 DCHECK(tab_contents); | 416 (*it)->GetSelectedTabContentsWrapper()->web_contents(); |
| 416 if (!tab_contents) | 417 DCHECK(web_contents); |
| 418 if (!web_contents) |
| 417 continue; | 419 continue; |
| 418 const GURL& url = tab_contents->GetURL(); | 420 const GURL& url = web_contents->GetURL(); |
| 419 | 421 |
| 420 if (url.SchemeIs(chrome::kChromeUIScheme) && | 422 if (url.SchemeIs(chrome::kChromeUIScheme) && |
| 421 url.host() == chrome::kChromeUIActiveDownloadsHost) { | 423 url.host() == chrome::kChromeUIActiveDownloadsHost) { |
| 422 return (*it); | 424 return (*it); |
| 423 } | 425 } |
| 424 } | 426 } |
| 425 } | 427 } |
| 426 return NULL; | 428 return NULL; |
| 427 } | 429 } |
| 428 | 430 |
| 429 const ActiveDownloadsUI::DownloadList& ActiveDownloadsUI::GetDownloads() const { | 431 const ActiveDownloadsUI::DownloadList& ActiveDownloadsUI::GetDownloads() const { |
| 430 return handler_->downloads(); | 432 return handler_->downloads(); |
| 431 } | 433 } |
| OLD | NEW |