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 27 matching lines...) Expand all Loading... | |
79 | 83 |
80 DownloadTargetDeterminerDelegate::~DownloadTargetDeterminerDelegate() { | 84 DownloadTargetDeterminerDelegate::~DownloadTargetDeterminerDelegate() { |
81 } | 85 } |
82 | 86 |
83 DownloadTargetDeterminer::DownloadTargetDeterminer( | 87 DownloadTargetDeterminer::DownloadTargetDeterminer( |
84 DownloadItem* download, | 88 DownloadItem* download, |
85 const base::FilePath& initial_virtual_path, | 89 const base::FilePath& initial_virtual_path, |
86 DownloadPrefs* download_prefs, | 90 DownloadPrefs* download_prefs, |
87 DownloadTargetDeterminerDelegate* delegate, | 91 DownloadTargetDeterminerDelegate* delegate, |
88 const CompletionCallback& callback) | 92 const CompletionCallback& callback) |
89 : next_state_(STATE_GENERATE_TARGET_PATH), | 93 : next_state_(STATE_PROMPT_USER_FOR_PERMISSION), |
90 should_prompt_(false), | 94 should_prompt_(false), |
91 should_notify_extensions_(false), | 95 should_notify_extensions_(false), |
92 create_target_directory_(false), | 96 create_target_directory_(false), |
93 conflict_action_(DownloadPathReservationTracker::OVERWRITE), | 97 conflict_action_(DownloadPathReservationTracker::OVERWRITE), |
94 danger_type_(download->GetDangerType()), | 98 danger_type_(download->GetDangerType()), |
95 is_dangerous_file_(false), | 99 is_dangerous_file_(false), |
96 virtual_path_(initial_virtual_path), | 100 virtual_path_(initial_virtual_path), |
97 is_filetype_handled_safely_(false), | 101 is_filetype_handled_safely_(false), |
98 download_(download), | 102 download_(download), |
99 is_resumption_(download_->GetLastReason() != | 103 is_resumption_(download_->GetLastReason() != |
100 content::DOWNLOAD_INTERRUPT_REASON_NONE && | 104 content::DOWNLOAD_INTERRUPT_REASON_NONE && |
101 !initial_virtual_path.empty()), | 105 !initial_virtual_path.empty()), |
102 download_prefs_(download_prefs), | 106 download_prefs_(download_prefs), |
103 delegate_(delegate), | 107 delegate_(delegate), |
104 completion_callback_(callback), | 108 completion_callback_(callback), |
109 #if defined(OS_ANDROID) | |
110 download_controller_(content::DownloadControllerAndroid::Get()), | |
asanka
2015/07/07 05:03:33
A member variable is unnecessary for this.
qinmin
2015/07/07 06:14:05
Done.
| |
111 #endif | |
105 weak_ptr_factory_(this) { | 112 weak_ptr_factory_(this) { |
106 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 113 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
107 DCHECK(download_); | 114 DCHECK(download_); |
108 DCHECK(delegate); | 115 DCHECK(delegate); |
109 download_->AddObserver(this); | 116 download_->AddObserver(this); |
110 | 117 |
111 DoLoop(); | 118 DoLoop(); |
112 } | 119 } |
113 | 120 |
114 DownloadTargetDeterminer::~DownloadTargetDeterminer() { | 121 DownloadTargetDeterminer::~DownloadTargetDeterminer() { |
115 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 122 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
116 DCHECK(download_); | 123 DCHECK(download_); |
117 DCHECK(completion_callback_.is_null()); | 124 DCHECK(completion_callback_.is_null()); |
118 download_->RemoveObserver(this); | 125 download_->RemoveObserver(this); |
119 } | 126 } |
120 | 127 |
121 void DownloadTargetDeterminer::DoLoop() { | 128 void DownloadTargetDeterminer::DoLoop() { |
122 Result result = CONTINUE; | 129 Result result = CONTINUE; |
123 do { | 130 do { |
124 State current_state = next_state_; | 131 State current_state = next_state_; |
125 next_state_ = STATE_NONE; | 132 next_state_ = STATE_NONE; |
126 | 133 |
127 switch (current_state) { | 134 switch (current_state) { |
135 case STATE_PROMPT_USER_FOR_PERMISSION: | |
136 result = DoPromptUserForPermission(); | |
137 break; | |
128 case STATE_GENERATE_TARGET_PATH: | 138 case STATE_GENERATE_TARGET_PATH: |
129 result = DoGenerateTargetPath(); | 139 result = DoGenerateTargetPath(); |
130 break; | 140 break; |
131 case STATE_NOTIFY_EXTENSIONS: | 141 case STATE_NOTIFY_EXTENSIONS: |
132 result = DoNotifyExtensions(); | 142 result = DoNotifyExtensions(); |
133 break; | 143 break; |
134 case STATE_RESERVE_VIRTUAL_PATH: | 144 case STATE_RESERVE_VIRTUAL_PATH: |
135 result = DoReserveVirtualPath(); | 145 result = DoReserveVirtualPath(); |
136 break; | 146 break; |
137 case STATE_PROMPT_USER_FOR_DOWNLOAD_PATH: | 147 case STATE_PROMPT_USER_FOR_DOWNLOAD_PATH: |
(...skipping 27 matching lines...) Expand all Loading... | |
165 } while (result == CONTINUE); | 175 } while (result == CONTINUE); |
166 // Note that if a callback completes synchronously, the handler will still | 176 // 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 | 177 // return QUIT_DOLOOP. In this case, an inner DoLoop() may complete the target |
168 // determination and delete |this|. | 178 // determination and delete |this|. |
169 | 179 |
170 if (result == COMPLETE) | 180 if (result == COMPLETE) |
171 ScheduleCallbackAndDeleteSelf(); | 181 ScheduleCallbackAndDeleteSelf(); |
172 } | 182 } |
173 | 183 |
174 DownloadTargetDeterminer::Result | 184 DownloadTargetDeterminer::Result |
185 DownloadTargetDeterminer::DoPromptUserForPermission() { | |
186 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
187 next_state_ = STATE_GENERATE_TARGET_PATH; | |
188 #if defined(OS_ANDROID) | |
189 content::WebContents* web_contents = download_->GetWebContents(); | |
190 download_controller_->AcquireFileAccessPermission( | |
191 web_contents, | |
192 base::Bind(&DownloadTargetDeterminer::PromptUserForPermissionDone, | |
193 weak_ptr_factory_.GetWeakPtr())); | |
194 return QUIT_DOLOOP; | |
195 #endif | |
asanka
2015/07/07 05:03:33
#else
return CONTINUE;
#endif
qinmin
2015/07/07 06:14:05
Done.
| |
196 return CONTINUE; | |
197 } | |
198 | |
199 void DownloadTargetDeterminer::PromptUserForPermissionDone(bool granted) { | |
200 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
201 DCHECK_EQ(STATE_GENERATE_TARGET_PATH, next_state_); | |
202 if (!granted) { | |
203 CancelOnFailureAndDeleteSelf(); | |
204 return; | |
205 } | |
206 | |
207 DoLoop(); | |
208 } | |
209 | |
210 DownloadTargetDeterminer::Result | |
175 DownloadTargetDeterminer::DoGenerateTargetPath() { | 211 DownloadTargetDeterminer::DoGenerateTargetPath() { |
176 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 212 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
177 DCHECK(local_path_.empty()); | 213 DCHECK(local_path_.empty()); |
178 DCHECK(!should_prompt_); | 214 DCHECK(!should_prompt_); |
179 DCHECK(!should_notify_extensions_); | 215 DCHECK(!should_notify_extensions_); |
180 DCHECK_EQ(DownloadPathReservationTracker::OVERWRITE, conflict_action_); | 216 DCHECK_EQ(DownloadPathReservationTracker::OVERWRITE, conflict_action_); |
181 bool is_forced_path = !download_->GetForcedFilePath().empty(); | 217 bool is_forced_path = !download_->GetForcedFilePath().empty(); |
182 | 218 |
183 next_state_ = STATE_NOTIFY_EXTENSIONS; | 219 next_state_ = STATE_NOTIFY_EXTENSIONS; |
184 | 220 |
(...skipping 710 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
895 const base::FilePath& suggested_path) { | 931 const base::FilePath& suggested_path) { |
896 return base::FilePath(suggested_path.value() + kCrdownloadSuffix); | 932 return base::FilePath(suggested_path.value() + kCrdownloadSuffix); |
897 } | 933 } |
898 | 934 |
899 #if defined(OS_WIN) | 935 #if defined(OS_WIN) |
900 // static | 936 // static |
901 bool DownloadTargetDeterminer::IsAdobeReaderUpToDate() { | 937 bool DownloadTargetDeterminer::IsAdobeReaderUpToDate() { |
902 return g_is_adobe_reader_up_to_date_; | 938 return g_is_adobe_reader_up_to_date_; |
903 } | 939 } |
904 #endif | 940 #endif |
OLD | NEW |