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 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 ReadFileAndInitiateStartDecode, | 354 ReadFileAndInitiateStartDecode, |
349 this, wallpaper_path, fallback_path)); | 355 this, wallpaper_path, fallback_path)); |
350 return true; | 356 return true; |
351 } | 357 } |
352 | 358 |
353 void WallpaperPrivateSetWallpaperIfExistsFunction:: | 359 void WallpaperPrivateSetWallpaperIfExistsFunction:: |
354 ReadFileAndInitiateStartDecode(const base::FilePath& file_path, | 360 ReadFileAndInitiateStartDecode(const base::FilePath& file_path, |
355 const base::FilePath& fallback_path) { | 361 const base::FilePath& fallback_path) { |
356 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( | 362 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( |
357 sequence_token_)); | 363 sequence_token_)); |
358 std::string data; | |
359 base::FilePath path = file_path; | 364 base::FilePath path = file_path; |
360 | 365 |
361 if (!base::PathExists(file_path)) | 366 if (!base::PathExists(file_path)) |
362 path = fallback_path; | 367 path = fallback_path; |
363 | 368 |
| 369 std::string data; |
364 if (base::PathExists(path) && | 370 if (base::PathExists(path) && |
365 base::ReadFileToString(path, &data)) { | 371 base::ReadFileToString(path, &data)) { |
366 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 372 BrowserThread::PostTask( |
| 373 BrowserThread::UI, FROM_HERE, |
367 base::Bind(&WallpaperPrivateSetWallpaperIfExistsFunction::StartDecode, | 374 base::Bind(&WallpaperPrivateSetWallpaperIfExistsFunction::StartDecode, |
368 this, data)); | 375 this, std::vector<char>(data.begin(), data.end()))); |
369 return; | 376 return; |
370 } | 377 } |
371 std::string error = base::StringPrintf( | 378 std::string error = base::StringPrintf( |
372 "Failed to set wallpaper %s from file system.", | 379 "Failed to set wallpaper %s from file system.", |
373 path.BaseName().value().c_str()); | 380 path.BaseName().value().c_str()); |
374 BrowserThread::PostTask( | 381 BrowserThread::PostTask( |
375 BrowserThread::UI, FROM_HERE, | 382 BrowserThread::UI, FROM_HERE, |
376 base::Bind(&WallpaperPrivateSetWallpaperIfExistsFunction::OnFileNotExists, | 383 base::Bind(&WallpaperPrivateSetWallpaperIfExistsFunction::OnFileNotExists, |
377 this, error)); | 384 this, error)); |
378 } | 385 } |
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
824 const std::string& file_name) { | 831 const std::string& file_name) { |
825 SetError(base::StringPrintf("Failed to create/write thumbnail of %s.", | 832 SetError(base::StringPrintf("Failed to create/write thumbnail of %s.", |
826 file_name.c_str())); | 833 file_name.c_str())); |
827 SendResponse(false); | 834 SendResponse(false); |
828 } | 835 } |
829 | 836 |
830 void WallpaperPrivateSaveThumbnailFunction::Success() { | 837 void WallpaperPrivateSaveThumbnailFunction::Success() { |
831 SendResponse(true); | 838 SendResponse(true); |
832 } | 839 } |
833 | 840 |
834 void WallpaperPrivateSaveThumbnailFunction::Save(const std::string& data, | 841 void WallpaperPrivateSaveThumbnailFunction::Save(const std::vector<char>& data, |
835 const std::string& file_name) { | 842 const std::string& file_name) { |
836 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( | 843 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( |
837 sequence_token_)); | 844 sequence_token_)); |
838 if (SaveData(chrome::DIR_CHROMEOS_WALLPAPER_THUMBNAILS, file_name, data)) { | 845 if (SaveData(chrome::DIR_CHROMEOS_WALLPAPER_THUMBNAILS, file_name, data)) { |
839 BrowserThread::PostTask( | 846 BrowserThread::PostTask( |
840 BrowserThread::UI, FROM_HERE, | 847 BrowserThread::UI, FROM_HERE, |
841 base::Bind(&WallpaperPrivateSaveThumbnailFunction::Success, this)); | 848 base::Bind(&WallpaperPrivateSaveThumbnailFunction::Success, this)); |
842 } else { | 849 } else { |
843 BrowserThread::PostTask( | 850 BrowserThread::PostTask( |
844 BrowserThread::UI, FROM_HERE, | 851 BrowserThread::UI, FROM_HERE, |
845 base::Bind(&WallpaperPrivateSaveThumbnailFunction::Failure, | 852 base::Bind(&WallpaperPrivateSaveThumbnailFunction::Failure, |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
892 this, file_list)); | 899 this, file_list)); |
893 } | 900 } |
894 | 901 |
895 void WallpaperPrivateGetOfflineWallpaperListFunction::OnComplete( | 902 void WallpaperPrivateGetOfflineWallpaperListFunction::OnComplete( |
896 const std::vector<std::string>& file_list) { | 903 const std::vector<std::string>& file_list) { |
897 base::ListValue* results = new base::ListValue(); | 904 base::ListValue* results = new base::ListValue(); |
898 results->AppendStrings(file_list); | 905 results->AppendStrings(file_list); |
899 SetResult(results); | 906 SetResult(results); |
900 SendResponse(true); | 907 SendResponse(true); |
901 } | 908 } |
OLD | NEW |