Commit c49162e3 authored by vkuzkokov's avatar vkuzkokov Committed by Commit bot

[DevTools] Fix favicon images on chrome://inspect

Favicon is resolved as data URL to avoid discrepancies if favicon depends on environment (e.g. user-agent).

BUG=461581

Review URL: https://codereview.chromium.org/1124023004

Cr-Commit-Position: refs/heads/master@{#329831}
parent 73b49ca2
...@@ -1832,6 +1832,7 @@ public class Tab implements ViewGroup.OnHierarchyChangeListener, ...@@ -1832,6 +1832,7 @@ public class Tab implements ViewGroup.OnHierarchyChangeListener,
* @return The bitmap of the favicon scaled to 16x16dp. null if no favicon * @return The bitmap of the favicon scaled to 16x16dp. null if no favicon
* is specified or it requires the default favicon. * is specified or it requires the default favicon.
*/ */
@CalledByNative
public Bitmap getFavicon() { public Bitmap getFavicon() {
// If we have no content or a native page, return null. // If we have no content or a native page, return null.
if (getContentViewCore() == null) return null; if (getContentViewCore() == null) return null;
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "chrome/browser/android/dev_tools_discovery_provider_android.h" #include "chrome/browser/android/dev_tools_discovery_provider_android.h"
#include "base/base64.h"
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/bind.h" #include "base/bind.h"
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
...@@ -19,20 +20,14 @@ ...@@ -19,20 +20,14 @@
#include "content/public/browser/favicon_status.h" #include "content/public/browser/favicon_status.h"
#include "content/public/browser/navigation_entry.h" #include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/png_codec.h"
using content::DevToolsAgentHost; using content::DevToolsAgentHost;
using content::WebContents; using content::WebContents;
namespace { namespace {
GURL GetFaviconURLForContents(WebContents* web_contents) {
content::NavigationController& controller = web_contents->GetController();
content::NavigationEntry* entry = controller.GetActiveEntry();
if (entry != NULL && entry->GetURL().is_valid())
return entry->GetFavicon().url;
return GURL();
}
class TabDescriptor : public devtools_discovery::DevToolsTargetDescriptor { class TabDescriptor : public devtools_discovery::DevToolsTargetDescriptor {
public: public:
static TabDescriptor* CreateForWebContents(int tab_id, static TabDescriptor* CreateForWebContents(int tab_id,
...@@ -138,14 +133,42 @@ class TabDescriptor : public devtools_discovery::DevToolsTargetDescriptor { ...@@ -138,14 +133,42 @@ class TabDescriptor : public devtools_discovery::DevToolsTargetDescriptor {
: tab_id_(tab_id), : tab_id_(tab_id),
title_(base::UTF16ToUTF8(web_contents->GetTitle())), title_(base::UTF16ToUTF8(web_contents->GetTitle())),
url_(web_contents->GetURL()), url_(web_contents->GetURL()),
favicon_url_(GetFaviconURLForContents(web_contents)), favicon_url_(CalculateFaviconURL()),
last_activity_time_(web_contents->GetLastActiveTime()) { last_activity_time_(web_contents->GetLastActiveTime()) {
} }
TabDescriptor(int tab_id, const base::string16& title, const GURL& url) TabDescriptor(int tab_id, const base::string16& title, const GURL& url)
: tab_id_(tab_id), : tab_id_(tab_id),
title_(base::UTF16ToUTF8(title)), title_(base::UTF16ToUTF8(title)),
url_(url) { url_(url),
favicon_url_(CalculateFaviconURL()) {
}
GURL CalculateFaviconURL() {
TabModel* model;
int index;
if (!FindTab(&model, &index))
return GURL();
TabAndroid* tab = model->GetTabAt(index);
if (!tab)
return GURL();
SkBitmap bitmap = tab->GetFaviconBitmap();
if (bitmap.empty())
return GURL();
std::vector<unsigned char> data;
SkAutoLockPixels lock_image(bitmap);
bool encoded = gfx::PNGCodec::FastEncodeBGRASkBitmap(bitmap, false, &data);
if (!encoded)
return GURL();
std::string base_64_data;
base::Base64Encode(
base::StringPiece(reinterpret_cast<char*>(&data[0]), data.size()),
&base_64_data);
return GURL("data:image/png;base64," + base_64_data);
} }
bool FindTab(TabModel** model_result, int* index_result) const { bool FindTab(TabModel** model_result, int* index_result) const {
......
...@@ -687,6 +687,16 @@ ScopedJavaLocalRef<jobject> TabAndroid::GetFavicon(JNIEnv* env, ...@@ -687,6 +687,16 @@ ScopedJavaLocalRef<jobject> TabAndroid::GetFavicon(JNIEnv* env,
return bitmap; return bitmap;
} }
SkBitmap TabAndroid::GetFaviconBitmap() {
JNIEnv* env = base::android::AttachCurrentThread();
ScopedJavaLocalRef<jobject> javaBitmap =
Java_Tab_getFavicon(env, weak_java_tab_.get(env).obj());
if (!javaBitmap.obj())
return SkBitmap();
return gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(javaBitmap.obj()));
}
prerender::PrerenderManager* TabAndroid::GetPrerenderManager() const { prerender::PrerenderManager* TabAndroid::GetPrerenderManager() const {
Profile* profile = GetProfile(); Profile* profile = GetProfile();
if (!profile) if (!profile)
......
...@@ -117,6 +117,8 @@ class TabAndroid : public CoreTabHelperDelegate, ...@@ -117,6 +117,8 @@ class TabAndroid : public CoreTabHelperDelegate,
chrome::NavigateParams* params, chrome::NavigateParams* params,
content::NavigationController::LoadURLParams* load_url_params); content::NavigationController::LoadURLParams* load_url_params);
SkBitmap GetFaviconBitmap();
// CoreTabHelperDelegate ---------------------------------------------------- // CoreTabHelperDelegate ----------------------------------------------------
void SwapTabContents(content::WebContents* old_contents, void SwapTabContents(content::WebContents* old_contents,
......
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