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

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

Issue 11280300: Revert 171011 - Broke Linux ChromiumOS Tests (dbg)(2) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: 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"
17 #include "base/synchronization/cancellation_flag.h" 16 #include "base/synchronization/cancellation_flag.h"
18 #include "base/threading/sequenced_worker_pool.h" 17 #include "base/threading/sequenced_worker_pool.h"
19 #include "base/threading/worker_pool.h" 18 #include "base/threading/worker_pool.h"
20 #include "chrome/browser/browser_process.h" 19 #include "chrome/browser/browser_process.h"
21 #include "chrome/browser/chromeos/login/user.h" 20 #include "chrome/browser/chromeos/login/user.h"
22 #include "chrome/browser/chromeos/login/user_image.h" 21 #include "chrome/browser/chromeos/login/user_image.h"
23 #include "chrome/browser/chromeos/login/user_manager.h" 22 #include "chrome/browser/chromeos/login/user_manager.h"
24 #include "chrome/browser/chromeos/login/wallpaper_manager.h" 23 #include "chrome/browser/chromeos/login/wallpaper_manager.h"
25 #include "chrome/browser/extensions/event_router.h" 24 #include "chrome/browser/extensions/event_router.h"
26 #include "chrome/browser/image_decoder.h" 25 #include "chrome/browser/image_decoder.h"
(...skipping 26 matching lines...) Expand all
53 52
54 ash::WallpaperLayout GetLayoutEnum(const std::string& layout) { 53 ash::WallpaperLayout GetLayoutEnum(const std::string& layout) {
55 for (int i = 0; i < kWallpaperLayoutCount; i++) { 54 for (int i = 0; i < kWallpaperLayoutCount; i++) {
56 if (layout.compare(kWallpaperLayoutArrays[i]) == 0) 55 if (layout.compare(kWallpaperLayoutArrays[i]) == 0)
57 return static_cast<ash::WallpaperLayout>(i); 56 return static_cast<ash::WallpaperLayout>(i);
58 } 57 }
59 // Default to use CENTER layout. 58 // Default to use CENTER layout.
60 return ash::WALLPAPER_LAYOUT_CENTER; 59 return ash::WALLPAPER_LAYOUT_CENTER;
61 } 60 }
62 61
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);
94 }
95
96 class WindowStateManager; 62 class WindowStateManager;
97 63
98 // static 64 // static
99 WindowStateManager* g_window_state_manager = NULL; 65 WindowStateManager* g_window_state_manager = NULL;
100 66
101 // WindowStateManager remembers which windows have been minimized in order to 67 // WindowStateManager remembers which windows have been minimized in order to
102 // restore them when the wallpaper viewer is hidden. 68 // restore them when the wallpaper viewer is hidden.
103 class WindowStateManager : public aura::WindowObserver { 69 class WindowStateManager : public aura::WindowObserver {
104 public: 70 public:
105 71
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 } 249 }
284 250
285 WallpaperSetWallpaperFunction::WallpaperSetWallpaperFunction() { 251 WallpaperSetWallpaperFunction::WallpaperSetWallpaperFunction() {
286 } 252 }
287 253
288 WallpaperSetWallpaperFunction::~WallpaperSetWallpaperFunction() { 254 WallpaperSetWallpaperFunction::~WallpaperSetWallpaperFunction() {
289 } 255 }
290 256
291 bool WallpaperSetWallpaperFunction::RunImpl() { 257 bool WallpaperSetWallpaperFunction::RunImpl() {
292 BinaryValue* input = NULL; 258 BinaryValue* input = NULL;
293 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(0, &input)); 259 if (args_ == NULL || !args_->GetBinary(0, &input)) {
294 260 return false;
261 }
295 std::string layout_string; 262 std::string layout_string;
296 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &layout_string)); 263 if (!args_->GetString(1, &layout_string) || layout_string.empty()) {
297 EXTENSION_FUNCTION_VALIDATE(!layout_string.empty()); 264 return false;
265 }
298 layout_ = GetLayoutEnum(layout_string); 266 layout_ = GetLayoutEnum(layout_string);
299 267 if (!args_->GetString(2, &url_) || url_.empty())
300 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &url_)); 268 return false;
301 EXTENSION_FUNCTION_VALIDATE(!url_.empty());
302 269
303 // Gets email address while at UI thread. 270 // Gets email address while at UI thread.
304 email_ = chromeos::UserManager::Get()->GetLoggedInUser()->email(); 271 email_ = chromeos::UserManager::Get()->GetLoggedInUser()->email();
305 272
306 image_data_.assign(input->GetBuffer(), input->GetSize()); 273 image_data_.assign(input->GetBuffer(), input->GetSize());
307 if (wallpaper_decoder_) 274 if (wallpaper_decoder_)
308 wallpaper_decoder_->Cancel(); 275 wallpaper_decoder_->Cancel();
309 wallpaper_decoder_ = new WallpaperDecoder(this); 276 wallpaper_decoder_ = new WallpaperDecoder(this);
310 wallpaper_decoder_->Start(image_data_); 277 wallpaper_decoder_->Start(image_data_);
311 278
312 return true; 279 return true;
313 } 280 }
314 281
315 void WallpaperSetWallpaperFunction::OnWallpaperDecoded( 282 void WallpaperSetWallpaperFunction::OnWallpaperDecoded(
316 const gfx::ImageSkia& wallpaper) { 283 const gfx::ImageSkia& wallpaper) {
317 wallpaper_ = wallpaper; 284 wallpaper_ = wallpaper;
318 // Set wallpaper_decoder_ to null since the decoding already finished. 285 // Set wallpaper_decoder_ to null since the decoding already finished.
319 wallpaper_decoder_ = NULL; 286 wallpaper_decoder_ = NULL;
320 287
321 sequence_token_ = BrowserThread::GetBlockingPool()-> 288 sequence_token_ = BrowserThread::GetBlockingPool()->
322 GetNamedSequenceToken(chromeos::kWallpaperSequenceTokenName); 289 GetNamedSequenceToken(chromeos::kWallpaperSequenceTokenName);
323 scoped_refptr<base::SequencedTaskRunner> task_runner = 290 scoped_refptr<base::SequencedTaskRunner> task_runner =
324 BrowserThread::GetBlockingPool()-> 291 BrowserThread::GetBlockingPool()->
325 GetSequencedTaskRunnerWithShutdownBehavior(sequence_token_, 292 GetSequencedTaskRunnerWithShutdownBehavior(sequence_token_,
326 base::SequencedWorkerPool::BLOCK_SHUTDOWN); 293 base::SequencedWorkerPool::BLOCK_SHUTDOWN);
327 294
328 task_runner->PostTask(FROM_HERE, 295 task_runner->PostTask(FROM_HERE,
329 base::Bind(&WallpaperSetWallpaperFunction::SaveToFile, this)); 296 base::Bind(&WallpaperSetWallpaperFunction::SaveToFile,
297 this));
330 } 298 }
331 299
332 void WallpaperSetWallpaperFunction::SaveToFile() { 300 void WallpaperSetWallpaperFunction::SaveToFile() {
333 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( 301 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
334 sequence_token_)); 302 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 }
335 std::string file_name = GURL(url_).ExtractFileName(); 314 std::string file_name = GURL(url_).ExtractFileName();
336 if (SaveData(chrome::DIR_CHROMEOS_WALLPAPERS, file_name, image_data_)) { 315 FilePath file_path = wallpaper_dir.Append(file_name);
316 if (file_util::PathExists(file_path) ||
317 file_util::WriteFile(file_path, image_data_.c_str(),
318 image_data_.size()) != -1 ) {
337 wallpaper_.EnsureRepsForSupportedScaleFactors(); 319 wallpaper_.EnsureRepsForSupportedScaleFactors();
338 scoped_ptr<gfx::ImageSkia> deep_copy(wallpaper_.DeepCopy()); 320 scoped_ptr<gfx::ImageSkia> deep_copy(wallpaper_.DeepCopy());
339 // ImageSkia is not RefCountedThreadSafe. Use a deep copied ImageSkia if 321 // ImageSkia is not RefCountedThreadSafe. Use a deep copied ImageSkia if
340 // post to another thread. 322 // post to another thread.
341 BrowserThread::PostTask( 323 BrowserThread::PostTask(
342 BrowserThread::UI, FROM_HERE, 324 BrowserThread::UI, FROM_HERE,
343 base::Bind(&WallpaperSetWallpaperFunction::SetDecodedWallpaper, 325 base::Bind(&WallpaperSetWallpaperFunction::SetDecodedWallpaper,
344 this, base::Passed(&deep_copy))); 326 this, base::Passed(&deep_copy)));
345 chromeos::UserImage wallpaper(wallpaper_); 327 chromeos::UserImage wallpaper(wallpaper_);
346 328
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;
353 // Generates and saves small resolution wallpaper. Uses CENTER_CROPPED to 329 // Generates and saves small resolution wallpaper. Uses CENTER_CROPPED to
354 // maintain the aspect ratio after resize. 330 // maintain the aspect ratio after resize.
355 chromeos::WallpaperManager::Get()->ResizeAndSaveWallpaper( 331 chromeos::WallpaperManager::Get()->ResizeAndSaveWallpaper(
356 wallpaper, 332 wallpaper,
357 file_path, 333 file_path.InsertBeforeExtension(chromeos::kSmallWallpaperSuffix),
358 ash::WALLPAPER_LAYOUT_CENTER_CROPPED, 334 ash::WALLPAPER_LAYOUT_CENTER_CROPPED,
359 ash::kSmallWallpaperMaxWidth, 335 ash::kSmallWallpaperMaxWidth,
360 ash::kSmallWallpaperMaxHeight); 336 ash::kSmallWallpaperMaxHeight);
361 } else { 337 } else {
362 std::string error = base::StringPrintf(
363 "Failed to create/write wallpaper to %s.", file_name.c_str());
364 BrowserThread::PostTask( 338 BrowserThread::PostTask(
365 BrowserThread::UI, FROM_HERE, 339 BrowserThread::UI, FROM_HERE,
366 base::Bind(&WallpaperSetWallpaperFunction::OnFailureOrCancel, 340 base::Bind(&WallpaperSetWallpaperFunction::OnFailureOrCancel,
367 this, error)); 341 this, ""));
342 LOG(ERROR) << "Failed to save downloaded wallpaper.";
368 } 343 }
369 } 344 }
370 345
371 void WallpaperSetWallpaperFunction::SetDecodedWallpaper( 346 void WallpaperSetWallpaperFunction::SetDecodedWallpaper(
372 scoped_ptr<gfx::ImageSkia> wallpaper) { 347 scoped_ptr<gfx::ImageSkia> wallpaper) {
373 chromeos::WallpaperManager* wallpaper_manager = 348 chromeos::WallpaperManager* wallpaper_manager =
374 chromeos::WallpaperManager::Get(); 349 chromeos::WallpaperManager::Get();
375 wallpaper_manager->SetWallpaperFromImageSkia(*wallpaper.get(), layout_); 350 wallpaper_manager->SetWallpaperFromImageSkia(*wallpaper.get(), layout_);
376 bool is_persistent = 351 bool is_persistent =
377 !chromeos::UserManager::Get()->IsCurrentUserEphemeral(); 352 !chromeos::UserManager::Get()->IsCurrentUserEphemeral();
378 chromeos::WallpaperInfo info = { 353 chromeos::WallpaperInfo info = {
379 url_, 354 url_,
380 layout_, 355 layout_,
381 chromeos::User::ONLINE, 356 chromeos::User::ONLINE,
382 base::Time::Now().LocalMidnight() 357 base::Time::Now().LocalMidnight()
383 }; 358 };
384 wallpaper_manager->SetUserWallpaperInfo(email_, info, is_persistent); 359 wallpaper_manager->SetUserWallpaperInfo(email_, info, is_persistent);
385 SendResponse(true); 360 SendResponse(true);
386 } 361 }
387 362
388 WallpaperSetCustomWallpaperFunction::WallpaperSetCustomWallpaperFunction() { 363 WallpaperSetCustomWallpaperFunction::WallpaperSetCustomWallpaperFunction() {
389 } 364 }
390 365
391 WallpaperSetCustomWallpaperFunction::~WallpaperSetCustomWallpaperFunction() { 366 WallpaperSetCustomWallpaperFunction::~WallpaperSetCustomWallpaperFunction() {
392 } 367 }
393 368
394 bool WallpaperSetCustomWallpaperFunction::RunImpl() { 369 bool WallpaperSetCustomWallpaperFunction::RunImpl() {
395 BinaryValue* input = NULL; 370 BinaryValue* input = NULL;
396 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(0, &input)); 371 if (args_ == NULL || !args_->GetBinary(0, &input)) {
397 372 return false;
373 }
398 std::string layout_string; 374 std::string layout_string;
399 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &layout_string)); 375 if (!args_->GetString(1, &layout_string) || layout_string.empty()) {
400 EXTENSION_FUNCTION_VALIDATE(!layout_string.empty()); 376 return false;
377 }
401 layout_ = GetLayoutEnum(layout_string); 378 layout_ = GetLayoutEnum(layout_string);
402 379
403 // Gets email address while at UI thread. 380 // Gets email address while at UI thread.
404 email_ = chromeos::UserManager::Get()->GetLoggedInUser()->email(); 381 email_ = chromeos::UserManager::Get()->GetLoggedInUser()->email();
405 382
406 image_data_.assign(input->GetBuffer(), input->GetSize()); 383 image_data_.assign(input->GetBuffer(), input->GetSize());
407 if (wallpaper_decoder_) 384 if (wallpaper_decoder_)
408 wallpaper_decoder_->Cancel(); 385 wallpaper_decoder_->Cancel();
409 wallpaper_decoder_ = new WallpaperDecoder(this); 386 wallpaper_decoder_ = new WallpaperDecoder(this);
410 wallpaper_decoder_->Start(image_data_); 387 wallpaper_decoder_->Start(image_data_);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 } 420 }
444 421
445 WallpaperRestoreMinimizedWindowsFunction:: 422 WallpaperRestoreMinimizedWindowsFunction::
446 ~WallpaperRestoreMinimizedWindowsFunction() { 423 ~WallpaperRestoreMinimizedWindowsFunction() {
447 } 424 }
448 425
449 bool WallpaperRestoreMinimizedWindowsFunction::RunImpl() { 426 bool WallpaperRestoreMinimizedWindowsFunction::RunImpl() {
450 WindowStateManager::RestoreWindows(); 427 WindowStateManager::RestoreWindows();
451 return true; 428 return true;
452 } 429 }
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