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 89cdcc3a7752beb64ccfeb283a2cb7652b684f47..79d138c8277fb66a3b5cf56dcf057d7ab6fe61c5 100644 |
--- a/chrome/browser/chromeos/extensions/wallpaper_manager_api.cc |
+++ b/chrome/browser/chromeos/extensions/wallpaper_manager_api.cc |
@@ -6,9 +6,12 @@ |
#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/chromeos/login/wallpaper_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" |
@@ -20,11 +23,62 @@ |
#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 content::BrowserThread; |
const char kWallpaperManagerDomain[] = "obklkkbkpaoaejdabbfldmcfplpdgolj"; |
+namespace { |
+ |
+std::string GetIDFromCode(WallpaperErrorCode code) { |
+ switch (code) { |
+ case HTTP_BAD_REQUEST: |
+ return "HTTP_BAD_REQUEST"; |
+ case HTTP_UNAUTHORIZED: |
+ return "HTTP_UNAUTHORIZED"; |
+ case HTTP_FORBIDDEN: |
+ return "HTTP_FORBIDDEN"; |
+ case HTTP_NOT_FOUND: |
+ return "HTTP_NOT_FOUND"; |
+ case HTTP_CONFLICT: |
+ return "HTTP_CONFLICT"; |
+ case HTTP_LENGTH_REQUIRED: |
+ return "HTTP_LENGTH_REQUIRED"; |
+ case HTTP_PRECONDITION: |
+ return "HTTP_PRECONDITION"; |
+ case HTTP_INTERNAL_SERVER_ERROR: |
+ return "HTTP_INTERNAL_SERVER_ERROR"; |
+ case HTTP_SERVICE_UNAVAILABLE: |
+ return "HTTP_SERVICE_UNAVAILABLE"; |
+ case WALLPAPER_NO_CONNECTION: |
+ return "WALLPAPER_NO_CONNECTION"; |
+ case WALLPAPER_FILE_ERROR: |
+ return "WALLPAPER_FILE_ERROR"; |
+ default: |
+ return "DOWNLOAD_DEFAULT_ERROR"; |
+ } |
+} |
+ |
+} // namespace |
+ |
+namespace events { |
+ |
+ const char kDownloadStartEvent[] = |
+ "wallpaperManagerPrivate.onDownloadStart"; |
+ const char kDownloadProgressEvent[] = |
+ "wallpaperManagerPrivate.onDownloadProgress"; |
+ const char kDownloadDoneEvent[] = |
+ "wallpaperManagerPrivate.onDownloadDone"; |
+ const char kDownloadErrorEvent[] = |
+ "wallpaperManagerPrivate.onDownloadError"; |
+ |
+} // namespace events |
+ |
namespace wallpaper_manager_util { |
void OpenWallpaperManager() { |
@@ -49,9 +103,256 @@ void OpenWallpaperManager() { |
application_launch::OpenApplication(params); |
} else { |
Browser* browser = browser::FindOrCreateTabbedBrowser( |
- ProfileManager::GetDefaultProfileOrOffTheRecord()); |
+ ProfileManager::GetDefaultProfileOrOffTheRecord()); |
chrome::ShowSettingsSubPage(browser, "setWallpaper"); |
} |
} |
-} // namespace wallpaper_manager_util |
+} //namespace wallpaper_manager_util |
+ |
+bool WallpaperManagerStringsFunction::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_WALLPAPER_MANAGER, HTTP_BAD_REQUEST); |
+ SET_STRING(IDS_WALLPAPER_MANAGER, HTTP_UNAUTHORIZED); |
+ SET_STRING(IDS_WALLPAPER_MANAGER, HTTP_FORBIDDEN); |
+ SET_STRING(IDS_WALLPAPER_MANAGER, HTTP_NOT_FOUND); |
+ SET_STRING(IDS_WALLPAPER_MANAGER, HTTP_CONFLICT); |
+ SET_STRING(IDS_WALLPAPER_MANAGER, HTTP_LENGTH_REQUIRED); |
+ SET_STRING(IDS_WALLPAPER_MANAGER, HTTP_PRECONDITION); |
+ SET_STRING(IDS_WALLPAPER_MANAGER, HTTP_INTERNAL_SERVER_ERROR); |
+ SET_STRING(IDS_WALLPAPER_MANAGER, HTTP_SERVICE_UNAVAILABLE); |
+ SET_STRING(IDS_WALLPAPER_MANAGER, WALLPAPER_NO_CONNECTION); |
+ SET_STRING(IDS_WALLPAPER_MANAGER, WALLPAPER_FILE_ERROR); |
+ SET_STRING(IDS_WALLPAPER_MANAGER, DOWNLOAD_DEFAULT_ERROR); |
+ SET_STRING(IDS_OPTIONS, SET_WALLPAPER_DAILY); |
+#undef SET_STRING |
+ |
+ ChromeURLDataManager::DataSource::SetFontAndTextDirection(dict); |
+ |
+ return true; |
+} |
+ |
+// static |
+WallpaperManagerSetWallpaperFunction::WallpaperFetcher* |
+ WallpaperManagerSetWallpaperFunction::fetcher_; |
+ |
+WallpaperManagerSetWallpaperFunction::WallpaperFetcher::WallpaperFetcher( |
+ scoped_refptr<WallpaperManagerSetWallpaperFunction> function) |
+ : function_(function) { |
+} |
+ |
+void WallpaperManagerSetWallpaperFunction::WallpaperFetcher::Cancel() { |
+ fetcher_.reset(); |
+ delete this; |
+} |
+ |
+void WallpaperManagerSetWallpaperFunction::WallpaperFetcher::Start( |
+ const GURL& wallpaper_url, const FilePath& file_path) { |
+ 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(); |
+} |
+ |
+void WallpaperManagerSetWallpaperFunction::WallpaperFetcher::OnURLFetchComplete( |
+ const net::URLFetcher* source) { |
+ function_->OnDownloadComplete(source); |
+ delete this; |
+} |
+ |
+void WallpaperManagerSetWallpaperFunction::WallpaperFetcher |
+ ::OnURLFetchDownloadProgress(const net::URLFetcher* source, |
+ int64 current, |
+ int64 total) { |
+ if (source == fetcher_.get()) |
+ function_->OnDownloadProgress(source, current, total); |
+} |
+ |
+WallpaperManagerSetWallpaperFunction::~WallpaperManagerSetWallpaperFunction() { |
+} |
+ |
+bool WallpaperManagerSetWallpaperFunction::RunImpl() { |
+ // First param is url of the selected wallpaper. |
+ std::string url; |
+ if (!args_->GetString(0, &url) || url.empty()) |
+ return false; |
+ // Second param is the default layout of the selected wallpaper. |
+ std::string layout_string; |
+ if (!args_->GetString(1, &layout_string) || layout_string.empty()) { |
+ return false; |
+ } |
+ layout_ = ash::GetLayoutEnum(layout_string.c_str()); |
+ |
+ // Gets email address while at UI thread. |
+ email_ = chromeos::UserManager::Get()->GetLoggedInUser().email(); |
+ |
+ BrowserThread::PostTask( |
+ BrowserThread::FILE, FROM_HERE, |
+ base::Bind( |
+ &WallpaperManagerSetWallpaperFunction::RequestOnFileThread, |
+ this, url)); |
+ // Will finish asynchronously. |
+ return true; |
+} |
+ |
+void WallpaperManagerSetWallpaperFunction::OnDownloadComplete( |
+ const net::URLFetcher* source) { |
+ WallpaperErrorCode code = GetErrorCode(source); |
+ if (code != HTTP_SUCCESS) { |
+ DispatchErrorEvent(code); |
+ fetcher_ = NULL; |
+ return; |
+ } |
+ FilePath temp_file; |
+ if (code == HTTP_SUCCESS && |
+ !source->GetResponseAsFilePath(true, &temp_file)) { |
+ code = WALLPAPER_FILE_ERROR; |
+ DispatchErrorEvent(code); |
+ fetcher_ = NULL; |
+ return; |
+ } |
+ |
+ profile_->GetExtensionEventRouter()->DispatchEventToExtension( |
+ std::string(kWallpaperManagerDomain), |
+ events::kDownloadDoneEvent, |
+ "[]", profile_, GURL()); |
+ |
+ FilePath path = wallpaper_dir_.Append( |
+ source->GetOriginalURL().ExtractFileName()); |
+ |
+ chromeos::WallpaperManager::Get()->SetWallpaperFromFile(email_, |
+ path.value(), |
+ layout_); |
+ |
+ fetcher_ = NULL; |
+} |
+ |
+void WallpaperManagerSetWallpaperFunction::OnDownloadProgress( |
+ const net::URLFetcher* source, |
+ int64 current, |
+ int64 total) { |
+ 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("remainingTime", time_remaining.InMilliseconds()); |
+ dict->SetDouble("progress", static_cast<double>(current) / total); |
+ 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()); |
+ |
+ // If the wallpaper was already downloaded, use it immediately. |
+ if (file_util::PathExists(file_path)) { |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ base::Bind( |
+ &WallpaperManagerSetWallpaperFunction::SetWallpaperOnUIThread, |
+ this, |
+ file_path)); |
+ return; |
+ } |
+ |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ base::Bind( |
+ &WallpaperManagerSetWallpaperFunction::SetupFetcherOnUIThread, |
+ this, |
+ wallpaper_url)); |
+} |
+ |
+void WallpaperManagerSetWallpaperFunction::SetWallpaperOnUIThread( |
+ const FilePath& file_path) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ chromeos::WallpaperManager::Get()->SetWallpaperFromFile(email_, |
+ file_path.value(), |
+ layout_); |
+} |
+ |
+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 (fetcher_) |
+ fetcher_->Cancel(); |
+ fetcher_ = new WallpaperFetcher(this); |
+ FilePath file_path = wallpaper_dir_.Append(wallpaper_url.ExtractFileName()); |
+ fetcher_->Start(wallpaper_url, file_path); |
+ |
+ profile_->GetExtensionEventRouter()->DispatchEventToExtension( |
+ std::string(kWallpaperManagerDomain), |
+ events::kDownloadStartEvent, |
+ "[]", profile_, GURL()); |
+} |
+ |
+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("errorMessageID", GetIDFromCode(code)); |
+ 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()); |
+} |