summaryrefslogtreecommitdiffstats
path: root/chrome/browser/views
diff options
context:
space:
mode:
authorxiyuan@chromium.org <xiyuan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-12-11 06:13:51 +0000
committerxiyuan@chromium.org <xiyuan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-12-11 06:13:51 +0000
commiteabfdae9101aa33a212fdaf240d8e0af70874e16 (patch)
tree4b27ea5ae52ce96ccfaab87960d067fc1b58968c /chrome/browser/views
parentffb01d3e998a32156790af7c1367d733bb9e75dd (diff)
downloadchromium_src-eabfdae9101aa33a212fdaf240d8e0af70874e16.zip
chromium_src-eabfdae9101aa33a212fdaf240d8e0af70874e16.tar.gz
chromium_src-eabfdae9101aa33a212fdaf240d8e0af70874e16.tar.bz2
Get web app icon via FavIconHelper and auto repair/update
- Expose a DownloadImage method from FavIconHelper to download/decode image for an image url; - Expose FavIconHelper from TabContents; - Update CreateApplicationShortcutView to use the exposed DownloadImage method to get web app icon instead of do it via URLFetcher/PNGCodec; - Check and update web app icon and shortcuts when chrome is lauched as app for OS_WIN; - Code cleanup: - Move a bunch of FavIconHelper methods that are not used externally to private; - Remove an unused cancelable_consumer_ from TabContents; BUG=8539 TEST=Verify issue 8539 is fixed. And create a web page with a non PNG shortcut icon and verify it shows up in create application shortcut dialog. Review URL: http://codereview.chromium.org/482003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@34332 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/views')
-rw-r--r--chrome/browser/views/create_application_shortcut_view.cc131
-rw-r--r--chrome/browser/views/create_application_shortcut_view.h41
2 files changed, 64 insertions, 108 deletions
diff --git a/chrome/browser/views/create_application_shortcut_view.cc b/chrome/browser/views/create_application_shortcut_view.cc
index 0736d55..6551d15 100644
--- a/chrome/browser/views/create_application_shortcut_view.cc
+++ b/chrome/browser/views/create_application_shortcut_view.cc
@@ -35,12 +35,6 @@ namespace {
const int kAppIconSize = 32;
-bool IconPrecedes(
- const webkit_glue::WebApplicationInfo::IconInfo& left,
- const webkit_glue::WebApplicationInfo::IconInfo& right) {
- return left.width < right.width;
-}
-
// AppInfoView shows the application icon and title.
class AppInfoView : public views::View {
public:
@@ -203,37 +197,52 @@ void ShowCreateShortcutsDialog(gfx::NativeWindow parent_window,
}; // namespace browser
+class CreateApplicationShortcutView::IconDownloadCallbackFunctor {
+ public:
+ explicit IconDownloadCallbackFunctor(CreateApplicationShortcutView* owner)
+ : owner_(owner) {
+ }
+
+ void Run(int download_id, bool errored, const SkBitmap& image) {
+ if (owner_)
+ owner_->OnIconDownloaded(errored, image);
+ delete this;
+ }
+
+ void Cancel() {
+ owner_ = NULL;
+ }
+
+ private:
+ CreateApplicationShortcutView* owner_;
+};
+
CreateApplicationShortcutView::CreateApplicationShortcutView(
TabContents* tab_contents)
- : tab_contents_(tab_contents) {
+ : tab_contents_(tab_contents),
+ pending_download_(NULL) {
Init();
}
CreateApplicationShortcutView::~CreateApplicationShortcutView() {
+ if (pending_download_)
+ pending_download_->Cancel();
}
void CreateApplicationShortcutView::Init() {
// Prepare data
+ web_app::GetShortcutInfoForTab(tab_contents_, &shortcut_info_);
+
const webkit_glue::WebApplicationInfo& app_info =
tab_contents_->web_app_info();
-
- url_ = app_info.app_url.is_empty() ? tab_contents_->GetURL() :
- app_info.app_url;
- title_ = app_info.title.empty() ? tab_contents_->GetTitle() :
- app_info.title;
- description_ = app_info.description;
-
- icon_ = tab_contents_->GetFavIcon();
if (!app_info.icons.empty()) {
- SetIconsInfo(app_info.icons);
+ web_app::GetIconsInfo(app_info, &unprocessed_icons_);
FetchIcon();
}
- if (title_.empty())
- title_ = UTF8ToUTF16(url_.spec());
-
// Create controls
- app_info_ = new AppInfoView(title_, description_, icon_);
+ app_info_ = new AppInfoView(shortcut_info_.title, shortcut_info_.description,
+ shortcut_info_.favicon);
create_shortcuts_label_ = new views::Label(
l10n_util::GetString(IDS_CREATE_SHORTCUTS_LABEL));
create_shortcuts_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
@@ -359,27 +368,22 @@ bool CreateApplicationShortcutView::Accept() {
return false;
}
- ShellIntegration::ShortcutInfo shortcut_info;
- shortcut_info.url = url_;
- shortcut_info.title = title_;
- shortcut_info.description = description_;
- shortcut_info.favicon = icon_;
- shortcut_info.create_on_desktop = desktop_check_box_->checked();
- shortcut_info.create_in_applications_menu = menu_check_box_ == NULL ? false :
+ shortcut_info_.create_on_desktop = desktop_check_box_->checked();
+ shortcut_info_.create_in_applications_menu = menu_check_box_ == NULL ? false :
menu_check_box_->checked();
#if defined(OS_WIN)
- shortcut_info.create_in_quick_launch_bar = quick_launch_check_box_ == NULL ?
+ shortcut_info_.create_in_quick_launch_bar = quick_launch_check_box_ == NULL ?
NULL : quick_launch_check_box_->checked();
#elif defined(OS_POSIX)
// Create shortcut in Mac dock or as Linux (gnome/kde) application launcher
// are not implemented yet.
- shortcut_info.create_in_quick_launch_bar = false;
+ shortcut_info_.create_in_quick_launch_bar = false;
#endif
- web_app::CreateShortcut(
- tab_contents_->profile()->GetPath().Append(chrome::kWebAppDirname),
- shortcut_info, NULL);
+ web_app::CreateShortcut(web_app::GetDataDir(tab_contents_->profile()),
+ shortcut_info_,
+ NULL);
if (tab_contents_->delegate())
tab_contents_->delegate()->ConvertContentsToApplication(tab_contents_);
@@ -398,39 +402,25 @@ views::Checkbox* CreateApplicationShortcutView::AddCheckbox(
return checkbox;
}
-void CreateApplicationShortcutView::SetIconsInfo(const IconInfoList& icons) {
- unprocessed_icons_.clear();
- for (size_t i = 0; i < icons.size(); ++i) {
- // We only take square shaped icons (i.e. width == height).
- if (icons[i].width == icons[i].height) {
- unprocessed_icons_.push_back(icons[i]);
- }
- }
-
- std::sort(unprocessed_icons_.begin(), unprocessed_icons_.end(),
- &IconPrecedes);
-}
-
void CreateApplicationShortcutView::FetchIcon() {
// There should only be fetch job at a time.
- DCHECK(icon_fetcher_ == NULL);
+ DCHECK(pending_download_ == NULL);
if (unprocessed_icons_.empty()) {
// No icons to fetch.
return;
}
- icon_fetcher_.reset(new URLFetcher(unprocessed_icons_.back().url,
- URLFetcher::GET,
- this));
- DCHECK(icon_fetcher_.get() != NULL);
- unprocessed_icons_.pop_back();
+ pending_download_ = new IconDownloadCallbackFunctor(this);
+ DCHECK(pending_download_);
+
+ tab_contents_->fav_icon_helper().DownloadImage(
+ unprocessed_icons_.back().url,
+ std::max(unprocessed_icons_.back().width,
+ unprocessed_icons_.back().height),
+ NewCallback(pending_download_, &IconDownloadCallbackFunctor::Run));
- icon_fetcher_->set_load_flags(icon_fetcher_->load_flags() |
- net::LOAD_IS_DOWNLOAD);
- icon_fetcher_->set_request_context(
- tab_contents_->profile()->GetRequestContext());
- icon_fetcher_->Start();
+ unprocessed_icons_.pop_back();
}
void CreateApplicationShortcutView::ButtonPressed(views::Button* sender,
@@ -450,27 +440,14 @@ void CreateApplicationShortcutView::ButtonPressed(views::Button* sender,
GetDialogClientView()->UpdateDialogButtons();
}
-void CreateApplicationShortcutView::OnURLFetchComplete(const URLFetcher* source,
- const GURL& url,
- const URLRequestStatus& status,
- int response_code,
- const ResponseCookies& cookies,
- const std::string& data) {
- // Delete the fetcher on this function's exit.
- scoped_ptr<URLFetcher> clean_up_fetcher(icon_fetcher_.release());
-
- bool success = status.is_success() && (response_code == 200) && !data.empty();
-
- if (success) {
- success = gfx::PNGCodec::Decode(
- reinterpret_cast<const unsigned char*>(data.c_str()),
- data.size(),
- &icon_);
-
- if (success)
- static_cast<AppInfoView*>(app_info_)->UpdateIcon(icon_);
- }
+void CreateApplicationShortcutView::OnIconDownloaded(bool errored,
+ const SkBitmap& image) {
+ pending_download_ = NULL;
- if (!success)
+ if (!errored && !image.isNull()) {
+ shortcut_info_.favicon = image;
+ static_cast<AppInfoView*>(app_info_)->UpdateIcon(shortcut_info_.favicon);
+ } else {
FetchIcon();
+ }
}
diff --git a/chrome/browser/views/create_application_shortcut_view.h b/chrome/browser/views/create_application_shortcut_view.h
index 28972ca..8daefd1 100644
--- a/chrome/browser/views/create_application_shortcut_view.h
+++ b/chrome/browser/views/create_application_shortcut_view.h
@@ -8,13 +8,11 @@
#include <string>
#include <vector>
-#include "chrome/browser/net/url_fetcher.h"
#include "chrome/browser/web_applications/web_app.h"
#include "views/controls/label.h"
#include "views/view.h"
#include "views/window/dialog_delegate.h"
#include "third_party/skia/include/core/SkBitmap.h"
-#include "webkit/glue/dom_operations.h"
namespace views {
class Checkbox;
@@ -30,8 +28,7 @@ class TabContents;
// the shortcut for given web app.
class CreateApplicationShortcutView : public views::View,
public views::DialogDelegate,
- public views::ButtonListener,
- public URLFetcher::Delegate {
+ public views::ButtonListener {
public:
explicit CreateApplicationShortcutView(TabContents* tab_contents);
virtual ~CreateApplicationShortcutView();
@@ -59,27 +56,17 @@ class CreateApplicationShortcutView : public views::View,
// Overridden from views::ButtonListener:
virtual void ButtonPressed(views::Button* sender, const views::Event& event);
- // URLFetcher::Delegate
- virtual void OnURLFetchComplete(const URLFetcher* source,
- const GURL& url,
- const URLRequestStatus& status,
- int response_code,
- const ResponseCookies& cookies,
- const std::string& data);
-
private:
- typedef std::vector<webkit_glue::WebApplicationInfo::IconInfo> IconInfoList;
-
// Adds a new check-box as a child to the view.
views::Checkbox* AddCheckbox(const std::wstring& text, bool checked);
- // Set icons info from passed-in WebApplicationInfo.
- void SetIconsInfo(const IconInfoList& icons);
-
// Fetch the largest unprocessed icon.
// The first largest icon downloaded and decoded successfully will be used.
void FetchIcon();
+ // Callback of icon download.
+ void OnIconDownloaded(bool errored, const SkBitmap& image);
+
// TabContents of the page that we want to create shortcut.
TabContents* tab_contents_;
@@ -90,23 +77,15 @@ class CreateApplicationShortcutView : public views::View,
views::Checkbox* menu_check_box_;
views::Checkbox* quick_launch_check_box_;
- // Target url of the app shortcut.
- GURL url_;
-
- // Title of the app shortcut.
- string16 title_;
-
- // Description of the app shortcut.
- string16 description_;
-
- // Icon of the app shortcut.
- SkBitmap icon_;
+ // Target shortcut info.
+ ShellIntegration::ShortcutInfo shortcut_info_;
// Unprocessed icons from the WebApplicationInfo passed in.
- IconInfoList unprocessed_icons_;
+ web_app::IconInfoList unprocessed_icons_;
- // Icon fetcher.
- scoped_ptr<URLFetcher> icon_fetcher_;
+ // Pending app icon download tracked by us.
+ class IconDownloadCallbackFunctor;
+ IconDownloadCallbackFunctor* pending_download_;
DISALLOW_COPY_AND_ASSIGN(CreateApplicationShortcutView);
};