1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/plugin_installer.h"
8 #include "base/bind_helpers.h"
9 #include "base/process.h"
10 #include "chrome/browser/platform_util.h"
11 #include "chrome/browser/plugin_download_helper.h"
12 #include "chrome/browser/plugin_installer_observer.h"
14 PluginInstaller::~PluginInstaller() {
17 PluginInstaller::PluginInstaller(const std::string
& identifier
,
18 const GURL
& plugin_url
,
23 identifier_(identifier
),
24 plugin_url_(plugin_url
),
27 url_for_display_(url_for_display
) {
30 void PluginInstaller::AddObserver(PluginInstallerObserver
* observer
) {
31 observers_
.AddObserver(observer
);
34 void PluginInstaller::RemoveObserver(PluginInstallerObserver
* observer
) {
35 observers_
.RemoveObserver(observer
);
36 if (observers_
.size() == weak_observers_
.size()) {
37 FOR_EACH_OBSERVER(WeakPluginInstallerObserver
, weak_observers_
,
38 OnlyWeakObserversLeft());
42 void PluginInstaller::AddWeakObserver(WeakPluginInstallerObserver
* observer
) {
43 weak_observers_
.AddObserver(observer
);
46 void PluginInstaller::RemoveWeakObserver(
47 WeakPluginInstallerObserver
* observer
) {
48 weak_observers_
.RemoveObserver(observer
);
51 void PluginInstaller::StartInstalling(
52 net::URLRequestContextGetter
* request_context
) {
53 DCHECK(state_
== kStateIdle
);
54 DCHECK(!url_for_display_
);
55 state_
= kStateDownloading
;
56 FOR_EACH_OBSERVER(PluginInstallerObserver
, observers_
, DidStartDownload());
57 // |downloader| will delete itself after running the callback.
58 PluginDownloadUrlHelper
* downloader
= new PluginDownloadUrlHelper();
59 downloader
->InitiateDownload(
62 base::Bind(&PluginInstaller::DidFinishDownload
, base::Unretained(this)),
63 base::Bind(&PluginInstaller::DownloadError
, base::Unretained(this)));
66 void PluginInstaller::DidOpenDownloadURL() {
67 DCHECK(state_
== kStateIdle
);
68 DCHECK(url_for_display_
);
69 FOR_EACH_OBSERVER(PluginInstallerObserver
, observers_
, DidFinishDownload());
72 void PluginInstaller::DidFinishDownload(const FilePath
& downloaded_file
) {
73 DCHECK(state_
== kStateDownloading
);
75 DVLOG(1) << "Plug-in installer is at \"" << downloaded_file
.value() << "\"";
76 FOR_EACH_OBSERVER(PluginInstallerObserver
, observers_
, DidFinishDownload());
77 platform_util::OpenItem(downloaded_file
);
80 void PluginInstaller::DownloadError(const std::string
& msg
) {
81 DCHECK(state_
== kStateDownloading
);
83 FOR_EACH_OBSERVER(PluginInstallerObserver
, observers_
, DownloadError(msg
));