summaryrefslogtreecommitdiffstats
path: root/chrome/browser/plugin_download_helper.h
blob: 834088874f953101dc4c1050b6818e74fda72243 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_PLUGIN_DOWNLOAD_HELPER_H_
#define CHROME_BROWSER_PLUGIN_DOWNLOAD_HELPER_H_
#pragma once

#include "base/callback.h"
#include "base/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "content/public/common/url_fetcher_delegate.h"
#include "googleurl/src/gurl.h"

namespace base {
class MessageLoopProxy;
}

namespace net {
class URLRequestContextGetter;
}

// The PluginDownloadUrlHelper is used to handle one download URL request
// from the plugin. Each download request is handled by a new instance
// of this class.
class PluginDownloadUrlHelper : public content::URLFetcherDelegate {
 public:
  typedef base::Callback<void(const FilePath&)> DownloadFinishedCallback;
  typedef base::Callback<void(const std::string&)> ErrorCallback;

  PluginDownloadUrlHelper();
  virtual ~PluginDownloadUrlHelper();

  void InitiateDownload(const GURL& download_url,
                        net::URLRequestContextGetter* request_context,
                        const DownloadFinishedCallback& callback,
                        const ErrorCallback& error_callback);

  // content::URLFetcherDelegate
  virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE;

 private:
  // Renames the file (which was downloaded to a temporary file) to the filename
  // of the download URL.
  void RenameDownloadedFile();

  // Runs the success callback and deletes itself.
  void RunFinishedCallback();

  // Runs the error callback and deletes itself.
  void RunErrorCallback(const std::string& error);

  // The download file request initiated by the plugin.
  scoped_ptr<content::URLFetcher> download_file_fetcher_;

  GURL download_url_;
  FilePath downloaded_file_;

  DownloadFinishedCallback download_finished_callback_;
  ErrorCallback error_callback_;

  DISALLOW_COPY_AND_ASSIGN(PluginDownloadUrlHelper);
};

#endif  // CHROME_BROWSER_PLUGIN_DOWNLOAD_HELPER_H_