Index: chrome/browser/chromeos/extensions/wallpaper_manager_api.cc |
diff --git a/chrome/browser/chromeos/extensions/wallpaper_manager_api.cc b/chrome/browser/chromeos/extensions/wallpaper_manager_api.cc |
index 9e48b000b30ff732c2ffd24a26f0a36043333cfa..8b8bbb232019ab86856c91fc7d7ae05c99eee265 100644 |
--- a/chrome/browser/chromeos/extensions/wallpaper_manager_api.cc |
+++ b/chrome/browser/chromeos/extensions/wallpaper_manager_api.cc |
@@ -4,27 +4,47 @@ |
#include "chrome/browser/chromeos/extensions/wallpaper_manager_api.h" |
+#include "ash/desktop_background/desktop_background_resources.h" |
+#include "ash/shell.h" |
+#include "ash/shell_window_ids.h" |
#include "base/command_line.h" |
#include "base/file_util.h" |
+#include "base/json/json_writer.h" |
#include "base/path_service.h" |
#include "chrome/browser/browser_process.h" |
#include "chrome/browser/chromeos/login/user_manager.h" |
+#include "chrome/browser/extensions/extension_event_router.h" |
#include "chrome/browser/extensions/extension_service.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/browser_list.h" |
#include "chrome/browser/ui/chrome_pages.h" |
#include "chrome/browser/ui/extensions/application_launch.h" |
+#include "chrome/browser/ui/webui/chrome_url_data_manager.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/aura/root_window.h" |
+#include "ui/aura/window.h" |
+#include "ui/base/l10n/l10n_util.h" |
using content::BrowserThread; |
const char kWallpaperManagerDomain[] = "obklkkbkpaoaejdabbfldmcfplpdgolj"; |
+namespace events { |
+ const char kDownloadProgressEvent[] = "experimental.wallpaperManager.onDownloadProgress"; |
+ const char kDownloadDoneEvent[] = "experimental.wallpaperManager.onDownloadDone"; |
+ const char kDownloadErrorEvent[] = "experimental.wallpaperManager.onDownloadError"; |
+} // namespace events |
+ |
namespace wallpaper_manager_util { |
void OpenWallpaperManager() { |
@@ -46,9 +66,198 @@ void OpenWallpaperManager() { |
extension_misc::LAUNCH_WINDOW, GURL(url), NEW_FOREGROUND_TAB, NULL); |
} else { |
Browser* browser = browser::FindOrCreateTabbedBrowser( |
- ProfileManager::GetDefaultProfileOrOffTheRecord()); |
+ ProfileManager::GetDefaultProfileOrOffTheRecord()); |
chrome::ShowSettingsSubPage(browser, "setWallpaper"); |
} |
} |
-} // namespace wallpaper_manager_util |
+} //namespace wallpaper_manager_util |
+ |
+bool WallpaperManagerStringsFunction::RunImpl() { |
+ result_.reset(new DictionaryValue()); |
+ DictionaryValue* dict = reinterpret_cast<DictionaryValue*>(result_.get()); |
+ |
+#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; |
+} |
+ |
+WallpaperManagerSetWallpaperFunction::~WallpaperManagerSetWallpaperFunction() { |
+} |
+ |
+bool WallpaperManagerSetWallpaperFunction::RunImpl() { |
+ // TODO(bshe): Add layout parameter. |
+ // First param is url of the selected wallpaper. |
+ std::string url; |
+ if (!args_->GetString(0, &url) || url.empty()) |
+ return false; |
+ |
+ BrowserThread::PostTask( |
+ BrowserThread::FILE, FROM_HERE, |
+ base::Bind( |
+ &WallpaperManagerSetWallpaperFunction::RequestOnFileThread, |
+ this, |
+ url)); |
+ // Will finish asynchronously. |
+ return true; |
+} |
+ |
+void WallpaperManagerSetWallpaperFunction::OnURLFetchComplete( |
+ const net::URLFetcher* source) { |
+ WallpaperErrorCode code = GetErrorCode(source); |
+ if (code != HTTP_SUCCESS) { |
+ DispatchErrorEvent(code); |
+ return; |
+ } |
+ FilePath temp_file; |
+ if (code == HTTP_SUCCESS && |
+ !source->GetResponseAsFilePath(true, &temp_file)) { |
+ code = WALLPAPER_FILE_ERROR; |
+ DispatchErrorEvent(code); |
+ return; |
+ } |
+ |
+ profile_->GetExtensionEventRouter()->DispatchEventToExtension( |
+ std::string(kWallpaperManagerDomain), |
+ events::kDownloadDoneEvent, |
+ "[]", profile_, GURL()); |
+ |
+ FilePath path = wallpaper_dir_.Append( |
+ source->GetOriginalURL().ExtractFileName() + ".jpg"); |
+ |
+ // TODO(bshe): Call function to set wallpaper to the image which file_path |
+ // points to. And save the file_path and layout to local state. |
+ NOTIMPLEMENTED(); |
+ this->Release(); |
+} |
+ |
+void WallpaperManagerSetWallpaperFunction::OnURLFetchDownloadProgress( |
+ const net::URLFetcher* source, |
+ int64 current, |
+ int64 total) { |
+ if (source == fetcher_.get()) { |
+ if (current >= bytes_wallpaper_download_progress_last_reported_ + |
+ kBytesWallpaperDownloadProgressReportInterval) { |
+ bytes_wallpaper_download_progress_last_reported_ = current; |
+ base::TimeDelta time_remaining; |
+ if (current > 0) { |
+ const base::TimeDelta diff = |
+ base::TimeTicks::Now() - tick_wallpaper_download_start_; |
+ time_remaining = diff*(total - current)/current; |
+ |
+ DictionaryValue* dict = new DictionaryValue(); |
+ dict->SetInteger("progress", time_remaining.InMilliseconds()); |
+ ListValue args; |
+ args.Append(dict); |
+ std::string json_args; |
+ base::JSONWriter::Write(&args, &json_args); |
+ profile_->GetExtensionEventRouter()->DispatchEventToExtension( |
+ std::string(kWallpaperManagerDomain), |
+ events::kDownloadProgressEvent, |
+ json_args, profile_, GURL()); |
+ } |
+ } |
+ } |
+} |
+ |
+void WallpaperManagerSetWallpaperFunction::RequestOnFileThread( |
+ const std::string& source_url) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
+ CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir_)); |
+ |
+ GURL wallpaper_url(source_url); |
+ if (!file_util::DirectoryExists(wallpaper_dir_) && |
+ !file_util::CreateDirectory(wallpaper_dir_)) { |
+ DispatchErrorEvent(WALLPAPER_FILE_ERROR); |
+ return; |
+ } |
+ |
+ FilePath file_path = wallpaper_dir_.Append(wallpaper_url.ExtractFileName() + |
+ ".jpg"); |
+ |
+ // If the wallpaper already downloaded, uses it directly. |
+ if (file_util::PathExists(file_path)) { |
+ // TODO(bshe): Call function to set wallpaper to the image which file_path |
+ // points to. And save the file_path and layout to local state. |
+ NOTIMPLEMENTED(); |
+ return; |
+ } |
+ |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ base::Bind( |
+ &WallpaperManagerSetWallpaperFunction::SetupFetcherOnUIThread, |
+ this, |
+ wallpaper_url)); |
+} |
+ |
+void WallpaperManagerSetWallpaperFunction::SetupFetcherOnUIThread( |
+ const GURL& wallpaper_url) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ tick_wallpaper_download_start_ = base::TimeTicks::Now(); |
+ bytes_wallpaper_download_progress_last_reported_ = 0; |
+ |
+ // If the wallpaper already downloaded, uses it directly. |
+ FilePath file_path = wallpaper_dir_.Append(wallpaper_url.ExtractFileName() + |
+ ".jpg"); |
+ |
+ this->AddRef(); |
bshe
2012/07/10 02:03:58
Here I use a hack to make it work. Do you have any
|
+ fetcher_.reset(net::URLFetcher::Create(wallpaper_url, |
+ net::URLFetcher::GET, |
+ this)); |
+ fetcher_->SetRequestContext( |
+ g_browser_process->system_request_context()); |
+ |
+ fetcher_->SaveResponseToFileAtPath( |
+ file_path, |
+ BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); |
+ fetcher_->Start(); |
+} |
+ |
+WallpaperErrorCode WallpaperManagerSetWallpaperFunction::GetErrorCode( |
+ const net::URLFetcher* source) const { |
+ WallpaperErrorCode code = static_cast<WallpaperErrorCode>( |
+ source->GetResponseCode()); |
+ if (code == HTTP_SUCCESS && !source->GetStatus().is_success()) { |
+ // If the HTTP response code is SUCCESS yet the URL request failed, it is |
+ // likely that the failure is due to loss of connection. |
+ code = WALLPAPER_NO_CONNECTION; |
+ } |
+ return code; |
+} |
+ |
+void WallpaperManagerSetWallpaperFunction::DispatchErrorEvent( |
+ WallpaperErrorCode code) { |
+ DictionaryValue* dict = new DictionaryValue(); |
+ dict->SetString("message", |
+ l10n_util::GetStringUTF16(IDS_WALLPAPER_MANAGER_SEARCH_TEXT_LABEL)); |
+ ListValue args; |
+ args.Append(dict); |
+ std::string json_args; |
+ base::JSONWriter::Write(&args, &json_args); |
+ profile_->GetExtensionEventRouter()->DispatchEventToExtension( |
+ std::string(kWallpaperManagerDomain), |
+ events::kDownloadErrorEvent, |
+ json_args, profile_, GURL()); |
+} |
+ |
+bool WallpaperManagerSetWallpaperFunction::CreateDirectory(const FilePath& path) { |
+ bool success = false; |
+ if (!file_util::DirectoryExists(path)) |
+ success = file_util::CreateDirectory(path); |
+ else |
+ success = true; |
+ return success; |
+} |