Commit 6d33da17 authored by sail@chromium.org's avatar sail@chromium.org

Make ProfileImageDownloader available to non-chromeos code

BUG=91241
TEST=


Review URL: http://codereview.chromium.org/8510069

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@111098 0039d316-1c4b-4281-b951-d872f2087c98
parent 0de5d860
......@@ -8,7 +8,7 @@
#include <string>
#include "chrome/browser/chromeos/login/image_decoder.h"
#include "chrome/browser/image_decoder.h"
#include "googleurl/src/gurl.h"
#include "ui/views/window/dialog_delegate.h"
#include "views/controls/button/button.h"
......
......@@ -10,7 +10,7 @@
#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/chromeos/login/image_decoder.h"
#include "chrome/browser/image_decoder.h"
#include "content/public/common/url_fetcher_delegate.h"
#include "googleurl/src/gurl.h"
......
......@@ -30,6 +30,7 @@
#include "chrome/common/net/gaia/gaia_auth_consumer.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_pref_service.h"
#include "content/public/common/url_fetcher_delegate.h"
#include "content/test/test_browser_thread.h"
#include "content/test/test_url_fetcher_factory.h"
#include "net/url_request/url_request.h"
......
......@@ -9,7 +9,6 @@
#include "base/file_util.h"
#include "base/message_loop.h"
#include "chrome/browser/chromeos/login/helper.h"
#include "chrome/browser/chromeos/login/image_decoder.h"
#include "content/public/browser/browser_thread.h"
#include "skia/ext/image_operations.h"
#include "third_party/skia/include/core/SkBitmap.h"
......
......@@ -11,7 +11,7 @@
#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "chrome/browser/chromeos/login/image_decoder.h"
#include "chrome/browser/image_decoder.h"
class MessageLoop;
class SkBitmap;
......
......@@ -36,6 +36,8 @@
#include "chrome/browser/defaults.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/prefs/scoped_user_pref_update.h"
#include "chrome/browser/profiles/profile_downloader.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/webui/web_ui_util.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_paths.h"
......@@ -85,6 +87,17 @@ const long kProfileImageDownloadDelayMs = 10000;
base::LazyInstance<UserManager> g_user_manager = LAZY_INSTANCE_INITIALIZER;
// Enum for reporting histograms about profile picture download.
enum ProfileDownloadResult {
kDownloadSuccessChanged,
kDownloadSuccess,
kDownloadFailure,
kDownloadDefault,
// Must be the last, convenient count.
kDownloadResultsCount
};
// Used to handle the asynchronous response of deleting a cryptohome directory.
class RemoveAttempt : public CryptohomeLibrary::Delegate {
public:
......@@ -454,7 +467,13 @@ void UserManager::DownloadProfileImage() {
// Another download is already in progress
return;
}
profile_image_downloader_.reset(new ProfileImageDownloader(this));
if (logged_in_user().email().empty()) {
// This is a guest login so there's no profile image to download.
return;
}
profile_image_downloader_.reset(new ProfileDownloader(this));
profile_image_downloader_->Start();
profile_image_load_start_time_ = base::Time::Now();
}
......@@ -789,65 +808,65 @@ void UserManager::CheckOwnership() {
is_owner));
}
void UserManager::OnDownloadSuccess(const SkBitmap& image) {
VLOG(1) << "Downloaded profile image for logged-in user.";
UMA_HISTOGRAM_ENUMERATION("UserImageDownloadResult.LoggedIn",
ProfileImageDownloader::kDownloadSuccess,
ProfileImageDownloader::kDownloadResultsCount);
int UserManager::GetDesiredImageSideLength() {
return login::kUserImageSize;
}
// Check if this image is not the same as already downloaded.
std::string new_image_data_url = web_ui_util::GetImageDataUrl(image);
if (!downloaded_profile_image_data_url_.empty() &&
new_image_data_url == downloaded_profile_image_data_url_)
return;
Profile* UserManager::GetBrowserProfile() {
return ProfileManager::GetDefaultProfile();
}
downloaded_profile_image_data_url_ = new_image_data_url;
downloaded_profile_image_ = image;
void UserManager::OnDownloadComplete(ProfileDownloader* downloader,
bool success) {
ProfileDownloadResult result;
if (!success)
result = kDownloadFailure;
else if (downloader->GetProfilePicture().isNull())
result = kDownloadDefault;
else
result = kDownloadSuccess;
UMA_HISTOGRAM_ENUMERATION("UserImage.ProfileDownloadResult",
result, kDownloadResultsCount);
if (logged_in_user().image_index() == User::kProfileImageIndex) {
std::string current_image_data_url =
web_ui_util::GetImageDataUrl(logged_in_user().image());
if (current_image_data_url == new_image_data_url)
if (result == kDownloadSuccess) {
// Check if this image is not the same as already downloaded.
std::string new_image_data_url =
web_ui_util::GetImageDataUrl(downloader->GetProfilePicture());
if (!downloaded_profile_image_data_url_.empty() &&
new_image_data_url == downloaded_profile_image_data_url_)
return;
VLOG(1) << "Updating profile image for logged-in user";
UMA_HISTOGRAM_ENUMERATION("UserImageDownloadResult.LoggedIn",
ProfileImageDownloader::kDownloadSuccessChanged,
ProfileImageDownloader::kDownloadResultsCount);
downloaded_profile_image_data_url_ = new_image_data_url;
downloaded_profile_image_ = downloader->GetProfilePicture();
// This will persist |downloaded_profile_image_| to file.
SaveUserImageFromProfileImage(logged_in_user().email());
}
if (logged_in_user().image_index() == User::kProfileImageIndex) {
std::string current_image_data_url =
web_ui_util::GetImageDataUrl(logged_in_user().image());
if (current_image_data_url == new_image_data_url)
return;
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_PROFILE_IMAGE_UPDATED,
content::Source<UserManager>(this),
content::Details<const SkBitmap>(&image));
VLOG(1) << "Updating profile image for logged-in user";
UMA_HISTOGRAM_ENUMERATION("UserImage.ProfileDownloadResult",
kDownloadSuccessChanged,
kDownloadResultsCount);
profile_image_downloader_.reset();
}
// This will persist |downloaded_profile_image_| to file.
SaveUserImageFromProfileImage(logged_in_user().email());
}
}
void UserManager::OnDownloadFailure() {
VLOG(1) << "Download of profile image for logged-in user failed.";
UMA_HISTOGRAM_ENUMERATION("UserImageDownloadResult.LoggedIn",
ProfileImageDownloader::kDownloadFailure,
ProfileImageDownloader::kDownloadResultsCount);
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_PROFILE_IMAGE_UPDATE_FAILED,
content::Source<UserManager>(this),
content::NotificationService::NoDetails());
profile_image_downloader_.reset();
}
if (result == kDownloadSuccess) {
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_PROFILE_IMAGE_UPDATED,
content::Source<UserManager>(this),
content::Details<const SkBitmap>(&downloader->GetProfilePicture()));
} else {
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_PROFILE_IMAGE_UPDATE_FAILED,
content::Source<UserManager>(this),
content::NotificationService::NoDetails());
}
void UserManager::OnDownloadDefaultImage() {
VLOG(1) << "Logged-in user still has the default profile image.";
UMA_HISTOGRAM_ENUMERATION("UserImageDownloadResult.LoggedIn",
ProfileImageDownloader::kDownloadDefault,
ProfileImageDownloader::kDownloadResultsCount);
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_PROFILE_IMAGE_UPDATE_FAILED,
content::Source<UserManager>(this),
content::NotificationService::NoDetails());
profile_image_downloader_.reset();
}
......
......@@ -14,15 +14,16 @@
#include "base/observer_list.h"
#include "base/synchronization/lock.h"
#include "base/time.h"
#include "chrome/browser/chromeos/login/profile_image_downloader.h"
#include "chrome/browser/chromeos/login/user.h"
#include "chrome/browser/chromeos/login/user_image_loader.h"
#include "chrome/browser/profiles/profile_downloader_delegate.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "third_party/skia/include/core/SkBitmap.h"
class FilePath;
class PrefService;
class ProfileDownloader;
namespace base {
template<typename> struct DefaultLazyInstanceTraits;
......@@ -34,7 +35,7 @@ class RemoveUserDelegate;
// This class provides a mechanism for discovering users who have logged
// into this chromium os device before and updating that list.
class UserManager : public ProfileImageDownloader::Delegate,
class UserManager : public ProfileDownloaderDelegate,
public content::NotificationObserver {
public:
// Returns a shared instance of a UserManager. Not thread-safe, should only be
......@@ -210,10 +211,11 @@ class UserManager : public ProfileImageDownloader::Delegate,
// Checks current user's ownership on file thread.
void CheckOwnership();
// ProfileImageDownloader::Delegate implementation.
virtual void OnDownloadSuccess(const SkBitmap& image) OVERRIDE;
virtual void OnDownloadFailure() OVERRIDE;
virtual void OnDownloadDefaultImage() OVERRIDE;
// ProfileDownloaderDelegate implementation.
virtual int GetDesiredImageSideLength() OVERRIDE;
virtual Profile* GetBrowserProfile() OVERRIDE;
virtual void OnDownloadComplete(ProfileDownloader* downloader,
bool success) OVERRIDE;
// Creates a new User instance.
User* CreateUser(const std::string& email) const;
......@@ -259,7 +261,7 @@ class UserManager : public ProfileImageDownloader::Delegate,
ObserverList<Observer> observer_list_;
// Download user profile image on login to update it if it's changed.
scoped_ptr<ProfileImageDownloader> profile_image_downloader_;
scoped_ptr<ProfileDownloader> profile_image_downloader_;
// Time when the profile image download has started.
base::Time profile_image_load_start_time_;
......
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/login/image_decoder.h"
#include "chrome/browser/image_decoder.h"
#include "base/bind.h"
#include "chrome/browser/browser_process.h"
......@@ -11,8 +11,6 @@
using content::BrowserThread;
namespace chromeos {
ImageDecoder::ImageDecoder(Delegate* delegate,
const std::string& image_data)
: delegate_(delegate),
......@@ -64,5 +62,3 @@ void ImageDecoder::DecodeImageInSandbox(
target_thread_id_);
utility_process_host->Send(new ChromeUtilityMsg_DecodeImage(image_data));
}
} // namespace chromeos
......@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_CHROMEOS_LOGIN_IMAGE_DECODER_H_
#define CHROME_BROWSER_CHROMEOS_LOGIN_IMAGE_DECODER_H_
#ifndef CHROME_BROWSER_IMAGE_DECODER_H_
#define CHROME_BROWSER_IMAGE_DECODER_H_
#pragma once
#include <string>
......@@ -13,8 +13,6 @@
class SkBitmap;
namespace chromeos {
// Decodes an image in a sandboxed process.
class ImageDecoder : public UtilityProcessHost::Client {
public:
......@@ -61,6 +59,4 @@ class ImageDecoder : public UtilityProcessHost::Client {
DISALLOW_COPY_AND_ASSIGN(ImageDecoder);
};
} // namespace chromeos
#endif // CHROME_BROWSER_CHROMEOS_LOGIN_IMAGE_DECODER_H_
#endif // CHROME_BROWSER_IMAGE_DECODER_H_
......@@ -2,63 +2,47 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_CHROMEOS_LOGIN_PROFILE_IMAGE_DOWNLOADER_H_
#define CHROME_BROWSER_CHROMEOS_LOGIN_PROFILE_IMAGE_DOWNLOADER_H_
#ifndef CHROME_BROWSER_PROFILES_PROFILE_DOWNLOADER_H_
#define CHROME_BROWSER_PROFILES_PROFILE_DOWNLOADER_H_
#pragma once
#include <string>
#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/chromeos/login/image_decoder.h"
#include "base/string16.h"
#include "chrome/browser/image_decoder.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/common/url_fetcher_delegate.h"
#include "googleurl/src/gurl.h"
#include "third_party/skia/include/core/SkBitmap.h"
namespace chromeos {
class ProfileDownloaderDelegate;
// Downloads user profile image, decodes it in a sandboxed process.
class ProfileImageDownloader : public content::URLFetcherDelegate,
public ImageDecoder::Delegate,
public content::NotificationObserver {
// Downloads user profile information. The profile picture is decoded in a
// sandboxed process.
class ProfileDownloader : public content::URLFetcherDelegate,
public ImageDecoder::Delegate,
public content::NotificationObserver {
public:
// Enum for reporting histograms about profile picture download.
enum DownloadResult {
kDownloadSuccessChanged,
kDownloadSuccess,
kDownloadFailure,
kDownloadDefault,
// Must be the last, convenient count.
kDownloadResultsCount
};
// Reports on success or failure of Profile image download. It is OK to
// delete the |ProfileImageDownloader| instance in any of these handlers.
class Delegate {
public:
virtual ~Delegate() {}
// Called when image is successfully downloaded and decoded.
virtual void OnDownloadSuccess(const SkBitmap& image) = 0;
// Called on download failure.
virtual void OnDownloadFailure() {}
// Called when user has the default profile image and we won't download
// it.
virtual void OnDownloadDefaultImage() {}
};
explicit ProfileImageDownloader(Delegate* delegate);
virtual ~ProfileImageDownloader();
// Starts downloading the picture if the necessary authorization token is
// ready. If not, subscribes to token service and starts fetching if the
explicit ProfileDownloader(ProfileDownloaderDelegate* delegate);
virtual ~ProfileDownloader();
// Starts downloading profile information if the necessary authorization token
// is ready. If not, subscribes to token service and starts fetching if the
// token is available. Should not be called more than once.
void Start();
// On successful download this returns the full name of the user. For example
// "Pat Smith".
const string16& GetProfileFullName() const;
// On successful download this returns the profile picture of the user.
// For users with no profile picture set (that is, they have the default
// profile picture) this will return an Null bitmap.
const SkBitmap& GetProfilePicture() const;
private:
// Overriden from content::URLFetcherDelegate:
virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE;
......@@ -73,9 +57,11 @@ class ProfileImageDownloader : public content::URLFetcherDelegate,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
// Searches for profile image URL in user entry response from Picasa.
// Returns an empty string on failure.
std::string GetProfileImageURL(const std::string& data) const;
// Parses the entry response from Picasa and gets the nick name and
// and profile image URL. Returns false to indicate a parsing error.
bool GetProfileNickNameAndImageURL(const std::string& data,
string16* nick_name,
std::string* url) const;
// Returns true if the image url is url of the default profile picture.
bool IsDefaultProfileImageURL(const std::string& url) const;
......@@ -83,15 +69,15 @@ class ProfileImageDownloader : public content::URLFetcherDelegate,
// Issues the first request to get user profile image.
void StartFetchingImage();
Delegate* delegate_;
ProfileDownloaderDelegate* delegate_;
std::string auth_token_;
scoped_ptr<content::URLFetcher> user_entry_fetcher_;
scoped_ptr<content::URLFetcher> profile_image_fetcher_;
content::NotificationRegistrar registrar_;
string16 profile_full_name_;
SkBitmap profile_picture_;
DISALLOW_COPY_AND_ASSIGN(ProfileImageDownloader);
DISALLOW_COPY_AND_ASSIGN(ProfileDownloader);
};
} // namespace chromeos
#endif // CHROME_BROWSER_CHROMEOS_LOGIN_PROFILE_IMAGE_DOWNLOADER_H_
#endif // CHROME_BROWSER_PROFILES_PROFILE_DOWNLOADER_H_
// Copyright (c) 2011 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.
#ifndef CHROME_BROWSER_PROFILES_PROFILE_DOWNLOADER_DELEGATE_H_
#define CHROME_BROWSER_PROFILES_PROFILE_DOWNLOADER_DELEGATE_H_
#pragma once
#include <string>
#include "base/basictypes.h"
#include "base/string16.h"
class Profile;
class ProfileDownloader;
// Reports on success or failure of Profile download. It is OK to delete the
// |ProfileImageDownloader| instance in any of these handlers.
class ProfileDownloaderDelegate {
public:
virtual ~ProfileDownloaderDelegate() {}
// Returns the desired side length of the profile image. If 0, returns image
// of the originally uploaded size.
virtual int GetDesiredImageSideLength() = 0;
// Returns the browser profile associated with this download request.
virtual Profile* GetBrowserProfile() = 0;
// Called when the download is complete. On success delegate should query
// the downloader for values.
virtual void OnDownloadComplete(ProfileDownloader* downloader,
bool success) = 0;
};
#endif // CHROME_BROWSER_PROFILES_PROFILE_DOWNLOADER_DELEGATE_H_
......@@ -570,8 +570,6 @@
'browser/chromeos/login/helper.h',
'browser/chromeos/login/html_page_screen.cc',
'browser/chromeos/login/html_page_screen.h',
'browser/chromeos/login/image_decoder.cc',
'browser/chromeos/login/image_decoder.h',
'browser/chromeos/login/image_downloader.cc',
'browser/chromeos/login/image_downloader.h',
'browser/chromeos/login/issue_response_handler.cc',
......@@ -613,8 +611,6 @@
'browser/chromeos/login/parallel_authenticator.h',
'browser/chromeos/login/password_changed_view.cc',
'browser/chromeos/login/password_changed_view.h',
'browser/chromeos/login/profile_image_downloader.cc',
'browser/chromeos/login/profile_image_downloader.h',
'browser/chromeos/login/proxy_settings_dialog.cc',
'browser/chromeos/login/proxy_settings_dialog.h',
'browser/chromeos/login/registration_screen.cc',
......@@ -1394,6 +1390,8 @@
'browser/idle_query_linux.cc',
'browser/idle_query_linux.h',
'browser/idle_win.cc',
'browser/image_decoder.cc',
'browser/image_decoder.h',
'browser/importer/external_process_importer_bridge.cc',
'browser/importer/external_process_importer_bridge.h',
'browser/importer/external_process_importer_client.cc',
......@@ -1952,6 +1950,9 @@
'browser/profiles/profile.h',
'browser/profiles/profile_dependency_manager.cc',
'browser/profiles/profile_dependency_manager.h',
'browser/profiles/profile_downloader.cc',
'browser/profiles/profile_downloader.h',
'browser/profiles/profile_downloader_delegate.h',
'browser/profiles/profile_impl.cc',
'browser/profiles/profile_impl.h',
'browser/profiles/profile_impl_io_data.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment