1 // Copyright (c) 2011 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/prerender/prerender_util.h"
7 #include "base/logging.h"
8 #include "base/metrics/histogram.h"
9 #include "base/metrics/sparse_histogram.h"
10 #include "base/strings/string_util.h"
11 #include "content/public/browser/resource_request_info.h"
12 #include "content/public/common/resource_type.h"
13 #include "net/http/http_response_headers.h"
14 #include "net/url_request/url_request.h"
15 #include "url/url_canon.h"
16 #include "url/url_parse.h"
17 #include "url/url_util.h"
19 using content::ResourceType
;
25 enum PrerenderSchemeCancelReason
{
26 PRERENDER_SCHEME_CANCEL_REASON_EXTERNAL_PROTOCOL
,
27 PRERENDER_SCHEME_CANCEL_REASON_DATA
,
28 PRERENDER_SCHEME_CANCEL_REASON_BLOB
,
29 PRERENDER_SCHEME_CANCEL_REASON_FILE
,
30 PRERENDER_SCHEME_CANCEL_REASON_FILESYSTEM
,
31 PRERENDER_SCHEME_CANCEL_REASON_WEBSOCKET
,
32 PRERENDER_SCHEME_CANCEL_REASON_FTP
,
33 PRERENDER_SCHEME_CANCEL_REASON_CHROME
,
34 PRERENDER_SCHEME_CANCEL_REASON_CHROME_EXTENSION
,
35 PRERENDER_SCHEME_CANCEL_REASON_ABOUT
,
36 PRERENDER_SCHEME_CANCEL_REASON_UNKNOWN
,
37 PRERENDER_SCHEME_CANCEL_REASON_MAX
,
40 void ReportPrerenderSchemeCancelReason(PrerenderSchemeCancelReason reason
) {
41 UMA_HISTOGRAM_ENUMERATION(
42 "Prerender.SchemeCancelReason", reason
,
43 PRERENDER_SCHEME_CANCEL_REASON_MAX
);
48 bool MaybeGetQueryStringBasedAliasURL(
49 const GURL
& url
, GURL
* alias_url
) {
52 url::ParseStandardURL(url
.spec().c_str(), url
.spec().length(), &parsed
);
53 url::Component query
= parsed
.query
;
54 url::Component key
, value
;
55 while (url::ExtractQueryKeyValue(url
.spec().c_str(), &query
, &key
, &value
)) {
56 if (key
.len
!= 3 || strncmp(url
.spec().c_str() + key
.begin
, "url", key
.len
))
58 // We found a url= query string component.
61 url::RawCanonOutputW
<1024> decoded_url
;
62 url::DecodeURLEscapeSequences(url
.spec().c_str() + value
.begin
, value
.len
,
64 GURL
new_url(base::string16(decoded_url
.data(), decoded_url
.length()));
65 if (!new_url
.is_empty() && new_url
.is_valid()) {
74 bool IsGoogleDomain(const GURL
& url
) {
75 return StartsWithASCII(url
.host(), std::string("www.google."), true);
78 bool IsGoogleSearchResultURL(const GURL
& url
) {
79 if (!IsGoogleDomain(url
))
81 return (url
.path().empty() ||
82 StartsWithASCII(url
.path(), std::string("/search"), true) ||
83 (url
.path() == "/") ||
84 StartsWithASCII(url
.path(), std::string("/webhp"), true));
87 void ReportPrerenderExternalURL() {
88 ReportPrerenderSchemeCancelReason(
89 PRERENDER_SCHEME_CANCEL_REASON_EXTERNAL_PROTOCOL
);
92 void ReportUnsupportedPrerenderScheme(const GURL
& url
) {
93 if (url
.SchemeIs("data")) {
94 ReportPrerenderSchemeCancelReason(PRERENDER_SCHEME_CANCEL_REASON_DATA
);
95 } else if (url
.SchemeIs("blob")) {
96 ReportPrerenderSchemeCancelReason(PRERENDER_SCHEME_CANCEL_REASON_BLOB
);
97 } else if (url
.SchemeIsFile()) {
98 ReportPrerenderSchemeCancelReason(PRERENDER_SCHEME_CANCEL_REASON_FILE
);
99 } else if (url
.SchemeIsFileSystem()) {
100 ReportPrerenderSchemeCancelReason(
101 PRERENDER_SCHEME_CANCEL_REASON_FILESYSTEM
);
102 } else if (url
.SchemeIs("ws") || url
.SchemeIs("wss")) {
103 ReportPrerenderSchemeCancelReason(PRERENDER_SCHEME_CANCEL_REASON_WEBSOCKET
);
104 } else if (url
.SchemeIs("ftp")) {
105 ReportPrerenderSchemeCancelReason(PRERENDER_SCHEME_CANCEL_REASON_FTP
);
106 } else if (url
.SchemeIs("chrome")) {
107 ReportPrerenderSchemeCancelReason(PRERENDER_SCHEME_CANCEL_REASON_CHROME
);
108 } else if (url
.SchemeIs("chrome-extension")) {
109 ReportPrerenderSchemeCancelReason(
110 PRERENDER_SCHEME_CANCEL_REASON_CHROME_EXTENSION
);
111 } else if (url
.SchemeIs("about")) {
112 ReportPrerenderSchemeCancelReason(PRERENDER_SCHEME_CANCEL_REASON_ABOUT
);
114 ReportPrerenderSchemeCancelReason(PRERENDER_SCHEME_CANCEL_REASON_UNKNOWN
);
118 } // namespace prerender