| 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" | 11 #include "chrome/browser/download/download_service.h" |
| 12 #include "chrome/browser/download/download_service_factory.h" | 12 #include "chrome/browser/download/download_service_factory.h" |
| 13 #include "chrome/browser/download/download_stats.h" | 13 #include "chrome/browser/download/download_stats.h" |
| 14 #include "chrome/browser/platform_util.h" | 14 #include "chrome/browser/platform_util.h" |
| 15 #include "chrome/browser/plugins/plugin_installer_observer.h" | 15 #include "chrome/browser/plugins/plugin_installer_observer.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "content/public/browser/browser_context.h" | 17 #include "content/public/browser/browser_context.h" |
| 18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/download_item.h" | 19 #include "content/public/browser/download_item.h" |
| 20 #include "content/public/browser/download_save_info.h" | 20 #include "content/public/browser/download_save_info.h" |
| 21 #include "content/public/browser/download_url_parameters.h" |
| 21 #include "content/public/browser/render_process_host.h" | 22 #include "content/public/browser/render_process_host.h" |
| 22 #include "content/public/browser/render_view_host.h" | 23 #include "content/public/browser/render_view_host.h" |
| 23 #include "content/public/browser/resource_context.h" | 24 #include "content/public/browser/resource_context.h" |
| 24 #include "content/public/browser/resource_dispatcher_host.h" | 25 #include "content/public/browser/resource_dispatcher_host.h" |
| 25 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| 26 #include "content/public/common/referrer.h" | 27 #include "content/public/common/referrer.h" |
| 27 #include "net/base/request_priority.h" | 28 #include "net/base/request_priority.h" |
| 28 #include "net/url_request/url_request.h" | 29 #include "net/url_request/url_request.h" |
| 29 #include "net/url_request/url_request_context.h" | 30 #include "net/url_request/url_request_context.h" |
| 30 | 31 |
| 31 using content::BrowserContext; | 32 using content::BrowserContext; |
| 32 using content::BrowserThread; | 33 using content::BrowserThread; |
| 33 using content::DownloadItem; | 34 using content::DownloadItem; |
| 34 using content::ResourceDispatcherHost; | 35 using content::ResourceDispatcherHost; |
| 35 | 36 |
| 36 namespace { | 37 namespace { |
| 37 | 38 |
| 38 void BeginDownload( | 39 void BeginDownload( |
| 39 const GURL& url, | 40 const GURL& url, |
| 40 content::ResourceContext* resource_context, | 41 content::ResourceContext* resource_context, |
| 41 int render_process_host_id, | 42 int render_process_host_id, |
| 42 int render_view_host_routing_id, | 43 int render_view_host_routing_id, |
| 43 const ResourceDispatcherHost::DownloadStartedCallback& callback) { | 44 const ResourceDispatcherHost::DownloadStartedCallback& callback) { |
| 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 45 | 46 |
| 46 ResourceDispatcherHost* rdh = ResourceDispatcherHost::Get(); | 47 ResourceDispatcherHost* rdh = ResourceDispatcherHost::Get(); |
| 47 scoped_ptr<net::URLRequest> request( | 48 scoped_ptr<net::URLRequest> request( |
| 48 resource_context->GetRequestContext()->CreateRequest( | 49 resource_context->GetRequestContext()->CreateRequest( |
| 49 url, net::DEFAULT_PRIORITY, NULL)); | 50 url, net::DEFAULT_PRIORITY, NULL)); |
| 50 net::Error error = rdh->BeginDownload( | 51 content::DownloadInterruptReason error = rdh->BeginDownload( |
| 51 request.Pass(), | 52 request.Pass(), |
| 52 content::Referrer(), | 53 content::Referrer(), |
| 53 false, // is_content_initiated | 54 false, // is_content_initiated |
| 54 resource_context, | 55 resource_context, |
| 55 render_process_host_id, | 56 render_process_host_id, |
| 56 render_view_host_routing_id, | 57 render_view_host_routing_id, |
| 57 true, // prefer_cache | 58 true, // prefer_cache |
| 58 scoped_ptr<content::DownloadSaveInfo>(new content::DownloadSaveInfo()), | 59 scoped_ptr<content::DownloadSaveInfo>(new content::DownloadSaveInfo()), |
| 59 content::DownloadItem::kInvalidId, | 60 content::DownloadItem::kInvalidId, |
| 60 callback); | 61 callback); |
| 61 | 62 |
| 62 if (error != net::OK) { | 63 if (error != content::DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 63 BrowserThread::PostTask( | 64 BrowserThread::PostTask( |
| 64 BrowserThread::UI, FROM_HERE, | 65 BrowserThread::UI, FROM_HERE, |
| 65 base::Bind(callback, static_cast<DownloadItem*>(NULL), error)); | 66 base::Bind(callback, static_cast<DownloadItem*>(NULL), error)); |
| 66 } | 67 } |
| 67 } | 68 } |
| 68 | 69 |
| 69 } // namespace | 70 } // namespace |
| 70 | 71 |
| 71 PluginInstaller::PluginInstaller() | 72 PluginInstaller::PluginInstaller() |
| 72 : state_(INSTALLER_STATE_IDLE), | 73 : state_(INSTALLER_STATE_IDLE), |
| (...skipping 14 matching lines...) Expand all Loading... |
| 87 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, | 88 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, |
| 88 DownloadFinished()); | 89 DownloadFinished()); |
| 89 break; | 90 break; |
| 90 } | 91 } |
| 91 case DownloadItem::CANCELLED: { | 92 case DownloadItem::CANCELLED: { |
| 92 DownloadCancelled(); | 93 DownloadCancelled(); |
| 93 break; | 94 break; |
| 94 } | 95 } |
| 95 case DownloadItem::INTERRUPTED: { | 96 case DownloadItem::INTERRUPTED: { |
| 96 content::DownloadInterruptReason reason = download->GetLastReason(); | 97 content::DownloadInterruptReason reason = download->GetLastReason(); |
| 97 DownloadError(content::InterruptReasonDebugString(reason)); | 98 DownloadError(content::DownloadInterruptReasonToString(reason)); |
| 98 break; | 99 break; |
| 99 } | 100 } |
| 100 case DownloadItem::MAX_DOWNLOAD_STATE: { | 101 case DownloadItem::MAX_DOWNLOAD_STATE: { |
| 101 NOTREACHED(); | 102 NOTREACHED(); |
| 102 return; | 103 return; |
| 103 } | 104 } |
| 104 } | 105 } |
| 105 download->RemoveObserver(this); | 106 download->RemoveObserver(this); |
| 106 } | 107 } |
| 107 | 108 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 BrowserThread::IO, FROM_HERE, | 147 BrowserThread::IO, FROM_HERE, |
| 147 base::Bind(&BeginDownload, | 148 base::Bind(&BeginDownload, |
| 148 plugin_url, | 149 plugin_url, |
| 149 profile->GetResourceContext(), | 150 profile->GetResourceContext(), |
| 150 web_contents->GetRenderProcessHost()->GetID(), | 151 web_contents->GetRenderProcessHost()->GetID(), |
| 151 web_contents->GetRenderViewHost()->GetRoutingID(), | 152 web_contents->GetRenderViewHost()->GetRoutingID(), |
| 152 base::Bind(&PluginInstaller::DownloadStarted, | 153 base::Bind(&PluginInstaller::DownloadStarted, |
| 153 base::Unretained(this)))); | 154 base::Unretained(this)))); |
| 154 } | 155 } |
| 155 | 156 |
| 156 void PluginInstaller::DownloadStarted(content::DownloadItem* item, | 157 void PluginInstaller::DownloadStarted( |
| 157 net::Error error) { | 158 content::DownloadItem* item, |
| 159 content::DownloadInterruptReason interrupt_reason) { |
| 158 if (!item) { | 160 if (!item) { |
| 159 DCHECK_NE(net::OK, error); | 161 DCHECK_NE(content::DOWNLOAD_INTERRUPT_REASON_NONE, interrupt_reason); |
| 160 std::string msg = | 162 std::string msg = base::StringPrintf( |
| 161 base::StringPrintf("Error %d: %s", error, net::ErrorToString(error)); | 163 "Error %d: %s", |
| 164 interrupt_reason, |
| 165 content::DownloadInterruptReasonToString(interrupt_reason).c_str()); |
| 162 DownloadError(msg); | 166 DownloadError(msg); |
| 163 return; | 167 return; |
| 164 } | 168 } |
| 165 DCHECK_EQ(net::OK, error); | 169 DCHECK_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, interrupt_reason); |
| 166 item->SetOpenWhenComplete(true); | 170 item->SetOpenWhenComplete(true); |
| 167 item->AddObserver(this); | 171 item->AddObserver(this); |
| 168 } | 172 } |
| 169 | 173 |
| 170 void PluginInstaller::OpenDownloadURL(const GURL& plugin_url, | 174 void PluginInstaller::OpenDownloadURL(const GURL& plugin_url, |
| 171 content::WebContents* web_contents) { | 175 content::WebContents* web_contents) { |
| 172 DCHECK_EQ(INSTALLER_STATE_IDLE, state_); | 176 DCHECK_EQ(INSTALLER_STATE_IDLE, state_); |
| 173 web_contents->OpenURL(content::OpenURLParams( | 177 web_contents->OpenURL(content::OpenURLParams( |
| 174 plugin_url, | 178 plugin_url, |
| 175 content::Referrer(web_contents->GetURL(), | 179 content::Referrer(web_contents->GetURL(), |
| 176 blink::WebReferrerPolicyDefault), | 180 blink::WebReferrerPolicyDefault), |
| 177 NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_TYPED, false)); | 181 NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_TYPED, false)); |
| 178 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadFinished()); | 182 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadFinished()); |
| 179 } | 183 } |
| 180 | 184 |
| 181 void PluginInstaller::DownloadError(const std::string& msg) { | 185 void PluginInstaller::DownloadError(const std::string& msg) { |
| 182 DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); | 186 DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); |
| 183 state_ = INSTALLER_STATE_IDLE; | 187 state_ = INSTALLER_STATE_IDLE; |
| 184 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadError(msg)); | 188 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadError(msg)); |
| 185 } | 189 } |
| 186 | 190 |
| 187 void PluginInstaller::DownloadCancelled() { | 191 void PluginInstaller::DownloadCancelled() { |
| 188 DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); | 192 DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); |
| 189 state_ = INSTALLER_STATE_IDLE; | 193 state_ = INSTALLER_STATE_IDLE; |
| 190 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadCancelled()); | 194 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadCancelled()); |
| 191 } | 195 } |
| OLD | NEW |