Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/download_target_determiner.h" | 5 #include "chrome/browser/download/download_target_determiner.h" |
| 6 | 6 |
| 7 #include "base/location.h" | 7 #include "base/location.h" |
| 8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
| 9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
| 10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 32 #include "chrome/browser/extensions/webstore_installer.h" | 32 #include "chrome/browser/extensions/webstore_installer.h" |
| 33 #include "extensions/common/feature_switch.h" | 33 #include "extensions/common/feature_switch.h" |
| 34 #endif | 34 #endif |
| 35 | 35 |
| 36 #if defined(ENABLE_PLUGINS) | 36 #if defined(ENABLE_PLUGINS) |
| 37 #include "chrome/browser/plugins/plugin_prefs.h" | 37 #include "chrome/browser/plugins/plugin_prefs.h" |
| 38 #include "content/public/browser/plugin_service.h" | 38 #include "content/public/browser/plugin_service.h" |
| 39 #include "content/public/common/webplugininfo.h" | 39 #include "content/public/common/webplugininfo.h" |
| 40 #endif | 40 #endif |
| 41 | 41 |
| 42 #if defined(OS_ANDROID) | |
| 43 #include "content/public/browser/android/download_controller_android.h" | |
| 44 #endif | |
| 45 | |
| 42 #if defined(OS_WIN) | 46 #if defined(OS_WIN) |
| 43 #include "chrome/browser/ui/pdf/adobe_reader_info_win.h" | 47 #include "chrome/browser/ui/pdf/adobe_reader_info_win.h" |
| 44 #endif | 48 #endif |
| 45 | 49 |
| 46 using content::BrowserThread; | 50 using content::BrowserThread; |
| 47 using content::DownloadItem; | 51 using content::DownloadItem; |
| 48 | 52 |
| 49 namespace { | 53 namespace { |
| 50 | 54 |
| 51 const base::FilePath::CharType kCrdownloadSuffix[] = | 55 const base::FilePath::CharType kCrdownloadSuffix[] = |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 63 callback.Run( | 67 callback.Run( |
| 64 found_visits && count > 0 && | 68 found_visits && count > 0 && |
| 65 (first_visit.LocalMidnight() < base::Time::Now().LocalMidnight())); | 69 (first_visit.LocalMidnight() < base::Time::Now().LocalMidnight())); |
| 66 } | 70 } |
| 67 | 71 |
| 68 #if defined(OS_WIN) | 72 #if defined(OS_WIN) |
| 69 // Keeps track of whether Adobe Reader is up to date. | 73 // Keeps track of whether Adobe Reader is up to date. |
| 70 bool g_is_adobe_reader_up_to_date_ = false; | 74 bool g_is_adobe_reader_up_to_date_ = false; |
| 71 #endif | 75 #endif |
| 72 | 76 |
| 77 #if defined(OS_ANDROID) | |
| 78 // Download controller for file access prompt. | |
|
Ted C
2015/07/06 20:03:39
these two lines shouldn't be indented.
qinmin
2015/07/06 20:36:02
Done.
| |
| 79 content::DownloadControllerAndroid* g_download_controller_; | |
|
asanka
2015/07/06 21:09:41
I know this'll end up being nullptr, but please in
qinmin
2015/07/07 00:54:22
Done. make this a member variable and initialize i
| |
| 80 #endif | |
| 73 } // namespace | 81 } // namespace |
| 74 | 82 |
| 75 DownloadTargetInfo::DownloadTargetInfo() | 83 DownloadTargetInfo::DownloadTargetInfo() |
| 76 : is_filetype_handled_safely(false) {} | 84 : is_filetype_handled_safely(false) {} |
| 77 | 85 |
| 78 DownloadTargetInfo::~DownloadTargetInfo() {} | 86 DownloadTargetInfo::~DownloadTargetInfo() {} |
| 79 | 87 |
| 80 DownloadTargetDeterminerDelegate::~DownloadTargetDeterminerDelegate() { | 88 DownloadTargetDeterminerDelegate::~DownloadTargetDeterminerDelegate() { |
| 81 } | 89 } |
| 82 | 90 |
| 83 DownloadTargetDeterminer::DownloadTargetDeterminer( | 91 DownloadTargetDeterminer::DownloadTargetDeterminer( |
| 84 DownloadItem* download, | 92 DownloadItem* download, |
| 85 const base::FilePath& initial_virtual_path, | 93 const base::FilePath& initial_virtual_path, |
| 86 DownloadPrefs* download_prefs, | 94 DownloadPrefs* download_prefs, |
| 87 DownloadTargetDeterminerDelegate* delegate, | 95 DownloadTargetDeterminerDelegate* delegate, |
| 88 const CompletionCallback& callback) | 96 const CompletionCallback& callback) |
| 89 : next_state_(STATE_GENERATE_TARGET_PATH), | 97 : next_state_(STATE_PROMPT_USER_FOR_PERMISSION), |
| 90 should_prompt_(false), | 98 should_prompt_(false), |
| 91 should_notify_extensions_(false), | 99 should_notify_extensions_(false), |
| 92 create_target_directory_(false), | 100 create_target_directory_(false), |
| 93 conflict_action_(DownloadPathReservationTracker::OVERWRITE), | 101 conflict_action_(DownloadPathReservationTracker::OVERWRITE), |
| 94 danger_type_(download->GetDangerType()), | 102 danger_type_(download->GetDangerType()), |
| 95 is_dangerous_file_(false), | 103 is_dangerous_file_(false), |
| 96 virtual_path_(initial_virtual_path), | 104 virtual_path_(initial_virtual_path), |
| 97 is_filetype_handled_safely_(false), | 105 is_filetype_handled_safely_(false), |
| 98 download_(download), | 106 download_(download), |
| 99 is_resumption_(download_->GetLastReason() != | 107 is_resumption_(download_->GetLastReason() != |
| 100 content::DOWNLOAD_INTERRUPT_REASON_NONE && | 108 content::DOWNLOAD_INTERRUPT_REASON_NONE && |
| 101 !initial_virtual_path.empty()), | 109 !initial_virtual_path.empty()), |
| 102 download_prefs_(download_prefs), | 110 download_prefs_(download_prefs), |
| 103 delegate_(delegate), | 111 delegate_(delegate), |
| 104 completion_callback_(callback), | 112 completion_callback_(callback), |
| 105 weak_ptr_factory_(this) { | 113 weak_ptr_factory_(this) { |
| 106 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 114 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 107 DCHECK(download_); | 115 DCHECK(download_); |
| 108 DCHECK(delegate); | 116 DCHECK(delegate); |
| 109 download_->AddObserver(this); | 117 download_->AddObserver(this); |
| 110 | 118 #if defined(OS_ANDROID) |
| 119 if (!g_download_controller_) | |
| 120 g_download_controller_ = content::DownloadControllerAndroid::Get(); | |
| 121 #endif | |
| 111 DoLoop(); | 122 DoLoop(); |
| 112 } | 123 } |
| 113 | 124 |
| 114 DownloadTargetDeterminer::~DownloadTargetDeterminer() { | 125 DownloadTargetDeterminer::~DownloadTargetDeterminer() { |
| 115 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 126 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 116 DCHECK(download_); | 127 DCHECK(download_); |
| 117 DCHECK(completion_callback_.is_null()); | 128 DCHECK(completion_callback_.is_null()); |
| 118 download_->RemoveObserver(this); | 129 download_->RemoveObserver(this); |
| 119 } | 130 } |
| 120 | 131 |
| 121 void DownloadTargetDeterminer::DoLoop() { | 132 void DownloadTargetDeterminer::DoLoop() { |
| 122 Result result = CONTINUE; | 133 Result result = CONTINUE; |
| 123 do { | 134 do { |
| 124 State current_state = next_state_; | 135 State current_state = next_state_; |
| 125 next_state_ = STATE_NONE; | 136 next_state_ = STATE_NONE; |
| 126 | 137 |
| 127 switch (current_state) { | 138 switch (current_state) { |
| 139 case STATE_PROMPT_USER_FOR_PERMISSION: | |
| 140 result = DoPromptUserForPermission(); | |
| 141 break; | |
| 128 case STATE_GENERATE_TARGET_PATH: | 142 case STATE_GENERATE_TARGET_PATH: |
| 129 result = DoGenerateTargetPath(); | 143 result = DoGenerateTargetPath(); |
| 130 break; | 144 break; |
| 131 case STATE_NOTIFY_EXTENSIONS: | 145 case STATE_NOTIFY_EXTENSIONS: |
| 132 result = DoNotifyExtensions(); | 146 result = DoNotifyExtensions(); |
| 133 break; | 147 break; |
| 134 case STATE_RESERVE_VIRTUAL_PATH: | 148 case STATE_RESERVE_VIRTUAL_PATH: |
| 135 result = DoReserveVirtualPath(); | 149 result = DoReserveVirtualPath(); |
| 136 break; | 150 break; |
| 137 case STATE_PROMPT_USER_FOR_DOWNLOAD_PATH: | 151 case STATE_PROMPT_USER_FOR_DOWNLOAD_PATH: |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 165 } while (result == CONTINUE); | 179 } while (result == CONTINUE); |
| 166 // Note that if a callback completes synchronously, the handler will still | 180 // Note that if a callback completes synchronously, the handler will still |
| 167 // return QUIT_DOLOOP. In this case, an inner DoLoop() may complete the target | 181 // return QUIT_DOLOOP. In this case, an inner DoLoop() may complete the target |
| 168 // determination and delete |this|. | 182 // determination and delete |this|. |
| 169 | 183 |
| 170 if (result == COMPLETE) | 184 if (result == COMPLETE) |
| 171 ScheduleCallbackAndDeleteSelf(); | 185 ScheduleCallbackAndDeleteSelf(); |
| 172 } | 186 } |
| 173 | 187 |
| 174 DownloadTargetDeterminer::Result | 188 DownloadTargetDeterminer::Result |
| 189 DownloadTargetDeterminer::DoPromptUserForPermission() { | |
| 190 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 191 DCHECK(local_path_.empty()); | |
|
asanka
2015/07/06 21:09:41
Why are you checking these (here and below)?
In g
qinmin
2015/07/07 00:54:22
Ok, removed.
On 2015/07/06 21:09:41, asanka wrote
| |
| 192 DCHECK(!should_prompt_); | |
| 193 DCHECK(!should_notify_extensions_); | |
| 194 DCHECK_EQ(DownloadPathReservationTracker::OVERWRITE, conflict_action_); | |
| 195 next_state_ = STATE_GENERATE_TARGET_PATH; | |
| 196 #if defined(OS_ANDROID) | |
| 197 content::WebContents* web_contents = download_->GetWebContents(); | |
| 198 if (g_download_controller_->RequireUserPermissionPrompt(web_contents)) { | |
| 199 g_download_controller_->PromptUserForPermission( | |
| 200 web_contents, | |
| 201 base::Bind(&DownloadTargetDeterminer::PromptUserForPermissionDone, | |
| 202 weak_ptr_factory_.GetWeakPtr())); | |
| 203 return QUIT_DOLOOP; | |
| 204 } | |
| 205 #endif | |
| 206 return CONTINUE; | |
| 207 } | |
| 208 | |
| 209 void DownloadTargetDeterminer::PromptUserForPermissionDone(bool granted) { | |
| 210 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 211 DCHECK_EQ(STATE_GENERATE_TARGET_PATH, next_state_); | |
| 212 if (!granted) { | |
| 213 CancelOnFailureAndDeleteSelf(); | |
| 214 return; | |
| 215 } | |
| 216 | |
| 217 DoLoop(); | |
| 218 } | |
| 219 | |
| 220 DownloadTargetDeterminer::Result | |
| 175 DownloadTargetDeterminer::DoGenerateTargetPath() { | 221 DownloadTargetDeterminer::DoGenerateTargetPath() { |
| 176 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 222 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 177 DCHECK(local_path_.empty()); | 223 DCHECK(local_path_.empty()); |
| 178 DCHECK(!should_prompt_); | 224 DCHECK(!should_prompt_); |
| 179 DCHECK(!should_notify_extensions_); | 225 DCHECK(!should_notify_extensions_); |
| 180 DCHECK_EQ(DownloadPathReservationTracker::OVERWRITE, conflict_action_); | 226 DCHECK_EQ(DownloadPathReservationTracker::OVERWRITE, conflict_action_); |
| 227 | |
| 181 bool is_forced_path = !download_->GetForcedFilePath().empty(); | 228 bool is_forced_path = !download_->GetForcedFilePath().empty(); |
| 182 | 229 |
| 183 next_state_ = STATE_NOTIFY_EXTENSIONS; | 230 next_state_ = STATE_NOTIFY_EXTENSIONS; |
| 184 | 231 |
| 185 if (!virtual_path_.empty() && HasPromptedForPath() && !is_forced_path) { | 232 if (!virtual_path_.empty() && HasPromptedForPath() && !is_forced_path) { |
| 186 // The download is being resumed and the user has already been prompted for | 233 // The download is being resumed and the user has already been prompted for |
| 187 // a path. Assume that it's okay to overwrite the file if there's a conflict | 234 // a path. Assume that it's okay to overwrite the file if there's a conflict |
| 188 // and reuse the selection. | 235 // and reuse the selection. |
| 189 should_prompt_ = ShouldPromptForDownload(virtual_path_); | 236 should_prompt_ = ShouldPromptForDownload(virtual_path_); |
| 190 } else if (!is_forced_path) { | 237 } else if (!is_forced_path) { |
| (...skipping 704 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 895 const base::FilePath& suggested_path) { | 942 const base::FilePath& suggested_path) { |
| 896 return base::FilePath(suggested_path.value() + kCrdownloadSuffix); | 943 return base::FilePath(suggested_path.value() + kCrdownloadSuffix); |
| 897 } | 944 } |
| 898 | 945 |
| 899 #if defined(OS_WIN) | 946 #if defined(OS_WIN) |
| 900 // static | 947 // static |
| 901 bool DownloadTargetDeterminer::IsAdobeReaderUpToDate() { | 948 bool DownloadTargetDeterminer::IsAdobeReaderUpToDate() { |
| 902 return g_is_adobe_reader_up_to_date_; | 949 return g_is_adobe_reader_up_to_date_; |
| 903 } | 950 } |
| 904 #endif | 951 #endif |
| 952 | |
| 953 #if defined(OS_ANDROID) | |
| 954 // static | |
| 955 void DownloadTargetDeterminer::SetDownloadControllerAndroidForTestings( | |
|
asanka
2015/07/06 21:09:41
Rather than setting a global override here just fo
qinmin
2015/07/07 00:54:22
Done. DownloadControllerAndroid::Get() will now re
| |
| 956 content::DownloadControllerAndroid* download_controller) { | |
| 957 g_download_controller_ = download_controller; | |
| 958 } | |
| 959 #endif | |
| OLD | NEW |