OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chromeos/extensions/wallpaper_private_api.h" | |
6 | |
7 #include "ash/desktop_background/desktop_background_controller.h" | |
8 #include "ash/shell.h" | |
9 #include "base/command_line.h" | |
10 #include "base/file_util.h" | |
11 #include "base/json/json_writer.h" | |
12 #include "base/path_service.h" | |
13 #include "base/synchronization/cancellation_flag.h" | |
14 #include "chrome/browser/browser_process.h" | |
15 #include "chrome/browser/chromeos/login/user_manager.h" | |
16 #include "chrome/browser/chromeos/login/wallpaper_manager.h" | |
17 #include "chrome/browser/extensions/extension_event_router.h" | |
18 #include "chrome/browser/extensions/extension_service.h" | |
19 #include "chrome/browser/image_decoder.h" | |
20 #include "chrome/browser/profiles/profile.h" | |
21 #include "chrome/browser/profiles/profile_manager.h" | |
22 #include "chrome/browser/ui/browser.h" | |
23 #include "chrome/browser/ui/browser_finder.h" | |
24 #include "chrome/browser/ui/chrome_pages.h" | |
25 #include "chrome/browser/ui/extensions/application_launch.h" | |
26 #include "chrome/common/chrome_paths.h" | |
27 #include "chrome/common/chrome_switches.h" | |
28 #include "chrome/common/url_constants.h" | |
29 #include "content/public/browser/browser_thread.h" | |
30 #include "net/url_request/url_fetcher.h" | |
31 #include "net/url_request/url_fetcher_delegate.h" | |
32 #include "net/url_request/url_request_status.h" | |
33 #include "grit/generated_resources.h" | |
34 #include "ui/base/l10n/l10n_util.h" | |
35 | |
36 using base::BinaryValue; | |
37 using content::BrowserThread; | |
38 | |
39 const char kWallpaperManagerDomain[] = "obklkkbkpaoaejdabbfldmcfplpdgolj"; | |
Mihai Parparita -not on Chrome
2012/07/20 00:17:05
We generally refer to these as "extension ID", not
bshe
2012/07/22 23:59:42
Done.
| |
40 | |
41 namespace wallpaper_manager_util { | |
42 | |
43 void OpenWallpaperManager() { | |
44 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); | |
45 // Hides the new UI container behind a flag. | |
46 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
47 switches::kExperimentalWallpaperUI)) { | |
48 std::string url = chrome::kChromeUIWallpaperURL; | |
49 ExtensionService* service = profile->GetExtensionService(); | |
50 if (!service) | |
51 return; | |
52 | |
53 const extensions::Extension* extension = | |
54 service->GetExtensionById(kWallpaperManagerDomain, false); | |
55 if (!extension) | |
56 return; | |
57 | |
58 application_launch::LaunchParams params(profile, extension, | |
59 extension_misc::LAUNCH_WINDOW, | |
60 NEW_FOREGROUND_TAB); | |
61 params.override_url = GURL(url); | |
62 application_launch::OpenApplication(params); | |
63 } else { | |
64 Browser* browser = browser::FindOrCreateTabbedBrowser( | |
65 ProfileManager::GetDefaultProfileOrOffTheRecord()); | |
66 chrome::ShowSettingsSubPage(browser, "setWallpaper"); | |
67 } | |
68 } | |
69 | |
70 } //namespace wallpaper_manager_util | |
71 | |
72 bool WallpaperStringsFunction::RunImpl() { | |
73 DictionaryValue* dict = new DictionaryValue(); | |
74 SetResult(dict); | |
75 | |
76 #define SET_STRING(ns, id) \ | |
77 dict->SetString(#id, l10n_util::GetStringUTF16(ns##_##id)) | |
78 SET_STRING(IDS_WALLPAPER_MANAGER, SEARCH_TEXT_LABEL); | |
79 SET_STRING(IDS_WALLPAPER_MANAGER, AUTHOR_LABEL); | |
80 SET_STRING(IDS_WALLPAPER_MANAGER, CUSTOM_CATEGORY_LABEL); | |
81 SET_STRING(IDS_WALLPAPER_MANAGER, SELECT_CUSTOM_LABEL); | |
82 SET_STRING(IDS_WALLPAPER_MANAGER, POSITION_LABEL); | |
83 SET_STRING(IDS_WALLPAPER_MANAGER, COLOR_LABEL); | |
84 SET_STRING(IDS_WALLPAPER_MANAGER, PREVIEW_LABEL); | |
85 SET_STRING(IDS_OPTIONS, SET_WALLPAPER_DAILY); | |
86 #undef SET_STRING | |
87 | |
88 ChromeURLDataManager::DataSource::SetFontAndTextDirection(dict); | |
89 | |
90 return true; | |
91 } | |
92 | |
93 class WallpaperSetWallpaperFunction::WallpaperDecoder | |
94 : public ImageDecoder::Delegate { | |
95 public: | |
96 WallpaperDecoder(scoped_refptr<WallpaperSetWallpaperFunction> function) | |
97 : function_(function) { | |
98 } | |
99 | |
100 void Start(const std::string& image_data) { | |
101 image_decoder_ = new ImageDecoder(this, image_data); | |
102 image_decoder_->Start(); | |
103 } | |
104 | |
105 void Cancel() { | |
106 cancel_flag_.Set(); | |
107 } | |
108 | |
109 virtual void OnImageDecoded(const ImageDecoder* decoder, | |
110 const SkBitmap& decoded_image) OVERRIDE { | |
111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
112 gfx::ImageSkia final_image(decoded_image); | |
113 if (cancel_flag_.IsSet()) | |
114 delete this; | |
Mihai Parparita -not on Chrome
2012/07/20 00:17:05
Should there be a return after the delete?
bshe
2012/07/22 23:59:42
Done.
| |
115 function_->SetWallpaper(final_image); | |
116 delete this; | |
117 } | |
118 | |
119 virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE { | |
120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
121 if (cancel_flag_.IsSet()) | |
122 delete this; | |
Mihai Parparita -not on Chrome
2012/07/20 00:17:05
Ditto here.
bshe
2012/07/22 23:59:42
Done.
| |
123 // TODO(bshe): Dispatches an encoding error event. | |
124 delete this; | |
125 } | |
126 | |
127 private: | |
128 scoped_refptr<WallpaperSetWallpaperFunction> function_; | |
129 scoped_refptr<ImageDecoder> image_decoder_; | |
130 base::CancellationFlag cancel_flag_; | |
131 | |
132 DISALLOW_COPY_AND_ASSIGN(WallpaperDecoder); | |
133 }; | |
134 | |
135 WallpaperSetWallpaperFunction::WallpaperDecoder* | |
136 WallpaperSetWallpaperFunction::wallpaper_decoder_; | |
137 | |
138 bool WallpaperSetWallpaperFunction::RunImpl() { | |
139 BinaryValue* input = NULL; | |
140 if (args_ == NULL || !args_->GetBinary(0, &input)) { | |
141 return false; | |
142 } | |
143 std::string layout_string; | |
144 if (!args_->GetString(1, &layout_string) || layout_string.empty()) { | |
145 return false; | |
146 } | |
147 layout_ = ash::GetLayoutEnum(layout_string.c_str()); | |
148 std::string url; | |
149 if (!args_->GetString(2, &url) || url.empty()) { | |
150 return false; | |
151 } | |
152 file_name_ = GURL(url).ExtractFileName(); | |
153 | |
154 // Gets email address while at UI thread. | |
155 email_ = chromeos::UserManager::Get()->GetLoggedInUser().email(); | |
156 | |
157 image_data_.assign(input->GetBuffer(), input->GetSize()); | |
158 if (wallpaper_decoder_) | |
159 wallpaper_decoder_->Cancel(); | |
160 wallpaper_decoder_ = new WallpaperDecoder(this); | |
161 wallpaper_decoder_->Start(image_data_); | |
162 | |
163 return true; | |
164 } | |
165 | |
166 void WallpaperSetWallpaperFunction::SetWallpaper( | |
167 const gfx::ImageSkia& wallpaper) { | |
168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
169 | |
170 if(SaveToFile()) { | |
171 ash::Shell::GetInstance()->desktop_background_controller()-> | |
172 SetCustomWallpaper(wallpaper, layout_); | |
Mihai Parparita -not on Chrome
2012/07/20 00:17:05
This needs a SendResponse(true) call to indicate t
bshe
2012/07/22 23:59:42
Done.
| |
173 wallpaper_decoder_ = NULL; | |
174 } | |
175 } | |
176 | |
177 bool WallpaperSetWallpaperFunction::SaveToFile() { | |
178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
179 FilePath wallpaper_dir; | |
180 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir)); | |
181 if (!file_util::DirectoryExists(wallpaper_dir) && | |
182 !file_util::CreateDirectory(wallpaper_dir)) { | |
183 return false; | |
184 } | |
185 FilePath file_path = wallpaper_dir.Append(file_name_); | |
186 if (file_util::PathExists(file_path)) | |
187 return true; | |
188 return file_util::WriteFile(file_path, image_data_.c_str(), | |
189 image_data_.size()) != -1; | |
190 } | |
OLD | NEW |