blob: d8b7f31ad111d691bfc036189b66a62c10016232 [file] [log] [blame]
[email protected]01239ea2012-02-19 00:11:101// 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
3// found in the LICENSE file.
4
5#include "chrome/renderer/chrome_content_renderer_client.h"
6
avi2729e442015-12-26 05:27:457#include <stddef.h>
8
megjablon050660f2016-11-02 20:11:319#include <map>
10#include <string>
jdoerrie97df4442017-05-17 16:29:3411#include <utility>
[email protected]2309e912013-10-01 01:33:3012#include <vector>
13
jdoerrie97df4442017-05-17 16:29:3414#include "base/memory/ptr_util.h"
kdsilvaaf8d2572016-08-09 17:22:2015#include "base/metrics/histogram_samples.h"
[email protected]abfd1492013-06-07 21:23:2616#include "base/strings/utf_string_conversions.h"
kdsilvaaf8d2572016-08-09 17:22:2017#include "base/test/histogram_tester.h"
jdoerrie97df4442017-05-17 16:29:3418#include "base/values.h"
avi2729e442015-12-26 05:27:4519#include "build/build_config.h"
[email protected]2309e912013-10-01 01:33:3020#include "chrome/renderer/searchbox/search_bouncer.h"
megjablon050660f2016-11-02 20:11:3121#include "components/data_reduction_proxy/core/common/data_reduction_proxy_headers.h"
[email protected]d7bd3e52013-07-21 04:29:2022#include "content/public/common/webplugininfo.h"
brettw00899e62016-11-12 02:10:1723#include "extensions/features/features.h"
thestig52a87b3b2014-10-23 22:02:3824#include "testing/gtest/include/gtest/gtest.h"
megjablon050660f2016-11-02 20:11:3125#include "third_party/WebKit/public/platform/WebString.h"
26#include "third_party/WebKit/public/platform/WebURLResponse.h"
thestig52a87b3b2014-10-23 22:02:3827#include "url/gurl.h"
28
brettw00899e62016-11-12 02:10:1729#if BUILDFLAG(ENABLE_EXTENSIONS)
[email protected]e4452d32013-11-15 23:07:4130#include "extensions/common/extension.h"
[email protected]22b7b2c2013-11-05 22:52:4231#include "extensions/common/extension_builder.h"
[email protected]6bf90612013-08-15 00:36:2732#include "extensions/common/manifest_constants.h"
thestig52a87b3b2014-10-23 22:02:3833#endif
34
35#if !defined(DISABLE_NACL)
[email protected]e48cf4f2013-05-30 12:40:1236#include "third_party/WebKit/public/platform/WebString.h"
37#include "third_party/WebKit/public/platform/WebVector.h"
[email protected]2255a9332013-06-17 05:12:3138#include "third_party/WebKit/public/web/WebPluginParams.h"
thestig52a87b3b2014-10-23 22:02:3839#endif
[email protected]01239ea2012-02-19 00:11:1040
thestig52a87b3b2014-10-23 22:02:3841#if !defined(DISABLE_NACL)
[email protected]a1221aea2013-11-07 01:31:3042using blink::WebPluginParams;
43using blink::WebString;
44using blink::WebVector;
thestig52a87b3b2014-10-23 22:02:3845#endif
46
[email protected]d7bd3e52013-07-21 04:29:2047using content::WebPluginInfo;
48using content::WebPluginMimeType;
[email protected]01239ea2012-02-19 00:11:1049
[email protected]01239ea2012-02-19 00:11:1050namespace {
thestig52a87b3b2014-10-23 22:02:3851
52#if !defined(DISABLE_NACL)
[email protected]01239ea2012-02-19 00:11:1053const bool kNaClRestricted = false;
54const bool kNaClUnrestricted = true;
[email protected]01239ea2012-02-19 00:11:1055const bool kExtensionNotFromWebStore = false;
56const bool kExtensionFromWebStore = true;
thestig52a87b3b2014-10-23 22:02:3857#endif
58
brettw00899e62016-11-12 02:10:1759#if BUILDFLAG(ENABLE_EXTENSIONS)
[email protected]3aacd9482013-03-19 09:49:3660const bool kNotHostedApp = false;
61const bool kHostedApp = true;
thestig52a87b3b2014-10-23 22:02:3862#endif
[email protected]01239ea2012-02-19 00:11:1063
thestig52a87b3b2014-10-23 22:02:3864#if !defined(DISABLE_NACL)
[email protected]e3cb4522013-03-07 22:18:4565const char kExtensionUrl[] = "chrome-extension://extension_id/background.html";
[email protected]01239ea2012-02-19 00:11:1066
qaz81dcbd62016-05-20 21:36:0867const char kChatManifestFS[] = "filesystem:https://talkgadget.google.com/foo";
thestig52a87b3b2014-10-23 22:02:3868#endif
[email protected]7ca58f22013-06-24 08:21:2769
qaz81dcbd62016-05-20 21:36:0870const char kChatAppURL[] = "https://talkgadget.google.com/hangouts/foo";
thestig52a87b3b2014-10-23 22:02:3871
72#if !defined(DISABLE_NACL)
[email protected]01239ea2012-02-19 00:11:1073bool AllowsDevInterfaces(const WebPluginParams& params) {
Blink Reformat1c4d759e2017-04-09 16:34:5474 for (size_t i = 0; i < params.attribute_names.size(); ++i) {
75 if (params.attribute_names[i] == "@dev")
[email protected]01239ea2012-02-19 00:11:1076 return true;
77 }
78 return false;
79}
80
81void AddFakeDevAttribute(WebPluginParams* params) {
82 WebVector<WebString> names(static_cast<size_t>(1));
83 WebVector<WebString> values(static_cast<size_t>(1));
Blink Reformat1c4d759e2017-04-09 16:34:5484 names[0] = WebString::FromUTF8("@dev");
[email protected]01239ea2012-02-19 00:11:1085 values[0] = WebString();
Blink Reformat1c4d759e2017-04-09 16:34:5486 params->attribute_names.Swap(names);
87 params->attribute_values.Swap(values);
[email protected]01239ea2012-02-19 00:11:1088}
thestig52a87b3b2014-10-23 22:02:3889#endif
[email protected]01239ea2012-02-19 00:11:1090
[email protected]d7bd3e52013-07-21 04:29:2091void AddContentTypeHandler(content::WebPluginInfo* info,
[email protected]01239ea2012-02-19 00:11:1092 const char* mime_type,
93 const char* manifest_url) {
[email protected]d7bd3e52013-07-21 04:29:2094 content::WebPluginMimeType mime_type_info;
[email protected]01239ea2012-02-19 00:11:1095 mime_type_info.mime_type = mime_type;
[email protected]c91b9d42013-12-25 00:53:4296 mime_type_info.additional_param_names.push_back(base::UTF8ToUTF16("nacl"));
[email protected]01239ea2012-02-19 00:11:1097 mime_type_info.additional_param_values.push_back(
[email protected]c91b9d42013-12-25 00:53:4298 base::UTF8ToUTF16(manifest_url));
[email protected]01239ea2012-02-19 00:11:1099 info->mime_types.push_back(mime_type_info);
100}
thestig52a87b3b2014-10-23 22:02:38101
[email protected]01239ea2012-02-19 00:11:10102} // namespace
103
104typedef testing::Test ChromeContentRendererClientTest;
105
[email protected]3aacd9482013-03-19 09:49:36106
brettw00899e62016-11-12 02:10:17107#if BUILDFLAG(ENABLE_EXTENSIONS)
[email protected]3aacd9482013-03-19 09:49:36108scoped_refptr<const extensions::Extension> CreateTestExtension(
elijahtaylordea8f6522015-06-23 18:52:50109 extensions::Manifest::Location location, bool is_from_webstore,
110 bool is_hosted_app, const std::string& app_url) {
[email protected]3aacd9482013-03-19 09:49:36111 int flags = is_from_webstore ?
112 extensions::Extension::FROM_WEBSTORE:
113 extensions::Extension::NO_FLAGS;
114
[email protected]a371b4b2013-06-18 20:29:27115 base::DictionaryValue manifest;
[email protected]3aacd9482013-03-19 09:49:36116 manifest.SetString("name", "NaCl Extension");
117 manifest.SetString("version", "1");
118 manifest.SetInteger("manifest_version", 2);
119 if (is_hosted_app) {
jdoerrie97df4442017-05-17 16:29:34120 auto url_list = base::MakeUnique<base::ListValue>();
dchengd9ea63862016-06-03 02:27:18121 url_list->AppendString(app_url);
jdoerrie97df4442017-05-17 16:29:34122 manifest.Set(extensions::manifest_keys::kWebURLs, std::move(url_list));
[email protected]6bf90612013-08-15 00:36:27123 manifest.SetString(extensions::manifest_keys::kLaunchWebURL, app_url);
[email protected]3aacd9482013-03-19 09:49:36124 }
125 std::string error;
126 return extensions::Extension::Create(base::FilePath(), location, manifest,
127 flags, &error);
128}
129
130scoped_refptr<const extensions::Extension> CreateExtension(
elijahtaylordea8f6522015-06-23 18:52:50131 bool is_from_webstore) {
[email protected]007b3f82013-04-09 08:46:45132 return CreateTestExtension(
elijahtaylordea8f6522015-06-23 18:52:50133 extensions::Manifest::INTERNAL, is_from_webstore, kNotHostedApp,
134 std::string());
135}
136
137scoped_refptr<const extensions::Extension> CreateExtensionWithLocation(
138 extensions::Manifest::Location location, bool is_from_webstore) {
139 return CreateTestExtension(
140 location, is_from_webstore, kNotHostedApp, std::string());
[email protected]3aacd9482013-03-19 09:49:36141}
142
143scoped_refptr<const extensions::Extension> CreateHostedApp(
elijahtaylordea8f6522015-06-23 18:52:50144 bool is_from_webstore, const std::string& app_url) {
145 return CreateTestExtension(extensions::Manifest::INTERNAL,
146 is_from_webstore,
147 kHostedApp,
[email protected]3aacd9482013-03-19 09:49:36148 app_url);
149}
brettw00899e62016-11-12 02:10:17150#endif // BUILDFLAG(ENABLE_EXTENSIONS)
[email protected]3aacd9482013-03-19 09:49:36151
[email protected]01239ea2012-02-19 00:11:10152TEST_F(ChromeContentRendererClientTest, NaClRestriction) {
153 // Unknown content types have no NaCl module.
154 {
155 WebPluginInfo info;
156 EXPECT_EQ(GURL(),
157 ChromeContentRendererClient::GetNaClContentHandlerURL(
158 "application/x-foo", info));
159 }
160 // Known content types have a NaCl module.
161 {
162 WebPluginInfo info;
163 AddContentTypeHandler(&info, "application/x-foo", "www.foo.com");
164 EXPECT_EQ(GURL("www.foo.com"),
165 ChromeContentRendererClient::GetNaClContentHandlerURL(
166 "application/x-foo", info));
167 }
thestig52a87b3b2014-10-23 22:02:38168#if !defined(DISABLE_NACL)
[email protected]01239ea2012-02-19 00:11:10169 // --enable-nacl allows all NaCl apps, with 'dev' interfaces.
170 {
171 WebPluginParams params;
172 EXPECT_TRUE(ChromeContentRendererClient::IsNaClAllowed(
[email protected]cadac622013-06-11 16:46:36173 GURL(),
174 GURL(),
175 kNaClUnrestricted,
elijahtaylordea8f6522015-06-23 18:52:50176 CreateExtension(kExtensionNotFromWebStore).get(),
[email protected]3aacd9482013-03-19 09:49:36177 &params));
[email protected]01239ea2012-02-19 00:11:10178 EXPECT_TRUE(AllowsDevInterfaces(params));
179 }
elijahtaylordea8f6522015-06-23 18:52:50180 // Unpacked extensions are allowed without --enable-nacl, with
181 // 'dev' interfaces.
[email protected]01239ea2012-02-19 00:11:10182 {
183 WebPluginParams params;
184 EXPECT_TRUE(ChromeContentRendererClient::IsNaClAllowed(
[email protected]cadac622013-06-11 16:46:36185 GURL(),
186 GURL(kExtensionUrl),
187 kNaClRestricted,
elijahtaylordea8f6522015-06-23 18:52:50188 CreateExtensionWithLocation(extensions::Manifest::UNPACKED,
189 kExtensionNotFromWebStore).get(),
[email protected]3aacd9482013-03-19 09:49:36190 &params));
[email protected]01239ea2012-02-19 00:11:10191 EXPECT_TRUE(AllowsDevInterfaces(params));
192 }
elijahtaylordea8f6522015-06-23 18:52:50193 // Component extensions are allowed without --enable-nacl, with
194 // 'dev' interfaces.
195 {
196 WebPluginParams params;
197 EXPECT_TRUE(ChromeContentRendererClient::IsNaClAllowed(
198 GURL(),
199 GURL(kExtensionUrl),
200 kNaClRestricted,
201 CreateExtensionWithLocation(extensions::Manifest::COMPONENT,
202 kExtensionNotFromWebStore).get(),
203 &params));
204 EXPECT_TRUE(AllowsDevInterfaces(params));
205 }
206 {
207 WebPluginParams params;
208 EXPECT_TRUE(ChromeContentRendererClient::IsNaClAllowed(
209 GURL(),
210 GURL(kExtensionUrl),
211 kNaClRestricted,
212 CreateExtensionWithLocation(extensions::Manifest::EXTERNAL_COMPONENT,
213 kExtensionNotFromWebStore).get(),
214 &params));
215 EXPECT_TRUE(AllowsDevInterfaces(params));
216 }
217 // Extensions that are force installed by policy are allowed without
218 // --enable-nacl, without 'dev' interfaces.
219 {
220 WebPluginParams params;
221 EXPECT_TRUE(ChromeContentRendererClient::IsNaClAllowed(
222 GURL(),
223 GURL(kExtensionUrl),
224 kNaClRestricted,
225 CreateExtensionWithLocation(extensions::Manifest::EXTERNAL_POLICY,
226 kExtensionNotFromWebStore).get(),
227 &params));
228 EXPECT_FALSE(AllowsDevInterfaces(params));
229 EXPECT_TRUE(ChromeContentRendererClient::IsNaClAllowed(
230 GURL(),
231 GURL(kExtensionUrl),
232 kNaClRestricted,
233 CreateExtensionWithLocation(
234 extensions::Manifest::EXTERNAL_POLICY_DOWNLOAD,
235 kExtensionNotFromWebStore).get(),
236 &params));
237 EXPECT_FALSE(AllowsDevInterfaces(params));
238 }
[email protected]01239ea2012-02-19 00:11:10239 // CWS extensions are allowed without --enable-nacl, without 'dev'
[email protected]e3cb4522013-03-07 22:18:45240 // interfaces if called from an extension url.
[email protected]01239ea2012-02-19 00:11:10241 {
242 WebPluginParams params;
243 EXPECT_TRUE(ChromeContentRendererClient::IsNaClAllowed(
[email protected]cadac622013-06-11 16:46:36244 GURL(),
245 GURL(kExtensionUrl),
246 kNaClRestricted,
elijahtaylordea8f6522015-06-23 18:52:50247 CreateExtension(kExtensionFromWebStore).get(),
[email protected]3aacd9482013-03-19 09:49:36248 &params));
[email protected]01239ea2012-02-19 00:11:10249 EXPECT_FALSE(AllowsDevInterfaces(params));
250 }
251 // CWS extensions can't get 'dev' interfaces with --enable-nacl.
252 {
253 WebPluginParams params;
254 EXPECT_TRUE(ChromeContentRendererClient::IsNaClAllowed(
[email protected]cadac622013-06-11 16:46:36255 GURL(),
256 GURL(kExtensionUrl),
257 kNaClUnrestricted,
elijahtaylordea8f6522015-06-23 18:52:50258 CreateExtension(kExtensionFromWebStore).get(),
[email protected]3aacd9482013-03-19 09:49:36259 &params));
[email protected]01239ea2012-02-19 00:11:10260 EXPECT_FALSE(AllowsDevInterfaces(params));
261 }
262 // CWS extensions can't get 'dev' interfaces by injecting a fake
263 // '@dev' attribute.
264 {
265 WebPluginParams params;
266 AddFakeDevAttribute(&params);
267 EXPECT_TRUE(ChromeContentRendererClient::IsNaClAllowed(
[email protected]cadac622013-06-11 16:46:36268 GURL(),
269 GURL(kExtensionUrl),
270 kNaClRestricted,
elijahtaylordea8f6522015-06-23 18:52:50271 CreateExtension(kExtensionFromWebStore).get(),
[email protected]3aacd9482013-03-19 09:49:36272 &params));
[email protected]01239ea2012-02-19 00:11:10273 EXPECT_FALSE(AllowsDevInterfaces(params));
274 }
elijahtaylordea8f6522015-06-23 18:52:50275
[email protected]01239ea2012-02-19 00:11:10276 // Whitelisted URLs are allowed without --enable-nacl, without 'dev'
[email protected]7ca58f22013-06-24 08:21:27277 // interfaces. There is a whitelist for the app URL and the manifest URL.
[email protected]01239ea2012-02-19 00:11:10278 {
279 WebPluginParams params;
[email protected]e533ae12013-09-17 16:04:45280 // Whitelisted Chat app is allowed.
281 EXPECT_TRUE(ChromeContentRendererClient::IsNaClAllowed(
qaz81dcbd62016-05-20 21:36:08282 GURL(kChatManifestFS),
283 GURL(kChatAppURL),
[email protected]1864d422013-12-16 13:06:00284 kNaClRestricted,
elijahtaylordea8f6522015-06-23 18:52:50285 nullptr,
[email protected]1864d422013-12-16 13:06:00286 &params));
[email protected]7ca58f22013-06-24 08:21:27287
288 // Whitelisted manifest URL, bad app URLs, NOT allowed.
[email protected]a5716202013-01-30 10:00:26289 EXPECT_FALSE(ChromeContentRendererClient::IsNaClAllowed(
bradnelson52deea2a2017-02-27 18:06:29290 GURL(kChatManifestFS),
[email protected]7ca58f22013-06-24 08:21:27291 GURL("http://plus.google.com/foo"), // http scheme
bradnelson52deea2a2017-02-27 18:06:29292 kNaClRestricted, nullptr, &params));
[email protected]a5716202013-01-30 10:00:26293 EXPECT_FALSE(ChromeContentRendererClient::IsNaClAllowed(
bradnelson52deea2a2017-02-27 18:06:29294 GURL(kChatManifestFS),
[email protected]7ca58f22013-06-24 08:21:27295 GURL("http://plus.sandbox.google.com/foo"), // http scheme
bradnelson52deea2a2017-02-27 18:06:29296 kNaClRestricted, nullptr, &params));
[email protected]7ca58f22013-06-24 08:21:27297 EXPECT_FALSE(ChromeContentRendererClient::IsNaClAllowed(
bradnelson52deea2a2017-02-27 18:06:29298 GURL(kChatManifestFS),
[email protected]7ca58f22013-06-24 08:21:27299 GURL("https://plus.google.evil.com/foo"), // bad host
bradnelson52deea2a2017-02-27 18:06:29300 kNaClRestricted, nullptr, &params));
[email protected]7ca58f22013-06-24 08:21:27301 // Whitelisted app URL, bad manifest URL, NOT allowed.
302 EXPECT_FALSE(ChromeContentRendererClient::IsNaClAllowed(
303 GURL("http://ssl.gstatic.com/s2/oz/nacl/foo"), // http scheme
bradnelson52deea2a2017-02-27 18:06:29304 GURL(kChatAppURL), kNaClRestricted, nullptr, &params));
[email protected]7ca58f22013-06-24 08:21:27305 EXPECT_FALSE(ChromeContentRendererClient::IsNaClAllowed(
306 GURL("https://ssl.gstatic.evil.com/s2/oz/nacl/foo"), // bad host
bradnelson52deea2a2017-02-27 18:06:29307 GURL(kChatAppURL), kNaClRestricted, nullptr, &params));
[email protected]7ca58f22013-06-24 08:21:27308 EXPECT_FALSE(ChromeContentRendererClient::IsNaClAllowed(
309 GURL("https://ssl.gstatic.com/wrong/s2/oz/nacl/foo"), // bad path
bradnelson52deea2a2017-02-27 18:06:29310 GURL(kChatAppURL), kNaClRestricted, nullptr, &params));
[email protected]01239ea2012-02-19 00:11:10311 }
312 // Whitelisted URLs can't get 'dev' interfaces with --enable-nacl.
313 {
314 WebPluginParams params;
315 EXPECT_TRUE(ChromeContentRendererClient::IsNaClAllowed(
bradnelson52deea2a2017-02-27 18:06:29316 GURL(kChatManifestFS), GURL(kChatAppURL), kNaClUnrestricted, nullptr,
[email protected]01239ea2012-02-19 00:11:10317 &params));
318 EXPECT_FALSE(AllowsDevInterfaces(params));
319 }
320 // Whitelisted URLs can't get 'dev' interfaces by injecting a fake
321 // '@dev' attribute.
322 {
323 WebPluginParams params;
324 AddFakeDevAttribute(&params);
325 EXPECT_TRUE(ChromeContentRendererClient::IsNaClAllowed(
bradnelson52deea2a2017-02-27 18:06:29326 GURL(kChatManifestFS), GURL(kChatAppURL), kNaClRestricted, nullptr,
[email protected]01239ea2012-02-19 00:11:10327 &params));
328 EXPECT_FALSE(AllowsDevInterfaces(params));
329 }
330 // Non-whitelisted URLs are blocked without --enable-nacl.
331 {
332 WebPluginParams params;
333 EXPECT_FALSE(ChromeContentRendererClient::IsNaClAllowed(
[email protected]cadac622013-06-11 16:46:36334 GURL(),
335 GURL("https://plus.google.com.evil.com/foo1"),
[email protected]3aacd9482013-03-19 09:49:36336 kNaClRestricted,
elijahtaylordea8f6522015-06-23 18:52:50337 nullptr,
[email protected]3aacd9482013-03-19 09:49:36338 &params));
339 }
elijahtaylordea8f6522015-06-23 18:52:50340 // Non chrome-extension:// URLs belonging to hosted apps are allowed for
341 // webstore installed hosted apps.
[email protected]3aacd9482013-03-19 09:49:36342 {
343 WebPluginParams params;
344 EXPECT_TRUE(ChromeContentRendererClient::IsNaClAllowed(
[email protected]cadac622013-06-11 16:46:36345 GURL(),
346 GURL("http://example.com/test.html"),
[email protected]3aacd9482013-03-19 09:49:36347 kNaClRestricted,
elijahtaylordea8f6522015-06-23 18:52:50348 CreateHostedApp(kExtensionFromWebStore,
349 "http://example.com/").get(),
350 &params));
351 EXPECT_FALSE(ChromeContentRendererClient::IsNaClAllowed(
352 GURL(),
353 GURL("http://example.com/test.html"),
354 kNaClRestricted,
355 CreateHostedApp(kExtensionNotFromWebStore,
[email protected]cadac622013-06-11 16:46:36356 "http://example.com/").get(),
[email protected]3aacd9482013-03-19 09:49:36357 &params));
358 EXPECT_FALSE(ChromeContentRendererClient::IsNaClAllowed(
[email protected]cadac622013-06-11 16:46:36359 GURL(),
360 GURL("http://example.evil.com/test.html"),
[email protected]3aacd9482013-03-19 09:49:36361 kNaClRestricted,
elijahtaylordea8f6522015-06-23 18:52:50362 CreateHostedApp(kExtensionNotFromWebStore,
[email protected]cadac622013-06-11 16:46:36363 "http://example.com/").get(),
[email protected]e3cb4522013-03-07 22:18:45364 &params));
[email protected]01239ea2012-02-19 00:11:10365 }
thestig52a87b3b2014-10-23 22:02:38366#endif // !defined(DISABLE_NACL)
[email protected]01239ea2012-02-19 00:11:10367}
[email protected]01239ea2012-02-19 00:11:10368
[email protected]e533ae12013-09-17 16:04:45369TEST_F(ChromeContentRendererClientTest, AllowPepperMediaStreamAPI) {
370 ChromeContentRendererClient test;
qaz81dcbd62016-05-20 21:36:08371#if defined(OS_ANDROID)
372 EXPECT_FALSE(test.AllowPepperMediaStreamAPI(GURL(kChatAppURL)));
[email protected]e533ae12013-09-17 16:04:45373#else
qaz81dcbd62016-05-20 21:36:08374 EXPECT_TRUE(test.AllowPepperMediaStreamAPI(GURL(kChatAppURL)));
[email protected]e533ae12013-09-17 16:04:45375#endif
[email protected]e533ae12013-09-17 16:04:45376}
377
[email protected]2309e912013-10-01 01:33:30378TEST_F(ChromeContentRendererClientTest, ShouldSuppressErrorPage) {
379 ChromeContentRendererClient client;
bmcquadefab65e32015-07-14 14:39:32380 SearchBouncer::GetInstance()->OnSetSearchURLs(
[email protected]2309e912013-10-01 01:33:30381 std::vector<GURL>(), GURL("http://example.com/n"));
elijahtaylordea8f6522015-06-23 18:52:50382 EXPECT_FALSE(client.ShouldSuppressErrorPage(nullptr,
[email protected]24a57cc2014-01-10 22:45:30383 GURL("http://example.com")));
elijahtaylordea8f6522015-06-23 18:52:50384 EXPECT_TRUE(client.ShouldSuppressErrorPage(nullptr,
[email protected]24a57cc2014-01-10 22:45:30385 GURL("http://example.com/n")));
bmcquadefab65e32015-07-14 14:39:32386 SearchBouncer::GetInstance()->OnSetSearchURLs(
387 std::vector<GURL>(), GURL::EmptyGURL());
[email protected]2309e912013-10-01 01:33:30388}
kdsilvae0135592016-08-09 17:08:11389
megjablon050660f2016-11-02 20:11:31390TEST_F(ChromeContentRendererClientTest, AddImageContextMenuProperties) {
391 ChromeContentRendererClient client;
392 blink::WebURLResponse web_url_response;
Blink Reformat1c4d759e2017-04-09 16:34:54393 web_url_response.AddHTTPHeaderField(
394 blink::WebString::FromUTF8(
megjablon050660f2016-11-02 20:11:31395 data_reduction_proxy::chrome_proxy_content_transform_header()),
Blink Reformat1c4d759e2017-04-09 16:34:54396 blink::WebString::FromUTF8(
megjablon050660f2016-11-02 20:11:31397 data_reduction_proxy::empty_image_directive()));
398 std::map<std::string, std::string> properties;
399 client.AddImageContextMenuProperties(web_url_response, &properties);
400 EXPECT_EQ(
401 data_reduction_proxy::empty_image_directive(),
402 properties
403 [data_reduction_proxy::chrome_proxy_content_transform_header()]);
404}
405
kdsilva621478c2016-08-09 17:31:11406// These are tests that are common for both Android and desktop browsers.
407TEST_F(ChromeContentRendererClientTest, RewriteEmbedCommon) {
kdsilvae0135592016-08-09 17:08:11408 struct TestData {
409 std::string original;
410 std::string expected;
411 } test_data[] = {
412 // { original, expected }
413 {"youtube.com", ""},
414 {"www.youtube.com", ""},
415 {"http://www.youtube.com", ""},
416 {"https://www.youtube.com", ""},
417 {"http://www.foo.youtube.com", ""},
418 {"https://www.foo.youtube.com", ""},
419 // Non-YouTube domains shouldn't be modified
420 {"http://www.plus.google.com", ""},
421 // URL isn't using Flash
422 {"http://www.youtube.com/embed/deadbeef", ""},
423 // URL isn't using Flash, no www
424 {"http://youtube.com/embed/deadbeef", ""},
kdsilvae0135592016-08-09 17:08:11425 // URL isn't using Flash, invalid parameter construct
426 {"http://www.youtube.com/embed/deadbeef&start=4", ""},
427 // URL is using Flash, no www
428 {"http://youtube.com/v/deadbeef", "http://youtube.com/embed/deadbeef"},
kdsilvae0135592016-08-09 17:08:11429 // URL is using Flash, is valid, https
430 {"https://www.youtube.com/v/deadbeef",
431 "https://www.youtube.com/embed/deadbeef"},
432 // URL is using Flash, is valid, http
433 {"http://www.youtube.com/v/deadbeef",
434 "http://www.youtube.com/embed/deadbeef"},
435 // URL is using Flash, is valid, not a complete URL, no www or protocol
436 {"youtube.com/v/deadbeef", ""},
437 // URL is using Flash, is valid, not a complete URL,or protocol
438 {"www.youtube.com/v/deadbeef", ""},
439 // URL is using Flash, valid
440 {"https://www.foo.youtube.com/v/deadbeef",
441 "https://www.foo.youtube.com/embed/deadbeef"},
442 // URL is using Flash, is valid, has one parameter
443 {"http://www.youtube.com/v/deadbeef?start=4",
444 "http://www.youtube.com/embed/deadbeef?start=4"},
445 // URL is using Flash, is valid, has multiple parameters
446 {"http://www.youtube.com/v/deadbeef?start=4&fs=1",
447 "http://www.youtube.com/embed/deadbeef?start=4&fs=1"},
448 // URL is using Flash, invalid parameter construct, has one parameter
449 {"http://www.youtube.com/v/deadbeef&start=4",
450 "http://www.youtube.com/embed/deadbeef?start=4"},
451 // URL is using Flash, invalid parameter construct, has multiple
452 // parameters
453 {"http://www.youtube.com/v/deadbeef&start=4&fs=1?foo=bar",
454 "http://www.youtube.com/embed/deadbeef?start=4&fs=1&foo=bar"},
455 // URL is using Flash, invalid parameter construct, has multiple
456 // parameters
457 {"http://www.youtube.com/v/deadbeef&start=4&fs=1",
458 "http://www.youtube.com/embed/deadbeef?start=4&fs=1"},
459 // Invalid parameter construct
460 {"http://www.youtube.com/abcd/v/deadbeef", ""},
461 // Invalid parameter construct
462 {"http://www.youtube.com/v/abcd/", "http://www.youtube.com/embed/abcd/"},
463 // Invalid parameter construct
464 {"http://www.youtube.com/v/123/", "http://www.youtube.com/embed/123/"},
465 // youtube-nocookie.com
466 {"http://www.youtube-nocookie.com/v/123/",
467 "http://www.youtube-nocookie.com/embed/123/"},
468 // youtube-nocookie.com, isn't using flash
469 {"http://www.youtube-nocookie.com/embed/123/", ""},
kdsilvae0135592016-08-09 17:08:11470 // youtube-nocookie.com, has one parameter
471 {"http://www.youtube-nocookie.com/v/123?start=foo",
472 "http://www.youtube-nocookie.com/embed/123?start=foo"},
473 // youtube-nocookie.com, has multiple parameters
474 {"http://www.youtube-nocookie.com/v/123?start=foo&bar=baz",
475 "http://www.youtube-nocookie.com/embed/123?start=foo&bar=baz"},
476 // youtube-nocookie.com, invalid parameter construct, has one parameter
477 {"http://www.youtube-nocookie.com/v/123&start=foo",
478 "http://www.youtube-nocookie.com/embed/123?start=foo"},
479 // youtube-nocookie.com, invalid parameter construct, has multiple
480 // parameters
481 {"http://www.youtube-nocookie.com/v/123&start=foo&bar=baz",
482 "http://www.youtube-nocookie.com/embed/123?start=foo&bar=baz"},
483 // youtube-nocookie.com, https
484 {"https://www.youtube-nocookie.com/v/123/",
485 "https://www.youtube-nocookie.com/embed/123/"},
486 };
487
488 ChromeContentRendererClient client;
489
490 for (const auto& data : test_data)
491 EXPECT_EQ(GURL(data.expected),
492 client.OverrideFlashEmbedWithHTML(GURL(data.original)));
493}
kdsilvaaf8d2572016-08-09 17:22:20494
kdsilva621478c2016-08-09 17:31:11495#if defined(OS_ANDROID)
496TEST_F(ChromeContentRendererClientTest, RewriteEmbedAndroid) {
497 struct TestData {
498 std::string original;
499 std::string expected;
500 } test_data[] = {
501 // URL isn't using Flash, has JS API enabled
502 {"http://www.youtube.com/embed/deadbeef?enablejsapi=1", ""},
503 // URL is using Flash, has JS API enabled
504 {"http://www.youtube.com/v/deadbeef?enablejsapi=1",
505 "http://www.youtube.com/embed/deadbeef?enablejsapi=1"},
506 // youtube-nocookie.com, has JS API enabled
507 {"http://www.youtube-nocookie.com/v/123?enablejsapi=1",
508 "http://www.youtube-nocookie.com/embed/123?enablejsapi=1"},
509 // URL is using Flash, has JS API enabled, invalid parameter construct
510 {"http://www.youtube.com/v/deadbeef&enablejsapi=1",
511 "http://www.youtube.com/embed/deadbeef?enablejsapi=1"},
512 // URL is using Flash, has JS API enabled, invalid parameter construct,
513 // has multiple parameters
514 {"http://www.youtube.com/v/deadbeef&start=4&enablejsapi=1",
515 "http://www.youtube.com/embed/deadbeef?start=4&enablejsapi=1"},
516 };
517
518 ChromeContentRendererClient client;
519
520 for (const auto& data : test_data) {
521 EXPECT_EQ(GURL(data.expected),
522 client.OverrideFlashEmbedWithHTML(GURL(data.original)));
523 }
524}
525#else
526TEST_F(ChromeContentRendererClientTest, RewriteEmbedDesktop) {
527 struct TestData {
528 std::string original;
529 std::string expected;
530 } test_data[] = {
531 // URL isn't using Flash, has JS API enabled
532 {"http://www.youtube.com/embed/deadbeef?enablejsapi=1", ""},
533 // URL is using Flash, has JS API enabled
534 {"http://www.youtube.com/v/deadbeef?enablejsapi=1", ""},
535 // youtube-nocookie.com, has JS API enabled
536 {"http://www.youtube-nocookie.com/v/123?enablejsapi=1", ""},
537 // URL is using Flash, has JS API enabled, invalid parameter construct
538 {"http://www.youtube.com/v/deadbeef&enablejsapi=1", ""},
539 // URL is using Flash, has JS API enabled, invalid parameter construct,
540 // has multiple parameters
541 {"http://www.youtube.com/v/deadbeef&start=4&enablejsapi=1", ""},
542 };
543
544 ChromeContentRendererClient client;
545
546 for (const auto& data : test_data) {
547 EXPECT_EQ(GURL(data.expected),
548 client.OverrideFlashEmbedWithHTML(GURL(data.original)));
549 }
550}
551#endif
kdsilvaaf8d2572016-08-09 17:22:20552class ChromeContentRendererClientMetricsTest : public testing::Test {
553 public:
554 ChromeContentRendererClientMetricsTest() = default;
555
556 std::unique_ptr<base::HistogramSamples> GetHistogramSamples() {
557 return histogram_tester_.GetHistogramSamplesSinceCreation(
558 internal::kFlashYouTubeRewriteUMA);
559 }
560
561 void OverrideFlashEmbed(const GURL& gurl) {
562 client_.OverrideFlashEmbedWithHTML(gurl);
563 }
564
565 private:
566 ChromeContentRendererClient client_;
567 base::HistogramTester histogram_tester_;
568
569 DISALLOW_COPY_AND_ASSIGN(ChromeContentRendererClientMetricsTest);
570};
571
572TEST_F(ChromeContentRendererClientMetricsTest, RewriteEmbedIneligibleURL) {
573 std::unique_ptr<base::HistogramSamples> samples = GetHistogramSamples();
574 EXPECT_EQ(0, samples->TotalCount());
575
576 const std::string test_data[] = {
577 // HTTP, www, no flash
578 "http://www.youtube.com",
579 // No flash, subdomain
580 "http://www.foo.youtube.com",
581 // No flash
582 "youtube.com",
583 // Not youtube
584 "http://www.plus.google.com",
585 // Already using HTML5
586 "http://youtube.com/embed/deadbeef",
587 // Already using HTML5, enablejsapi=1
588 "http://www.youtube.com/embed/deadbeef?enablejsapi=1"};
589
590 for (const auto& data : test_data) {
591 GURL gurl = GURL(data);
592 OverrideFlashEmbed(gurl);
593 samples = GetHistogramSamples();
594 EXPECT_EQ(0, samples->GetCount(internal::SUCCESS));
595 EXPECT_EQ(0, samples->TotalCount());
596 }
597}
598
599TEST_F(ChromeContentRendererClientMetricsTest, RewriteEmbedSuccess) {
600 ChromeContentRendererClient client;
601
602 std::unique_ptr<base::HistogramSamples> samples = GetHistogramSamples();
603 auto total_count = 0;
604 EXPECT_EQ(total_count, samples->TotalCount());
605
606 const std::string test_data[] = {
607 // HTTP, www, flash
608 "http://www.youtube.com/v/deadbeef",
609 // HTTP, no www, flash
610 "http://youtube.com/v/deadbeef",
611 // HTTPS, www, flash
612 "https://www.youtube.com/v/deadbeef",
613 // HTTPS, no www, flash
614 "https://youtube.com/v/deadbeef",
615 // Invalid parameter construct
616 "http://www.youtube.com/v/abcd/",
617 // Invalid parameter construct
618 "http://www.youtube.com/v/1234/",
619 };
620
621 for (const auto& data : test_data) {
622 ++total_count;
623 GURL gurl = GURL(data);
624 OverrideFlashEmbed(gurl);
625 samples = GetHistogramSamples();
626 EXPECT_EQ(total_count, samples->GetCount(internal::SUCCESS));
627 EXPECT_EQ(total_count, samples->TotalCount());
628 }
629
630 // Invalid parameter construct
631 GURL gurl = GURL("http://www.youtube.com/abcd/v/deadbeef");
632 samples = GetHistogramSamples();
633 EXPECT_EQ(total_count, samples->GetCount(internal::SUCCESS));
634 EXPECT_EQ(total_count, samples->TotalCount());
635}
636
637TEST_F(ChromeContentRendererClientMetricsTest, RewriteEmbedSuccessRewrite) {
638 ChromeContentRendererClient client;
639
640 std::unique_ptr<base::HistogramSamples> samples = GetHistogramSamples();
641 auto total_count = 0;
642 EXPECT_EQ(total_count, samples->TotalCount());
643
644 const std::string test_data[] = {
645 // Invalid parameter construct, one parameter
646 "http://www.youtube.com/v/deadbeef&start=4",
647 // Invalid parameter construct, has multiple parameters
648 "http://www.youtube.com/v/deadbeef&start=4&fs=1?foo=bar",
649 };
650
651 for (const auto& data : test_data) {
652 ++total_count;
653 GURL gurl = GURL(data);
654 client.OverrideFlashEmbedWithHTML(gurl);
655 samples = GetHistogramSamples();
656 EXPECT_EQ(total_count, samples->GetCount(internal::SUCCESS_PARAMS_REWRITE));
657 EXPECT_EQ(total_count, samples->TotalCount());
658 }
659
660 // Invalid parameter construct, not flash
661 GURL gurl = GURL("http://www.youtube.com/embed/deadbeef&start=4");
662 OverrideFlashEmbed(gurl);
663 samples = GetHistogramSamples();
664 EXPECT_EQ(total_count, samples->GetCount(internal::SUCCESS_PARAMS_REWRITE));
665 EXPECT_EQ(total_count, samples->TotalCount());
666}
667
kdsilva621478c2016-08-09 17:31:11668#if defined(OS_ANDROID)
669TEST_F(ChromeContentRendererClientMetricsTest,
670 RewriteEmbedFailureJSAPIAndroid) {
kdsilvaaf8d2572016-08-09 17:22:20671 ChromeContentRendererClient client;
672
673 std::unique_ptr<base::HistogramSamples> samples = GetHistogramSamples();
674 auto total_count = 0;
675 EXPECT_EQ(total_count, samples->TotalCount());
676
677 const std::string test_data[] = {
kdsilva621478c2016-08-09 17:31:11678 // Valid parameter construct, one parameter
679 "http://www.youtube.com/v/deadbeef?enablejsapi=1",
680 // Valid parameter construct, has multiple parameters
681 "http://www.youtube.com/v/deadbeef?enablejsapi=1&foo=2",
682 // Invalid parameter construct, one parameter
683 "http://www.youtube.com/v/deadbeef&enablejsapi=1",
684 // Invalid parameter construct, has multiple parameters
685 "http://www.youtube.com/v/deadbeef&enablejsapi=1&foo=2"};
686
687 for (const auto& data : test_data) {
688 ++total_count;
689 GURL gurl = GURL(data);
690 OverrideFlashEmbed(gurl);
691 samples = GetHistogramSamples();
692 EXPECT_EQ(total_count, samples->GetCount(internal::SUCCESS_ENABLEJSAPI));
693 EXPECT_EQ(total_count, samples->TotalCount());
694 }
695}
696
697#else
698TEST_F(ChromeContentRendererClientMetricsTest,
699 RewriteEmbedFailureJSAPIDesktop) {
700 ChromeContentRendererClient client;
701
702 std::unique_ptr<base::HistogramSamples> samples = GetHistogramSamples();
703 auto total_count = 0;
704 EXPECT_EQ(total_count, samples->TotalCount());
705
706 const std::string test_data[] = {
707 // Valid parameter construct, one parameter
708 "http://www.youtube.com/v/deadbeef?enablejsapi=1",
kdsilvaaf8d2572016-08-09 17:22:20709 // Invalid parameter construct, one parameter
710 "http://www.youtube.com/v/deadbeef&enablejsapi=1",
711 // Invalid parameter construct, has multiple parameters
712 "http://www.youtube.com/v/deadbeef&start=4&enablejsapi=1?foo=2"};
713
714 for (const auto& data : test_data) {
715 ++total_count;
716 GURL gurl = GURL(data);
717 OverrideFlashEmbed(gurl);
718 samples = GetHistogramSamples();
719 EXPECT_EQ(total_count, samples->GetCount(internal::FAILURE_ENABLEJSAPI));
720 EXPECT_EQ(total_count, samples->TotalCount());
721 }
722}
kdsilva621478c2016-08-09 17:31:11723#endif