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

@ -30,6 +30,7 @@
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "brightray/browser/brightray_paths.h"
#include "chrome/browser/icon_manager.h"
#include "chrome/common/chrome_paths.h"
#include "content/public/browser/browser_accessibility_state.h"
#include "content/public/browser/client_certificate_delegate.h"
@ -314,6 +315,26 @@ struct Converter<Browser::LoginItemSettings> {
}
};
template <>
struct Converter<IconLoader::IconSize> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
IconLoader::IconSize* out) {
std::string icon_size_string;
if (!ConvertFromV8(isolate, val, &icon_size_string))
return false;
if (icon_size_string == "small")
*out = IconLoader::IconSize::SMALL;
else if (icon_size_string == "normal")
*out = IconLoader::IconSize::NORMAL;
else if (icon_size_string == "large")
*out = IconLoader::IconSize::LARGE;
else
return false;
return true;
}
};
template<>
struct Converter<content::CertificateRequestResultType> {
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
@ -462,6 +483,11 @@ int ImportIntoCertStore(
}
#endif
void OnIconDataAvailable(const App::FileIconCallback& callback,
gfx::Image* icon) {
callback.Run(icon ? *icon : gfx::Image());
}
} // namespace
App::App(v8::Isolate* isolate) {
@ -841,6 +867,19 @@ JumpListResult App::SetJumpList(v8::Local<v8::Value> val,
}
#endif // defined(OS_WIN)
void App::GetFileIcon(const base::FilePath& path,
IconLoader::IconSize icon_size,
const FileIconCallback& callback) {
IconManager* icon_manager = IconManager::GetInstance();
gfx::Image* icon = icon_manager->LookupIconFromFilepath(path, icon_size);
if (icon) {
callback.Run(*icon);
} else {
icon_manager->LoadIcon(path, icon_size,
base::Bind(&OnIconDataAvailable, callback));
}
}
// static
mate::Handle<App> App::Create(v8::Isolate* isolate) {
return mate::CreateHandle(isolate, new App(isolate));
@ -909,7 +948,8 @@ void App::BuildPrototype(
.SetMethod("isAccessibilitySupportEnabled",
&App::IsAccessibilitySupportEnabled)
.SetMethod("disableHardwareAcceleration",
&App::DisableHardwareAcceleration);
&App::DisableHardwareAcceleration)
.SetMethod("getFileIcon", &App::GetFileIcon);
}
} // namespace api

View file

