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/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 Loading... |
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 Loading... |
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 Loading... |
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 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
681 render_view()->GetContentStateImmediately() ? | 644 render_view()->GetContentStateImmediately() ? |
682 0 : kDelayForCaptureMs)); | 645 0 : kDelayForCaptureMs)); |
683 | 646 |
684 WebFrame* main_frame = render_view()->GetWebView()->mainFrame(); | 647 WebFrame* main_frame = render_view()->GetWebView()->mainFrame(); |
685 GURL osd_url = main_frame->document().openSearchDescriptionURL(); | 648 GURL osd_url = main_frame->document().openSearchDescriptionURL(); |
686 if (!osd_url.is_empty()) { | 649 if (!osd_url.is_empty()) { |
687 Send(new ChromeViewHostMsg_PageHasOSDD( | 650 Send(new ChromeViewHostMsg_PageHasOSDD( |
688 routing_id(), render_view()->GetPageId(), osd_url, | 651 routing_id(), render_view()->GetPageId(), osd_url, |
689 search_provider::AUTODETECTED_PROVIDER)); | 652 search_provider::AUTODETECTED_PROVIDER)); |
690 } | 653 } |
691 | |
692 int icon_types = WebIconURL::TypeFavicon; | |
693 if (chrome::kEnableTouchIcon) | |
694 icon_types |= WebIconURL::TypeTouchPrecomposed | WebIconURL::TypeTouch; | |
695 | |
696 WebVector<WebIconURL> icon_urls = | |
697 render_view()->GetWebView()->mainFrame()->iconURLs(icon_types); | |
698 std::vector<FaviconURL> urls; | |
699 for (size_t i = 0; i < icon_urls.size(); i++) { | |
700 WebURL url = icon_urls[i].iconURL(); | |
701 if (!url.isEmpty()) | |
702 urls.push_back(FaviconURL(url, ToFaviconType(icon_urls[i].iconType()))); | |
703 } | |
704 if (!urls.empty()) { | |
705 Send(new IconHostMsg_UpdateFaviconURL( | |
706 routing_id(), render_view()->GetPageId(), urls)); | |
707 } | |
708 } | |
709 | |
710 void ChromeRenderViewObserver::DidChangeIcon(WebFrame* frame, | |
711 WebIconURL::Type icon_type) { | |
712 if (frame->parent()) | |
713 return; | |
714 | |
715 if (!chrome::kEnableTouchIcon && | |
716 icon_type != WebIconURL::TypeFavicon) | |
717 return; | |
718 | |
719 WebVector<WebIconURL> icon_urls = frame->iconURLs(icon_type); | |
720 std::vector<FaviconURL> urls; | |
721 for (size_t i = 0; i < icon_urls.size(); i++) { | |
722 urls.push_back(FaviconURL(icon_urls[i].iconURL(), | |
723 ToFaviconType(icon_urls[i].iconType()))); | |
724 } | |
725 Send(new IconHostMsg_UpdateFaviconURL( | |
726 routing_id(), render_view()->GetPageId(), urls)); | |
727 } | 654 } |
728 | 655 |
729 void ChromeRenderViewObserver::DidCommitProvisionalLoad( | 656 void ChromeRenderViewObserver::DidCommitProvisionalLoad( |
730 WebFrame* frame, bool is_new_navigation) { | 657 WebFrame* frame, bool is_new_navigation) { |
731 if (!is_new_navigation) | 658 if (!is_new_navigation) |
732 return; | 659 return; |
733 | 660 |
734 CapturePageInfoLater( | 661 CapturePageInfoLater( |
735 true, // preliminary_capture | 662 true, // preliminary_capture |
736 base::TimeDelta::FromMilliseconds(kDelayForForcedCaptureMs)); | 663 base::TimeDelta::FromMilliseconds(kDelayForForcedCaptureMs)); |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
912 } | 839 } |
913 | 840 |
914 ExternalHostBindings* ChromeRenderViewObserver::GetExternalHostBindings() { | 841 ExternalHostBindings* ChromeRenderViewObserver::GetExternalHostBindings() { |
915 if (!external_host_bindings_.get()) { | 842 if (!external_host_bindings_.get()) { |
916 external_host_bindings_.reset(new ExternalHostBindings( | 843 external_host_bindings_.reset(new ExternalHostBindings( |
917 render_view(), routing_id())); | 844 render_view(), routing_id())); |
918 } | 845 } |
919 return external_host_bindings_.get(); | 846 return external_host_bindings_.get(); |
920 } | 847 } |
921 | 848 |
922 bool ChromeRenderViewObserver::DownloadFavicon(int id, | |
923 const GURL& image_url, | |
924 int image_size) { | |
925 // Make sure webview was not shut down. | |
926 if (!render_view()->GetWebView()) | |
927 return false; | |
928 // Create an image resource fetcher and assign it with a call back object. | |
929 image_fetchers_.push_back(linked_ptr<MultiResolutionImageResourceFetcher>( | |
930 new MultiResolutionImageResourceFetcher( | |
931 image_url, render_view()->GetWebView()->mainFrame(), id, | |
932 WebURLRequest::TargetIsFavicon, | |
933 base::Bind(&ChromeRenderViewObserver::DidDownloadFavicon, | |
934 base::Unretained(this), image_size)))); | |
935 return true; | |
936 } | |
937 | |
938 void ChromeRenderViewObserver::DidDownloadFavicon( | |
939 int requested_size, | |
940 MultiResolutionImageResourceFetcher* fetcher, | |
941 const std::vector<SkBitmap>& images) { | |
942 // Notify requester of image download status. | |
943 Send(new IconHostMsg_DidDownloadFavicon(routing_id(), | |
944 fetcher->id(), | |
945 fetcher->image_url(), | |
946 images.empty(), | |
947 requested_size, | |
948 images)); | |
949 | |
950 // Remove the image fetcher from our pending list. We're in the callback from | |
951 // MultiResolutionImageResourceFetcher, best to delay deletion. | |
952 ImageResourceFetcherList::iterator iter; | |
953 for (iter = image_fetchers_.begin(); iter != image_fetchers_.end(); ++iter) { | |
954 if (iter->get() == fetcher) { | |
955 iter->release(); | |
956 image_fetchers_.erase(iter); | |
957 break; | |
958 } | |
959 } | |
960 MessageLoop::current()->DeleteSoon(FROM_HERE, fetcher); | |
961 } | |
962 | |
963 SkBitmap ChromeRenderViewObserver::ImageFromDataUrl(const GURL& url) const { | |
964 std::string mime_type, char_set, data; | |
965 if (net::DataURL::Parse(url, &mime_type, &char_set, &data) && !data.empty()) { | |
966 // Decode the favicon using WebKit's image decoder. | |
967 webkit_glue::ImageDecoder decoder( | |
968 gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize)); | |
969 const unsigned char* src_data = | |
970 reinterpret_cast<const unsigned char*>(&data[0]); | |
971 | |
972 return decoder.Decode(src_data, data.size()); | |
973 } | |
974 return SkBitmap(); | |
975 } | |
976 | |
977 bool ChromeRenderViewObserver::IsStrictSecurityHost(const std::string& host) { | 849 bool ChromeRenderViewObserver::IsStrictSecurityHost(const std::string& host) { |
978 return (strict_security_hosts_.find(host) != strict_security_hosts_.end()); | 850 return (strict_security_hosts_.find(host) != strict_security_hosts_.end()); |
979 } | 851 } |
OLD | NEW |