OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/chromeos/extensions/wallpaper_private_api.h" | 5 #include "chrome/browser/chromeos/extensions/wallpaper_private_api.h" |
6 | 6 |
| 7 #include <map> |
| 8 #include <set> |
| 9 #include <string> |
7 #include <vector> | 10 #include <vector> |
8 | 11 |
9 #include "ash/desktop_background/desktop_background_controller.h" | 12 #include "ash/desktop_background/desktop_background_controller.h" |
10 #include "ash/shell.h" | 13 #include "ash/shell.h" |
11 #include "ash/wm/mru_window_tracker.h" | 14 #include "ash/wm/mru_window_tracker.h" |
12 #include "ash/wm/window_state.h" | 15 #include "ash/wm/window_state.h" |
13 #include "ash/wm/window_util.h" | 16 #include "ash/wm/window_util.h" |
14 #include "base/command_line.h" | 17 #include "base/command_line.h" |
15 #include "base/files/file_enumerator.h" | 18 #include "base/files/file_enumerator.h" |
16 #include "base/files/file_util.h" | 19 #include "base/files/file_util.h" |
17 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
18 #include "base/path_service.h" | 21 #include "base/path_service.h" |
19 #include "base/prefs/pref_service.h" | 22 #include "base/prefs/pref_service.h" |
| 23 #include "base/stl_util.h" |
20 #include "base/strings/string_number_conversions.h" | 24 #include "base/strings/string_number_conversions.h" |
21 #include "base/strings/stringprintf.h" | 25 #include "base/strings/stringprintf.h" |
22 #include "base/threading/worker_pool.h" | 26 #include "base/threading/worker_pool.h" |
23 #include "chrome/browser/browser_process.h" | 27 #include "chrome/browser/browser_process.h" |
24 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h" | 28 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h" |
25 #include "chrome/browser/profiles/profile.h" | 29 #include "chrome/browser/profiles/profile.h" |
26 #include "chrome/browser/sync/profile_sync_service.h" | 30 #include "chrome/browser/sync/profile_sync_service.h" |
27 #include "chrome/browser/sync/profile_sync_service_factory.h" | 31 #include "chrome/browser/sync/profile_sync_service_factory.h" |
28 #include "chrome/common/chrome_paths.h" | 32 #include "chrome/common/chrome_paths.h" |
29 #include "chrome/common/pref_names.h" | 33 #include "chrome/common/pref_names.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 "https://storage.googleapis.com/chromeos-wallpaper-public/manifest_"; | 65 "https://storage.googleapis.com/chromeos-wallpaper-public/manifest_"; |
62 #endif | 66 #endif |
63 | 67 |
64 bool IsOEMDefaultWallpaper() { | 68 bool IsOEMDefaultWallpaper() { |
65 return base::CommandLine::ForCurrentProcess()->HasSwitch( | 69 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
66 chromeos::switches::kDefaultWallpaperIsOem); | 70 chromeos::switches::kDefaultWallpaperIsOem); |
67 } | 71 } |
68 | 72 |
69 // Saves |data| as |file_name| to directory with |key|. Return false if the | 73 // Saves |data| as |file_name| to directory with |key|. Return false if the |
70 // directory can not be found/created or failed to write file. | 74 // directory can not be found/created or failed to write file. |
71 bool SaveData(int key, const std::string& file_name, const std::string& data) { | 75 bool SaveData(int key, |
| 76 const std::string& file_name, |
| 77 const std::vector<char>& data) { |
72 base::FilePath data_dir; | 78 base::FilePath data_dir; |
73 CHECK(PathService::Get(key, &data_dir)); | 79 CHECK(PathService::Get(key, &data_dir)); |
74 if (!base::DirectoryExists(data_dir) && | 80 if (!base::DirectoryExists(data_dir) && |
75 !base::CreateDirectory(data_dir)) { | 81 !base::CreateDirectory(data_dir)) { |
76 return false; | 82 return false; |
77 } | 83 } |
78 base::FilePath file_path = data_dir.Append(file_name); | 84 base::FilePath file_path = data_dir.Append(file_name); |
79 | 85 |
80 return base::PathExists(file_path) || | 86 return base::PathExists(file_path) || |
81 (base::WriteFile(file_path, data.c_str(), data.size()) != -1); | 87 base::WriteFile(file_path, vector_as_array(&data), data.size()) != -1; |
82 } | 88 } |
83 | 89 |
84 // Gets |file_name| from directory with |key|. Return false if the directory can | 90 // Gets |file_name| from directory with |key|. Return false if the directory can |
85 // not be found or failed to read file to string |data|. Note if the |file_name| | 91 // not be found or failed to read file to string |data|. Note if the |file_name| |
86 // can not be found in the directory, return true with empty |data|. It is | 92 // can not be found in the directory, return true with empty |data|. It is |
87 // expected that we may try to access file which did not saved yet. | 93 // expected that we may try to access file which did not saved yet. |
88 bool GetData(const base::FilePath& path, std::string* data) { | 94 bool GetData(const base::FilePath& path, std::string* data) { |
89 base::FilePath data_dir = path.DirName(); | 95 base::FilePath data_dir = path.DirName(); |
90 if (!base::DirectoryExists(data_dir) && | 96 if (!base::DirectoryExists(data_dir) && |
91 !base::CreateDirectory(data_dir)) | 97 !base::CreateDirectory(data_dir)) |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 this, wallpaper_path, fallback_path)); | 357 this, wallpaper_path, fallback_path)); |
352 #endif | 358 #endif |
353 return true; | 359 return true; |
354 } | 360 } |
355 | 361 |
356 void WallpaperPrivateSetWallpaperIfExistsFunction:: | 362 void WallpaperPrivateSetWallpaperIfExistsFunction:: |
357 ReadFileAndInitiateStartDecode(const base::FilePath& file_path, | 363 ReadFileAndInitiateStartDecode(const base::FilePath& file_path, |
358 const base::FilePath& fallback_path) { | 364 const base::FilePath& fallback_path) { |
359 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( | 365 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( |
360 sequence_token_)); | 366 sequence_token_)); |
361 std::string data; | |
362 base::FilePath path = file_path; | 367 base::FilePath path = file_path; |
363 | 368 |
364 if (!base::PathExists(file_path)) | 369 if (!base::PathExists(file_path)) |
365 path = fallback_path; | 370 path = fallback_path; |
366 | 371 |
| 372 std::string data; |
367 if (base::PathExists(path) && | 373 if (base::PathExists(path) && |
368 base::ReadFileToString(path, &data)) { | 374 base::ReadFileToString(path, &data)) { |
369 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 375 BrowserThread::PostTask( |
| 376 BrowserThread::UI, FROM_HERE, |
370 base::Bind(&WallpaperPrivateSetWallpaperIfExistsFunction::StartDecode, | 377 base::Bind(&WallpaperPrivateSetWallpaperIfExistsFunction::StartDecode, |
371 this, data)); | 378 this, std::vector<char>(data.begin(), data.end()))); |
372 return; | 379 return; |
373 } | 380 } |
374 std::string error = base::StringPrintf( | 381 std::string error = base::StringPrintf( |
375 "Failed to set wallpaper %s from file system.", | 382 "Failed to set wallpaper %s from file system.", |
376 path.BaseName().value().c_str()); | 383 path.BaseName().value().c_str()); |
377 BrowserThread::PostTask( | 384 BrowserThread::PostTask( |
378 BrowserThread::UI, FROM_HERE, | 385 BrowserThread::UI, FROM_HERE, |
379 base::Bind(&WallpaperPrivateSetWallpaperIfExistsFunction::OnFileNotExists, | 386 base::Bind(&WallpaperPrivateSetWallpaperIfExistsFunction::OnFileNotExists, |
380 this, error)); | 387 this, error)); |
381 } | 388 } |
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
827 const std::string& file_name) { | 834 const std::string& file_name) { |
828 SetError(base::StringPrintf("Failed to create/write thumbnail of %s.", | 835 SetError(base::StringPrintf("Failed to create/write thumbnail of %s.", |
829 file_name.c_str())); | 836 file_name.c_str())); |
830 SendResponse(false); | 837 SendResponse(false); |
831 } | 838 } |
832 | 839 |
833 void WallpaperPrivateSaveThumbnailFunction::Success() { | 840 void WallpaperPrivateSaveThumbnailFunction::Success() { |
834 SendResponse(true); | 841 SendResponse(true); |
835 } | 842 } |
836 | 843 |
837 void WallpaperPrivateSaveThumbnailFunction::Save(const std::string& data, | 844 void WallpaperPrivateSaveThumbnailFunction::Save(const std::vector<char>& data, |
838 const std::string& file_name) { | 845 const std::string& file_name) { |
839 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( | 846 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( |
840 sequence_token_)); | 847 sequence_token_)); |
841 if (SaveData(chrome::DIR_CHROMEOS_WALLPAPER_THUMBNAILS, file_name, data)) { | 848 if (SaveData(chrome::DIR_CHROMEOS_WALLPAPER_THUMBNAILS, file_name, data)) { |
842 BrowserThread::PostTask( | 849 BrowserThread::PostTask( |
843 BrowserThread::UI, FROM_HERE, | 850 BrowserThread::UI, FROM_HERE, |
844 base::Bind(&WallpaperPrivateSaveThumbnailFunction::Success, this)); | 851 base::Bind(&WallpaperPrivateSaveThumbnailFunction::Success, this)); |
845 } else { | 852 } else { |
846 BrowserThread::PostTask( | 853 BrowserThread::PostTask( |
847 BrowserThread::UI, FROM_HERE, | 854 BrowserThread::UI, FROM_HERE, |
848 base::Bind(&WallpaperPrivateSaveThumbnailFunction::Failure, | 855 base::Bind(&WallpaperPrivateSaveThumbnailFunction::Failure, |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
895 this, file_list)); | 902 this, file_list)); |
896 } | 903 } |
897 | 904 |
898 void WallpaperPrivateGetOfflineWallpaperListFunction::OnComplete( | 905 void WallpaperPrivateGetOfflineWallpaperListFunction::OnComplete( |
899 const std::vector<std::string>& file_list) { | 906 const std::vector<std::string>& file_list) { |
900 base::ListValue* results = new base::ListValue(); | 907 base::ListValue* results = new base::ListValue(); |
901 results->AppendStrings(file_list); | 908 results->AppendStrings(file_list); |
902 SetResult(results); | 909 SetResult(results); |
903 SendResponse(true); | 910 SendResponse(true); |
904 } | 911 } |
OLD | NEW |