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/download/chrome_download_manager_delegate.h" | 5 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
14 #include "base/rand_util.h" | 14 #include "base/rand_util.h" |
15 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
16 #include "base/time.h" | 16 #include "base/time.h" |
| 17 #include "base/utf_string_conversions.h" |
17 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
18 #include "chrome/browser/download/download_crx_util.h" | 19 #include "chrome/browser/download/download_crx_util.h" |
19 #include "chrome/browser/download/download_extensions.h" | 20 #include "chrome/browser/download/download_extensions.h" |
20 #include "chrome/browser/download/download_file_picker.h" | 21 #include "chrome/browser/download/download_file_picker.h" |
21 #include "chrome/browser/download/download_history.h" | 22 #include "chrome/browser/download/download_history.h" |
22 #include "chrome/browser/download/download_prefs.h" | 23 #include "chrome/browser/download/download_prefs.h" |
23 #include "chrome/browser/download/download_status_updater.h" | 24 #include "chrome/browser/download/download_status_updater.h" |
24 #include "chrome/browser/download/download_util.h" | 25 #include "chrome/browser/download/download_util.h" |
25 #include "chrome/browser/download/save_package_file_picker.h" | 26 #include "chrome/browser/download/save_package_file_picker.h" |
26 #include "chrome/browser/extensions/crx_installer.h" | 27 #include "chrome/browser/extensions/crx_installer.h" |
27 #include "chrome/browser/extensions/extension_service.h" | 28 #include "chrome/browser/extensions/extension_service.h" |
28 #include "chrome/browser/prefs/pref_member.h" | 29 #include "chrome/browser/prefs/pref_member.h" |
29 #include "chrome/browser/prefs/pref_service.h" | 30 #include "chrome/browser/prefs/pref_service.h" |
30 #include "chrome/browser/profiles/profile.h" | 31 #include "chrome/browser/profiles/profile.h" |
31 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 32 #include "chrome/browser/safe_browsing/safe_browsing_service.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" |
36 #include "content/public/browser/download_item.h" | 37 #include "content/public/browser/download_item.h" |
37 #include "content/public/browser/download_manager.h" | 38 #include "content/public/browser/download_manager.h" |
38 #include "content/public/browser/notification_source.h" | 39 #include "content/public/browser/notification_source.h" |
39 #include "content/public/browser/web_contents.h" | 40 #include "content/public/browser/web_contents.h" |
| 41 #include "content/public/browser/web_intents_dispatcher.h" |
40 #include "grit/generated_resources.h" | 42 #include "grit/generated_resources.h" |
41 #include "ui/base/l10n/l10n_util.h" | 43 #include "ui/base/l10n/l10n_util.h" |
| 44 #include "webkit/glue/web_intent_data.h" |
42 | 45 |
43 #if !defined(OS_ANDROID) | 46 #if !defined(OS_ANDROID) |
44 #include "chrome/browser/ui/browser.h" | 47 #include "chrome/browser/ui/browser.h" |
45 #include "chrome/browser/ui/browser_list.h" | 48 #include "chrome/browser/ui/browser_list.h" |
46 #endif | 49 #endif |
47 | 50 |
48 #if defined(OS_CHROMEOS) | 51 #if defined(OS_CHROMEOS) |
49 #include "chrome/browser/chromeos/gdata/gdata_download_observer.h" | 52 #include "chrome/browser/chromeos/gdata/gdata_download_observer.h" |
50 #include "chrome/browser/download/download_file_picker_chromeos.h" | 53 #include "chrome/browser/download/download_file_picker_chromeos.h" |
51 #include "chrome/browser/download/save_package_file_picker_chromeos.h" | 54 #include "chrome/browser/download/save_package_file_picker_chromeos.h" |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 #if defined(OS_CHROMEOS) | 222 #if defined(OS_CHROMEOS) |
220 // If there's a GData upload associated with this download, we wait until that | 223 // If there's a GData upload associated with this download, we wait until that |
221 // is complete before allowing the download item to complete. | 224 // is complete before allowing the download item to complete. |
222 if (!gdata::GDataDownloadObserver::IsReadyToComplete(item)) | 225 if (!gdata::GDataDownloadObserver::IsReadyToComplete(item)) |
223 return false; | 226 return false; |
224 #endif | 227 #endif |
225 return true; | 228 return true; |
226 } | 229 } |
227 | 230 |
228 bool ChromeDownloadManagerDelegate::ShouldOpenDownload(DownloadItem* item) { | 231 bool ChromeDownloadManagerDelegate::ShouldOpenDownload(DownloadItem* item) { |
229 if (!IsExtensionDownload(item)) { | 232 if (IsExtensionDownload(item)) { |
230 return true; | 233 scoped_refptr<CrxInstaller> crx_installer = |
| 234 download_crx_util::OpenChromeExtension(profile_, *item); |
| 235 |
| 236 // CRX_INSTALLER_DONE will fire when the install completes. Observe() |
| 237 // will call DelayedDownloadOpened() on this item. If this DownloadItem is |
| 238 // not around when CRX_INSTALLER_DONE fires, Complete() will not be called. |
| 239 registrar_.Add(this, |
| 240 chrome::NOTIFICATION_CRX_INSTALLER_DONE, |
| 241 content::Source<CrxInstaller>(crx_installer.get())); |
| 242 |
| 243 crx_installers_[crx_installer.get()] = item->GetId(); |
| 244 // The status text and percent complete indicator will change now |
| 245 // that we are installing a CRX. Update observers so that they pick |
| 246 // up the change. |
| 247 item->UpdateObservers(); |
| 248 return false; |
231 } | 249 } |
232 | 250 |
233 scoped_refptr<CrxInstaller> crx_installer = | 251 if (ShouldOpenWithWebIntents(item)) { |
234 download_crx_util::OpenChromeExtension(profile_, *item); | 252 OpenWithWebIntent(item); |
| 253 item->DelayedDownloadOpened(); |
| 254 return false; |
| 255 } |
235 | 256 |
236 // CRX_INSTALLER_DONE will fire when the install completes. Observe() | 257 return true; |
237 // will call DelayedDownloadOpened() on this item. If this DownloadItem is | 258 } |
238 // not around when CRX_INSTALLER_DONE fires, Complete() will not be called. | |
239 registrar_.Add(this, | |
240 chrome::NOTIFICATION_CRX_INSTALLER_DONE, | |
241 content::Source<CrxInstaller>(crx_installer.get())); | |
242 | 259 |
243 crx_installers_[crx_installer.get()] = item->GetId(); | 260 bool ChromeDownloadManagerDelegate::ShouldOpenWithWebIntents( |
244 // The status text and percent complete indicator will change now | 261 const DownloadItem* item) { |
245 // that we are installing a CRX. Update observers so that they pick | |
246 // up the change. | |
247 item->UpdateObservers(); | |
248 return false; | 262 return false; |
249 } | 263 } |
250 | 264 |
| 265 void ChromeDownloadManagerDelegate::OpenWithWebIntent( |
| 266 const DownloadItem* item) { |
| 267 webkit_glue::WebIntentData intent_data( |
| 268 ASCIIToUTF16("http://webintents.org/view"), |
| 269 ASCIIToUTF16(item->GetMimeType()), |
| 270 item->GetFullPath(), |
| 271 item->GetReceivedBytes()); |
| 272 |
| 273 // TODO(gbillock): Should we pass this? RCH specifies that the receiver gets |
| 274 // the url, but with web intents we don't need to pass it. |
| 275 intent_data.extra_data.insert(make_pair( |
| 276 ASCIIToUTF16("url"), ASCIIToUTF16(item->GetURL().spec()))); |
| 277 |
| 278 content::WebIntentsDispatcher* dispatcher = |
| 279 content::WebIntentsDispatcher::Create(intent_data); |
| 280 // TODO(gbillock): try to get this to be able to delegate to the Browser |
| 281 // object directly, passing a NULL WebContents? |
| 282 item->GetWebContents()->GetDelegate()->WebIntentDispatch( |
| 283 item->GetWebContents(), dispatcher); |
| 284 } |
| 285 |
251 bool ChromeDownloadManagerDelegate::GenerateFileHash() { | 286 bool ChromeDownloadManagerDelegate::GenerateFileHash() { |
252 #if defined(ENABLE_SAFE_BROWSING) | 287 #if defined(ENABLE_SAFE_BROWSING) |
253 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && | 288 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && |
254 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); | 289 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); |
255 #else | 290 #else |
256 return false; | 291 return false; |
257 #endif | 292 #endif |
258 } | 293 } |
259 | 294 |
260 void ChromeDownloadManagerDelegate::AddItemToPersistentStore( | 295 void ChromeDownloadManagerDelegate::AddItemToPersistentStore( |
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
641 int32 download_id, int64 db_handle) { | 676 int32 download_id, int64 db_handle) { |
642 // It's not immediately obvious, but HistoryBackend::CreateDownload() can | 677 // It's not immediately obvious, but HistoryBackend::CreateDownload() can |
643 // call this function with an invalid |db_handle|. For instance, this can | 678 // call this function with an invalid |db_handle|. For instance, this can |
644 // happen when the history database is offline. We cannot have multiple | 679 // happen when the history database is offline. We cannot have multiple |
645 // DownloadItems with the same invalid db_handle, so we need to assign a | 680 // DownloadItems with the same invalid db_handle, so we need to assign a |
646 // unique |db_handle| here. | 681 // unique |db_handle| here. |
647 if (db_handle == DownloadItem::kUninitializedHandle) | 682 if (db_handle == DownloadItem::kUninitializedHandle) |
648 db_handle = download_history_->GetNextFakeDbHandle(); | 683 db_handle = download_history_->GetNextFakeDbHandle(); |
649 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); | 684 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); |
650 } | 685 } |
OLD | NEW |