aw_contents_io_thread_client.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // 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. #ifndef ANDROID_WEBVIEW_BROWSER_AW_CONTENTS_IO_THREAD_CLIENT_H_
  5. #define ANDROID_WEBVIEW_BROWSER_AW_CONTENTS_IO_THREAD_CLIENT_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include "android_webview/browser/aw_settings.h"
  10. #include "base/android/scoped_java_ref.h"
  11. #include "base/callback_forward.h"
  12. #include "base/compiler_specific.h"
  13. #include "base/task/thread_pool.h"
  14. #include "content/public/browser/global_routing_id.h"
  15. namespace content {
  16. class WebContents;
  17. }
  18. namespace android_webview {
  19. class AwWebResourceInterceptResponse;
  20. struct AwWebResourceRequest;
  21. // This class provides a means of calling Java methods on an instance that has
  22. // a 1:1 relationship with a WebContents instance directly from the IO thread.
  23. //
  24. // Specifically this is used to associate URLRequests with the WebContents that
  25. // the URLRequest is made for.
  26. //
  27. // The native class is intended to be a short-lived handle that pins the
  28. // Java-side instance. It is preferable to use the static getter methods to
  29. // obtain a new instance of the class rather than holding on to one for
  30. // prolonged periods of time (see note for more details).
  31. //
  32. // Note: The native AwContentsIoThreadClient instance has a Global ref to
  33. // the Java object. By keeping the native AwContentsIoThreadClient
  34. // instance alive you're also prolonging the lifetime of the Java instance, so
  35. // don't keep a AwContentsIoThreadClient if you don't need to.
  36. class AwContentsIoThreadClient {
  37. public:
  38. // Corresponds to WebSettings cache mode constants.
  39. enum CacheMode {
  40. LOAD_DEFAULT = -1,
  41. LOAD_NORMAL = 0,
  42. LOAD_CACHE_ELSE_NETWORK = 1,
  43. LOAD_NO_CACHE = 2,
  44. LOAD_CACHE_ONLY = 3,
  45. };
  46. // Associates the |jclient| instance (which must implement the
  47. // AwContentsIoThreadClient Java interface) with the |web_contents|.
  48. // This should be called at most once per |web_contents|.
  49. static void Associate(content::WebContents* web_contents,
  50. const base::android::JavaRef<jobject>& jclient);
  51. // Sets the |jclient| java instance to which service worker related
  52. // callbacks should be delegated.
  53. static void SetServiceWorkerIoThreadClient(
  54. const base::android::JavaRef<jobject>& jclient,
  55. const base::android::JavaRef<jobject>& browser_context);
  56. // |jclient| must hold a non-null Java object.
  57. explicit AwContentsIoThreadClient(
  58. const base::android::JavaRef<jobject>& jclient);
  59. AwContentsIoThreadClient(const AwContentsIoThreadClient&) = delete;
  60. AwContentsIoThreadClient& operator=(const AwContentsIoThreadClient&) = delete;
  61. ~AwContentsIoThreadClient();
  62. // Implementation of AwContentsIoThreadClient.
  63. // Retrieve CacheMode setting value of this AwContents.
  64. // This method is called on the IO thread only.
  65. CacheMode GetCacheMode() const;
  66. // This will attempt to fetch the AwContentsIoThreadClient for the given
  67. // RenderFrameHost id.
  68. // This method can be called from any thread.
  69. // A null std::unique_ptr is a valid return value.
  70. static std::unique_ptr<AwContentsIoThreadClient> FromID(
  71. content::GlobalRenderFrameHostId render_frame_host_id);
  72. // This map is useful when browser side navigations are enabled as
  73. // render_frame_ids will not be valid anymore for some of the navigations.
  74. static std::unique_ptr<AwContentsIoThreadClient> FromID(
  75. int frame_tree_node_id);
  76. // Returns the global thread client for service worker related callbacks.
  77. // A null std::unique_ptr is a valid return value.
  78. static std::unique_ptr<AwContentsIoThreadClient>
  79. GetServiceWorkerIoThreadClient();
  80. // Called on the IO thread when a subframe is created.
  81. static void SubFrameCreated(int render_process_id,
  82. int parent_render_frame_id,
  83. int child_render_frame_id);
  84. // This method is called on the IO thread only.
  85. using ShouldInterceptRequestResponseCallback =
  86. base::OnceCallback<void(std::unique_ptr<AwWebResourceInterceptResponse>)>;
  87. void ShouldInterceptRequestAsync(
  88. AwWebResourceRequest request,
  89. ShouldInterceptRequestResponseCallback callback);
  90. // Retrieve the AllowContentAccess setting value of this AwContents.
  91. // This method is called on the IO thread only.
  92. bool ShouldBlockContentUrls() const;
  93. // Retrieve the AllowFileAccess setting value of this AwContents.
  94. // This method is called on the IO thread only.
  95. bool ShouldBlockFileUrls() const;
  96. // Retrieve the BlockNetworkLoads setting value of this AwContents.
  97. // This method is called on the IO thread only.
  98. bool ShouldBlockNetworkLoads() const;
  99. // Retrieve the AcceptThirdPartyCookies setting value of this AwContents.
  100. bool ShouldAcceptThirdPartyCookies() const;
  101. // Retrieve the SafeBrowsingEnabled setting value of this AwContents.
  102. bool GetSafeBrowsingEnabled() const;
  103. // Retrieve RequestedWithHeaderMode setting value of this AwContents.
  104. // This method is called on the IO thread only.
  105. AwSettings::RequestedWithHeaderMode GetRequestedWithHeaderMode() const;
  106. private:
  107. base::android::ScopedJavaGlobalRef<jobject> java_object_;
  108. base::android::ScopedJavaGlobalRef<jobject> bg_thread_client_object_;
  109. scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_ =
  110. base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()});
  111. };
  112. } // namespace android_webview
  113. #endif // ANDROID_WEBVIEW_BROWSER_AW_CONTENTS_IO_THREAD_CLIENT_H_