OLD | NEW |
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/extensions/extension_helper.h" | 23 #include "chrome/renderer/extensions/extension_helper.h" |
24 #include "chrome/renderer/external_host_bindings.h" | 24 #include "chrome/renderer/external_host_bindings.h" |
| 25 #include "chrome/renderer/favicon_helper.h" |
25 #include "chrome/renderer/frame_sniffer.h" | 26 #include "chrome/renderer/frame_sniffer.h" |
26 #include "chrome/renderer/prerender/prerender_helper.h" | 27 #include "chrome/renderer/prerender/prerender_helper.h" |
27 #include "chrome/renderer/safe_browsing/phishing_classifier_delegate.h" | 28 #include "chrome/renderer/safe_browsing/phishing_classifier_delegate.h" |
28 #include "chrome/renderer/translate_helper.h" | 29 #include "chrome/renderer/translate_helper.h" |
29 #include "chrome/renderer/webview_color_overlay.h" | 30 #include "chrome/renderer/webview_color_overlay.h" |
30 #include "content/public/common/bindings_policy.h" | 31 #include "content/public/common/bindings_policy.h" |
31 #include "content/public/renderer/render_view.h" | 32 #include "content/public/renderer/render_view.h" |
32 #include "content/public/renderer/content_renderer_client.h" | 33 #include "content/public/renderer/content_renderer_client.h" |
33 #include "net/base/data_url.h" | 34 #include "net/base/data_url.h" |
34 #include "skia/ext/platform_canvas.h" | 35 #include "skia/ext/platform_canvas.h" |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 replacements.ClearRef(); | 194 replacements.ClearRef(); |
194 return url.ReplaceComponents(replacements); | 195 return url.ReplaceComponents(replacements); |
195 } | 196 } |
196 } // namespace | 197 } // namespace |
197 | 198 |
198 ChromeRenderViewObserver::ChromeRenderViewObserver( | 199 ChromeRenderViewObserver::ChromeRenderViewObserver( |
199 content::RenderView* render_view, | 200 content::RenderView* render_view, |
200 ContentSettingsObserver* content_settings, | 201 ContentSettingsObserver* content_settings, |
201 ChromeRenderProcessObserver* chrome_render_process_observer, | 202 ChromeRenderProcessObserver* chrome_render_process_observer, |
202 extensions::Dispatcher* extension_dispatcher, | 203 extensions::Dispatcher* extension_dispatcher, |
203 TranslateHelper* translate_helper) | 204 TranslateHelper* translate_helper, |
| 205 FaviconHelper* favicon_helper) |
204 : content::RenderViewObserver(render_view), | 206 : content::RenderViewObserver(render_view), |
205 chrome_render_process_observer_(chrome_render_process_observer), | 207 chrome_render_process_observer_(chrome_render_process_observer), |
206 extension_dispatcher_(extension_dispatcher), | 208 extension_dispatcher_(extension_dispatcher), |
207 content_settings_(content_settings), | 209 content_settings_(content_settings), |
208 translate_helper_(translate_helper), | 210 translate_helper_(translate_helper), |
209 phishing_classifier_(NULL), | 211 phishing_classifier_(NULL), |
| 212 favicon_helper_(favicon_helper), |
210 last_indexed_page_id_(-1), | 213 last_indexed_page_id_(-1), |
211 allow_displaying_insecure_content_(false), | 214 allow_displaying_insecure_content_(false), |
212 allow_running_insecure_content_(false), | 215 allow_running_insecure_content_(false), |
213 capture_timer_(false, false) { | 216 capture_timer_(false, false) { |
214 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 217 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
215 render_view->GetWebView()->setPermissionClient(this); | 218 render_view->GetWebView()->setPermissionClient(this); |
216 if (!command_line.HasSwitch(switches::kDisableClientSidePhishingDetection)) | 219 if (!command_line.HasSwitch(switches::kDisableClientSidePhishingDetection)) |
217 OnSetClientSidePhishingDetection(true); | 220 OnSetClientSidePhishingDetection(true); |
218 } | 221 } |
219 | 222 |
220 ChromeRenderViewObserver::~ChromeRenderViewObserver() { | 223 ChromeRenderViewObserver::~ChromeRenderViewObserver() { |
221 } | 224 } |
222 | 225 |
223 bool ChromeRenderViewObserver::OnMessageReceived(const IPC::Message& message) { | 226 bool ChromeRenderViewObserver::OnMessageReceived(const IPC::Message& message) { |
224 bool handled = true; | 227 bool handled = true; |
225 IPC_BEGIN_MESSAGE_MAP(ChromeRenderViewObserver, message) | 228 IPC_BEGIN_MESSAGE_MAP(ChromeRenderViewObserver, message) |
226 IPC_MESSAGE_HANDLER(ChromeViewMsg_WebUIJavaScript, OnWebUIJavaScript) | 229 IPC_MESSAGE_HANDLER(ChromeViewMsg_WebUIJavaScript, OnWebUIJavaScript) |
227 IPC_MESSAGE_HANDLER(ChromeViewMsg_CaptureSnapshot, OnCaptureSnapshot) | 230 IPC_MESSAGE_HANDLER(ChromeViewMsg_CaptureSnapshot, OnCaptureSnapshot) |
228 IPC_MESSAGE_HANDLER(ChromeViewMsg_HandleMessageFromExternalHost, | 231 IPC_MESSAGE_HANDLER(ChromeViewMsg_HandleMessageFromExternalHost, |
229 OnHandleMessageFromExternalHost) | 232 OnHandleMessageFromExternalHost) |
230 IPC_MESSAGE_HANDLER(ChromeViewMsg_JavaScriptStressTestControl, | 233 IPC_MESSAGE_HANDLER(ChromeViewMsg_JavaScriptStressTestControl, |
231 OnJavaScriptStressTestControl) | 234 OnJavaScriptStressTestControl) |
232 IPC_MESSAGE_HANDLER(IconMsg_DownloadFavicon, OnDownloadFavicon) | |
233 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetAllowDisplayingInsecureContent, | 235 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetAllowDisplayingInsecureContent, |
234 OnSetAllowDisplayingInsecureContent) | 236 OnSetAllowDisplayingInsecureContent) |
235 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetAllowRunningInsecureContent, | 237 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetAllowRunningInsecureContent, |
236 OnSetAllowRunningInsecureContent) | 238 OnSetAllowRunningInsecureContent) |
237 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetClientSidePhishingDetection, | 239 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetClientSidePhishingDetection, |
238 OnSetClientSidePhishingDetection) | 240 OnSetClientSidePhishingDetection) |
239 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetVisuallyDeemphasized, | 241 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetVisuallyDeemphasized, |
240 OnSetVisuallyDeemphasized) | 242 OnSetVisuallyDeemphasized) |
241 #if defined(OS_CHROMEOS) | 243 #if defined(OS_CHROMEOS) |
242 IPC_MESSAGE_HANDLER(ChromeViewMsg_StartFrameSniffer, OnStartFrameSniffer) | 244 IPC_MESSAGE_HANDLER(ChromeViewMsg_StartFrameSniffer, OnStartFrameSniffer) |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 | 299 |
298 void ChromeRenderViewObserver::OnJavaScriptStressTestControl(int cmd, | 300 void ChromeRenderViewObserver::OnJavaScriptStressTestControl(int cmd, |
299 int param) { | 301 int param) { |
300 if (cmd == kJavaScriptStressTestSetStressRunType) { | 302 if (cmd == kJavaScriptStressTestSetStressRunType) { |
301 v8::Testing::SetStressRunType(static_cast<v8::Testing::StressType>(param)); | 303 v8::Testing::SetStressRunType(static_cast<v8::Testing::StressType>(param)); |
302 } else if (cmd == kJavaScriptStressTestPrepareStressRun) { | 304 } else if (cmd == kJavaScriptStressTestPrepareStressRun) { |
303 v8::Testing::PrepareStressRun(param); | 305 v8::Testing::PrepareStressRun(param); |
304 } | 306 } |
305 } | 307 } |
306 | 308 |
307 void ChromeRenderViewObserver::OnDownloadFavicon(int id, | |
308 const GURL& image_url, | |
309 int image_size) { | |
310 bool data_image_failed = false; | |
311 if (image_url.SchemeIs("data")) { | |
312 SkBitmap data_image = ImageFromDataUrl(image_url); | |
313 data_image_failed = data_image.empty(); | |
314 if (!data_image_failed) { | |
315 std::vector<SkBitmap> images(1, data_image); | |
316 Send(new IconHostMsg_DidDownloadFavicon( | |
317 routing_id(), id, image_url, false, image_size, images)); | |
318 } | |
319 } | |
320 | |
321 if (data_image_failed || | |
322 !DownloadFavicon(id, image_url, image_size)) { | |
323 Send(new IconHostMsg_DidDownloadFavicon( | |
324 routing_id(), id, image_url, true, image_size, | |
325 std::vector<SkBitmap>())); | |
326 } | |
327 } | |
328 | |
329 void ChromeRenderViewObserver::OnSetAllowDisplayingInsecureContent(bool allow) { | 309 void ChromeRenderViewObserver::OnSetAllowDisplayingInsecureContent(bool allow) { |
330 allow_displaying_insecure_content_ = allow; | 310 allow_displaying_insecure_content_ = allow; |
331 WebFrame* main_frame = render_view()->GetWebView()->mainFrame(); | 311 WebFrame* main_frame = render_view()->GetWebView()->mainFrame(); |
332 if (main_frame) | 312 if (main_frame) |
333 main_frame->reload(); | 313 main_frame->reload(); |
334 } | 314 } |
335 | 315 |
336 void ChromeRenderViewObserver::OnSetAllowRunningInsecureContent(bool allow) { | 316 void ChromeRenderViewObserver::OnSetAllowRunningInsecureContent(bool allow) { |
337 allow_running_insecure_content_ = allow; | 317 allow_running_insecure_content_ = allow; |
338 OnSetAllowDisplayingInsecureContent(allow); | 318 OnSetAllowDisplayingInsecureContent(allow); |
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
706 icon_types |= WebIconURL::TypeTouchPrecomposed | WebIconURL::TypeTouch; | 686 icon_types |= WebIconURL::TypeTouchPrecomposed | WebIconURL::TypeTouch; |
707 | 687 |
708 WebVector<WebIconURL> icon_urls = | 688 WebVector<WebIconURL> icon_urls = |
709 render_view()->GetWebView()->mainFrame()->iconURLs(icon_types); | 689 render_view()->GetWebView()->mainFrame()->iconURLs(icon_types); |
710 std::vector<FaviconURL> urls; | 690 std::vector<FaviconURL> urls; |
711 for (size_t i = 0; i < icon_urls.size(); i++) { | 691 for (size_t i = 0; i < icon_urls.size(); i++) { |
712 WebURL url = icon_urls[i].iconURL(); | 692 WebURL url = icon_urls[i].iconURL(); |
713 if (!url.isEmpty()) | 693 if (!url.isEmpty()) |
714 urls.push_back(FaviconURL(url, ToFaviconType(icon_urls[i].iconType()))); | 694 urls.push_back(FaviconURL(url, ToFaviconType(icon_urls[i].iconType()))); |
715 } | 695 } |
716 if (!urls.empty()) { | 696 favicon_helper_->SendUpdateFaviconURL( |
717 Send(new IconHostMsg_UpdateFaviconURL( | 697 routing_id(), render_view()->GetPageId(), urls); |
718 routing_id(), render_view()->GetPageId(), urls)); | |
719 } | |
720 } | 698 } |
721 | 699 |
722 void ChromeRenderViewObserver::DidChangeIcon(WebFrame* frame, | 700 void ChromeRenderViewObserver::DidChangeIcon(WebFrame* frame, |
723 WebIconURL::Type icon_type) { | 701 WebIconURL::Type icon_type) { |
724 if (frame->parent()) | 702 if (frame->parent()) |
725 return; | 703 return; |
726 | 704 |
727 if (!chrome::kEnableTouchIcon && | 705 if (!chrome::kEnableTouchIcon && |
728 icon_type != WebIconURL::TypeFavicon) | 706 icon_type != WebIconURL::TypeFavicon) |
729 return; | 707 return; |
730 | 708 |
731 WebVector<WebIconURL> icon_urls = frame->iconURLs(icon_type); | 709 WebVector<WebIconURL> icon_urls = frame->iconURLs(icon_type); |
732 std::vector<FaviconURL> urls; | 710 std::vector<FaviconURL> urls; |
733 for (size_t i = 0; i < icon_urls.size(); i++) { | 711 for (size_t i = 0; i < icon_urls.size(); i++) { |
734 urls.push_back(FaviconURL(icon_urls[i].iconURL(), | 712 urls.push_back(FaviconURL(icon_urls[i].iconURL(), |
735 ToFaviconType(icon_urls[i].iconType()))); | 713 ToFaviconType(icon_urls[i].iconType()))); |
736 } | 714 } |
737 Send(new IconHostMsg_UpdateFaviconURL( | 715 favicon_helper_->SendUpdateFaviconURL( |
738 routing_id(), render_view()->GetPageId(), urls)); | 716 routing_id(), render_view()->GetPageId(), urls); |
739 } | 717 } |
740 | 718 |
741 void ChromeRenderViewObserver::DidCommitProvisionalLoad( | 719 void ChromeRenderViewObserver::DidCommitProvisionalLoad( |
742 WebFrame* frame, bool is_new_navigation) { | 720 WebFrame* frame, bool is_new_navigation) { |
743 if (!is_new_navigation) | 721 if (!is_new_navigation) |
744 return; | 722 return; |
745 | 723 |
746 CapturePageInfoLater( | 724 CapturePageInfoLater( |
747 true, // preliminary_capture | 725 true, // preliminary_capture |
748 base::TimeDelta::FromMilliseconds(kDelayForForcedCaptureMs)); | 726 base::TimeDelta::FromMilliseconds(kDelayForForcedCaptureMs)); |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
924 } | 902 } |
925 | 903 |
926 ExternalHostBindings* ChromeRenderViewObserver::GetExternalHostBindings() { | 904 ExternalHostBindings* ChromeRenderViewObserver::GetExternalHostBindings() { |
927 if (!external_host_bindings_.get()) { | 905 if (!external_host_bindings_.get()) { |
928 external_host_bindings_.reset(new ExternalHostBindings( | 906 external_host_bindings_.reset(new ExternalHostBindings( |
929 render_view(), routing_id())); | 907 render_view(), routing_id())); |
930 } | 908 } |
931 return external_host_bindings_.get(); | 909 return external_host_bindings_.get(); |
932 } | 910 } |
933 | 911 |
934 bool ChromeRenderViewObserver::DownloadFavicon(int id, | |
935 const GURL& image_url, | |
936 int image_size) { | |
937 // Make sure webview was not shut down. | |
938 if (!render_view()->GetWebView()) | |
939 return false; | |
940 // Create an image resource fetcher and assign it with a call back object. | |
941 image_fetchers_.push_back(linked_ptr<MultiResolutionImageResourceFetcher>( | |
942 new MultiResolutionImageResourceFetcher( | |
943 image_url, render_view()->GetWebView()->mainFrame(), id, | |
944 WebURLRequest::TargetIsFavicon, | |
945 base::Bind(&ChromeRenderViewObserver::DidDownloadFavicon, | |
946 base::Unretained(this), image_size)))); | |
947 return true; | |
948 } | |
949 | |
950 void ChromeRenderViewObserver::DidDownloadFavicon( | |
951 int requested_size, | |
952 MultiResolutionImageResourceFetcher* fetcher, | |
953 const std::vector<SkBitmap>& images) { | |
954 // Notify requester of image download status. | |
955 Send(new IconHostMsg_DidDownloadFavicon(routing_id(), | |
956 fetcher->id(), | |
957 fetcher->image_url(), | |
958 images.empty(), | |
959 requested_size, | |
960 images)); | |
961 | |
962 // Remove the image fetcher from our pending list. We're in the callback from | |
963 // MultiResolutionImageResourceFetcher, best to delay deletion. | |
964 ImageResourceFetcherList::iterator iter; | |
965 for (iter = image_fetchers_.begin(); iter != image_fetchers_.end(); ++iter) { | |
966 if (iter->get() == fetcher) { | |
967 iter->release(); | |
968 image_fetchers_.erase(iter); | |
969 break; | |
970 } | |
971 } | |
972 MessageLoop::current()->DeleteSoon(FROM_HERE, fetcher); | |
973 } | |
974 | |
975 SkBitmap ChromeRenderViewObserver::ImageFromDataUrl(const GURL& url) const { | |
976 std::string mime_type, char_set, data; | |
977 if (net::DataURL::Parse(url, &mime_type, &char_set, &data) && !data.empty()) { | |
978 // Decode the favicon using WebKit's image decoder. | |
979 webkit_glue::ImageDecoder decoder( | |
980 gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize)); | |
981 const unsigned char* src_data = | |
982 reinterpret_cast<const unsigned char*>(&data[0]); | |
983 | |
984 return decoder.Decode(src_data, data.size()); | |
985 } | |
986 return SkBitmap(); | |
987 } | |
988 | |
989 bool ChromeRenderViewObserver::IsStrictSecurityHost(const std::string& host) { | 912 bool ChromeRenderViewObserver::IsStrictSecurityHost(const std::string& host) { |
990 return (strict_security_hosts_.find(host) != strict_security_hosts_.end()); | 913 return (strict_security_hosts_.find(host) != strict_security_hosts_.end()); |
991 } | 914 } |
OLD | NEW |