Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1174)

Side by Side Diff: chrome/renderer/chrome_render_view_observer.cc

Issue 11232068: Extract renderer-side favicon downloading code into separate helper class (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase + fix Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/renderer/chrome_render_view_observer.h" 5 #include "chrome/renderer/chrome_render_view_observer.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/debug/trace_event.h" 10 #include "base/debug/trace_event.h"
11 #include "base/message_loop.h" 11 #include "base/message_loop.h"
12 #include "base/metrics/histogram.h" 12 #include "base/metrics/histogram.h"
13 #include "base/string_util.h" 13 #include "base/string_util.h"
14 #include "chrome/common/chrome_constants.h" 14 #include "chrome/common/chrome_constants.h"
15 #include "chrome/common/chrome_switches.h" 15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/icon_messages.h" 16 #include "chrome/common/favicon_url.h"
17 #include "chrome/common/prerender_messages.h" 17 #include "chrome/common/prerender_messages.h"
18 #include "chrome/common/render_messages.h" 18 #include "chrome/common/render_messages.h"
19 #include "chrome/common/url_constants.h" 19 #include "chrome/common/url_constants.h"
20 #include "chrome/renderer/chrome_render_process_observer.h" 20 #include "chrome/renderer/chrome_render_process_observer.h"
21 #include "chrome/renderer/content_settings_observer.h" 21 #include "chrome/renderer/content_settings_observer.h"
22 #include "chrome/renderer/extensions/dispatcher.h" 22 #include "chrome/renderer/extensions/dispatcher.h"
23 #include "chrome/renderer/external_host_bindings.h" 23 #include "chrome/renderer/external_host_bindings.h"
24 #include "chrome/renderer/frame_sniffer.h" 24 #include "chrome/renderer/frame_sniffer.h"
25 #include "chrome/renderer/prerender/prerender_helper.h" 25 #include "chrome/renderer/prerender/prerender_helper.h"
26 #include "chrome/renderer/safe_browsing/phishing_classifier_delegate.h" 26 #include "chrome/renderer/safe_browsing/phishing_classifier_delegate.h"
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 return false; 158 return false;
159 159
160 view->paint(webkit_glue::ToWebCanvas(&canvas), 160 view->paint(webkit_glue::ToWebCanvas(&canvas),
161 WebRect(0, 0, size.width, size.height)); 161 WebRect(0, 0, size.width, size.height));
162 // TODO: Add a way to snapshot the whole page, not just the currently 162 // TODO: Add a way to snapshot the whole page, not just the currently
163 // visible part. 163 // visible part.
164 164
165 return true; 165 return true;
166 } 166 }
167 167
168 static FaviconURL::IconType ToFaviconType(WebIconURL::Type type) {
169 switch (type) {
170 case WebIconURL::TypeFavicon:
171 return FaviconURL::FAVICON;
172 case WebIconURL::TypeTouch:
173 return FaviconURL::TOUCH_ICON;
174 case WebIconURL::TypeTouchPrecomposed:
175 return FaviconURL::TOUCH_PRECOMPOSED_ICON;
176 case WebIconURL::TypeInvalid:
177 return FaviconURL::INVALID_ICON;
178 }
179 return FaviconURL::INVALID_ICON;
180 }
181
182 static bool isHostInDomain(const std::string& host, const std::string& domain) { 168 static bool isHostInDomain(const std::string& host, const std::string& domain) {
183 return (EndsWith(host, domain, false) && 169 return (EndsWith(host, domain, false) &&
184 (host.length() == domain.length() || 170 (host.length() == domain.length() ||
185 (host.length() > domain.length() && 171 (host.length() > domain.length() &&
186 host[host.length() - domain.length() - 1] == '.'))); 172 host[host.length() - domain.length() - 1] == '.')));
187 } 173 }
188 174
189 namespace { 175 namespace {
190 GURL StripRef(const GURL& url) { 176 GURL StripRef(const GURL& url) {
191 GURL::Replacements replacements; 177 GURL::Replacements replacements;
(...skipping 29 matching lines...) Expand all
221 207
222 bool ChromeRenderViewObserver::OnMessageReceived(const IPC::Message& message) { 208 bool ChromeRenderViewObserver::OnMessageReceived(const IPC::Message& message) {
223 bool handled = true; 209 bool handled = true;
224 IPC_BEGIN_MESSAGE_MAP(ChromeRenderViewObserver, message) 210 IPC_BEGIN_MESSAGE_MAP(ChromeRenderViewObserver, message)
225 IPC_MESSAGE_HANDLER(ChromeViewMsg_WebUIJavaScript, OnWebUIJavaScript) 211 IPC_MESSAGE_HANDLER(ChromeViewMsg_WebUIJavaScript, OnWebUIJavaScript)
226 IPC_MESSAGE_HANDLER(ChromeViewMsg_CaptureSnapshot, OnCaptureSnapshot) 212 IPC_MESSAGE_HANDLER(ChromeViewMsg_CaptureSnapshot, OnCaptureSnapshot)
227 IPC_MESSAGE_HANDLER(ChromeViewMsg_HandleMessageFromExternalHost, 213 IPC_MESSAGE_HANDLER(ChromeViewMsg_HandleMessageFromExternalHost,
228 OnHandleMessageFromExternalHost) 214 OnHandleMessageFromExternalHost)
229 IPC_MESSAGE_HANDLER(ChromeViewMsg_JavaScriptStressTestControl, 215 IPC_MESSAGE_HANDLER(ChromeViewMsg_JavaScriptStressTestControl,
230 OnJavaScriptStressTestControl) 216 OnJavaScriptStressTestControl)
231 IPC_MESSAGE_HANDLER(IconMsg_DownloadFavicon, OnDownloadFavicon)
232 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetAllowDisplayingInsecureContent, 217 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetAllowDisplayingInsecureContent,
233 OnSetAllowDisplayingInsecureContent) 218 OnSetAllowDisplayingInsecureContent)
234 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetAllowRunningInsecureContent, 219 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetAllowRunningInsecureContent,
235 OnSetAllowRunningInsecureContent) 220 OnSetAllowRunningInsecureContent)
236 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetClientSidePhishingDetection, 221 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetClientSidePhishingDetection,
237 OnSetClientSidePhishingDetection) 222 OnSetClientSidePhishingDetection)
238 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetVisuallyDeemphasized, 223 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetVisuallyDeemphasized,
239 OnSetVisuallyDeemphasized) 224 OnSetVisuallyDeemphasized)
240 #if defined(OS_CHROMEOS) 225 #if defined(OS_CHROMEOS)
241 IPC_MESSAGE_HANDLER(ChromeViewMsg_StartFrameSniffer, OnStartFrameSniffer) 226 IPC_MESSAGE_HANDLER(ChromeViewMsg_StartFrameSniffer, OnStartFrameSniffer)
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
296 281
297 void ChromeRenderViewObserver::OnJavaScriptStressTestControl(int cmd, 282 void ChromeRenderViewObserver::OnJavaScriptStressTestControl(int cmd,
298 int param) { 283 int param) {
299 if (cmd == kJavaScriptStressTestSetStressRunType) { 284 if (cmd == kJavaScriptStressTestSetStressRunType) {
300 v8::Testing::SetStressRunType(static_cast<v8::Testing::StressType>(param)); 285 v8::Testing::SetStressRunType(static_cast<v8::Testing::StressType>(param));
301 } else if (cmd == kJavaScriptStressTestPrepareStressRun) { 286 } else if (cmd == kJavaScriptStressTestPrepareStressRun) {
302 v8::Testing::PrepareStressRun(param); 287 v8::Testing::PrepareStressRun(param);
303 } 288 }
304 } 289 }
305 290
306 void ChromeRenderViewObserver::OnDownloadFavicon(int id,
307 const GURL& image_url,
308 int image_size) {
309 bool data_image_failed = false;
310 if (image_url.SchemeIs("data")) {
311 SkBitmap data_image = ImageFromDataUrl(image_url);
312 data_image_failed = data_image.empty();
313 if (!data_image_failed) {
314 std::vector<SkBitmap> images(1, data_image);
315 Send(new IconHostMsg_DidDownloadFavicon(
316 routing_id(), id, image_url, false, image_size, images));
317 }
318 }
319
320 if (data_image_failed ||
321 !DownloadFavicon(id, image_url, image_size)) {
322 Send(new IconHostMsg_DidDownloadFavicon(
323 routing_id(), id, image_url, true, image_size,
324 std::vector<SkBitmap>()));
325 }
326 }
327
328 void ChromeRenderViewObserver::OnSetAllowDisplayingInsecureContent(bool allow) { 291 void ChromeRenderViewObserver::OnSetAllowDisplayingInsecureContent(bool allow) {
329 allow_displaying_insecure_content_ = allow; 292 allow_displaying_insecure_content_ = allow;
330 WebFrame* main_frame = render_view()->GetWebView()->mainFrame(); 293 WebFrame* main_frame = render_view()->GetWebView()->mainFrame();
331 if (main_frame) 294 if (main_frame)
332 main_frame->reload(); 295 main_frame->reload();
333 } 296 }
334 297
335 void ChromeRenderViewObserver::OnSetAllowRunningInsecureContent(bool allow) { 298 void ChromeRenderViewObserver::OnSetAllowRunningInsecureContent(bool allow) {
336 allow_running_insecure_content_ = allow; 299 allow_running_insecure_content_ = allow;
337 OnSetAllowDisplayingInsecureContent(allow); 300 OnSetAllowDisplayingInsecureContent(allow);
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after
688 render_view()->GetContentStateImmediately() ? 651 render_view()->GetContentStateImmediately() ?
689 0 : kDelayForCaptureMs)); 652 0 : kDelayForCaptureMs));
690 653
691 WebFrame* main_frame = render_view()->GetWebView()->mainFrame(); 654 WebFrame* main_frame = render_view()->GetWebView()->mainFrame();
692 GURL osd_url = main_frame->document().openSearchDescriptionURL(); 655 GURL osd_url = main_frame->document().openSearchDescriptionURL();
693 if (!osd_url.is_empty()) { 656 if (!osd_url.is_empty()) {
694 Send(new ChromeViewHostMsg_PageHasOSDD( 657 Send(new ChromeViewHostMsg_PageHasOSDD(
695 routing_id(), render_view()->GetPageId(), osd_url, 658 routing_id(), render_view()->GetPageId(), osd_url,
696 search_provider::AUTODETECTED_PROVIDER)); 659 search_provider::AUTODETECTED_PROVIDER));
697 } 660 }
698
699 int icon_types = WebIconURL::TypeFavicon;
700 if (chrome::kEnableTouchIcon)
701 icon_types |= WebIconURL::TypeTouchPrecomposed | WebIconURL::TypeTouch;
702
703 WebVector<WebIconURL> icon_urls =
704 render_view()->GetWebView()->mainFrame()->iconURLs(icon_types);
705 std::vector<FaviconURL> urls;
706 for (size_t i = 0; i < icon_urls.size(); i++) {
707 WebURL url = icon_urls[i].iconURL();
708 if (!url.isEmpty())
709 urls.push_back(FaviconURL(url, ToFaviconType(icon_urls[i].iconType())));
710 }
711 if (!urls.empty()) {
712 Send(new IconHostMsg_UpdateFaviconURL(
713 routing_id(), render_view()->GetPageId(), urls));
714 }
715 }
716
717 void ChromeRenderViewObserver::DidChangeIcon(WebFrame* frame,
718 WebIconURL::Type icon_type) {
719 if (frame->parent())
720 return;
721
722 if (!chrome::kEnableTouchIcon &&
723 icon_type != WebIconURL::TypeFavicon)
724 return;
725
726 WebVector<WebIconURL> icon_urls = frame->iconURLs(icon_type);
727 std::vector<FaviconURL> urls;
728 for (size_t i = 0; i < icon_urls.size(); i++) {
729 urls.push_back(FaviconURL(icon_urls[i].iconURL(),
730 ToFaviconType(icon_urls[i].iconType())));
731 }
732 Send(new IconHostMsg_UpdateFaviconURL(
733 routing_id(), render_view()->GetPageId(), urls));
734 } 661 }
735 662
736 void ChromeRenderViewObserver::DidCommitProvisionalLoad( 663 void ChromeRenderViewObserver::DidCommitProvisionalLoad(
737 WebFrame* frame, bool is_new_navigation) { 664 WebFrame* frame, bool is_new_navigation) {
738 if (!is_new_navigation) 665 if (!is_new_navigation)
739 return; 666 return;
740 667
741 CapturePageInfoLater( 668 CapturePageInfoLater(
742 true, // preliminary_capture 669 true, // preliminary_capture
743 base::TimeDelta::FromMilliseconds(kDelayForForcedCaptureMs)); 670 base::TimeDelta::FromMilliseconds(kDelayForForcedCaptureMs));
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
919 } 846 }
920 847
921 ExternalHostBindings* ChromeRenderViewObserver::GetExternalHostBindings() { 848 ExternalHostBindings* ChromeRenderViewObserver::GetExternalHostBindings() {
922 if (!external_host_bindings_.get()) { 849 if (!external_host_bindings_.get()) {
923 external_host_bindings_.reset(new ExternalHostBindings( 850 external_host_bindings_.reset(new ExternalHostBindings(
924 render_view(), routing_id())); 851 render_view(), routing_id()));
925 } 852 }
926 return external_host_bindings_.get(); 853 return external_host_bindings_.get();
927 } 854 }
928 855
929 bool ChromeRenderViewObserver::DownloadFavicon(int id,
930 const GURL& image_url,
931 int image_size) {
932 // Make sure webview was not shut down.
933 if (!render_view()->GetWebView())
934 return false;
935 // Create an image resource fetcher and assign it with a call back object.
936 image_fetchers_.push_back(linked_ptr<MultiResolutionImageResourceFetcher>(
937 new MultiResolutionImageResourceFetcher(
938 image_url, render_view()->GetWebView()->mainFrame(), id,
939 WebURLRequest::TargetIsFavicon,
940 base::Bind(&ChromeRenderViewObserver::DidDownloadFavicon,
941 base::Unretained(this), image_size))));
942 return true;
943 }
944
945 void ChromeRenderViewObserver::DidDownloadFavicon(
946 int requested_size,
947 MultiResolutionImageResourceFetcher* fetcher,
948 const std::vector<SkBitmap>& images) {
949 // Notify requester of image download status.
950 Send(new IconHostMsg_DidDownloadFavicon(routing_id(),
951 fetcher->id(),
952 fetcher->image_url(),
953 images.empty(),
954 requested_size,
955 images));
956
957 // Remove the image fetcher from our pending list. We're in the callback from
958 // MultiResolutionImageResourceFetcher, best to delay deletion.
959 ImageResourceFetcherList::iterator iter;
960 for (iter = image_fetchers_.begin(); iter != image_fetchers_.end(); ++iter) {
961 if (iter->get() == fetcher) {
962 iter->release();
963 image_fetchers_.erase(iter);
964 break;
965 }
966 }
967 MessageLoop::current()->DeleteSoon(FROM_HERE, fetcher);
968 }
969
970 SkBitmap ChromeRenderViewObserver::ImageFromDataUrl(const GURL& url) const {
971 std::string mime_type, char_set, data;
972 if (net::DataURL::Parse(url, &mime_type, &char_set, &data) && !data.empty()) {
973 // Decode the favicon using WebKit's image decoder.
974 webkit_glue::ImageDecoder decoder(
975 gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize));
976 const unsigned char* src_data =
977 reinterpret_cast<const unsigned char*>(&data[0]);
978
979 return decoder.Decode(src_data, data.size());
980 }
981 return SkBitmap();
982 }
983
984 bool ChromeRenderViewObserver::IsStrictSecurityHost(const std::string& host) { 856 bool ChromeRenderViewObserver::IsStrictSecurityHost(const std::string& host) {
985 return (strict_security_hosts_.find(host) != strict_security_hosts_.end()); 857 return (strict_security_hosts_.find(host) != strict_security_hosts_.end());
986 } 858 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698