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

Side by Side Diff: chrome/browser/download/chrome_download_manager_delegate.cc

Issue 9651020: Pass content-type resources to web intents. Goes through download, then invokes the p… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add ability to send unserialized intent data from browser process. Created 8 years, 9 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
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/download/chrome_download_manager_delegate.h" 5 #include "chrome/browser/download/chrome_download_manager_delegate.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/callback.h" 9 #include "base/callback.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/path_service.h" 11 #include "base/path_service.h"
12 #include "base/rand_util.h" 12 #include "base/rand_util.h"
13 #include "base/stringprintf.h" 13 #include "base/stringprintf.h"
14 #include "base/time.h" 14 #include "base/time.h"
15 #include "base/utf_string_conversions.h"
15 #include "chrome/browser/browser_process.h" 16 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/download/download_crx_util.h" 17 #include "chrome/browser/download/download_crx_util.h"
17 #include "chrome/browser/download/download_extensions.h" 18 #include "chrome/browser/download/download_extensions.h"
18 #include "chrome/browser/download/download_file_picker.h" 19 #include "chrome/browser/download/download_file_picker.h"
19 #include "chrome/browser/download/download_history.h" 20 #include "chrome/browser/download/download_history.h"
20 #include "chrome/browser/download/download_prefs.h" 21 #include "chrome/browser/download/download_prefs.h"
21 #include "chrome/browser/download/download_status_updater.h" 22 #include "chrome/browser/download/download_status_updater.h"
22 #include "chrome/browser/download/download_util.h" 23 #include "chrome/browser/download/download_util.h"
23 #include "chrome/browser/download/save_package_file_picker.h" 24 #include "chrome/browser/download/save_package_file_picker.h"
24 #include "chrome/browser/extensions/crx_installer.h" 25 #include "chrome/browser/extensions/crx_installer.h"
25 #include "chrome/browser/extensions/extension_service.h" 26 #include "chrome/browser/extensions/extension_service.h"
26 #include "chrome/browser/prefs/pref_member.h" 27 #include "chrome/browser/prefs/pref_member.h"
27 #include "chrome/browser/prefs/pref_service.h" 28 #include "chrome/browser/prefs/pref_service.h"
28 #include "chrome/browser/profiles/profile.h" 29 #include "chrome/browser/profiles/profile.h"
29 #include "chrome/browser/safe_browsing/safe_browsing_service.h" 30 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
30 #include "chrome/browser/ui/browser.h" 31 #include "chrome/browser/ui/browser.h"
31 #include "chrome/browser/ui/browser_list.h" 32 #include "chrome/browser/ui/browser_list.h"
32 #include "chrome/common/chrome_notification_types.h" 33 #include "chrome/common/chrome_notification_types.h"
33 #include "chrome/common/chrome_paths.h" 34 #include "chrome/common/chrome_paths.h"
34 #include "chrome/common/extensions/user_script.h" 35 #include "chrome/common/extensions/user_script.h"
35 #include "chrome/common/pref_names.h" 36 #include "chrome/common/pref_names.h"
37 #include "content/browser/intents/internal_web_intents_dispatcher.h"
36 #include "content/public/browser/download_item.h" 38 #include "content/public/browser/download_item.h"
37 #include "content/public/browser/download_manager.h" 39 #include "content/public/browser/download_manager.h"
38 #include "content/public/browser/notification_source.h" 40 #include "content/public/browser/notification_source.h"
41 #include "content/public/browser/resource_context.h"
39 #include "content/public/browser/web_contents.h" 42 #include "content/public/browser/web_contents.h"
43 #include "content/public/browser/web_intents_dispatcher.h"
40 #include "grit/generated_resources.h" 44 #include "grit/generated_resources.h"
41 #include "ui/base/l10n/l10n_util.h" 45 #include "ui/base/l10n/l10n_util.h"
46 #include "webkit/blob/blob_data.h"
47 #include "webkit/blob/blob_storage_controller.h"
48 #include "webkit/glue/web_intent_data.h"
42 49
43 using content::BrowserThread; 50 using content::BrowserThread;
44 using content::DownloadId; 51 using content::DownloadId;
45 using content::DownloadItem; 52 using content::DownloadItem;
46 using content::DownloadManager; 53 using content::DownloadManager;
47 using content::WebContents; 54 using content::WebContents;
48 using safe_browsing::DownloadProtectionService; 55 using safe_browsing::DownloadProtectionService;
49 56
50 namespace { 57 namespace {
51 58
(...skipping 13 matching lines...) Expand all
65 72
66 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) 73 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile)
67 : profile_(profile), 74 : profile_(profile),
68 next_download_id_(0), 75 next_download_id_(0),
69 download_prefs_(new DownloadPrefs(profile->GetPrefs())) { 76 download_prefs_(new DownloadPrefs(profile->GetPrefs())) {
70 } 77 }
71 78
72 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { 79 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() {
73 } 80 }
74 81
82 // static
75 bool ChromeDownloadManagerDelegate::IsExtensionDownload( 83 bool ChromeDownloadManagerDelegate::IsExtensionDownload(
76 const DownloadItem* item) { 84 const DownloadItem* item) {
77 if (item->PromptUserForSaveLocation()) 85 if (item->PromptUserForSaveLocation())
78 return false; 86 return false;
79 87
80 return (item->GetMimeType() == Extension::kMimeType) || 88 return (item->GetMimeType() == Extension::kMimeType) ||
81 UserScript::IsURLUserScript(item->GetURL(), item->GetMimeType()); 89 UserScript::IsURLUserScript(item->GetURL(), item->GetMimeType());
82 } 90 }
83 91
92 // static
93 bool ChromeDownloadManagerDelegate::ShouldWebIntentsHandle(
jam 2012/03/13 20:05:01 why is this stuff in chrome vs content? it seems g
Greg Billock 2012/03/13 20:53:56 It could be defined more generically in DownloadMa
94 const DownloadItem& item) {
95 std::string mime_type = item.GetMimeType();
96 LOG(INFO) << "Testing " << mime_type << " for web intents";
97
98 // TODO: Check for explicit user-caused download?
michaeln 2012/03/12 21:16:12 Should this also check for mimes for which there a
Greg Billock 2012/03/13 17:56:10 Yes, it needs refinement. Checking the registry fo
99
100 if (mime_type == "application/rss+xml" ||
101 mime_type == "application/atom+xml" ||
102 mime_type == "application/ms-word" ||
103 mime_type == "appliation/pdf") {
104 return true;
105 }
106
107 return false;
108 }
109
84 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) { 110 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) {
85 download_manager_ = dm; 111 download_manager_ = dm;
86 download_history_.reset(new DownloadHistory(profile_)); 112 download_history_.reset(new DownloadHistory(profile_));
87 download_history_->Load( 113 download_history_->Load(
88 base::Bind(&DownloadManager::OnPersistentStoreQueryComplete, 114 base::Bind(&DownloadManager::OnPersistentStoreQueryComplete,
89 base::Unretained(dm))); 115 base::Unretained(dm)));
90 } 116 }
91 117
92 void ChromeDownloadManagerDelegate::Shutdown() { 118 void ChromeDownloadManagerDelegate::Shutdown() {
93 download_history_.reset(); 119 download_history_.reset();
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 base::Bind( 217 base::Bind(
192 &ChromeDownloadManagerDelegate::CheckClientDownloadDone, 218 &ChromeDownloadManagerDelegate::CheckClientDownloadDone,
193 this, 219 this,
194 item->GetId())); 220 item->GetId()));
195 return false; 221 return false;
196 } 222 }
197 #endif 223 #endif
198 return true; 224 return true;
199 } 225 }
200 226
227 // Helper method for ShouldOpenDownload.
228 content::WebIntentsDispatcher* CreateWebIntentsDispatcherFor(
michaeln 2012/03/12 21:16:12 At this point, we've fully downloaded a file to th
Greg Billock 2012/03/13 17:56:10 Yes, that's right.
229 const DownloadItem& item,
230 webkit_blob::BlobStorageController* blob_controller) {
231 // This blob url format is not the same as that prepared internally in webkit.
232 // It shouldn't matter, however.
233 GURL blob_url(StringPrintf("blob:chrome-internal/%llux",
234 static_cast<long long unsigned int>(base::RandUint64())));
235
236 blob_controller->StartBuildingBlob(blob_url);
237 webkit_blob::BlobData::Item blob_item;
238 // Need to get the item file time. item.GetLastModifiedTime() is a string
239 // and unset. :-(
240 blob_item.SetToFile(item.GetFullPath(),
241 0,
242 item.GetReceivedBytes(),
243 base::Time::Now());
michaeln 2012/03/12 21:16:12 just use base::Time() here to indicate "doesn't ma
Greg Billock 2012/03/13 17:56:10 Done.
244 blob_controller->AppendBlobDataItem(blob_url, blob_item);
245 blob_controller->FinishBuildingBlob(blob_url, item.GetMimeType());
246
247 webkit_glue::WebIntentData intent_data(
248 ASCIIToUTF16("http://webintents.org/view"),
249 ASCIIToUTF16(item.GetMimeType()),
250 ASCIIToUTF16(item.GetURL().spec()));
251
252 /*
253 // TODO: need to actually serialize this blob url, but can't really
254 // do that in-browser-process. Need to figure out what to do here.
255 // Hidden params in WebIntentData?
256 intent_data.data = ASCIIToUTF16(blob_url.spec());
257
258 // For now, send the url as extra. Need to re-examine this...
259 intent_data.extra_data.put(ASCIIToUTF16("url"),
260 ASCIIToUTF16(item.GetURL()).spec());
261 */
262
263 return new InternalWebIntentsDispatcher(intent_data);
264 }
265
201 bool ChromeDownloadManagerDelegate::ShouldOpenDownload(DownloadItem* item) { 266 bool ChromeDownloadManagerDelegate::ShouldOpenDownload(DownloadItem* item) {
202 if (!IsExtensionDownload(item)) { 267 if (IsExtensionDownload(item)) {
203 return true; 268 scoped_refptr<CrxInstaller> crx_installer =
269 download_crx_util::OpenChromeExtension(profile_, *item);
270
271 // CRX_INSTALLER_DONE will fire when the install completes. Observe()
272 // will call DelayedDownloadOpened() on this item. If this DownloadItem is
273 // not around when CRX_INSTALLER_DONE fires, Complete() will not be called.
274 registrar_.Add(this,
275 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
276 content::Source<CrxInstaller>(crx_installer.get()));
277
278 crx_installers_[crx_installer.get()] = item->GetId();
279 // The status text and percent complete indicator will change now
280 // that we are installing a CRX. Update observers so that they pick
281 // up the change.
282 item->UpdateObservers();
283 return false;
204 } 284 }
205 285
206 scoped_refptr<CrxInstaller> crx_installer = 286 if (ShouldWebIntentsHandle(*item)) {
207 download_crx_util::OpenChromeExtension(profile_, *item); 287 content::WebContents* web_contents = item->GetWebContents();
208 288
209 // CRX_INSTALLER_DONE will fire when the install completes. Observe() 289 webkit_blob::BlobStorageController* blob_controller =
210 // will call DelayedDownloadOpened() on this item. If this DownloadItem is 290 content::ResourceContext::GetBlobStorageController(
211 // not around when CRX_INSTALLER_DONE fires, Complete() will not be called. 291 profile_->GetResourceContext());
212 registrar_.Add(this, 292 content::WebIntentsDispatcher* dispatcher =
213 chrome::NOTIFICATION_CRX_INSTALLER_DONE, 293 CreateWebIntentsDispatcherFor(*item, blob_controller);
214 content::Source<CrxInstaller>(crx_installer.get())); 294 web_contents->GetDelegate()->WebIntentDispatch(web_contents, dispatcher);
295 // Update text/completion indicator on shelf.
michaeln 2012/03/12 21:16:12 just curious... what will the shelf indicator say
Greg Billock 2012/03/13 17:56:10 Not sure yet...
296 item->UpdateObservers();
297 return false;
298 }
215 299
216 crx_installers_[crx_installer.get()] = item->GetId(); 300 return true;
217 // The status text and percent complete indicator will change now
218 // that we are installing a CRX. Update observers so that they pick
219 // up the change.
220 item->UpdateObservers();
221 return false;
222 } 301 }
223 302
224 bool ChromeDownloadManagerDelegate::GenerateFileHash() { 303 bool ChromeDownloadManagerDelegate::GenerateFileHash() {
225 #if defined(ENABLE_SAFE_BROWSING) 304 #if defined(ENABLE_SAFE_BROWSING)
226 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && 305 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) &&
227 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); 306 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded();
228 #else 307 #else
229 return false; 308 return false;
230 #endif 309 #endif
231 } 310 }
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after
591 int32 download_id, int64 db_handle) { 670 int32 download_id, int64 db_handle) {
592 // It's not immediately obvious, but HistoryBackend::CreateDownload() can 671 // It's not immediately obvious, but HistoryBackend::CreateDownload() can
593 // call this function with an invalid |db_handle|. For instance, this can 672 // call this function with an invalid |db_handle|. For instance, this can
594 // happen when the history database is offline. We cannot have multiple 673 // happen when the history database is offline. We cannot have multiple
595 // DownloadItems with the same invalid db_handle, so we need to assign a 674 // DownloadItems with the same invalid db_handle, so we need to assign a
596 // unique |db_handle| here. 675 // unique |db_handle| here.
597 if (db_handle == DownloadItem::kUninitializedHandle) 676 if (db_handle == DownloadItem::kUninitializedHandle)
598 db_handle = download_history_->GetNextFakeDbHandle(); 677 db_handle = download_history_->GetNextFakeDbHandle();
599 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); 678 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle);
600 } 679 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698