OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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/win/jumplist.h" | 5 #include "chrome/browser/win/jumplist.h" |
6 | 6 |
7 #include <windows.h> | |
8 | |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
9 #include "base/callback_helpers.h" | 11 #include "base/callback_helpers.h" |
10 #include "base/command_line.h" | 12 #include "base/command_line.h" |
11 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
12 #include "base/macros.h" | 14 #include "base/macros.h" |
13 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
14 #include "base/path_service.h" | 16 #include "base/path_service.h" |
15 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
16 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
17 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
20 #include "base/threading/thread_restrictions.h" | |
18 #include "base/trace_event/trace_event.h" | 21 #include "base/trace_event/trace_event.h" |
19 #include "chrome/browser/chrome_notification_types.h" | 22 #include "chrome/browser/chrome_notification_types.h" |
20 #include "chrome/browser/favicon/favicon_service_factory.h" | 23 #include "chrome/browser/favicon/favicon_service_factory.h" |
21 #include "chrome/browser/history/top_sites_factory.h" | 24 #include "chrome/browser/history/top_sites_factory.h" |
22 #include "chrome/browser/metrics/jumplist_metrics_win.h" | 25 #include "chrome/browser/metrics/jumplist_metrics_win.h" |
23 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
24 #include "chrome/browser/sessions/tab_restore_service_factory.h" | 27 #include "chrome/browser/sessions/tab_restore_service_factory.h" |
25 #include "chrome/browser/shell_integration_win.h" | 28 #include "chrome/browser/shell_integration_win.h" |
26 #include "chrome/common/chrome_constants.h" | 29 #include "chrome/common/chrome_constants.h" |
27 #include "chrome/common/chrome_switches.h" | 30 #include "chrome/common/chrome_switches.h" |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
218 if (!UpdateTaskCategory(&jumplist_updater, incognito_availability)) | 221 if (!UpdateTaskCategory(&jumplist_updater, incognito_availability)) |
219 return false; | 222 return false; |
220 | 223 |
221 // Commit this transaction and send the updated JumpList to Windows. | 224 // Commit this transaction and send the updated JumpList to Windows. |
222 if (!jumplist_updater.CommitUpdate()) | 225 if (!jumplist_updater.CommitUpdate()) |
223 return false; | 226 return false; |
224 | 227 |
225 return true; | 228 return true; |
226 } | 229 } |
227 | 230 |
231 // Base::Move() tries to rename a file and if this fails, it tries copy-n-delete | |
grt (UTC plus 2)
2017/03/09 10:11:48
nit: document what this function does before expla
chengx
2017/03/09 23:57:36
Done.
| |
232 // This Rename method has the same functionality of its "rename" part. | |
233 bool Rename(const base::FilePath& from_path, const base::FilePath& to_path) { | |
234 if (from_path.ReferencesParent() || to_path.ReferencesParent()) | |
235 return false; | |
236 base::ThreadRestrictions::AssertIOAllowed(); | |
grt (UTC plus 2)
2017/03/09 10:11:48
nit: move the assert to the top of the function
chengx
2017/03/09 23:57:36
Done.
| |
237 if (from_path.value().length() >= MAX_PATH || | |
238 to_path.value().length() >= MAX_PATH) { | |
239 return false; | |
240 } | |
241 return MoveFileEx(from_path.value().c_str(), to_path.value().c_str(), | |
242 MOVEFILE_REPLACE_EXISTING) != 0; | |
grt (UTC plus 2)
2017/03/09 10:11:48
MOVEFILE_REPLACE_EXISTING -> 0 since you're moving
chengx
2017/03/09 23:57:36
Ah, yes you are right.
| |
243 } | |
244 | |
228 // Updates the jumplist, once all the data has been fetched. | 245 // Updates the jumplist, once all the data has been fetched. |
229 void RunUpdateOnFileThread( | 246 void RunUpdateOnFileThread( |
230 IncognitoModePrefs::Availability incognito_availability, | 247 IncognitoModePrefs::Availability incognito_availability, |
231 const std::wstring& app_id, | 248 const std::wstring& app_id, |
232 const base::FilePath& icon_dir, | 249 const base::FilePath& icon_dir, |
233 base::RefCountedData<JumpListData>* ref_counted_data) { | 250 base::RefCountedData<JumpListData>* ref_counted_data) { |
234 JumpListData* data = &ref_counted_data->data; | 251 JumpListData* data = &ref_counted_data->data; |
235 ShellLinkItemList local_most_visited_pages; | 252 ShellLinkItemList local_most_visited_pages; |
236 ShellLinkItemList local_recently_closed_pages; | 253 ShellLinkItemList local_recently_closed_pages; |
237 | 254 |
238 { | 255 { |
239 base::AutoLock auto_lock(data->list_lock_); | 256 base::AutoLock auto_lock(data->list_lock_); |
240 // Make sure we are not out of date: if icon_urls_ is not empty, then | 257 // Make sure we are not out of date: if icon_urls_ is not empty, then |
241 // another notification has been received since we processed this one | 258 // another notification has been received since we processed this one |
242 if (!data->icon_urls_.empty()) | 259 if (!data->icon_urls_.empty()) |
243 return; | 260 return; |
244 | 261 |
245 // Make local copies of lists so we can release the lock. | 262 // Make local copies of lists so we can release the lock. |
246 local_most_visited_pages = data->most_visited_pages_; | 263 local_most_visited_pages = data->most_visited_pages_; |
247 local_recently_closed_pages = data->recently_closed_pages_; | 264 local_recently_closed_pages = data->recently_closed_pages_; |
248 } | 265 } |
249 | 266 |
250 // Delete the directory which contains old icon files, rename the current | 267 // Delete the directory which contains old icon files, rename the current |
grt (UTC plus 2)
2017/03/09 10:11:48
why jump through these hoops with different direct
chengx
2017/03/09 23:57:36
This is a very good question. I was confused too w
| |
251 // icon directory, and create a new directory which contains new JumpList | 268 // icon directory, and create a new directory which contains new JumpList |
252 // icon files. | 269 // icon files. |
253 base::FilePath icon_dir_old(icon_dir.value() + L"Old"); | 270 base::FilePath icon_dir_old(icon_dir.value() + L"Old"); |
grt (UTC plus 2)
2017/03/09 10:11:48
this should be:
base::FilePath icon_dir_old = ic
chengx
2017/03/09 23:57:36
Done.
| |
254 | 271 |
255 enum FolderOperationResult { | 272 enum FolderOperationResult { |
256 SUCCESS = 0, | 273 SUCCESS = 0, |
257 DELETE_DEST_FAILED = 1 << 0, | 274 DELETE_DEST_FAILED = 1 << 0, |
258 MOVE_FAILED = 1 << 1, | 275 MOVE_FAILED = 1 << 1, |
259 DELETE_SRC_FAILED = 1 << 2, | 276 DELETE_SRC_FAILED = 1 << 2, |
260 CREATE_SRC_FAILED = 1 << 3, | 277 CREATE_SRC_FAILED = 1 << 3, |
261 // This value is beyond the sum of all bit fields above and | 278 // This value is beyond the sum of all bit fields above and |
262 // should remain last (shifted by one more than the last value) | 279 // should remain last (shifted by one more than the last value) |
263 END = 1 << 4 | 280 END = 1 << 4 |
264 }; | 281 }; |
265 | 282 |
266 // This variable records the status of three folder operations. | 283 // This variable records the status of three folder operations. |
267 uint32_t folder_operation_status = FolderOperationResult::SUCCESS; | 284 uint32_t folder_operation_status = FolderOperationResult::SUCCESS; |
268 | 285 |
269 base::ScopedClosureRunner log_operation_status_when_done(base::Bind( | 286 base::ScopedClosureRunner log_operation_status_when_done(base::Bind( |
270 [](uint32_t* folder_operation_status_ptr) { | 287 [](uint32_t* folder_operation_status_ptr) { |
271 UMA_HISTOGRAM_ENUMERATION("WinJumplist.DetailedFolderResults", | 288 UMA_HISTOGRAM_ENUMERATION("WinJumplist.DetailedFolderResults", |
272 *folder_operation_status_ptr, | 289 *folder_operation_status_ptr, |
273 FolderOperationResult::END); | 290 FolderOperationResult::END); |
274 }, | 291 }, |
275 base::Unretained(&folder_operation_status))); | 292 base::Unretained(&folder_operation_status))); |
276 | 293 |
277 // If deletion of |icon_dir_old| fails, do not move |icon_dir| to | 294 // If deletion of |icon_dir_old| fails, do not move |icon_dir| to |
278 // |icon_dir_old|, instead, delete |icon_dir| directly to avoid bloating | 295 // |icon_dir_old|, instead, delete |icon_dir| directly to avoid bloating |
279 // |icon_dir_old| by moving more things to it. | 296 // |icon_dir_old| by moving more things to it. |
280 if (!base::DeleteFile(icon_dir_old, true)) { | 297 if (!base::DeleteFile(icon_dir_old, true)) { |
grt (UTC plus 2)
2017/03/09 10:11:48
if you need to do this directory dance, you can ma
chengx
2017/03/09 23:57:36
We now have two folders, namely JumplistIcon and J
| |
281 folder_operation_status |= FolderOperationResult::DELETE_DEST_FAILED; | 298 folder_operation_status |= FolderOperationResult::DELETE_DEST_FAILED; |
282 // If deletion of |icon_dir| fails, exit early. This skips creating the same | 299 // If deletion of |icon_dir| fails, exit early. This skips creating the same |
283 // directory and updating jumplist icons to avoid bloating the JumplistIcons | 300 // directory and updating jumplist icons to avoid bloating the JumplistIcons |
284 // folder. | 301 // folder. |
285 if (!base::DeleteFile(icon_dir, true)) { | 302 if (!base::DeleteFile(icon_dir, true)) { |
286 folder_operation_status |= FolderOperationResult::DELETE_SRC_FAILED; | 303 folder_operation_status |= FolderOperationResult::DELETE_SRC_FAILED; |
287 return; | 304 return; |
288 } | 305 } |
289 } else if (!base::Move(icon_dir, icon_dir_old)) { | 306 } else if (!Rename(icon_dir, icon_dir_old)) { |
290 folder_operation_status |= FolderOperationResult::MOVE_FAILED; | 307 folder_operation_status |= FolderOperationResult::MOVE_FAILED; |
291 // If Move() fails, delete |icon_dir| to avoid file accumulation in this | 308 // If Move() fails, delete |icon_dir| to avoid file accumulation in this |
grt (UTC plus 2)
2017/03/09 10:11:48
"If Rename() fails..." (or RenameDirectory if you
chengx
2017/03/09 23:57:36
Done.
| |
292 // directory, which can eventually lead the folder to be huge. | 309 // directory, which can eventually lead the folder to be huge. |
293 if (!base::DeleteFile(icon_dir, true)) { | 310 if (!base::DeleteFile(icon_dir, true)) { |
grt (UTC plus 2)
2017/03/09 10:11:48
this could potentially delete the contents of |ico
chengx
2017/03/09 23:57:36
Agreed. I have added a check here.
grt (UTC plus 2)
2017/03/10 09:24:06
What does it mean if some of the icons that are re
chengx
2017/03/10 21:49:40
I thought you were asking "What if deletion of jum
grt (UTC plus 2)
2017/03/13 09:03:20
Yes, that's exactly my question. I was under the i
| |
294 folder_operation_status |= FolderOperationResult::DELETE_SRC_FAILED; | 311 folder_operation_status |= FolderOperationResult::DELETE_SRC_FAILED; |
295 return; | 312 return; |
296 } | 313 } |
297 } | 314 } |
298 | 315 |
299 if (!base::CreateDirectory(icon_dir)) | 316 if (!base::CreateDirectory(icon_dir)) |
300 folder_operation_status |= FolderOperationResult::CREATE_SRC_FAILED; | 317 folder_operation_status |= FolderOperationResult::CREATE_SRC_FAILED; |
301 | 318 |
302 // Create temporary icon files for shortcuts in the "Most Visited" category. | 319 // Create temporary icon files for shortcuts in the "Most Visited" category. |
303 CreateIconFiles(icon_dir, local_most_visited_pages); | 320 CreateIconFiles(icon_dir, local_most_visited_pages); |
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
633 void JumpList::TopSitesLoaded(history::TopSites* top_sites) { | 650 void JumpList::TopSitesLoaded(history::TopSites* top_sites) { |
634 } | 651 } |
635 | 652 |
636 void JumpList::TopSitesChanged(history::TopSites* top_sites, | 653 void JumpList::TopSitesChanged(history::TopSites* top_sites, |
637 ChangeReason change_reason) { | 654 ChangeReason change_reason) { |
638 top_sites->GetMostVisitedURLs( | 655 top_sites->GetMostVisitedURLs( |
639 base::Bind(&JumpList::OnMostVisitedURLsAvailable, | 656 base::Bind(&JumpList::OnMostVisitedURLsAvailable, |
640 weak_ptr_factory_.GetWeakPtr()), | 657 weak_ptr_factory_.GetWeakPtr()), |
641 false); | 658 false); |
642 } | 659 } |
OLD | NEW |