Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / ui / webui / favicon_source.cc
blob46cce9b82cf9dc957cca2ba43e8411d789c76380
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/webui/favicon_source.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "chrome/browser/favicon/favicon_service_factory.h"
11 #include "chrome/browser/history/top_sites.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/search/instant_io_context.h"
14 #include "chrome/browser/sync/open_tabs_ui_delegate.h"
15 #include "chrome/browser/sync/profile_sync_service.h"
16 #include "chrome/browser/sync/profile_sync_service_factory.h"
17 #include "chrome/common/favicon/favicon_url_parser.h"
18 #include "chrome/common/url_constants.h"
19 #include "grit/locale_settings.h"
20 #include "grit/ui_resources.h"
21 #include "net/url_request/url_request.h"
22 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/base/layout.h"
24 #include "ui/base/resource/resource_bundle.h"
25 #include "ui/base/webui/web_ui_util.h"
27 FaviconSource::IconRequest::IconRequest()
28 : size_in_dip(gfx::kFaviconSize),
29 scale_factor(ui::SCALE_FACTOR_NONE) {
32 FaviconSource::IconRequest::IconRequest(
33 const content::URLDataSource::GotDataCallback& cb,
34 const GURL& path,
35 int size,
36 ui::ScaleFactor scale)
37 : callback(cb),
38 request_path(path),
39 size_in_dip(size),
40 scale_factor(scale) {
43 FaviconSource::IconRequest::~IconRequest() {
46 FaviconSource::FaviconSource(Profile* profile, IconType type)
47 : profile_(profile->GetOriginalProfile()),
48 icon_types_(type == FAVICON ? favicon_base::FAVICON
49 : favicon_base::TOUCH_PRECOMPOSED_ICON |
50 favicon_base::TOUCH_ICON |
51 favicon_base::FAVICON) {}
53 FaviconSource::~FaviconSource() {
56 std::string FaviconSource::GetSource() const {
57 return icon_types_ == favicon_base::FAVICON ? chrome::kChromeUIFaviconHost
58 : chrome::kChromeUITouchIconHost;
61 void FaviconSource::StartDataRequest(
62 const std::string& path,
63 int render_process_id,
64 int render_frame_id,
65 const content::URLDataSource::GotDataCallback& callback) {
66 FaviconService* favicon_service =
67 FaviconServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
68 if (!favicon_service) {
69 SendDefaultResponse(callback);
70 return;
73 chrome::ParsedFaviconPath parsed;
74 bool success = chrome::ParseFaviconPath(path, icon_types_, &parsed);
75 if (!success) {
76 SendDefaultResponse(callback);
77 return;
80 GURL url(parsed.url);
82 if (parsed.is_icon_url) {
83 // TODO(michaelbai): Change GetRawFavicon to support combination of
84 // IconType.
85 favicon_service->GetRawFavicon(
86 url,
87 favicon_base::FAVICON,
88 parsed.size_in_dip,
89 parsed.scale_factor,
90 base::Bind(&FaviconSource::OnFaviconDataAvailable,
91 base::Unretained(this),
92 IconRequest(
93 callback, url, parsed.size_in_dip, parsed.scale_factor)),
94 &cancelable_task_tracker_);
95 } else {
96 // Intercept requests for prepopulated pages.
97 for (size_t i = 0; i < arraysize(history::kPrepopulatedPages); i++) {
98 if (url.spec() ==
99 l10n_util::GetStringUTF8(history::kPrepopulatedPages[i].url_id)) {
100 callback.Run(
101 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale(
102 history::kPrepopulatedPages[i].favicon_id,
103 parsed.scale_factor));
104 return;
108 favicon_service->GetRawFaviconForURL(
109 FaviconService::FaviconForURLParams(url, icon_types_,
110 parsed.size_in_dip),
111 parsed.scale_factor,
112 base::Bind(&FaviconSource::OnFaviconDataAvailable,
113 base::Unretained(this),
114 IconRequest(callback,
115 url,
116 parsed.size_in_dip,
117 parsed.scale_factor)),
118 &cancelable_task_tracker_);
122 std::string FaviconSource::GetMimeType(const std::string&) const {
123 // We need to explicitly return a mime type, otherwise if the user tries to
124 // drag the image they get no extension.
125 return "image/png";
128 bool FaviconSource::ShouldReplaceExistingSource() const {
129 // Leave the existing DataSource in place, otherwise we'll drop any pending
130 // requests on the floor.
131 return false;
134 bool FaviconSource::ShouldServiceRequest(const net::URLRequest* request) const {
135 if (request->url().SchemeIs(chrome::kChromeSearchScheme))
136 return InstantIOContext::ShouldServiceRequest(request);
137 return URLDataSource::ShouldServiceRequest(request);
140 bool FaviconSource::HandleMissingResource(const IconRequest& request) {
141 // If the favicon is not available, try to use the synced favicon.
142 ProfileSyncService* sync_service =
143 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_);
144 browser_sync::OpenTabsUIDelegate* open_tabs = sync_service ?
145 sync_service->GetOpenTabsUIDelegate() : NULL;
147 scoped_refptr<base::RefCountedMemory> response;
148 if (open_tabs &&
149 open_tabs->GetSyncedFaviconForPageURL(request.request_path.spec(),
150 &response)) {
151 request.callback.Run(response.get());
152 return true;
154 return false;
157 void FaviconSource::OnFaviconDataAvailable(
158 const IconRequest& request,
159 const favicon_base::FaviconBitmapResult& bitmap_result) {
160 if (bitmap_result.is_valid()) {
161 // Forward the data along to the networking system.
162 request.callback.Run(bitmap_result.bitmap_data.get());
163 } else if (!HandleMissingResource(request)) {
164 SendDefaultResponse(request);
168 void FaviconSource::SendDefaultResponse(
169 const content::URLDataSource::GotDataCallback& callback) {
170 SendDefaultResponse(
171 IconRequest(callback, GURL(), 16, ui::SCALE_FACTOR_100P));
174 void FaviconSource::SendDefaultResponse(const IconRequest& icon_request) {
175 int favicon_index;
176 int resource_id;
177 switch (icon_request.size_in_dip) {
178 case 64:
179 favicon_index = SIZE_64;
180 resource_id = IDR_DEFAULT_FAVICON_64;
181 break;
182 case 32:
183 favicon_index = SIZE_32;
184 resource_id = IDR_DEFAULT_FAVICON_32;
185 break;
186 default:
187 favicon_index = SIZE_16;
188 resource_id = IDR_DEFAULT_FAVICON;
189 break;
191 base::RefCountedMemory* default_favicon =
192 default_favicons_[favicon_index].get();
194 if (!default_favicon) {
195 ui::ScaleFactor scale_factor = icon_request.scale_factor;
196 default_favicon = ResourceBundle::GetSharedInstance()
197 .LoadDataResourceBytesForScale(resource_id, scale_factor);
198 default_favicons_[favicon_index] = default_favicon;
201 icon_request.callback.Run(default_favicon);