Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(105)

Side by Side Diff: chrome/browser/plugins/plugin_installer.cc

Issue 23496076: WIP - Refactor programmatic downloads Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/plugins/plugin_installer.h ('k') | chrome/common/extensions/api/downloads.idl » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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_manager.h"
21 #include "content/public/browser/download_url_parameters.h"
20 #include "content/public/browser/download_save_info.h" 22 #include "content/public/browser/download_save_info.h"
21 #include "content/public/browser/render_process_host.h" 23 #include "content/public/browser/render_process_host.h"
22 #include "content/public/browser/render_view_host.h" 24 #include "content/public/browser/render_view_host.h"
23 #include "content/public/browser/resource_context.h" 25 #include "content/public/browser/resource_context.h"
24 #include "content/public/browser/resource_dispatcher_host.h" 26 #include "content/public/browser/resource_dispatcher_host.h"
25 #include "content/public/browser/web_contents.h" 27 #include "content/public/browser/web_contents.h"
26 #include "content/public/common/referrer.h" 28 #include "content/public/common/referrer.h"
27 #include "net/url_request/url_request.h" 29 #include "net/url_request/url_request.h"
28 #include "net/url_request/url_request_context.h" 30 #include "net/url_request/url_request_context.h"
29 31
30 using content::BrowserContext; 32 using content::BrowserContext;
31 using content::BrowserThread; 33 using content::BrowserThread;
32 using content::DownloadItem; 34 using content::DownloadItem;
33 using content::ResourceDispatcherHost; 35 using content::ResourceDispatcherHost;
34 36
35 namespace {
36
37 void BeginDownload(
38 const GURL& url,
39 content::ResourceContext* resource_context,
40 int render_process_host_id,
41 int render_view_host_routing_id,
42 const ResourceDispatcherHost::DownloadStartedCallback& callback) {
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
44
45 ResourceDispatcherHost* rdh = ResourceDispatcherHost::Get();
46 scoped_ptr<net::URLRequest> request(
47 resource_context->GetRequestContext()->CreateRequest(url, NULL));
48 net::Error error = rdh->BeginDownload(
49 request.Pass(),
50 content::Referrer(),
51 false, // is_content_initiated
52 resource_context,
53 render_process_host_id,
54 render_view_host_routing_id,
55 true, // prefer_cache
56 scoped_ptr<content::DownloadSaveInfo>(new content::DownloadSaveInfo()),
57 content::DownloadItem::kInvalidId,
58 callback);
59
60 if (error != net::OK) {
61 BrowserThread::PostTask(
62 BrowserThread::UI, FROM_HERE,
63 base::Bind(callback, static_cast<DownloadItem*>(NULL), error));
64 }
65 }
66
67 } // namespace
68
69 PluginInstaller::PluginInstaller() 37 PluginInstaller::PluginInstaller()
70 : state_(INSTALLER_STATE_IDLE), 38 : state_(INSTALLER_STATE_IDLE),
71 strong_observer_count_(0) { 39 strong_observer_count_(0) {
72 } 40 }
73 41
74 PluginInstaller::~PluginInstaller() { 42 PluginInstaller::~PluginInstaller() {
75 } 43 }
76 44
77 void PluginInstaller::OnDownloadUpdated(DownloadItem* download) { 45 void PluginInstaller::OnDownloadUpdated(DownloadItem* download) {
78 DownloadItem::DownloadState state = download->GetState(); 46 DownloadItem::DownloadState state = download->GetState();
79 switch (state) { 47 switch (state) {
80 case DownloadItem::IN_PROGRESS: 48 case DownloadItem::IN_PROGRESS:
81 return; 49 return;
82 case DownloadItem::COMPLETE: { 50 case DownloadItem::COMPLETE: {
83 DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); 51 DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_);
84 state_ = INSTALLER_STATE_IDLE; 52 state_ = INSTALLER_STATE_IDLE;
85 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, 53 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_,
86 DownloadFinished()); 54 DownloadFinished());
87 break; 55 break;
88 } 56 }
89 case DownloadItem::CANCELLED: { 57 case DownloadItem::CANCELLED: {
90 DownloadCancelled(); 58 DownloadCancelled();
91 break; 59 break;
92 } 60 }
93 case DownloadItem::INTERRUPTED: { 61 case DownloadItem::INTERRUPTED: {
94 content::DownloadInterruptReason reason = download->GetLastReason(); 62 content::DownloadInterruptReason reason = download->GetLastReason();
95 DownloadError(content::InterruptReasonDebugString(reason)); 63 DownloadError(content::DownloadInterruptReasonToString(reason));
96 break; 64 break;
97 } 65 }
98 case DownloadItem::MAX_DOWNLOAD_STATE: { 66 case DownloadItem::MAX_DOWNLOAD_STATE: {
99 NOTREACHED(); 67 NOTREACHED();
100 return; 68 return;
101 } 69 }
102 } 70 }
103 download->RemoveObserver(this); 71 download->RemoveObserver(this);
104 } 72 }
105 73
(...skipping 24 matching lines...) Expand all
130 void PluginInstaller::RemoveWeakObserver( 98 void PluginInstaller::RemoveWeakObserver(
131 WeakPluginInstallerObserver* observer) { 99 WeakPluginInstallerObserver* observer) {
132 weak_observers_.RemoveObserver(observer); 100 weak_observers_.RemoveObserver(observer);
133 } 101 }
134 102
135 void PluginInstaller::StartInstalling(const GURL& plugin_url, 103 void PluginInstaller::StartInstalling(const GURL& plugin_url,
136 content::WebContents* web_contents) { 104 content::WebContents* web_contents) {
137 DCHECK_EQ(INSTALLER_STATE_IDLE, state_); 105 DCHECK_EQ(INSTALLER_STATE_IDLE, state_);
138 state_ = INSTALLER_STATE_DOWNLOADING; 106 state_ = INSTALLER_STATE_DOWNLOADING;
139 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadStarted()); 107 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadStarted());
140 Profile* profile =
141 Profile::FromBrowserContext(web_contents->GetBrowserContext());
142 RecordDownloadSource(DOWNLOAD_INITIATED_BY_PLUGIN_INSTALLER); 108 RecordDownloadSource(DOWNLOAD_INITIATED_BY_PLUGIN_INSTALLER);
143 BrowserThread::PostTask( 109 scoped_ptr<content::DownloadUrlParameters> download_params(
144 BrowserThread::IO, FROM_HERE, 110 content::DownloadUrlParameters::FromWebContents(web_contents,
145 base::Bind(&BeginDownload, 111 plugin_url));
146 plugin_url, 112 download_params->set_callback(
147 profile->GetResourceContext(), 113 base::Bind(&PluginInstaller::DownloadStarted, base::Unretained(this)));
148 web_contents->GetRenderProcessHost()->GetID(), 114 content::DownloadManager* download_manager =
149 web_contents->GetRenderViewHost()->GetRoutingID(), 115 content::BrowserContext::GetDownloadManager(
150 base::Bind(&PluginInstaller::DownloadStarted, 116 web_contents->GetBrowserContext());
151 base::Unretained(this)))); 117 download_manager->DownloadUrl(download_params.Pass());
152 } 118 }
153 119
154 void PluginInstaller::DownloadStarted(content::DownloadItem* item, 120 void PluginInstaller::DownloadStarted(
155 net::Error error) { 121 content::DownloadItem* item,
122 content::DownloadInterruptReason interrupt_reason) {
156 if (!item) { 123 if (!item) {
157 DCHECK_NE(net::OK, error); 124 DCHECK_NE(content::DOWNLOAD_INTERRUPT_REASON_NONE, interrupt_reason);
158 std::string msg = 125 std::string msg = base::StringPrintf(
159 base::StringPrintf("Error %d: %s", error, net::ErrorToString(error)); 126 "Error %d: %s",
127 interrupt_reason,
128 content::DownloadInterruptReasonToString(interrupt_reason).c_str());
160 DownloadError(msg); 129 DownloadError(msg);
161 return; 130 return;
162 } 131 }
163 DCHECK_EQ(net::OK, error); 132 DCHECK_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, interrupt_reason);
164 item->SetOpenWhenComplete(true); 133 item->SetOpenWhenComplete(true);
165 item->AddObserver(this); 134 item->AddObserver(this);
166 } 135 }
167 136
168 void PluginInstaller::OpenDownloadURL(const GURL& plugin_url, 137 void PluginInstaller::OpenDownloadURL(const GURL& plugin_url,
169 content::WebContents* web_contents) { 138 content::WebContents* web_contents) {
170 DCHECK_EQ(INSTALLER_STATE_IDLE, state_); 139 DCHECK_EQ(INSTALLER_STATE_IDLE, state_);
171 web_contents->OpenURL(content::OpenURLParams( 140 web_contents->OpenURL(content::OpenURLParams(
172 plugin_url, 141 plugin_url,
173 content::Referrer(web_contents->GetURL(), 142 content::Referrer(web_contents->GetURL(),
174 WebKit::WebReferrerPolicyDefault), 143 WebKit::WebReferrerPolicyDefault),
175 NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_TYPED, false)); 144 NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_TYPED, false));
176 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadFinished()); 145 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadFinished());
177 } 146 }
178 147
179 void PluginInstaller::DownloadError(const std::string& msg) { 148 void PluginInstaller::DownloadError(const std::string& msg) {
180 DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); 149 DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_);
181 state_ = INSTALLER_STATE_IDLE; 150 state_ = INSTALLER_STATE_IDLE;
182 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadError(msg)); 151 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadError(msg));
183 } 152 }
184 153
185 void PluginInstaller::DownloadCancelled() { 154 void PluginInstaller::DownloadCancelled() {
186 DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); 155 DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_);
187 state_ = INSTALLER_STATE_IDLE; 156 state_ = INSTALLER_STATE_IDLE;
188 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadCancelled()); 157 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadCancelled());
189 } 158 }
OLDNEW
« no previous file with comments | « chrome/browser/plugins/plugin_installer.h ('k') | chrome/common/extensions/api/downloads.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698