Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Side by Side Diff: chrome/browser/chromeos/extensions/wallpaper_private_api.cc

Issue 11348215: Make wallpaper picker manifest and thumbnails available when offline. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove success paramter and add a todo Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 <vector> 7 #include <vector>
8 8
9 #include "ash/shell.h" 9 #include "ash/shell.h"
10 #include "ash/wm/window_cycle_controller.h" 10 #include "ash/wm/window_cycle_controller.h"
11 #include "ash/wm/window_util.h" 11 #include "ash/wm/window_util.h"
12 #include "base/file_util.h" 12 #include "base/file_util.h"
13 #include "base/json/json_writer.h" 13 #include "base/json/json_writer.h"
14 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/path_service.h" 15 #include "base/path_service.h"
16 #include "base/stringprintf.h"
16 #include "base/synchronization/cancellation_flag.h" 17 #include "base/synchronization/cancellation_flag.h"
17 #include "base/threading/sequenced_worker_pool.h" 18 #include "base/threading/sequenced_worker_pool.h"
18 #include "base/threading/worker_pool.h" 19 #include "base/threading/worker_pool.h"
19 #include "chrome/browser/browser_process.h" 20 #include "chrome/browser/browser_process.h"
20 #include "chrome/browser/chromeos/login/user.h" 21 #include "chrome/browser/chromeos/login/user.h"
21 #include "chrome/browser/chromeos/login/user_image.h" 22 #include "chrome/browser/chromeos/login/user_image.h"
22 #include "chrome/browser/chromeos/login/user_manager.h" 23 #include "chrome/browser/chromeos/login/user_manager.h"
23 #include "chrome/browser/chromeos/login/wallpaper_manager.h" 24 #include "chrome/browser/chromeos/login/wallpaper_manager.h"
24 #include "chrome/browser/extensions/event_router.h" 25 #include "chrome/browser/extensions/event_router.h"
25 #include "chrome/browser/image_decoder.h" 26 #include "chrome/browser/image_decoder.h"
(...skipping 26 matching lines...) Expand all
52 53
53 ash::WallpaperLayout GetLayoutEnum(const std::string& layout) { 54 ash::WallpaperLayout GetLayoutEnum(const std::string& layout) {
54 for (int i = 0; i < kWallpaperLayoutCount; i++) { 55 for (int i = 0; i < kWallpaperLayoutCount; i++) {
55 if (layout.compare(kWallpaperLayoutArrays[i]) == 0) 56 if (layout.compare(kWallpaperLayoutArrays[i]) == 0)
56 return static_cast<ash::WallpaperLayout>(i); 57 return static_cast<ash::WallpaperLayout>(i);
57 } 58 }
58 // Default to use CENTER layout. 59 // Default to use CENTER layout.
59 return ash::WALLPAPER_LAYOUT_CENTER; 60 return ash::WALLPAPER_LAYOUT_CENTER;
60 } 61 }
61 62
63 // Saves |data| as |file_name| to directory with |key|. Return false if the
64 // directory can not be found/created or failed to write file.
65 bool SaveData(int key, const std::string& file_name, const std::string& data) {
66 FilePath data_dir;
67 CHECK(PathService::Get(key, &data_dir));
68 if (!file_util::DirectoryExists(data_dir) &&
69 !file_util::CreateDirectory(data_dir)) {
70 return false;
71 }
72 FilePath file_path = data_dir.Append(file_name);
73
74 return file_util::PathExists(file_path) ||
75 (file_util::WriteFile(file_path, data.c_str(),
76 data.size()) != -1);
77 }
78
79 // Gets |file_name| from directory with |key|. Return false if the directory can
80 // not be found or failed to read file to |data|. If the |file_name| can not be
81 // found in the directory, return true with empty |data|. It is expected that we
82 // may try to access file which did not saved yet.
83 bool GetData(int key, const std::string& file_name, std::string* data) {
84 FilePath data_dir;
85 CHECK(PathService::Get(key, &data_dir));
86 if (!file_util::DirectoryExists(data_dir) &&
87 !file_util::CreateDirectory(data_dir))
88 return false;
89
90 FilePath file_path = data_dir.Append(file_name);
91
92 return !file_util::PathExists(file_path) ||
93 (file_util::ReadFileToString(file_path, data) != -1);
Nico 2012/12/04 19:34:45 ReadFileToString returns a bool, not an int. I sen
94 }
95
62 class WindowStateManager; 96 class WindowStateManager;
63 97
64 // static 98 // static
65 WindowStateManager* g_window_state_manager = NULL; 99 WindowStateManager* g_window_state_manager = NULL;
66 100
67 // WindowStateManager remembers which windows have been minimized in order to 101 // WindowStateManager remembers which windows have been minimized in order to
68 // restore them when the wallpaper viewer is hidden. 102 // restore them when the wallpaper viewer is hidden.
69 class WindowStateManager : public aura::WindowObserver { 103 class WindowStateManager : public aura::WindowObserver {
70 public: 104 public:
71 105
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 } 283 }
250 284
251 WallpaperSetWallpaperFunction::WallpaperSetWallpaperFunction() { 285 WallpaperSetWallpaperFunction::WallpaperSetWallpaperFunction() {
252 } 286 }
253 287
254 WallpaperSetWallpaperFunction::~WallpaperSetWallpaperFunction() { 288 WallpaperSetWallpaperFunction::~WallpaperSetWallpaperFunction() {
255 } 289 }
256 290
257 bool WallpaperSetWallpaperFunction::RunImpl() { 291 bool WallpaperSetWallpaperFunction::RunImpl() {
258 BinaryValue* input = NULL; 292 BinaryValue* input = NULL;
259 if (args_ == NULL || !args_->GetBinary(0, &input)) { 293 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(0, &input));
260 return false; 294
261 }
262 std::string layout_string; 295 std::string layout_string;
263 if (!args_->GetString(1, &layout_string) || layout_string.empty()) { 296 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &layout_string));
264 return false; 297 EXTENSION_FUNCTION_VALIDATE(!layout_string.empty());
265 }
266 layout_ = GetLayoutEnum(layout_string); 298 layout_ = GetLayoutEnum(layout_string);
267 if (!args_->GetString(2, &url_) || url_.empty()) 299
268 return false; 300 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &url_));
301 EXTENSION_FUNCTION_VALIDATE(!url_.empty());
269 302
270 // Gets email address while at UI thread. 303 // Gets email address while at UI thread.
271 email_ = chromeos::UserManager::Get()->GetLoggedInUser()->email(); 304 email_ = chromeos::UserManager::Get()->GetLoggedInUser()->email();
272 305
273 image_data_.assign(input->GetBuffer(), input->GetSize()); 306 image_data_.assign(input->GetBuffer(), input->GetSize());
274 if (wallpaper_decoder_) 307 if (wallpaper_decoder_)
275 wallpaper_decoder_->Cancel(); 308 wallpaper_decoder_->Cancel();
276 wallpaper_decoder_ = new WallpaperDecoder(this); 309 wallpaper_decoder_ = new WallpaperDecoder(this);
277 wallpaper_decoder_->Start(image_data_); 310 wallpaper_decoder_->Start(image_data_);
278 311
279 return true; 312 return true;
280 } 313 }
281 314
282 void WallpaperSetWallpaperFunction::OnWallpaperDecoded( 315 void WallpaperSetWallpaperFunction::OnWallpaperDecoded(
283 const gfx::ImageSkia& wallpaper) { 316 const gfx::ImageSkia& wallpaper) {
284 wallpaper_ = wallpaper; 317 wallpaper_ = wallpaper;
285 // Set wallpaper_decoder_ to null since the decoding already finished. 318 // Set wallpaper_decoder_ to null since the decoding already finished.
286 wallpaper_decoder_ = NULL; 319 wallpaper_decoder_ = NULL;
287 320
288 sequence_token_ = BrowserThread::GetBlockingPool()-> 321 sequence_token_ = BrowserThread::GetBlockingPool()->
289 GetNamedSequenceToken(chromeos::kWallpaperSequenceTokenName); 322 GetNamedSequenceToken(chromeos::kWallpaperSequenceTokenName);
290 scoped_refptr<base::SequencedTaskRunner> task_runner = 323 scoped_refptr<base::SequencedTaskRunner> task_runner =
291 BrowserThread::GetBlockingPool()-> 324 BrowserThread::GetBlockingPool()->
292 GetSequencedTaskRunnerWithShutdownBehavior(sequence_token_, 325 GetSequencedTaskRunnerWithShutdownBehavior(sequence_token_,
293 base::SequencedWorkerPool::BLOCK_SHUTDOWN); 326 base::SequencedWorkerPool::BLOCK_SHUTDOWN);
294 327
295 task_runner->PostTask(FROM_HERE, 328 task_runner->PostTask(FROM_HERE,
296 base::Bind(&WallpaperSetWallpaperFunction::SaveToFile, 329 base::Bind(&WallpaperSetWallpaperFunction::SaveToFile, this));
297 this));
298 } 330 }
299 331
300 void WallpaperSetWallpaperFunction::SaveToFile() { 332 void WallpaperSetWallpaperFunction::SaveToFile() {
301 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( 333 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
302 sequence_token_)); 334 sequence_token_));
303 FilePath wallpaper_dir;
304 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir));
305 if (!file_util::DirectoryExists(wallpaper_dir) &&
306 !file_util::CreateDirectory(wallpaper_dir)) {
307 BrowserThread::PostTask(
308 BrowserThread::UI, FROM_HERE,
309 base::Bind(&WallpaperSetWallpaperFunction::OnFailureOrCancel,
310 this, ""));
311 LOG(ERROR) << "Failed to create wallpaper directory.";
312 return;
313 }
314 std::string file_name = GURL(url_).ExtractFileName(); 335 std::string file_name = GURL(url_).ExtractFileName();
315 FilePath file_path = wallpaper_dir.Append(file_name); 336 if (SaveData(chrome::DIR_CHROMEOS_WALLPAPERS, file_name, image_data_)) {
316 if (file_util::PathExists(file_path) ||
317 file_util::WriteFile(file_path, image_data_.c_str(),
318 image_data_.size()) != -1 ) {
319 wallpaper_.EnsureRepsForSupportedScaleFactors(); 337 wallpaper_.EnsureRepsForSupportedScaleFactors();
320 scoped_ptr<gfx::ImageSkia> deep_copy(wallpaper_.DeepCopy()); 338 scoped_ptr<gfx::ImageSkia> deep_copy(wallpaper_.DeepCopy());
321 // ImageSkia is not RefCountedThreadSafe. Use a deep copied ImageSkia if 339 // ImageSkia is not RefCountedThreadSafe. Use a deep copied ImageSkia if
322 // post to another thread. 340 // post to another thread.
323 BrowserThread::PostTask( 341 BrowserThread::PostTask(
324 BrowserThread::UI, FROM_HERE, 342 BrowserThread::UI, FROM_HERE,
325 base::Bind(&WallpaperSetWallpaperFunction::SetDecodedWallpaper, 343 base::Bind(&WallpaperSetWallpaperFunction::SetDecodedWallpaper,
326 this, base::Passed(&deep_copy))); 344 this, base::Passed(&deep_copy)));
327 chromeos::UserImage wallpaper(wallpaper_); 345 chromeos::UserImage wallpaper(wallpaper_);
328 346
347 FilePath wallpaper_dir;
348 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir));
349 FilePath file_path = wallpaper_dir.Append(file_name).InsertBeforeExtension(
350 chromeos::kSmallWallpaperSuffix);
351 if (file_util::PathExists(file_path))
352 return;
329 // Generates and saves small resolution wallpaper. Uses CENTER_CROPPED to 353 // Generates and saves small resolution wallpaper. Uses CENTER_CROPPED to
330 // maintain the aspect ratio after resize. 354 // maintain the aspect ratio after resize.
331 chromeos::WallpaperManager::Get()->ResizeAndSaveWallpaper( 355 chromeos::WallpaperManager::Get()->ResizeAndSaveWallpaper(
332 wallpaper, 356 wallpaper,
333 file_path.InsertBeforeExtension(chromeos::kSmallWallpaperSuffix), 357 file_path,
334 ash::WALLPAPER_LAYOUT_CENTER_CROPPED, 358 ash::WALLPAPER_LAYOUT_CENTER_CROPPED,
335 ash::kSmallWallpaperMaxWidth, 359 ash::kSmallWallpaperMaxWidth,
336 ash::kSmallWallpaperMaxHeight); 360 ash::kSmallWallpaperMaxHeight);
337 } else { 361 } else {
362 std::string error = base::StringPrintf(
363 "Failed to create/write wallpaper to %s.", file_name.c_str());
338 BrowserThread::PostTask( 364 BrowserThread::PostTask(
339 BrowserThread::UI, FROM_HERE, 365 BrowserThread::UI, FROM_HERE,
340 base::Bind(&WallpaperSetWallpaperFunction::OnFailureOrCancel, 366 base::Bind(&WallpaperSetWallpaperFunction::OnFailureOrCancel,
341 this, "")); 367 this, error));
342 LOG(ERROR) << "Failed to save downloaded wallpaper.";
343 } 368 }
344 } 369 }
345 370
346 void WallpaperSetWallpaperFunction::SetDecodedWallpaper( 371 void WallpaperSetWallpaperFunction::SetDecodedWallpaper(
347 scoped_ptr<gfx::ImageSkia> wallpaper) { 372 scoped_ptr<gfx::ImageSkia> wallpaper) {
348 chromeos::WallpaperManager* wallpaper_manager = 373 chromeos::WallpaperManager* wallpaper_manager =
349 chromeos::WallpaperManager::Get(); 374 chromeos::WallpaperManager::Get();
350 wallpaper_manager->SetWallpaperFromImageSkia(*wallpaper.get(), layout_); 375 wallpaper_manager->SetWallpaperFromImageSkia(*wallpaper.get(), layout_);
351 bool is_persistent = 376 bool is_persistent =
352 !chromeos::UserManager::Get()->IsCurrentUserEphemeral(); 377 !chromeos::UserManager::Get()->IsCurrentUserEphemeral();
353 chromeos::WallpaperInfo info = { 378 chromeos::WallpaperInfo info = {
354 url_, 379 url_,
355 layout_, 380 layout_,
356 chromeos::User::ONLINE, 381 chromeos::User::ONLINE,
357 base::Time::Now().LocalMidnight() 382 base::Time::Now().LocalMidnight()
358 }; 383 };
359 wallpaper_manager->SetUserWallpaperInfo(email_, info, is_persistent); 384 wallpaper_manager->SetUserWallpaperInfo(email_, info, is_persistent);
360 SendResponse(true); 385 SendResponse(true);
361 } 386 }
362 387
363 WallpaperSetCustomWallpaperFunction::WallpaperSetCustomWallpaperFunction() { 388 WallpaperSetCustomWallpaperFunction::WallpaperSetCustomWallpaperFunction() {
364 } 389 }
365 390
366 WallpaperSetCustomWallpaperFunction::~WallpaperSetCustomWallpaperFunction() { 391 WallpaperSetCustomWallpaperFunction::~WallpaperSetCustomWallpaperFunction() {
367 } 392 }
368 393
369 bool WallpaperSetCustomWallpaperFunction::RunImpl() { 394 bool WallpaperSetCustomWallpaperFunction::RunImpl() {
370 BinaryValue* input = NULL; 395 BinaryValue* input = NULL;
371 if (args_ == NULL || !args_->GetBinary(0, &input)) { 396 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(0, &input));
372 return false; 397
373 }
374 std::string layout_string; 398 std::string layout_string;
375 if (!args_->GetString(1, &layout_string) || layout_string.empty()) { 399 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &layout_string));
376 return false; 400 EXTENSION_FUNCTION_VALIDATE(!layout_string.empty());
377 }
378 layout_ = GetLayoutEnum(layout_string); 401 layout_ = GetLayoutEnum(layout_string);
379 402
380 // Gets email address while at UI thread. 403 // Gets email address while at UI thread.
381 email_ = chromeos::UserManager::Get()->GetLoggedInUser()->email(); 404 email_ = chromeos::UserManager::Get()->GetLoggedInUser()->email();
382 405
383 image_data_.assign(input->GetBuffer(), input->GetSize()); 406 image_data_.assign(input->GetBuffer(), input->GetSize());
384 if (wallpaper_decoder_) 407 if (wallpaper_decoder_)
385 wallpaper_decoder_->Cancel(); 408 wallpaper_decoder_->Cancel();
386 wallpaper_decoder_ = new WallpaperDecoder(this); 409 wallpaper_decoder_ = new WallpaperDecoder(this);
387 wallpaper_decoder_->Start(image_data_); 410 wallpaper_decoder_->Start(image_data_);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 } 443 }
421 444
422 WallpaperRestoreMinimizedWindowsFunction:: 445 WallpaperRestoreMinimizedWindowsFunction::
423 ~WallpaperRestoreMinimizedWindowsFunction() { 446 ~WallpaperRestoreMinimizedWindowsFunction() {
424 } 447 }
425 448
426 bool WallpaperRestoreMinimizedWindowsFunction::RunImpl() { 449 bool WallpaperRestoreMinimizedWindowsFunction::RunImpl() {
427 WindowStateManager::RestoreWindows(); 450 WindowStateManager::RestoreWindows();
428 return true; 451 return true;
429 } 452 }
453
454 WallpaperGetThumbnailFunction::WallpaperGetThumbnailFunction() {
455 }
456
457 WallpaperGetThumbnailFunction::~WallpaperGetThumbnailFunction() {
458 }
459
460 bool WallpaperGetThumbnailFunction::RunImpl() {
461 std::string url;
462 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &url));
463 EXTENSION_FUNCTION_VALIDATE(!url.empty());
464 std::string file_name = GURL(url).ExtractFileName();
465 sequence_token_ = BrowserThread::GetBlockingPool()->
466 GetNamedSequenceToken(chromeos::kWallpaperSequenceTokenName);
467 scoped_refptr<base::SequencedTaskRunner> task_runner =
468 BrowserThread::GetBlockingPool()->
469 GetSequencedTaskRunnerWithShutdownBehavior(sequence_token_,
470 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
471
472 task_runner->PostTask(FROM_HERE,
473 base::Bind(&WallpaperGetThumbnailFunction::Get, this, file_name));
474 return true;
475 }
476
477 void WallpaperGetThumbnailFunction::Failure(const std::string& file_name) {
478 SetError(base::StringPrintf("Failed to access wallpaper thumbnails for %s.",
479 file_name.c_str()));
480 SendResponse(false);
481 }
482
483 void WallpaperGetThumbnailFunction::FileNotLoaded() {
484 SendResponse(true);
485 }
486
487 void WallpaperGetThumbnailFunction::FileLoaded(const std::string& data) {
488 BinaryValue* thumbnail = BinaryValue::CreateWithCopiedBuffer(data.c_str(),
489 data.size());
490 SetResult(thumbnail);
491 SendResponse(true);
492 }
493
494 void WallpaperGetThumbnailFunction::Get(const std::string& file_name) {
495 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
496 sequence_token_));
497 std::string data;
498 if (GetData(chrome::DIR_CHROMEOS_WALLPAPER_THUMBNAILS, file_name, &data)) {
499 if (data.empty()) {
500 BrowserThread::PostTask(
501 BrowserThread::UI, FROM_HERE,
502 base::Bind(&WallpaperGetThumbnailFunction::FileNotLoaded, this));
503 } else {
504 BrowserThread::PostTask(
505 BrowserThread::UI, FROM_HERE,
506 base::Bind(&WallpaperGetThumbnailFunction::FileLoaded, this, data));
507 }
508 } else {
509 BrowserThread::PostTask(
510 BrowserThread::UI, FROM_HERE,
511 base::Bind(&WallpaperGetThumbnailFunction::Failure, this, file_name));
512 }
513 }
514
515 WallpaperSaveThumbnailFunction::WallpaperSaveThumbnailFunction() {
516 }
517
518 WallpaperSaveThumbnailFunction::~WallpaperSaveThumbnailFunction() {
519 }
520
521 bool WallpaperSaveThumbnailFunction::RunImpl() {
522 std::string url;
523 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &url));
524 EXTENSION_FUNCTION_VALIDATE(!url.empty());
525
526 BinaryValue* input = NULL;
527 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &input));
528
529 std::string file_name = GURL(url).ExtractFileName();
530 std::string data(input->GetBuffer(), input->GetSize());
531
532 sequence_token_ = BrowserThread::GetBlockingPool()->
533 GetNamedSequenceToken(chromeos::kWallpaperSequenceTokenName);
534 scoped_refptr<base::SequencedTaskRunner> task_runner =
535 BrowserThread::GetBlockingPool()->
536 GetSequencedTaskRunnerWithShutdownBehavior(sequence_token_,
537 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
538
539 task_runner->PostTask(FROM_HERE,
540 base::Bind(&WallpaperSaveThumbnailFunction::Save,
541 this, data, file_name));
542 return true;
543 }
544
545 void WallpaperSaveThumbnailFunction::Failure(const std::string& file_name) {
546 SetError(base::StringPrintf("Failed to create/write thumbnail of %s.",
547 file_name.c_str()));
548 SendResponse(false);
549 }
550
551 void WallpaperSaveThumbnailFunction::Success() {
552 SendResponse(true);
553 }
554
555 void WallpaperSaveThumbnailFunction::Save(const std::string& data,
556 const std::string& file_name) {
557 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
558 sequence_token_));
559 if (SaveData(chrome::DIR_CHROMEOS_WALLPAPER_THUMBNAILS, file_name, data)) {
560 BrowserThread::PostTask(
561 BrowserThread::UI, FROM_HERE,
562 base::Bind(&WallpaperSaveThumbnailFunction::Success, this));
563 } else {
564 BrowserThread::PostTask(
565 BrowserThread::UI, FROM_HERE,
566 base::Bind(&WallpaperSaveThumbnailFunction::Failure,
567 this, file_name));
568 }
569 }
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/extensions/wallpaper_private_api.h ('k') | chrome/browser/extensions/extension_function_registry.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698