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

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: 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 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 INSECURE_CONTENT_DISPLAY_HOST_GOOGLE_SUPPORT, 143 INSECURE_CONTENT_DISPLAY_HOST_GOOGLE_SUPPORT,
144 INSECURE_CONTENT_RUN_HOST_GOOGLE_SUPPORT, 144 INSECURE_CONTENT_RUN_HOST_GOOGLE_SUPPORT,
145 INSECURE_CONTENT_DISPLAY_HOST_GOOGLE_INTL, 145 INSECURE_CONTENT_DISPLAY_HOST_GOOGLE_INTL,
146 INSECURE_CONTENT_RUN_HOST_GOOGLE_INTL, 146 INSECURE_CONTENT_RUN_HOST_GOOGLE_INTL,
147 INSECURE_CONTENT_NUM_EVENTS 147 INSECURE_CONTENT_NUM_EVENTS
148 }; 148 };
149 149
150 // Constants for mixed-content blocking. 150 // Constants for mixed-content blocking.
151 static const char kGoogleDotCom[] = "google.com"; 151 static const char kGoogleDotCom[] = "google.com";
152 152
153 static FaviconURL::IconType ToFaviconType(WebIconURL::Type type) {
154 switch (type) {
155 case WebIconURL::TypeFavicon:
156 return FaviconURL::FAVICON;
157 case WebIconURL::TypeTouch:
158 return FaviconURL::TOUCH_ICON;
159 case WebIconURL::TypeTouchPrecomposed:
160 return FaviconURL::TOUCH_PRECOMPOSED_ICON;
161 case WebIconURL::TypeInvalid:
162 return FaviconURL::INVALID_ICON;
163 }
164 return FaviconURL::INVALID_ICON;
165 }
166
167 static bool isHostInDomain(const std::string& host, const std::string& domain) { 153 static bool isHostInDomain(const std::string& host, const std::string& domain) {
168 return (EndsWith(host, domain, false) && 154 return (EndsWith(host, domain, false) &&
169 (host.length() == domain.length() || 155 (host.length() == domain.length() ||
170 (host.length() > domain.length() && 156 (host.length() > domain.length() &&
171 host[host.length() - domain.length() - 1] == '.'))); 157 host[host.length() - domain.length() - 1] == '.')));
172 } 158 }
173 159
174 namespace { 160 namespace {
175 GURL StripRef(const GURL& url) { 161 GURL StripRef(const GURL& url) {
176 GURL::Replacements replacements; 162 GURL::Replacements replacements;
(...skipping 29 matching lines...) Expand all
206 192
207 bool ChromeRenderViewObserver::OnMessageReceived(const IPC::Message& message) { 193 bool ChromeRenderViewObserver::OnMessageReceived(const IPC::Message& message) {
208 bool handled = true; 194 bool handled = true;
209 IPC_BEGIN_MESSAGE_MAP(ChromeRenderViewObserver, message) 195 IPC_BEGIN_MESSAGE_MAP(ChromeRenderViewObserver, message)
210 IPC_MESSAGE_HANDLER(ChromeViewMsg_WebUIJavaScript, OnWebUIJavaScript) 196 IPC_MESSAGE_HANDLER(ChromeViewMsg_WebUIJavaScript, OnWebUIJavaScript)
211 IPC_MESSAGE_HANDLER(ChromeViewMsg_CaptureSnapshot, OnCaptureSnapshot) 197 IPC_MESSAGE_HANDLER(ChromeViewMsg_CaptureSnapshot, OnCaptureSnapshot)
212 IPC_MESSAGE_HANDLER(ChromeViewMsg_HandleMessageFromExternalHost, 198 IPC_MESSAGE_HANDLER(ChromeViewMsg_HandleMessageFromExternalHost,
213 OnHandleMessageFromExternalHost) 199 OnHandleMessageFromExternalHost)
214 IPC_MESSAGE_HANDLER(ChromeViewMsg_JavaScriptStressTestControl, 200 IPC_MESSAGE_HANDLER(ChromeViewMsg_JavaScriptStressTestControl,
215 OnJavaScriptStressTestControl) 201 OnJavaScriptStressTestControl)
216 IPC_MESSAGE_HANDLER(IconMsg_DownloadFavicon, OnDownloadFavicon)
217 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetAllowDisplayingInsecureContent, 202 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetAllowDisplayingInsecureContent,
218 OnSetAllowDisplayingInsecureContent) 203 OnSetAllowDisplayingInsecureContent)
219 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetAllowRunningInsecureContent, 204 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetAllowRunningInsecureContent,
220 OnSetAllowRunningInsecureContent) 205 OnSetAllowRunningInsecureContent)
221 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetClientSidePhishingDetection, 206 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetClientSidePhishingDetection,
222 OnSetClientSidePhishingDetection) 207 OnSetClientSidePhishingDetection)
223 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetVisuallyDeemphasized, 208 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetVisuallyDeemphasized,
224 OnSetVisuallyDeemphasized) 209 OnSetVisuallyDeemphasized)
225 #if defined(OS_CHROMEOS) 210 #if defined(OS_CHROMEOS)
226 IPC_MESSAGE_HANDLER(ChromeViewMsg_StartFrameSniffer, OnStartFrameSniffer) 211 IPC_MESSAGE_HANDLER(ChromeViewMsg_StartFrameSniffer, OnStartFrameSniffer)
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 266
282 void ChromeRenderViewObserver::OnJavaScriptStressTestControl(int cmd, 267 void ChromeRenderViewObserver::OnJavaScriptStressTestControl(int cmd,
283 int param) { 268 int param) {
284 if (cmd == kJavaScriptStressTestSetStressRunType) { 269 if (cmd == kJavaScriptStressTestSetStressRunType) {
285 v8::Testing::SetStressRunType(static_cast<v8::Testing::StressType>(param)); 270 v8::Testing::SetStressRunType(static_cast<v8::Testing::StressType>(param));
286 } else if (cmd == kJavaScriptStressTestPrepareStressRun) { 271 } else if (cmd == kJavaScriptStressTestPrepareStressRun) {
287 v8::Testing::PrepareStressRun(param); 272 v8::Testing::PrepareStressRun(param);
288 } 273 }
289 } 274 }
290 275
291 void ChromeRenderViewObserver::OnDownloadFavicon(int id,
292 const GURL& image_url,
293 int image_size) {
294 bool data_image_failed = false;
295 if (image_url.SchemeIs("data")) {
296 SkBitmap data_image = ImageFromDataUrl(image_url);
297 data_image_failed = data_image.empty();
298 if (!data_image_failed) {
299 std::vector<SkBitmap> images(1, data_image);
300 Send(new IconHostMsg_DidDownloadFavicon(
301 routing_id(), id, image_url, false, image_size, images));
302 }
303 }
304
305 if (data_image_failed ||
306 !DownloadFavicon(id, image_url, image_size)) {
307 Send(new IconHostMsg_DidDownloadFavicon(
308 routing_id(), id, image_url, true, image_size,
309 std::vector<SkBitmap>()));
310 }
311 }
312
313 void ChromeRenderViewObserver::OnSetAllowDisplayingInsecureContent(bool allow) { 276 void ChromeRenderViewObserver::OnSetAllowDisplayingInsecureContent(bool allow) {
314 allow_displaying_insecure_content_ = allow; 277 allow_displaying_insecure_content_ = allow;
315 WebFrame* main_frame = render_view()->GetWebView()->mainFrame(); 278 WebFrame* main_frame = render_view()->GetWebView()->mainFrame();
316 if (main_frame) 279 if (main_frame)
317 main_frame->reload(); 280 main_frame->reload();
318 } 281 }
319 282
320 void ChromeRenderViewObserver::OnSetAllowRunningInsecureContent(bool allow) { 283 void ChromeRenderViewObserver::OnSetAllowRunningInsecureContent(bool allow) {
321 allow_running_insecure_content_ = allow; 284 allow_running_insecure_content_ = allow;
322 OnSetAllowDisplayingInsecureContent(allow); 285 OnSetAllowDisplayingInsecureContent(allow);
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after
673 render_view()->GetContentStateImmediately() ? 636 render_view()->GetContentStateImmediately() ?
674 0 : kDelayForCaptureMs)); 637 0 : kDelayForCaptureMs));
675 638
676 WebFrame* main_frame = render_view()->GetWebView()->mainFrame(); 639 WebFrame* main_frame = render_view()->GetWebView()->mainFrame();
677 GURL osd_url = main_frame->document().openSearchDescriptionURL(); 640 GURL osd_url = main_frame->document().openSearchDescriptionURL();
678 if (!osd_url.is_empty()) { 641 if (!osd_url.is_empty()) {
679 Send(new ChromeViewHostMsg_PageHasOSDD( 642 Send(new ChromeViewHostMsg_PageHasOSDD(
680 routing_id(), render_view()->GetPageId(), osd_url, 643 routing_id(), render_view()->GetPageId(), osd_url,
681 search_provider::AUTODETECTED_PROVIDER)); 644 search_provider::AUTODETECTED_PROVIDER));
682 } 645 }
683
684 int icon_types = WebIconURL::TypeFavicon;
685 if (chrome::kEnableTouchIcon)
686 icon_types |= WebIconURL::TypeTouchPrecomposed | WebIconURL::TypeTouch;
687
688 WebVector<WebIconURL> icon_urls =
689 render_view()->GetWebView()->mainFrame()->iconURLs(icon_types);
690 std::vector<FaviconURL> urls;
691 for (size_t i = 0; i < icon_urls.size(); i++) {
692 WebURL url = icon_urls[i].iconURL();
693 if (!url.isEmpty())
694 urls.push_back(FaviconURL(url, ToFaviconType(icon_urls[i].iconType())));
695 }
696 if (!urls.empty()) {
697 Send(new IconHostMsg_UpdateFaviconURL(
698 routing_id(), render_view()->GetPageId(), urls));
699 }
700 }
701
702 void ChromeRenderViewObserver::DidChangeIcon(WebFrame* frame,
703 WebIconURL::Type icon_type) {
704 if (frame->parent())
705 return;
706
707 if (!chrome::kEnableTouchIcon &&
708 icon_type != WebIconURL::TypeFavicon)
709 return;
710
711 WebVector<WebIconURL> icon_urls = frame->iconURLs(icon_type);
712 std::vector<FaviconURL> urls;
713 for (size_t i = 0; i < icon_urls.size(); i++) {
714 urls.push_back(FaviconURL(icon_urls[i].iconURL(),
715 ToFaviconType(icon_urls[i].iconType())));
716 }
717 Send(new IconHostMsg_UpdateFaviconURL(
718 routing_id(), render_view()->GetPageId(), urls));
719 } 646 }
720 647
721 void ChromeRenderViewObserver::DidCommitProvisionalLoad( 648 void ChromeRenderViewObserver::DidCommitProvisionalLoad(
722 WebFrame* frame, bool is_new_navigation) { 649 WebFrame* frame, bool is_new_navigation) {
723 if (!is_new_navigation) 650 if (!is_new_navigation)
724 return; 651 return;
725 652
726 CapturePageInfoLater( 653 CapturePageInfoLater(
727 true, // preliminary_capture 654 true, // preliminary_capture
728 base::TimeDelta::FromMilliseconds(kDelayForForcedCaptureMs)); 655 base::TimeDelta::FromMilliseconds(kDelayForForcedCaptureMs));
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
914 } 841 }
915 842
916 ExternalHostBindings* ChromeRenderViewObserver::GetExternalHostBindings() { 843 ExternalHostBindings* ChromeRenderViewObserver::GetExternalHostBindings() {
917 if (!external_host_bindings_.get()) { 844 if (!external_host_bindings_.get()) {
918 external_host_bindings_.reset(new ExternalHostBindings( 845 external_host_bindings_.reset(new ExternalHostBindings(
919 render_view(), routing_id())); 846 render_view(), routing_id()));
920 } 847 }
921 return external_host_bindings_.get(); 848 return external_host_bindings_.get();
922 } 849 }
923 850
924 bool ChromeRenderViewObserver::DownloadFavicon(int id,
925 const GURL& image_url,
926 int image_size) {
927 // Make sure webview was not shut down.
928 if (!render_view()->GetWebView())
929 return false;
930 // Create an image resource fetcher and assign it with a call back object.
931 image_fetchers_.push_back(linked_ptr<MultiResolutionImageResourceFetcher>(
932 new MultiResolutionImageResourceFetcher(
933 image_url, render_view()->GetWebView()->mainFrame(), id,
934 WebURLRequest::TargetIsFavicon,
935 base::Bind(&ChromeRenderViewObserver::DidDownloadFavicon,
936 base::Unretained(this), image_size))));
937 return true;
938 }
939
940 void ChromeRenderViewObserver::DidDownloadFavicon(
941 int requested_size,
942 MultiResolutionImageResourceFetcher* fetcher,
943 const std::vector<SkBitmap>& images) {
944 // Notify requester of image download status.
945 Send(new IconHostMsg_DidDownloadFavicon(routing_id(),
946 fetcher->id(),
947 fetcher->image_url(),
948 images.empty(),
949 requested_size,
950 images));
951
952 // Remove the image fetcher from our pending list. We're in the callback from
953 // MultiResolutionImageResourceFetcher, best to delay deletion.
954 ImageResourceFetcherList::iterator iter;
955 for (iter = image_fetchers_.begin(); iter != image_fetchers_.end(); ++iter) {
956 if (iter->get() == fetcher) {
957 iter->release();
958 image_fetchers_.erase(iter);
959 break;
960 }
961 }
962 MessageLoop::current()->DeleteSoon(FROM_HERE, fetcher);
963 }
964
965 SkBitmap ChromeRenderViewObserver::ImageFromDataUrl(const GURL& url) const {
966 std::string mime_type, char_set, data;
967 if (net::DataURL::Parse(url, &mime_type, &char_set, &data) && !data.empty()) {
968 // Decode the favicon using WebKit's image decoder.
969 webkit_glue::ImageDecoder decoder(
970 gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize));
971 const unsigned char* src_data =
972 reinterpret_cast<const unsigned char*>(&data[0]);
973
974 return decoder.Decode(src_data, data.size());
975 }
976 return SkBitmap();
977 }
978
979 bool ChromeRenderViewObserver::IsStrictSecurityHost(const std::string& host) { 851 bool ChromeRenderViewObserver::IsStrictSecurityHost(const std::string& host) {
980 return (strict_security_hosts_.find(host) != strict_security_hosts_.end()); 852 return (strict_security_hosts_.find(host) != strict_security_hosts_.end());
981 } 853 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698