@ -13,6 +13,7 @@
#include "atom/browser/browser.h"
#include "atom/browser/browser_observer.h"
#include "atom/common/native_mate_converters/callback.h"
#include "chrome/browser/icon_loader.h"
#include "chrome/browser/process_singleton.h"
#include "content/public/browser/gpu_data_manager_observer.h"
#include "native_mate/handle.h"
@ -43,6 +44,8 @@ class App : public AtomBrowserClient::Delegate,
public BrowserObserver,
public content::GpuDataManagerObserver {
public:
using FileIconCallback = base::Callback<void(const gfx::Image&)>;
static mate::Handle<App> Create(v8::Isolate* isolate);
static void BuildPrototype(v8::Isolate* isolate,
@ -129,6 +132,9 @@ class App : public AtomBrowserClient::Delegate,
void ImportCertificate(const base::DictionaryValue& options,
const net::CompletionCallback& callback);
#endif
void GetFileIcon(const base::FilePath& path,
IconLoader::IconSize icon_size,
const FileIconCallback& callback);
#if defined(OS_WIN)
// Get the current Jump List settings.

View file

@ -17,7 +17,6 @@
#include "base/files/file_util.h"
#include "base/strings/pattern.h"
#include "base/strings/string_util.h"
#include "atom/common/fileicon_fetcher.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
#include "net/base/data_url.h"
@ -303,23 +302,6 @@ gfx::Size NativeImage::GetSize() {
return image_.Size();
}
void NativeImage::CreateFromFileIcon(v8::Isolate* isolate,
const base::FilePath& path,
const IconLoadedCallback& callback) {
IconLoader::IconSize icon_size = IconLoader::IconSize::NORMAL;
float scale_factor = 1.0f;
auto onready = base::Bind(&NativeImage::OnIconLoaded,
base::Unretained(isolate),
callback);
FileIconFetcher::FetchFileIcon(path, scale_factor, icon_size, onready);
}
void NativeImage::OnIconLoaded(v8::Isolate* isolate,
const IconLoadedCallback& callback,
gfx::Image& image) {
callback.Run(Create(isolate, image));
}
float NativeImage::GetAspectRatio() {
gfx::Size size = GetSize();
if (size.IsEmpty())
@ -533,8 +515,6 @@ void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
mate::Dictionary dict(context->GetIsolate(), exports);
dict.SetMethod("createEmpty", &atom::api::NativeImage::CreateEmpty);
dict.SetMethod("createFromPath", &atom::api::NativeImage::CreateFromPath);
dict.SetMethod("createFromFileIcon",
&atom::api::NativeImage::CreateFromFileIcon);
dict.SetMethod("createFromBuffer", &atom::api::NativeImage::CreateFromBuffer);
dict.SetMethod("createFromDataURL",
&atom::api::NativeImage::CreateFromDataURL);

View file

@ -38,7 +38,6 @@ namespace atom {
namespace api {
class NativeImage : public mate::Wrappable<NativeImage> {
using IconLoadedCallback = base::Callback<void(mate::Handle<NativeImage>)>;
public:
static mate::Handle<NativeImage> CreateEmpty(v8::Isolate* isolate);
static mate::Handle<NativeImage> Create(
@ -53,9 +52,6 @@ class NativeImage : public mate::Wrappable<NativeImage> {
mate::Arguments* args, v8::Local<v8::Value> buffer);
static mate::Handle<NativeImage> CreateFromDataURL(
v8::Isolate* isolate, const GURL& url);
static void CreateFromFileIcon(v8::Isolate* isolate,
const base::FilePath& path,
const IconLoadedCallback& callback);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
@ -90,9 +86,6 @@ class NativeImage : public mate::Wrappable<NativeImage> {
gfx::Size GetSize();
float GetAspectRatio();
static void OnIconLoaded(v8::Isolate* isolate,
const IconLoadedCallback& callback,
gfx::Image& image);
// Mark the image as template image.
void SetTemplateImage(bool setAsTemplate);
// Determine if the image is a template image.

View file

@ -1,76 +0,0 @@
// Copyright (c) 2016 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/common/fileicon_fetcher.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/files/file_path.h"
#include "base/memory/ref_counted_memory.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string_split.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "net/base/escape.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/webui/web_ui_util.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "url/gurl.h"
void FileIconFetcher::FetchFileIcon(const base::FilePath& path,
float scale_factor,
IconLoader::IconSize icon_size,
const IconFetchedCallback& callback) {
IconManager* im = g_browser_process->icon_manager();
gfx::Image* icon = im->LookupIconFromFilepath(path, icon_size);
if (icon) {
scoped_refptr<base::RefCountedBytes> icon_data(new base::RefCountedBytes);
gfx::PNGCodec::EncodeBGRASkBitmap(
icon->ToImageSkia()->GetRepresentation(scale_factor).sk_bitmap(),
false,
&icon_data->data());
callback.Run(icon_data.get());
} else {
// Attach the ChromeURLDataManager request ID to the history request.
IconRequestDetails details;
details.callback = callback;
details.scale_factor = scale_factor;
// Icon was not in cache, go fetch it slowly.
im->LoadIcon(path,
icon_size,
base::Bind(&FileIconFetcher::OnFileIconDataAvailable, details),
&cancelable_task_tracker_);
}
}
void FileIconFetcher::OnFileIconDataAvailable(const IconRequestDetails& details,
gfx::Image* icon) {
if (icon) {
scoped_refptr<base::RefCountedBytes> icon_data(new base::RefCountedBytes);
gfx::PNGCodec::EncodeBGRASkBitmap(
icon->ToImageSkia()->GetRepresentation(
details.scale_factor).sk_bitmap(),
false,
&icon_data->data());
details.callback.Run(icon_data.get());
} else {
details.callback.Run(NULL);
}
}
FileIconFetcher::IconRequestDetails::IconRequestDetails() : scale_factor(1.0f) {
}
FileIconFetcher::IconRequestDetails::IconRequestDetails(
const IconRequestDetails& other) = default;
FileIconFetcher::IconRequestDetails::~IconRequestDetails() {
}

View file

@ -1,48 +0,0 @@
// Copyright (c) 2016 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_COMMON_API_FILEICON_FETCHER_H_
#define ATOM_COMMON_API_FILEICON_FETCHER_H_
#include <string>
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/task/cancelable_task_tracker.h"
#include "chrome/browser/icon_manager.h"
#include "content/public/browser/url_data_source.h"
namespace gfx {
class Image;
}
class FileIconFetcher {
using IconFetchedCallback = base::Callback<void(gfx::Image&)>;
public:
static void FetchFileIcon(const base::FilePath& path,
float scale_factor,
IconLoader::IconSize icon_size,
const IconFetchedCallback& callback);
private:
struct IconRequestDetails {
IconRequestDetails();
IconRequestDetails(const IconRequestDetails& other);
~IconRequestDetails();
// The callback to run with the response.
IconFetchedCallback callback;
// The requested scale factor to respond with.
float scale_factor;
};
// Called when favicon data is available from the history backend.
static void OnFileIconDataAvailable(const IconRequestDetails& details,
gfx::Image* icon);
// Tracks tasks requesting file icons.
static base::CancelableTaskTracker cancelable_task_tracker_;
};
#endif // ATOM_COMMON_API_FILEICON_FETCHER_H_