extension_function_dispatcher.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright 2014 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 EXTENSIONS_BROWSER_EXTENSION_FUNCTION_DISPATCHER_H_
  5. #define EXTENSIONS_BROWSER_EXTENSION_FUNCTION_DISPATCHER_H_
  6. #include <map>
  7. #include <set>
  8. #include <string>
  9. #include <vector>
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "extensions/browser/extension_function.h"
  13. #include "extensions/common/mojom/frame.mojom.h"
  14. #include "ipc/ipc_sender.h"
  15. namespace content {
  16. class BrowserContext;
  17. class RenderFrameHost;
  18. class WebContents;
  19. }
  20. namespace extensions {
  21. class Extension;
  22. class ExtensionAPI;
  23. class ProcessMap;
  24. class WindowController;
  25. // ExtensionFunctionDispatcher receives requests to execute functions from
  26. // Chrome extensions running in a RenderFrameHost and dispatches them to the
  27. // appropriate handler. It lives entirely on the UI thread.
  28. //
  29. // ExtensionFunctionDispatcher should be a member of some class that hosts
  30. // RenderFrameHosts and wants them to be able to display extension content.
  31. // This class should also implement ExtensionFunctionDispatcher::Delegate.
  32. //
  33. // Note that a single ExtensionFunctionDispatcher does *not* correspond to a
  34. // single RVH, a single extension, or a single URL. This is by design so that
  35. // we can gracefully handle cases like WebContents, where the RVH, extension,
  36. // and URL can all change over the lifetime of the tab. Instead, these items
  37. // are all passed into each request.
  38. class ExtensionFunctionDispatcher
  39. : public base::SupportsWeakPtr<ExtensionFunctionDispatcher> {
  40. public:
  41. class Delegate {
  42. public:
  43. // Returns the WindowController associated with this delegate, or NULL if no
  44. // window is associated with the delegate.
  45. virtual WindowController* GetExtensionWindowController() const;
  46. // Asks the delegate for any relevant WebContents associated with this
  47. // context. For example, the WebContents in which an infobar or
  48. // chrome-extension://<id> URL are being shown. Callers must check for a
  49. // NULL return value (as in the case of a background page).
  50. virtual content::WebContents* GetAssociatedWebContents() const;
  51. // If the associated web contents is not null, returns that. Otherwise,
  52. // returns the next most relevant visible web contents. Callers must check
  53. // for a NULL return value (as in the case of a background page).
  54. virtual content::WebContents* GetVisibleWebContents() const;
  55. protected:
  56. virtual ~Delegate() {}
  57. };
  58. // Public constructor. Callers must ensure that:
  59. // - This object outlives any RenderFrameHost's passed to created
  60. // ExtensionFunctions.
  61. explicit ExtensionFunctionDispatcher(
  62. content::BrowserContext* browser_context);
  63. ~ExtensionFunctionDispatcher();
  64. // Dispatches a request and the response is sent in |callback| that is a reply
  65. // of mojom::LocalFrameHost::Request.
  66. void Dispatch(mojom::RequestParamsPtr params,
  67. content::RenderFrameHost& frame,
  68. mojom::LocalFrameHost::RequestCallback callback);
  69. // Message handlers.
  70. // Dispatches a request for service woker and the response is sent to the
  71. // corresponding render process in an ExtensionMsg_ResponseWorker message.
  72. void DispatchForServiceWorker(const mojom::RequestParams& params,
  73. int render_process_id);
  74. // Called when an ExtensionFunction is done executing, after it has sent
  75. // a response (if any) to the extension.
  76. void OnExtensionFunctionCompleted(const Extension* extension,
  77. bool is_from_service_worker,
  78. const char* name);
  79. // See the Delegate class for documentation on these methods.
  80. // TODO(devlin): None of these belong here. We should kill
  81. // ExtensionFunctionDispatcher::Delegate.
  82. WindowController* GetExtensionWindowController() const;
  83. content::WebContents* GetAssociatedWebContents() const;
  84. content::WebContents* GetVisibleWebContents() const;
  85. // The BrowserContext that this dispatcher is associated with.
  86. content::BrowserContext* browser_context() { return browser_context_; }
  87. void set_delegate(Delegate* delegate) { delegate_ = delegate; }
  88. // Adds a function object to the set of objects waiting for
  89. // responses from the renderer.
  90. void AddWorkerResponseTarget(ExtensionFunction* func);
  91. // Processes a Service Worker response from a renderer.
  92. void ProcessServiceWorkerResponse(int request_id,
  93. int64_t service_worker_version_id);
  94. private:
  95. // For a given RenderFrameHost instance, ResponseCallbackWrapper
  96. // creates ExtensionFunction::ResponseCallback instances which send responses
  97. // to the corresponding render view in ExtensionMsg_Response messages.
  98. // This class tracks the lifespan of the RenderFrameHost instance, and will be
  99. // destroyed automatically when it goes away.
  100. class ResponseCallbackWrapper;
  101. // Same as ResponseCallbackWrapper above, but applies to an extension
  102. // function from an extension Service Worker.
  103. class WorkerResponseCallbackWrapper;
  104. // Key used to store WorkerResponseCallbackWrapper in the map
  105. // |response_callback_wrappers_for_worker_|.
  106. struct WorkerResponseCallbackMapKey;
  107. // Helper to create an ExtensionFunction to handle the function given by
  108. // |params|. Can be called on any thread.
  109. // Does not set subclass properties, or include_incognito.
  110. static scoped_refptr<ExtensionFunction> CreateExtensionFunction(
  111. const mojom::RequestParams& params,
  112. const Extension* extension,
  113. int requesting_process_id,
  114. bool is_worker_request,
  115. const GURL* rfh_url,
  116. const ProcessMap& process_map,
  117. ExtensionAPI* api,
  118. void* profile_id,
  119. ExtensionFunction::ResponseCallback callback);
  120. void DispatchWithCallbackInternal(
  121. const mojom::RequestParams& params,
  122. content::RenderFrameHost* render_frame_host,
  123. int render_process_id,
  124. ExtensionFunction::ResponseCallback callback);
  125. void RemoveWorkerCallbacksForProcess(int render_process_id);
  126. raw_ptr<content::BrowserContext> browser_context_;
  127. raw_ptr<Delegate> delegate_;
  128. // This map doesn't own either the keys or the values. When a RenderFrameHost
  129. // instance goes away, the corresponding entry in this map (if exists) will be
  130. // removed.
  131. typedef std::map<content::RenderFrameHost*,
  132. std::unique_ptr<ResponseCallbackWrapper>>
  133. ResponseCallbackWrapperMap;
  134. ResponseCallbackWrapperMap response_callback_wrappers_;
  135. using WorkerResponseCallbackWrapperMap =
  136. std::map<WorkerResponseCallbackMapKey,
  137. std::unique_ptr<WorkerResponseCallbackWrapper>>;
  138. // TODO(lazyboy): The map entries are cleared upon RenderProcessHost shutown,
  139. // we should really be clearing it on service worker shutdown.
  140. WorkerResponseCallbackWrapperMap response_callback_wrappers_for_worker_;
  141. // The set of ExtensionFunction instances waiting for responses from
  142. // the renderer. These are removed once the response is processed.
  143. // The lifetimes of the instances are managed by the instances themselves.
  144. std::set<ExtensionFunction*> worker_response_targets_;
  145. };
  146. } // namespace extensions
  147. #endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_DISPATCHER_H_