[email protected] | 0dd9e8b | 2012-01-04 13:36:16 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | fd3ad51 | 2010-09-25 14:08:51 | [diff] [blame] | 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_PLUGIN_DOWNLOAD_HELPER_H_ | ||||
6 | #define CHROME_BROWSER_PLUGIN_DOWNLOAD_HELPER_H_ | ||||
7 | #pragma once | ||||
8 | |||||
[email protected] | 0dd9e8b | 2012-01-04 13:36:16 | [diff] [blame] | 9 | #include "base/callback.h" |
[email protected] | fd3ad51 | 2010-09-25 14:08:51 | [diff] [blame] | 10 | #include "base/file_path.h" |
[email protected] | 0dd9e8b | 2012-01-04 13:36:16 | [diff] [blame] | 11 | #include "base/memory/scoped_ptr.h" |
[email protected] | c530c85 | 2011-10-24 18:18:34 | [diff] [blame] | 12 | #include "content/public/common/url_fetcher_delegate.h" |
[email protected] | 0dd9e8b | 2012-01-04 13:36:16 | [diff] [blame] | 13 | #include "googleurl/src/gurl.h" |
14 | |||||
15 | namespace base { | ||||
16 | class MessageLoopProxy; | ||||
17 | } | ||||
[email protected] | fd3ad51 | 2010-09-25 14:08:51 | [diff] [blame] | 18 | |
[email protected] | c530c85 | 2011-10-24 18:18:34 | [diff] [blame] | 19 | namespace net { |
20 | class URLRequestContextGetter; | ||||
21 | } | ||||
22 | |||||
[email protected] | fd3ad51 | 2010-09-25 14:08:51 | [diff] [blame] | 23 | // The PluginDownloadUrlHelper is used to handle one download URL request |
24 | // from the plugin. Each download request is handled by a new instance | ||||
25 | // of this class. | ||||
[email protected] | c530c85 | 2011-10-24 18:18:34 | [diff] [blame] | 26 | class PluginDownloadUrlHelper : public content::URLFetcherDelegate { |
[email protected] | fd3ad51 | 2010-09-25 14:08:51 | [diff] [blame] | 27 | public: |
[email protected] | 0dd9e8b | 2012-01-04 13:36:16 | [diff] [blame] | 28 | typedef base::Callback<void(const FilePath&)> DownloadFinishedCallback; |
[email protected] | 725f688 | 2012-01-13 18:50:06 | [diff] [blame] | 29 | typedef base::Callback<void(const std::string&)> ErrorCallback; |
[email protected] | fd3ad51 | 2010-09-25 14:08:51 | [diff] [blame] | 30 | |
[email protected] | 0dd9e8b | 2012-01-04 13:36:16 | [diff] [blame] | 31 | PluginDownloadUrlHelper(); |
32 | virtual ~PluginDownloadUrlHelper(); | ||||
[email protected] | fd3ad51 | 2010-09-25 14:08:51 | [diff] [blame] | 33 | |
[email protected] | 0dd9e8b | 2012-01-04 13:36:16 | [diff] [blame] | 34 | void InitiateDownload(const GURL& download_url, |
35 | net::URLRequestContextGetter* request_context, | ||||
[email protected] | 725f688 | 2012-01-13 18:50:06 | [diff] [blame] | 36 | const DownloadFinishedCallback& callback, |
37 | const ErrorCallback& error_callback); | ||||
[email protected] | fd3ad51 | 2010-09-25 14:08:51 | [diff] [blame] | 38 | |
[email protected] | c530c85 | 2011-10-24 18:18:34 | [diff] [blame] | 39 | // content::URLFetcherDelegate |
[email protected] | 0dd9e8b | 2012-01-04 13:36:16 | [diff] [blame] | 40 | virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; |
[email protected] | fd3ad51 | 2010-09-25 14:08:51 | [diff] [blame] | 41 | |
[email protected] | 0dd9e8b | 2012-01-04 13:36:16 | [diff] [blame] | 42 | private: |
43 | // Renames the file (which was downloaded to a temporary file) to the filename | ||||
44 | // of the download URL. | ||||
45 | void RenameDownloadedFile(); | ||||
[email protected] | fd3ad51 | 2010-09-25 14:08:51 | [diff] [blame] | 46 | |
[email protected] | 725f688 | 2012-01-13 18:50:06 | [diff] [blame] | 47 | // Runs the success callback and deletes itself. |
48 | void RunFinishedCallback(); | ||||
49 | |||||
50 | // Runs the error callback and deletes itself. | ||||
51 | void RunErrorCallback(const std::string& error); | ||||
[email protected] | 0dd9e8b | 2012-01-04 13:36:16 | [diff] [blame] | 52 | |
[email protected] | fd3ad51 | 2010-09-25 14:08:51 | [diff] [blame] | 53 | // The download file request initiated by the plugin. |
[email protected] | 7cc6e563 | 2011-10-25 17:56:12 | [diff] [blame] | 54 | scoped_ptr<content::URLFetcher> download_file_fetcher_; |
[email protected] | fd3ad51 | 2010-09-25 14:08:51 | [diff] [blame] | 55 | |
[email protected] | 0dd9e8b | 2012-01-04 13:36:16 | [diff] [blame] | 56 | GURL download_url_; |
57 | FilePath downloaded_file_; | ||||
[email protected] | fd3ad51 | 2010-09-25 14:08:51 | [diff] [blame] | 58 | |
[email protected] | 725f688 | 2012-01-13 18:50:06 | [diff] [blame] | 59 | DownloadFinishedCallback download_finished_callback_; |
60 | ErrorCallback error_callback_; | ||||
[email protected] | fd3ad51 | 2010-09-25 14:08:51 | [diff] [blame] | 61 | |
62 | DISALLOW_COPY_AND_ASSIGN(PluginDownloadUrlHelper); | ||||
63 | }; | ||||
64 | |||||
[email protected] | fd3ad51 | 2010-09-25 14:08:51 | [diff] [blame] | 65 | #endif // CHROME_BROWSER_PLUGIN_DOWNLOAD_HELPER_H_ |