OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/browser/banners/app_banner_data_fetcher.h" | 5 #include "chrome/browser/banners/app_banner_data_fetcher.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "chrome/browser/banners/app_banner_debug_log.h" | |
14 #include "chrome/browser/banners/app_banner_metrics.h" | 13 #include "chrome/browser/banners/app_banner_metrics.h" |
15 #include "chrome/browser/banners/app_banner_settings_helper.h" | 14 #include "chrome/browser/banners/app_banner_settings_helper.h" |
| 15 #include "chrome/browser/banners/webapp_manifest_validator.h" |
16 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
17 #include "chrome/browser/manifest/manifest_icon_downloader.h" | 17 #include "chrome/browser/manifest/manifest_icon_downloader.h" |
18 #include "chrome/browser/manifest/manifest_icon_selector.h" | 18 #include "chrome/browser/manifest/manifest_icon_selector.h" |
19 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
20 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
21 #include "chrome/common/render_messages.h" | 21 #include "chrome/common/render_messages.h" |
22 #include "components/rappor/rappor_utils.h" | 22 #include "components/rappor/rappor_utils.h" |
23 #include "content/public/browser/browser_context.h" | 23 #include "content/public/browser/browser_context.h" |
24 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
25 #include "content/public/browser/navigation_details.h" | 25 #include "content/public/browser/navigation_details.h" |
26 #include "content/public/browser/render_frame_host.h" | 26 #include "content/public/browser/render_frame_host.h" |
27 #include "content/public/browser/service_worker_context.h" | 27 #include "content/public/browser/service_worker_context.h" |
28 #include "content/public/browser/storage_partition.h" | 28 #include "content/public/browser/storage_partition.h" |
29 #include "net/base/load_flags.h" | 29 #include "net/base/load_flags.h" |
30 #include "third_party/WebKit/public/platform/WebDisplayMode.h" | 30 #include "third_party/WebKit/public/platform/WebDisplayMode.h" |
31 #include "third_party/WebKit/public/platform/modules/app_banner/WebAppBannerProm
ptReply.h" | 31 #include "third_party/WebKit/public/platform/modules/app_banner/WebAppBannerProm
ptReply.h" |
32 | 32 |
33 namespace { | 33 namespace { |
34 | 34 |
35 base::LazyInstance<base::TimeDelta> gTimeDeltaForTesting = | 35 base::LazyInstance<base::TimeDelta> gTimeDeltaForTesting = |
36 LAZY_INSTANCE_INITIALIZER; | 36 LAZY_INSTANCE_INITIALIZER; |
37 int gCurrentRequestID = -1; | 37 int gCurrentRequestID = -1; |
38 const char kPngExtension[] = ".png"; | |
39 | |
40 // The requirement for now is an image/png that is at least 144x144. | |
41 const int kIconMinimumSize = 144; | |
42 bool DoesManifestContainRequiredIcon(const content::Manifest& manifest) { | |
43 for (const auto& icon : manifest.icons) { | |
44 // The type field is optional. If it isn't present, fall back on checking | |
45 // the src extension, and allow the icon if the extension ends with png. | |
46 if (!base::EqualsASCII(icon.type.string(), "image/png") && | |
47 !(icon.type.is_null() && | |
48 base::EndsWith(icon.src.ExtractFileName(), kPngExtension, | |
49 base::CompareCase::INSENSITIVE_ASCII))) | |
50 continue; | |
51 | |
52 for (const auto& size : icon.sizes) { | |
53 if (size.IsEmpty()) // "any" | |
54 return true; | |
55 if (size.width() >= kIconMinimumSize && size.height() >= kIconMinimumSize) | |
56 return true; | |
57 } | |
58 } | |
59 | |
60 return false; | |
61 } | |
62 | 38 |
63 } // anonymous namespace | 39 } // anonymous namespace |
64 | 40 |
65 namespace banners { | 41 namespace banners { |
66 | 42 |
67 // static | 43 // static |
68 base::Time AppBannerDataFetcher::GetCurrentTime() { | 44 base::Time AppBannerDataFetcher::GetCurrentTime() { |
69 return base::Time::Now() + gTimeDeltaForTesting.Get(); | 45 return base::Time::Now() + gTimeDeltaForTesting.Get(); |
70 } | 46 } |
71 | 47 |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 manifest.related_applications.size()) { | 265 manifest.related_applications.size()) { |
290 for (const auto& application : manifest.related_applications) { | 266 for (const auto& application : manifest.related_applications) { |
291 std::string platform = base::UTF16ToUTF8(application.platform.string()); | 267 std::string platform = base::UTF16ToUTF8(application.platform.string()); |
292 std::string id = base::UTF16ToUTF8(application.id.string()); | 268 std::string id = base::UTF16ToUTF8(application.id.string()); |
293 if (weak_delegate_->HandleNonWebApp(platform, application.url, id, | 269 if (weak_delegate_->HandleNonWebApp(platform, application.url, id, |
294 is_debug_mode_)) | 270 is_debug_mode_)) |
295 return; | 271 return; |
296 } | 272 } |
297 } | 273 } |
298 | 274 |
299 if (!IsManifestValidForWebApp(manifest, web_contents, is_debug_mode_)) { | 275 OutputDeveloperMessageCode error_code = OutputDeveloperMessageCode::kNone; |
| 276 if (!CheckManifest(manifest, &error_code)) { |
| 277 OutputDeveloperNotShownMessage(web_contents, error_code, is_debug_mode_); |
300 Cancel(); | 278 Cancel(); |
301 return; | 279 return; |
302 } | 280 } |
303 | 281 |
304 // Since the manifest is valid, one of short name or name must be non-null. | 282 // Since the manifest is valid, one of short name or name must be non-null. |
305 // Prefer name if it isn't null. | 283 // Prefer name if it isn't null. |
306 web_app_data_ = manifest; | 284 web_app_data_ = manifest; |
307 app_title_ = (web_app_data_.name.is_null()) | 285 app_title_ = (web_app_data_.name.is_null()) |
308 ? web_app_data_.short_name.string() | 286 ? web_app_data_.short_name.string() |
309 : web_app_data_.name.string(); | 287 : web_app_data_.name.string(); |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 | 388 |
411 app_icon_.reset(new SkBitmap(bitmap)); | 389 app_icon_.reset(new SkBitmap(bitmap)); |
412 event_request_id_ = ++gCurrentRequestID; | 390 event_request_id_ = ++gCurrentRequestID; |
413 web_contents->GetMainFrame()->Send( | 391 web_contents->GetMainFrame()->Send( |
414 new ChromeViewMsg_AppBannerPromptRequest( | 392 new ChromeViewMsg_AppBannerPromptRequest( |
415 web_contents->GetMainFrame()->GetRoutingID(), | 393 web_contents->GetMainFrame()->GetRoutingID(), |
416 event_request_id_, | 394 event_request_id_, |
417 GetBannerType())); | 395 GetBannerType())); |
418 } | 396 } |
419 | 397 |
| 398 bool AppBannerDataFetcher::CheckManifest(const content::Manifest& manifest, |
| 399 OutputDeveloperMessageCode* code) { |
| 400 return webapp_manifest_validator::IsManifestGoodForWebapp(manifest, code); |
| 401 } |
| 402 |
420 bool AppBannerDataFetcher::IsWebAppInstalled( | 403 bool AppBannerDataFetcher::IsWebAppInstalled( |
421 content::BrowserContext* browser_context, | 404 content::BrowserContext* browser_context, |
422 const GURL& start_url) { | 405 const GURL& start_url) { |
423 return false; | 406 return false; |
424 } | 407 } |
425 | 408 |
426 void AppBannerDataFetcher::RecordCouldShowBanner() { | 409 void AppBannerDataFetcher::RecordCouldShowBanner() { |
427 content::WebContents* web_contents = GetWebContents(); | 410 content::WebContents* web_contents = GetWebContents(); |
428 DCHECK(web_contents); | 411 DCHECK(web_contents); |
429 | 412 |
(...skipping 18 matching lines...) Expand all Loading... |
448 OutputDeveloperMessageCode::kUserNavigatedBeforeBannerShown, | 431 OutputDeveloperMessageCode::kUserNavigatedBeforeBannerShown, |
449 is_debug_mode_); | 432 is_debug_mode_); |
450 return false; | 433 return false; |
451 } | 434 } |
452 if (!web_contents) { | 435 if (!web_contents) { |
453 return false; // We cannot show a message if |web_contents| is null | 436 return false; // We cannot show a message if |web_contents| is null |
454 } | 437 } |
455 return true; | 438 return true; |
456 } | 439 } |
457 | 440 |
458 // static | |
459 bool AppBannerDataFetcher::IsManifestValidForWebApp( | |
460 const content::Manifest& manifest, | |
461 content::WebContents* web_contents, | |
462 bool is_debug_mode) { | |
463 if (manifest.IsEmpty()) { | |
464 OutputDeveloperNotShownMessage(web_contents, | |
465 OutputDeveloperMessageCode::kManifestEmpty, | |
466 is_debug_mode); | |
467 return false; | |
468 } | |
469 if (!manifest.start_url.is_valid()) { | |
470 OutputDeveloperNotShownMessage( | |
471 web_contents, OutputDeveloperMessageCode::kStartURLNotValid, | |
472 is_debug_mode); | |
473 return false; | |
474 } | |
475 if ((manifest.name.is_null() || manifest.name.string().empty()) && | |
476 (manifest.short_name.is_null() || manifest.short_name.string().empty())) { | |
477 OutputDeveloperNotShownMessage( | |
478 web_contents, | |
479 OutputDeveloperMessageCode::kManifestMissingNameOrShortName, | |
480 is_debug_mode); | |
481 return false; | |
482 } | |
483 | |
484 // TODO(dominickn,mlamouri): when Chrome supports "minimal-ui", it should be | |
485 // accepted. If we accept it today, it would fallback to "browser" and make | |
486 // this check moot. See https://crbug.com/604390 | |
487 if (manifest.display != blink::WebDisplayModeStandalone && | |
488 manifest.display != blink::WebDisplayModeFullscreen) { | |
489 OutputDeveloperNotShownMessage( | |
490 web_contents, | |
491 OutputDeveloperMessageCode::kManifestDisplayStandaloneFullscreen, | |
492 is_debug_mode); | |
493 return false; | |
494 } | |
495 | |
496 if (!DoesManifestContainRequiredIcon(manifest)) { | |
497 OutputDeveloperNotShownMessage( | |
498 web_contents, OutputDeveloperMessageCode::kManifestMissingSuitableIcon, | |
499 is_debug_mode); | |
500 return false; | |
501 } | |
502 return true; | |
503 } | |
504 | |
505 } // namespace banners | 441 } // namespace banners |
OLD | NEW |