Chromium Code Reviews| Index: chrome/browser/chromeos/extensions/wallpaper_private_api.cc |
| diff --git a/chrome/browser/chromeos/extensions/wallpaper_private_api.cc b/chrome/browser/chromeos/extensions/wallpaper_private_api.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e10d7aacd13d3e98727b805249f855b1f4b496af |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/extensions/wallpaper_private_api.cc |
| @@ -0,0 +1,190 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/extensions/wallpaper_private_api.h" |
| + |
| +#include "ash/desktop_background/desktop_background_controller.h" |
| +#include "ash/shell.h" |
| +#include "base/command_line.h" |
| +#include "base/file_util.h" |
| +#include "base/json/json_writer.h" |
| +#include "base/path_service.h" |
| +#include "base/synchronization/cancellation_flag.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/chromeos/login/user_manager.h" |
| +#include "chrome/browser/chromeos/login/wallpaper_manager.h" |
| +#include "chrome/browser/extensions/extension_event_router.h" |
| +#include "chrome/browser/extensions/extension_service.h" |
| +#include "chrome/browser/image_decoder.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/browser_finder.h" |
| +#include "chrome/browser/ui/chrome_pages.h" |
| +#include "chrome/browser/ui/extensions/application_launch.h" |
| +#include "chrome/common/chrome_paths.h" |
| +#include "chrome/common/chrome_switches.h" |
| +#include "chrome/common/url_constants.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "net/url_request/url_fetcher.h" |
| +#include "net/url_request/url_fetcher_delegate.h" |
| +#include "net/url_request/url_request_status.h" |
| +#include "grit/generated_resources.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| + |
| +using base::BinaryValue; |
| +using content::BrowserThread; |
| + |
| +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.
|
| + |
| +namespace wallpaper_manager_util { |
| + |
| +void OpenWallpaperManager() { |
| + Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); |
| + // Hides the new UI container behind a flag. |
| + if (CommandLine::ForCurrentProcess()->HasSwitch( |
| + switches::kExperimentalWallpaperUI)) { |
| + std::string url = chrome::kChromeUIWallpaperURL; |
| + ExtensionService* service = profile->GetExtensionService(); |
| + if (!service) |
| + return; |
| + |
| + const extensions::Extension* extension = |
| + service->GetExtensionById(kWallpaperManagerDomain, false); |
| + if (!extension) |
| + return; |
| + |
| + application_launch::LaunchParams params(profile, extension, |
| + extension_misc::LAUNCH_WINDOW, |
| + NEW_FOREGROUND_TAB); |
| + params.override_url = GURL(url); |
| + application_launch::OpenApplication(params); |
| + } else { |
| + Browser* browser = browser::FindOrCreateTabbedBrowser( |
| + ProfileManager::GetDefaultProfileOrOffTheRecord()); |
| + chrome::ShowSettingsSubPage(browser, "setWallpaper"); |
| + } |
| +} |
| + |
| +} //namespace wallpaper_manager_util |
| + |
| +bool WallpaperStringsFunction::RunImpl() { |
| + DictionaryValue* dict = new DictionaryValue(); |
| + SetResult(dict); |
| + |
| +#define SET_STRING(ns, id) \ |
| + dict->SetString(#id, l10n_util::GetStringUTF16(ns##_##id)) |
| + SET_STRING(IDS_WALLPAPER_MANAGER, SEARCH_TEXT_LABEL); |
| + SET_STRING(IDS_WALLPAPER_MANAGER, AUTHOR_LABEL); |
| + SET_STRING(IDS_WALLPAPER_MANAGER, CUSTOM_CATEGORY_LABEL); |
| + SET_STRING(IDS_WALLPAPER_MANAGER, SELECT_CUSTOM_LABEL); |
| + SET_STRING(IDS_WALLPAPER_MANAGER, POSITION_LABEL); |
| + SET_STRING(IDS_WALLPAPER_MANAGER, COLOR_LABEL); |
| + SET_STRING(IDS_WALLPAPER_MANAGER, PREVIEW_LABEL); |
| + SET_STRING(IDS_OPTIONS, SET_WALLPAPER_DAILY); |
| +#undef SET_STRING |
| + |
| + ChromeURLDataManager::DataSource::SetFontAndTextDirection(dict); |
| + |
| + return true; |
| +} |
| + |
| +class WallpaperSetWallpaperFunction::WallpaperDecoder |
| + : public ImageDecoder::Delegate { |
| + public: |
| + WallpaperDecoder(scoped_refptr<WallpaperSetWallpaperFunction> function) |
| + : function_(function) { |
| + } |
| + |
| + void Start(const std::string& image_data) { |
| + image_decoder_ = new ImageDecoder(this, image_data); |
| + image_decoder_->Start(); |
| + } |
| + |
| + void Cancel() { |
| + cancel_flag_.Set(); |
| + } |
| + |
| + virtual void OnImageDecoded(const ImageDecoder* decoder, |
| + const SkBitmap& decoded_image) OVERRIDE { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + gfx::ImageSkia final_image(decoded_image); |
| + if (cancel_flag_.IsSet()) |
| + 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.
|
| + function_->SetWallpaper(final_image); |
| + delete this; |
| + } |
| + |
| + virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + if (cancel_flag_.IsSet()) |
| + delete this; |
|
Mihai Parparita -not on Chrome
2012/07/20 00:17:05
Ditto here.
bshe
2012/07/22 23:59:42
Done.
|
| + // TODO(bshe): Dispatches an encoding error event. |
| + delete this; |
| + } |
| + |
| + private: |
| + scoped_refptr<WallpaperSetWallpaperFunction> function_; |
| + scoped_refptr<ImageDecoder> image_decoder_; |
| + base::CancellationFlag cancel_flag_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(WallpaperDecoder); |
| +}; |
| + |
| +WallpaperSetWallpaperFunction::WallpaperDecoder* |
| + WallpaperSetWallpaperFunction::wallpaper_decoder_; |
| + |
| +bool WallpaperSetWallpaperFunction::RunImpl() { |
| + BinaryValue* input = NULL; |
| + if (args_ == NULL || !args_->GetBinary(0, &input)) { |
| + return false; |
| + } |
| + std::string layout_string; |
| + if (!args_->GetString(1, &layout_string) || layout_string.empty()) { |
| + return false; |
| + } |
| + layout_ = ash::GetLayoutEnum(layout_string.c_str()); |
| + std::string url; |
| + if (!args_->GetString(2, &url) || url.empty()) { |
| + return false; |
| + } |
| + file_name_ = GURL(url).ExtractFileName(); |
| + |
| + // Gets email address while at UI thread. |
| + email_ = chromeos::UserManager::Get()->GetLoggedInUser().email(); |
| + |
| + image_data_.assign(input->GetBuffer(), input->GetSize()); |
| + if (wallpaper_decoder_) |
| + wallpaper_decoder_->Cancel(); |
| + wallpaper_decoder_ = new WallpaperDecoder(this); |
| + wallpaper_decoder_->Start(image_data_); |
| + |
| + return true; |
| +} |
| + |
| +void WallpaperSetWallpaperFunction::SetWallpaper( |
| + const gfx::ImageSkia& wallpaper) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + |
| + if(SaveToFile()) { |
| + ash::Shell::GetInstance()->desktop_background_controller()-> |
| + 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.
|
| + wallpaper_decoder_ = NULL; |
| + } |
| +} |
| + |
| +bool WallpaperSetWallpaperFunction::SaveToFile() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + FilePath wallpaper_dir; |
| + CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir)); |
| + if (!file_util::DirectoryExists(wallpaper_dir) && |
| + !file_util::CreateDirectory(wallpaper_dir)) { |
| + return false; |
| + } |
| + FilePath file_path = wallpaper_dir.Append(file_name_); |
| + if (file_util::PathExists(file_path)) |
| + return true; |
| + return file_util::WriteFile(file_path, image_data_.c_str(), |
| + image_data_.size()) != -1; |
| +} |