net_helpers.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2018 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 "android_webview/browser/network_service/net_helpers.h"
  5. #include "android_webview/browser/aw_contents_io_thread_client.h"
  6. #include "android_webview/common/url_constants.h"
  7. #include "base/check_op.h"
  8. #include "net/base/load_flags.h"
  9. #include "url/gurl.h"
  10. namespace android_webview {
  11. namespace {
  12. int UpdateCacheControlFlags(int load_flags, int cache_control_flags) {
  13. const int all_cache_control_flags =
  14. net::LOAD_BYPASS_CACHE | net::LOAD_VALIDATE_CACHE |
  15. net::LOAD_SKIP_CACHE_VALIDATION | net::LOAD_ONLY_FROM_CACHE;
  16. DCHECK_EQ((cache_control_flags & all_cache_control_flags),
  17. cache_control_flags);
  18. load_flags &= ~all_cache_control_flags;
  19. load_flags |= cache_control_flags;
  20. return load_flags;
  21. }
  22. // Gets the net-layer load_flags which reflect |client|'s cache mode.
  23. int GetCacheModeForClient(AwContentsIoThreadClient* client) {
  24. DCHECK(client);
  25. AwContentsIoThreadClient::CacheMode cache_mode = client->GetCacheMode();
  26. switch (cache_mode) {
  27. case AwContentsIoThreadClient::LOAD_CACHE_ELSE_NETWORK:
  28. // If the resource is in the cache (even if expired), load from cache.
  29. // Otherwise, fall back to network.
  30. return net::LOAD_SKIP_CACHE_VALIDATION;
  31. case AwContentsIoThreadClient::LOAD_NO_CACHE:
  32. // Always load from the network, don't use the cache.
  33. return net::LOAD_BYPASS_CACHE;
  34. case AwContentsIoThreadClient::LOAD_CACHE_ONLY:
  35. // If the resource is in the cache (even if expired), load from cache. Do
  36. // not fall back to the network.
  37. return net::LOAD_ONLY_FROM_CACHE | net::LOAD_SKIP_CACHE_VALIDATION;
  38. default:
  39. // If the resource is in the cache (and is valid), load from cache.
  40. // Otherwise, fall back to network. This is the usual (default) case.
  41. return 0;
  42. }
  43. }
  44. } // namespace
  45. int UpdateLoadFlags(int load_flags, AwContentsIoThreadClient* client) {
  46. if (!client)
  47. return load_flags;
  48. if (client->ShouldBlockNetworkLoads()) {
  49. return UpdateCacheControlFlags(
  50. load_flags,
  51. net::LOAD_ONLY_FROM_CACHE | net::LOAD_SKIP_CACHE_VALIDATION);
  52. }
  53. int cache_mode = GetCacheModeForClient(client);
  54. if (!cache_mode)
  55. return load_flags;
  56. return UpdateCacheControlFlags(load_flags, cache_mode);
  57. }
  58. bool ShouldBlockURL(const GURL& url, AwContentsIoThreadClient* client) {
  59. if (!client)
  60. return false;
  61. // Part of implementation of WebSettings.allowContentAccess.
  62. if (url.SchemeIs(url::kContentScheme) && client->ShouldBlockContentUrls())
  63. return true;
  64. // Part of implementation of WebSettings.allowFileAccess.
  65. if (url.SchemeIsFile() && client->ShouldBlockFileUrls()) {
  66. // Application's assets and resources are always available.
  67. return !IsAndroidSpecialFileUrl(url);
  68. }
  69. return client->ShouldBlockNetworkLoads() && url.SchemeIs(url::kFtpScheme);
  70. }
  71. int GetHttpCacheSize() {
  72. // This currently returns a constant value, but we may consider deciding cache
  73. // size dynamically, since Android provides better support on newer versions
  74. // (http://crbug.com/893318).
  75. return 20 * 1024 * 1024; // 20M
  76. }
  77. } // namespace android_webview