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..f708a41def94a462c3677d1a30a0bc1d5a3327ae 100644 |
--- a/chrome/browser/chromeos/extensions/wallpaper_manager_api.cc |
+++ b/chrome/browser/chromeos/extensions/wallpaper_manager_api.cc |
@@ -4,27 +4,52 @@ |
#include "chrome/browser/chromeos/extensions/wallpaper_manager_api.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/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" |
#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 +71,226 @@ 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()); |
flackr
2012/07/12 19:26:12
Perhaps swap the order of these and result_.reset(
bshe
2012/07/13 19:28:57
Done. Seems result_ is removed and SetResult is us
|
+ |
+#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::WallpaperFetcher::WallpaperFetcher( |
+ scoped_refptr<WallpaperManagerSetWallpaperFunction> function) |
+ : function_(function) { |
+} |
+ |
+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); |
+} |
+ |
+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::GetLayoutFromString(layout_string); |
+ |
+ // 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); |
+ 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"); |
flackr
2012/07/12 19:26:12
Can we copy the extension from the source file rat
bshe
2012/07/13 19:28:57
Removed the extension name since it's unnecessary
|
+ |
+ chromeos::WallpaperManager::Get()->SetWallpaperFromFile(email_, |
+ path.value(), |
+ layout_); |
+} |
+ |
+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("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. |
flackr
2012/07/12 19:26:12
// If the wallpaper was already downloaded it, use
bshe
2012/07/13 19:28:57
Done.
|
+ if (file_util::PathExists(file_path)) { |
+ chromeos::WallpaperManager::Get()->SetWallpaperFromFile(email_, |
+ file_path.value(), |
+ layout_); |
+ 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. |
flackr
2012/07/12 19:26:12
This comment doesn't seem to correspond to the cod
bshe
2012/07/13 19:28:57
Removed.
|
+ FilePath file_path = wallpaper_dir_.Append(wallpaper_url.ExtractFileName() + |
+ ".jpg"); |
+ |
+ fetcher_.reset(new WallpaperFetcher(this)); |
+ fetcher_->Start(wallpaper_url, file_path); |
+} |
+ |
+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) { |
flackr
2012/07/12 19:26:12
Are you eventually going to do something with this
bshe
2012/07/13 19:28:57
Yes. I am going to listen to the dispatched event
|
+ DictionaryValue* dict = new DictionaryValue(); |
+ dict->SetString("message", |
+ l10n_util::GetStringUTF16(IDS_WALLPAPER_MANAGER_DOWNLOAD_ERROR_MESSAGE)); |
+ 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)) |
flackr
2012/07/12 19:26:12
You already check this before calling CreateDirect
bshe
2012/07/13 19:28:57
You are absolutely right. This function actually w
|
+ success = file_util::CreateDirectory(path); |
+ else |
+ success = true; |
+ return success; |
+} |