background_fetch_permission_context.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2021 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. #include "weblayer/browser/background_fetch/background_fetch_permission_context.h"
  5. #include "components/content_settings/core/browser/host_content_settings_map.h"
  6. #include "content/public/browser/web_contents.h"
  7. #include "third_party/blink/public/mojom/permissions_policy/permissions_policy_feature.mojom.h"
  8. #include "weblayer/browser/host_content_settings_map_factory.h"
  9. namespace weblayer {
  10. BackgroundFetchPermissionContext::BackgroundFetchPermissionContext(
  11. content::BrowserContext* browser_context)
  12. : PermissionContextBase(browser_context,
  13. ContentSettingsType::BACKGROUND_FETCH,
  14. blink::mojom::PermissionsPolicyFeature::kNotFound) {
  15. }
  16. bool BackgroundFetchPermissionContext::IsRestrictedToSecureOrigins() const {
  17. return true;
  18. }
  19. ContentSetting BackgroundFetchPermissionContext::GetPermissionStatusInternal(
  20. content::RenderFrameHost* render_frame_host,
  21. const GURL& requesting_origin,
  22. const GURL& embedding_origin) const {
  23. // Follow the AUTOMATIC_DOWNLOADS setting. TODO(crbug.com/1189247): can this
  24. // be improved upon? It's not really "automatic" if it's in direct response to
  25. // a user action, but WebLayer doesn't implement Chrome's download request
  26. // limiting logic.
  27. auto* host_content_settings_map =
  28. HostContentSettingsMapFactory::GetForBrowserContext(browser_context());
  29. ContentSetting setting = host_content_settings_map->GetContentSetting(
  30. requesting_origin, requesting_origin,
  31. ContentSettingsType::AUTOMATIC_DOWNLOADS);
  32. // Matching Chrome behavior: when the request originates from a non-main frame
  33. // or a service worker, the most permissive we'll allow is ASK. This causes
  34. // the download to start in a paused state.
  35. if (setting == CONTENT_SETTING_ALLOW &&
  36. (!render_frame_host || render_frame_host->GetParent())) {
  37. setting = CONTENT_SETTING_ASK;
  38. }
  39. return setting;
  40. }
  41. } // namespace weblayer