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

Side by Side Diff: chrome/browser/android/download/download_manager_service.cc

Issue 2117343007: Show download error message if sdcard is not available (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: clean up document dir if no sdcard is found Created 4 years, 5 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/android/download/download_manager_service.h" 5 #include "chrome/browser/android/download/download_manager_service.h"
6 6
7 #include "base/android/jni_string.h" 7 #include "base/android/jni_string.h"
8 #include "base/files/file_util.h"
8 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/path_service.h"
9 #include "base/single_thread_task_runner.h" 11 #include "base/single_thread_task_runner.h"
10 #include "base/threading/thread_task_runner_handle.h" 12 #include "base/threading/thread_task_runner_handle.h"
11 #include "base/time/time.h" 13 #include "base/time/time.h"
12 #include "chrome/browser/android/download/download_controller.h" 14 #include "chrome/browser/android/download/download_controller.h"
13 #include "chrome/browser/download/download_service.h" 15 #include "chrome/browser/download/download_service.h"
14 #include "chrome/browser/download/download_service_factory.h" 16 #include "chrome/browser/download/download_service_factory.h"
15 #include "chrome/browser/profiles/profile_manager.h" 17 #include "chrome/browser/profiles/profile_manager.h"
18 #include "chrome/common/chrome_paths.h"
16 #include "chrome/grit/generated_resources.h" 19 #include "chrome/grit/generated_resources.h"
17 #include "content/public/browser/browser_context.h" 20 #include "content/public/browser/browser_context.h"
21 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/download_item.h" 22 #include "content/public/browser/download_item.h"
19 #include "jni/DownloadManagerService_jni.h" 23 #include "jni/DownloadManagerService_jni.h"
20 #include "ui/base/l10n/l10n_util.h" 24 #include "ui/base/l10n/l10n_util.h"
21 25
22 using base::android::JavaParamRef; 26 using base::android::JavaParamRef;
23 using base::android::ConvertJavaStringToUTF8; 27 using base::android::ConvertJavaStringToUTF8;
24 using base::android::ConvertUTF8ToJavaString; 28 using base::android::ConvertUTF8ToJavaString;
25 29
30 namespace {
31 // When sdcard is not available, check whether previously downloaded files under
32 // document dir has been cleaned.
33 bool are_downloads_under_document_dir_cleaned = false;
34 }
35
36 void ClearUnusedDownloads() {
asanka 2016/07/12 17:07:10 This one is a bit tricky. Are there other componen
qinmin 2016/07/12 18:13:04 Seems only download is using Documents dir, I adde
37 base::FilePath document_dir;
38 base::PathService::Get(chrome::DIR_USER_DOCUMENTS, &document_dir);
39 base::DeleteFile(document_dir, true);
40 }
41
26 // static 42 // static
27 bool DownloadManagerService::RegisterDownloadManagerService(JNIEnv* env) { 43 bool DownloadManagerService::RegisterDownloadManagerService(JNIEnv* env) {
28 return RegisterNativesImpl(env); 44 return RegisterNativesImpl(env);
29 } 45 }
30 46
47 // static
48 void DownloadManagerService::OnDownloadCanceled(
49 content::DownloadItem* download,
50 DownloadController::DownloadCancelReason reason) {
51 bool has_no_external_storage =
52 (reason == DownloadController::CANCEL_REASON_NO_EXTERNAL_STORAGE);
53 JNIEnv* env = base::android::AttachCurrentThread();
54 ScopedJavaLocalRef<jstring> jname =
55 ConvertUTF8ToJavaString(env, download->GetURL().ExtractFileName());
56 Java_DownloadManagerService_onDownloadItemCanceled(
57 env, jname.obj(), has_no_external_storage);
58 DownloadController::RecordDownloadCancelReason(reason);
59 if (has_no_external_storage && !are_downloads_under_document_dir_cleaned) {
60 content::BrowserThread::PostTask(
61 content::BrowserThread::FILE, FROM_HERE,
62 base::Bind(&ClearUnusedDownloads));
63 are_downloads_under_document_dir_cleaned = true;
64 }
65 }
66
67
31 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& jobj) { 68 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& jobj) {
32 Profile* profile = ProfileManager::GetActiveUserProfile(); 69 Profile* profile = ProfileManager::GetActiveUserProfile();
33 DownloadManagerService* service = 70 DownloadManagerService* service =
34 new DownloadManagerService(env, jobj); 71 new DownloadManagerService(env, jobj);
35 DownloadService* download_service = 72 DownloadService* download_service =
36 DownloadServiceFactory::GetForBrowserContext(profile); 73 DownloadServiceFactory::GetForBrowserContext(profile);
37 DownloadHistory* history = download_service->GetDownloadHistory(); 74 DownloadHistory* history = download_service->GetDownloadHistory();
38 if (history) 75 if (history)
39 history->AddObserver(service); 76 history->AddObserver(service);
40 return reinterpret_cast<intptr_t>(service); 77 return reinterpret_cast<intptr_t>(service);
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 resume_callback_for_testing_.Run(false); 252 resume_callback_for_testing_.Run(false);
216 } 253 }
217 254
218 content::DownloadManager* DownloadManagerService::GetDownloadManager( 255 content::DownloadManager* DownloadManagerService::GetDownloadManager(
219 bool is_off_the_record) { 256 bool is_off_the_record) {
220 Profile* profile = ProfileManager::GetActiveUserProfile(); 257 Profile* profile = ProfileManager::GetActiveUserProfile();
221 if (is_off_the_record) 258 if (is_off_the_record)
222 profile = profile->GetOffTheRecordProfile(); 259 profile = profile->GetOffTheRecordProfile();
223 return content::BrowserContext::GetDownloadManager(profile); 260 return content::BrowserContext::GetDownloadManager(profile);
224 } 261 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698