OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/plugins/plugin_installer.h" | 5 #include "chrome/browser/plugins/plugin_installer.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/process/process.h" | 9 #include "base/process/process.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
11 #include "chrome/browser/download/download_service.h" | |
12 #include "chrome/browser/download/download_service_factory.h" | |
13 #include "chrome/browser/download/download_stats.h" | 11 #include "chrome/browser/download/download_stats.h" |
14 #include "chrome/browser/platform_util.h" | 12 #include "chrome/browser/platform_util.h" |
15 #include "chrome/browser/plugins/plugin_installer_observer.h" | 13 #include "chrome/browser/plugins/plugin_installer_observer.h" |
16 #include "chrome/browser/profiles/profile.h" | |
17 #include "content/public/browser/browser_context.h" | 14 #include "content/public/browser/browser_context.h" |
18 #include "content/public/browser/browser_thread.h" | |
19 #include "content/public/browser/download_item.h" | 15 #include "content/public/browser/download_item.h" |
20 #include "content/public/browser/download_save_info.h" | 16 #include "content/public/browser/download_manager.h" |
21 #include "content/public/browser/download_url_parameters.h" | 17 #include "content/public/browser/download_url_parameters.h" |
22 #include "content/public/browser/render_process_host.h" | |
23 #include "content/public/browser/render_view_host.h" | |
24 #include "content/public/browser/resource_context.h" | |
25 #include "content/public/browser/resource_dispatcher_host.h" | |
26 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
27 #include "content/public/common/referrer.h" | |
28 #include "net/base/request_priority.h" | |
29 #include "net/url_request/url_request.h" | |
30 #include "net/url_request/url_request_context.h" | |
31 | 19 |
32 using content::BrowserContext; | |
33 using content::BrowserThread; | |
34 using content::DownloadItem; | 20 using content::DownloadItem; |
35 using content::ResourceDispatcherHost; | |
36 | |
37 namespace { | |
38 | |
39 void BeginDownload( | |
40 const GURL& url, | |
41 content::ResourceContext* resource_context, | |
42 int render_process_host_id, | |
43 int render_view_host_routing_id, | |
44 const ResourceDispatcherHost::DownloadStartedCallback& callback) { | |
45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
46 | |
47 ResourceDispatcherHost* rdh = ResourceDispatcherHost::Get(); | |
48 scoped_ptr<net::URLRequest> request( | |
49 resource_context->GetRequestContext()->CreateRequest( | |
50 url, net::DEFAULT_PRIORITY, NULL, NULL)); | |
51 content::DownloadInterruptReason error = rdh->BeginDownload( | |
52 request.Pass(), | |
53 content::Referrer(), | |
54 false, // is_content_initiated | |
55 resource_context, | |
56 render_process_host_id, | |
57 render_view_host_routing_id, | |
58 true, // prefer_cache | |
59 scoped_ptr<content::DownloadSaveInfo>(new content::DownloadSaveInfo()), | |
60 content::DownloadItem::kInvalidId, | |
61 callback); | |
62 | |
63 if (error != content::DOWNLOAD_INTERRUPT_REASON_NONE) { | |
64 BrowserThread::PostTask( | |
65 BrowserThread::UI, FROM_HERE, | |
66 base::Bind(callback, static_cast<DownloadItem*>(NULL), error)); | |
67 } | |
68 } | |
69 | |
70 } // namespace | |
71 | 21 |
72 PluginInstaller::PluginInstaller() | 22 PluginInstaller::PluginInstaller() |
73 : state_(INSTALLER_STATE_IDLE), | 23 : state_(INSTALLER_STATE_IDLE), |
74 strong_observer_count_(0) { | 24 strong_observer_count_(0) { |
75 } | 25 } |
76 | 26 |
77 PluginInstaller::~PluginInstaller() { | 27 PluginInstaller::~PluginInstaller() { |
78 } | 28 } |
79 | 29 |
80 void PluginInstaller::OnDownloadUpdated(DownloadItem* download) { | 30 void PluginInstaller::OnDownloadUpdated(DownloadItem* download) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 weak_observers_.AddObserver(observer); | 80 weak_observers_.AddObserver(observer); |
131 } | 81 } |
132 | 82 |
133 void PluginInstaller::RemoveWeakObserver( | 83 void PluginInstaller::RemoveWeakObserver( |
134 WeakPluginInstallerObserver* observer) { | 84 WeakPluginInstallerObserver* observer) { |
135 weak_observers_.RemoveObserver(observer); | 85 weak_observers_.RemoveObserver(observer); |
136 } | 86 } |
137 | 87 |
138 void PluginInstaller::StartInstalling(const GURL& plugin_url, | 88 void PluginInstaller::StartInstalling(const GURL& plugin_url, |
139 content::WebContents* web_contents) { | 89 content::WebContents* web_contents) { |
| 90 content::DownloadManager* download_manager = |
| 91 content::BrowserContext::GetDownloadManager( |
| 92 web_contents->GetBrowserContext()); |
| 93 StartInstallingWithDownloadManager( |
| 94 plugin_url, web_contents, download_manager); |
| 95 } |
| 96 |
| 97 void PluginInstaller::StartInstallingWithDownloadManager( |
| 98 const GURL& plugin_url, |
| 99 content::WebContents* web_contents, |
| 100 content::DownloadManager* download_manager) { |
140 DCHECK_EQ(INSTALLER_STATE_IDLE, state_); | 101 DCHECK_EQ(INSTALLER_STATE_IDLE, state_); |
141 state_ = INSTALLER_STATE_DOWNLOADING; | 102 state_ = INSTALLER_STATE_DOWNLOADING; |
142 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadStarted()); | 103 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadStarted()); |
143 Profile* profile = | 104 scoped_ptr<content::DownloadUrlParameters> download_parameters( |
144 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 105 content::DownloadUrlParameters::FromWebContents(web_contents, |
| 106 plugin_url)); |
| 107 download_parameters->set_callback( |
| 108 base::Bind(&PluginInstaller::DownloadStarted, base::Unretained(this))); |
145 RecordDownloadSource(DOWNLOAD_INITIATED_BY_PLUGIN_INSTALLER); | 109 RecordDownloadSource(DOWNLOAD_INITIATED_BY_PLUGIN_INSTALLER); |
146 BrowserThread::PostTask( | 110 download_manager->DownloadUrl(download_parameters.Pass()); |
147 BrowserThread::IO, FROM_HERE, | |
148 base::Bind(&BeginDownload, | |
149 plugin_url, | |
150 profile->GetResourceContext(), | |
151 web_contents->GetRenderProcessHost()->GetID(), | |
152 web_contents->GetRenderViewHost()->GetRoutingID(), | |
153 base::Bind(&PluginInstaller::DownloadStarted, | |
154 base::Unretained(this)))); | |
155 } | 111 } |
156 | 112 |
157 void PluginInstaller::DownloadStarted( | 113 void PluginInstaller::DownloadStarted( |
158 content::DownloadItem* item, | 114 content::DownloadItem* item, |
159 content::DownloadInterruptReason interrupt_reason) { | 115 content::DownloadInterruptReason interrupt_reason) { |
160 if (!item) { | 116 if (interrupt_reason != content::DOWNLOAD_INTERRUPT_REASON_NONE) { |
161 DCHECK_NE(content::DOWNLOAD_INTERRUPT_REASON_NONE, interrupt_reason); | |
162 std::string msg = base::StringPrintf( | 117 std::string msg = base::StringPrintf( |
163 "Error %d: %s", | 118 "Error %d: %s", |
164 interrupt_reason, | 119 interrupt_reason, |
165 content::DownloadInterruptReasonToString(interrupt_reason).c_str()); | 120 content::DownloadInterruptReasonToString(interrupt_reason).c_str()); |
166 DownloadError(msg); | 121 DownloadError(msg); |
167 return; | 122 return; |
168 } | 123 } |
169 DCHECK_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, interrupt_reason); | |
170 item->SetOpenWhenComplete(true); | 124 item->SetOpenWhenComplete(true); |
171 item->AddObserver(this); | 125 item->AddObserver(this); |
172 } | 126 } |
173 | 127 |
174 void PluginInstaller::OpenDownloadURL(const GURL& plugin_url, | 128 void PluginInstaller::OpenDownloadURL(const GURL& plugin_url, |
175 content::WebContents* web_contents) { | 129 content::WebContents* web_contents) { |
176 DCHECK_EQ(INSTALLER_STATE_IDLE, state_); | 130 DCHECK_EQ(INSTALLER_STATE_IDLE, state_); |
177 web_contents->OpenURL(content::OpenURLParams( | 131 web_contents->OpenURL(content::OpenURLParams( |
178 plugin_url, | 132 plugin_url, |
179 content::Referrer(web_contents->GetURL(), | 133 content::Referrer(web_contents->GetURL(), |
180 blink::WebReferrerPolicyDefault), | 134 blink::WebReferrerPolicyDefault), |
181 NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_TYPED, false)); | 135 NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_TYPED, false)); |
182 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadFinished()); | 136 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadFinished()); |
183 } | 137 } |
184 | 138 |
185 void PluginInstaller::DownloadError(const std::string& msg) { | 139 void PluginInstaller::DownloadError(const std::string& msg) { |
186 DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); | 140 DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); |
187 state_ = INSTALLER_STATE_IDLE; | 141 state_ = INSTALLER_STATE_IDLE; |
188 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadError(msg)); | 142 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadError(msg)); |
189 } | 143 } |
190 | 144 |
191 void PluginInstaller::DownloadCancelled() { | 145 void PluginInstaller::DownloadCancelled() { |
192 DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); | 146 DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); |
193 state_ = INSTALLER_STATE_IDLE; | 147 state_ = INSTALLER_STATE_IDLE; |
194 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadCancelled()); | 148 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadCancelled()); |
195 } | 149 } |
OLD | NEW |