summaryrefslogtreecommitdiffstats
path: root/chrome/browser/plugin_download_helper.cc
blob: 0621a6a6be7a19cadd03a1898b28b2f5219867f5 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) 2011 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.

#include "chrome/browser/plugin_download_helper.h"

#include <windows.h>

#include "base/file_util.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/url_fetcher.h"
#include "net/base/io_buffer.h"

PluginDownloadUrlHelper::PluginDownloadUrlHelper(
    const std::string& download_url,
    gfx::NativeWindow caller_window,
    PluginDownloadUrlHelper::DownloadDelegate* delegate)
    : download_file_fetcher_(NULL),
      download_file_caller_window_(caller_window),
      download_url_(download_url),
      delegate_(delegate) {
}

PluginDownloadUrlHelper::~PluginDownloadUrlHelper() {
}

void PluginDownloadUrlHelper::InitiateDownload(
    net::URLRequestContextGetter* request_context,
    base::MessageLoopProxy* file_thread_proxy) {
  download_file_fetcher_.reset(content::URLFetcher::Create(
      GURL(download_url_), content::URLFetcher::GET, this));
  download_file_fetcher_->SetRequestContext(request_context);
  download_file_fetcher_->SaveResponseToTemporaryFile(file_thread_proxy);
  download_file_fetcher_->Start();
}

void PluginDownloadUrlHelper::OnURLFetchComplete(
    const content::URLFetcher* source) {
  bool success = source->GetStatus().is_success();
  FilePath response_file;

  if (success) {
    if (source->GetResponseAsFilePath(true, &response_file)) {
      FilePath new_download_file_path =
          response_file.DirName().AppendASCII(
              download_file_fetcher_->GetURL().ExtractFileName());

      file_util::Delete(new_download_file_path, false);

      if (!file_util::ReplaceFileW(response_file,
                                   new_download_file_path)) {
        DLOG(ERROR) << "Failed to rename file:"
                    << response_file.value()
                    << " to file:"
                    << new_download_file_path.value();
      } else {
        response_file = new_download_file_path;
      }
    } else {
      NOTREACHED() << "Failed to download the plugin installer.";
      success = false;
    }
  }

  if (delegate_) {
    delegate_->OnDownloadCompleted(response_file, success);
  } else {
    std::wstring path = response_file.value();
    COPYDATASTRUCT download_file_data = {0};
    download_file_data.cbData =
        static_cast<unsigned long>((path.length() + 1) * sizeof(wchar_t));
    download_file_data.lpData = const_cast<wchar_t *>(path.c_str());
    download_file_data.dwData = success;

    if (::IsWindow(download_file_caller_window_)) {
      ::SendMessage(download_file_caller_window_, WM_COPYDATA, NULL,
                    reinterpret_cast<LPARAM>(&download_file_data));
    }
  }
  // Don't access any members after this.
  delete this;
}