create iconmanager as singleton class and cleanup code (#1)

* create iconmanager as singleton class and cleanup code
This commit is contained in:
Robo 2016-11-03 00:27:16 +05:30 committed by Kevin Sawicki
parent 1b3cd87fc9
commit b25b141642
15 changed files with 275 additions and 216 deletions

View file

@ -5,7 +5,6 @@
#include "chrome/browser/browser_process.h"
#include "chrome/browser/printing/print_job_manager.h"
#include "chrome/browser/icon_manager.h"
#include "ui/base/l10n/l10n_util.h"
BrowserProcess* g_browser_process = NULL;
@ -13,8 +12,6 @@ BrowserProcess* g_browser_process = NULL;
BrowserProcess::BrowserProcess()
: print_job_manager_(new printing::PrintJobManager) {
g_browser_process = this;
icon_manager_.reset(new IconManager);
}
BrowserProcess::~BrowserProcess() {
@ -28,7 +25,3 @@ std::string BrowserProcess::GetApplicationLocale() {
printing::PrintJobManager* BrowserProcess::print_job_manager() {
return print_job_manager_.get();
}
IconManager* BrowserProcess::icon_manager() {
return icon_manager_.get();
}

View file

@ -31,11 +31,9 @@ class BrowserProcess {
std::string GetApplicationLocale();
printing::PrintJobManager* print_job_manager();
IconManager* icon_manager();
private:
std::unique_ptr<printing::PrintJobManager> print_job_manager_;
std::unique_ptr<IconManager> icon_manager_;
DISALLOW_COPY_AND_ASSIGN(BrowserProcess);
};

View file

@ -18,15 +18,14 @@ IconLoader::IconLoader(const base::FilePath& file_path,
icon_size_(size),
delegate_(delegate) {}
IconLoader::~IconLoader() {
}
IconLoader::~IconLoader() {}
void IconLoader::Start() {
target_task_runner_ = base::ThreadTaskRunnerHandle::Get();
BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE,
base::Bind(&IconLoader::ReadGroup, this),
base::Bind(&IconLoader::OnReadGroup, this));
base::Bind(&IconLoader::ReadGroup, this),
base::Bind(&IconLoader::OnReadGroup, this));
}
void IconLoader::ReadGroup() {
@ -37,7 +36,7 @@ void IconLoader::OnReadGroup() {
if (IsIconMutableFromFilepath(file_path_) ||
!delegate_->OnGroupLoaded(this, group_)) {
BrowserThread::PostTask(ReadIconThreadID(), FROM_HERE,
base::Bind(&IconLoader::ReadIcon, this));
base::Bind(&IconLoader::ReadIcon, this));
}
}

View file

@ -0,0 +1,55 @@
// Copyright 2013 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.
#include "chrome/browser/icon_loader.h"
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/nix/mime_util_xdg.h"
#include "ui/views/linux_ui/linux_ui.h"
// static
IconGroupID IconLoader::ReadGroupIDFromFilepath(
const base::FilePath& filepath) {
return base::nix::GetFileMimeType(filepath);
}
// static
bool IconLoader::IsIconMutableFromFilepath(const base::FilePath&) {
return false;
}
// static
content::BrowserThread::ID IconLoader::ReadIconThreadID() {
// ReadIcon() calls into views::LinuxUI and GTK2 code, so it must be on the UI
// thread.
return content::BrowserThread::UI;
}
void IconLoader::ReadIcon() {
int size_pixels = 0;
switch (icon_size_) {
case IconLoader::SMALL:
size_pixels = 16;
break;
case IconLoader::NORMAL:
size_pixels = 32;
break;
case IconLoader::LARGE:
size_pixels = 48;
break;
default:
NOTREACHED();
}
views::LinuxUI* ui = views::LinuxUI::instance();
if (ui) {
gfx::Image image = ui->GetIconForContentType(group_, size_pixels);
if (!image.IsEmpty())
image_.reset(new gfx::Image(image));
}
target_task_runner_->PostTask(FROM_HERE,
base::Bind(&IconLoader::NotifyDelegate, this));
}

View file

@ -0,0 +1,62 @@
// Copyright (c) 2012 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.
#include "chrome/browser/icon_loader.h"
#import <AppKit/AppKit.h>
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/sys_string_conversions.h"
#include "base/threading/thread.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_util_mac.h"
// static
IconGroupID IconLoader::ReadGroupIDFromFilepath(
const base::FilePath& filepath) {
return filepath.Extension();
}
// static
bool IconLoader::IsIconMutableFromFilepath(const base::FilePath&) {
return false;
}
// static
content::BrowserThread::ID IconLoader::ReadIconThreadID() {
return content::BrowserThread::FILE;
}
void IconLoader::ReadIcon() {
NSString* group = base::SysUTF8ToNSString(group_);
NSWorkspace* workspace = [NSWorkspace sharedWorkspace];
NSImage* icon = [workspace iconForFileType:group];
if (icon_size_ == ALL) {
// The NSImage already has all sizes.
image_.reset(new gfx::Image([icon retain]));
} else {
NSSize size = NSZeroSize;
switch (icon_size_) {
case IconLoader::SMALL:
size = NSMakeSize(16, 16);
break;
case IconLoader::NORMAL:
size = NSMakeSize(32, 32);
break;
default:
NOTREACHED();
}
gfx::ImageSkia image_skia(gfx::ImageSkiaFromResizedNSImage(icon, size));
if (!image_skia.isNull()) {
image_skia.MakeThreadSafe();
image_.reset(new gfx::Image(image_skia));
}
}
target_task_runner_->PostTask(FROM_HERE,
base::Bind(&IconLoader::NotifyDelegate, this));
}

View file

@ -0,0 +1,75 @@
// Copyright (c) 2012 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.
#include "chrome/browser/icon_loader.h"
#include <windows.h>
#include <shellapi.h>
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/threading/thread.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/display/win/dpi.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/icon_util.h"
#include "ui/gfx/image/image_skia.h"
// static
IconGroupID IconLoader::ReadGroupIDFromFilepath(
const base::FilePath& filepath) {
if (!IsIconMutableFromFilepath(filepath))
return filepath.Extension();
return filepath.value();
}
// static
bool IconLoader::IsIconMutableFromFilepath(const base::FilePath& filepath) {
return filepath.MatchesExtension(L".exe") ||
filepath.MatchesExtension(L".dll") ||
filepath.MatchesExtension(L".ico");
}
// static
content::BrowserThread::ID IconLoader::ReadIconThreadID() {
return content::BrowserThread::FILE;
}
void IconLoader::ReadIcon() {
int size = 0;
switch (icon_size_) {
case IconLoader::SMALL:
size = SHGFI_SMALLICON;
break;
case IconLoader::NORMAL:
size = 0;
break;
case IconLoader::LARGE:
size = SHGFI_LARGEICON;
break;
default:
NOTREACHED();
}
image_.reset();
SHFILEINFO file_info = {0};
if (SHGetFileInfo(group_.c_str(), FILE_ATTRIBUTE_NORMAL, &file_info,
sizeof(SHFILEINFO),
SHGFI_ICON | size | SHGFI_USEFILEATTRIBUTES)) {
std::unique_ptr<SkBitmap> bitmap(
IconUtil::CreateSkBitmapFromHICON(file_info.hIcon));
if (bitmap.get()) {
gfx::ImageSkia image_skia(
gfx::ImageSkiaRep(*bitmap, display::win::GetDPIScale()));
image_skia.MakeThreadSafe();
image_.reset(new gfx::Image(image_skia));
DestroyIcon(file_info.hIcon);
}
}
// Always notify the delegate, regardless of success.
target_task_runner_->PostTask(FROM_HERE,
base::Bind(&IconLoader::NotifyDelegate, this));
}

View file

@ -13,30 +13,21 @@
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
namespace {
void RunCallbackIfNotCanceled(
const base::CancelableTaskTracker::IsCanceledCallback& is_canceled,
const IconManager::IconRequestCallback& callback,
gfx::Image* image) {
if (is_canceled.Run())
return;
callback.Run(image);
}
} // namespace
struct IconManager::ClientRequest {
IconRequestCallback callback;
base::FilePath file_path;
IconLoader::IconSize size;
};
IconManager::IconManager() {
// static
IconManager* IconManager::GetInstance() {
return base::Singleton<IconManager>::get();
}
IconManager::IconManager() {}
IconManager::~IconManager() {
base::STLDeleteValues(&icon_cache_);
STLDeleteValues(&icon_cache_);
}
gfx::Image* IconManager::LookupIconFromFilepath(const base::FilePath& file_name,
@ -54,33 +45,23 @@ gfx::Image* IconManager::LookupIconFromGroup(const IconGroupID& group,
if (it != icon_cache_.end())
return it->second;
return NULL;
return nullptr;
}
base::CancelableTaskTracker::TaskId IconManager::LoadIcon(
const base::FilePath& file_name,
IconLoader::IconSize size,
const IconRequestCallback& callback,
base::CancelableTaskTracker* tracker) {
void IconManager::LoadIcon(const base::FilePath& file_name,
IconLoader::IconSize size,
const IconRequestCallback& callback) {
IconLoader* loader = new IconLoader(file_name, size, this);
loader->AddRef();
loader->Start();
base::CancelableTaskTracker::IsCanceledCallback is_canceled;
base::CancelableTaskTracker::TaskId id =
tracker->NewTrackedTaskId(&is_canceled);
IconRequestCallback callback_runner = base::Bind(
&RunCallbackIfNotCanceled, is_canceled, callback);
ClientRequest client_request = { callback_runner, file_name, size };
ClientRequest client_request = {callback, file_name, size};
requests_[loader] = client_request;
return id;
}
// IconLoader::Delegate implementation -----------------------------------------
bool IconManager::OnGroupLoaded(IconLoader* loader,
const IconGroupID& group) {
bool IconManager::OnGroupLoaded(IconLoader* loader, const IconGroupID& group) {
ClientRequests::iterator rit = requests_.find(loader);
if (rit == requests_.end()) {
NOTREACHED();
@ -95,8 +76,9 @@ bool IconManager::OnGroupLoaded(IconLoader* loader,
return OnImageLoaded(loader, result, group);
}
bool IconManager::OnImageLoaded(
IconLoader* loader, gfx::Image* result, const IconGroupID& group) {
bool IconManager::OnImageLoaded(IconLoader* loader,
gfx::Image* result,
const IconGroupID& group) {
ClientRequests::iterator rit = requests_.find(loader);
// Balances the AddRef() in LoadIcon().
@ -139,10 +121,8 @@ bool IconManager::OnImageLoaded(
IconManager::CacheKey::CacheKey(const IconGroupID& group,
IconLoader::IconSize size)
: group(group),
size(size) {
}
: group(group), size(size) {}
bool IconManager::CacheKey::operator<(const CacheKey &other) const {
bool IconManager::CacheKey::operator<(const CacheKey& other) const {
return std::tie(group, size) < std::tie(other.group, other.size);
}

View file

@ -49,15 +49,13 @@
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/task/cancelable_task_tracker.h"
#include "base/memory/singleton.h"
#include "chrome/browser/icon_loader.h"
#include "ui/gfx/image/image.h"
class IconManager : public IconLoader::Delegate {
public:
IconManager();
~IconManager() override;
static IconManager* GetInstance();
// Synchronous call to examine the internal caches for the icon. Returns the
// icon if we have already loaded it, NULL if we don't have it and must load
// it via 'LoadIcon'. The returned bitmap is owned by the IconManager and must
@ -78,11 +76,9 @@ class IconManager : public IconLoader::Delegate {
// should never keep it or delete it.
// 3. The gfx::Image pointer passed to the callback may be NULL if decoding
// failed.
base::CancelableTaskTracker::TaskId LoadIcon(
const base::FilePath& file_name,
IconLoader::IconSize size,
const IconRequestCallback& callback,
base::CancelableTaskTracker* tracker);
void LoadIcon(const base::FilePath& file_name,
IconLoader::IconSize size,
const IconRequestCallback& callback);
// IconLoader::Delegate interface.
bool OnGroupLoaded(IconLoader* loader, const IconGroupID& group) override;
@ -91,11 +87,16 @@ class IconManager : public IconLoader::Delegate {
const IconGroupID& group) override;
private:
friend struct base::DefaultSingletonTraits<IconManager>;
IconManager();
~IconManager() override;
struct CacheKey {
CacheKey(const IconGroupID& group, IconLoader::IconSize size);
// Used as a key in the map below, so we need this comparator.
bool operator<(const CacheKey &other) const;
bool operator<(const CacheKey& other) const;
IconGroupID group;
IconLoader::IconSize size;