blob: b0749bfdffc4fff408749a6d1785847f77f8574d [file] [log] [blame]
[email protected]f55c90ee62014-04-12 00:50:031// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]f7885d42012-04-03 08:30:042// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]f55c90ee62014-04-12 00:50:035#ifndef EXTENSIONS_RENDERER_REQUEST_SENDER_H_
6#define EXTENSIONS_RENDERER_REQUEST_SENDER_H_
[email protected]f7885d42012-04-03 08:30:047
[email protected]f7885d42012-04-03 08:30:048#include <map>
[email protected]f55c90ee62014-04-12 00:50:039#include <string>
[email protected]f7885d42012-04-03 08:30:0410
avi2d124c02015-12-23 06:36:4211#include "base/macros.h"
[email protected]f7885d42012-04-03 08:30:0412#include "v8/include/v8.h"
13
lazyboyee4adef2016-05-24 00:55:1614struct ExtensionHostMsg_Request_Params;
15
[email protected]f7885d42012-04-03 08:30:0416namespace base {
17class ListValue;
18}
19
lazyboyee4adef2016-05-24 00:55:1620namespace content {
21class RenderFrame;
22}
23
[email protected]8fe74bf2012-08-07 21:08:4224namespace extensions {
[email protected]f55c90ee62014-04-12 00:50:0325class ScriptContext;
[email protected]8fe74bf2012-08-07 21:08:4226
[email protected]f7885d42012-04-03 08:30:0427struct PendingRequest;
28
29// Responsible for sending requests for named extension API functions to the
30// extension host and routing the responses back to the caller.
[email protected]8fe74bf2012-08-07 21:08:4231class RequestSender {
[email protected]f7885d42012-04-03 08:30:0432 public:
[email protected]ec47b082013-03-29 14:46:1733 // Source represents a user of RequestSender. Every request is associated with
34 // a Source object, which will be notified when the corresponding response
35 // arrives. When a Source object is going away and there are pending requests,
36 // it should call InvalidateSource() to make sure no notifications are sent to
37 // it later.
38 class Source {
39 public:
40 virtual ~Source() {}
41
[email protected]f55c90ee62014-04-12 00:50:0342 virtual ScriptContext* GetContext() = 0;
[email protected]ec47b082013-03-29 14:46:1743 virtual void OnResponseReceived(const std::string& name,
44 int request_id,
45 bool success,
46 const base::ListValue& response,
47 const std::string& error) = 0;
48 };
49
pkasting9d327592016-05-19 19:37:1450 RequestSender();
lazyboyee4adef2016-05-24 00:55:1651 virtual ~RequestSender();
[email protected]f7885d42012-04-03 08:30:0452
[email protected]ec47b082013-03-29 14:46:1753 // In order to avoid collision, all |request_id|s passed into StartRequest()
54 // should be generated by this method.
55 int GetNextRequestId() const;
56
[email protected]f7885d42012-04-03 08:30:0457 // Makes a call to the API function |name| that is to be handled by the
[email protected]74e21e72012-07-09 21:20:5358 // extension host. The response to this request will be received in
59 // HandleResponse().
60 // TODO(koz): Remove |request_id| and generate that internally.
[email protected]4f1633f2013-03-09 14:26:2461 // There are multiple of these per render view though, so we'll
62 // need to vend the IDs centrally.
rdevlin.croninb2b31392016-06-22 23:39:1363 // Returns true if the request is successfully sent.
64 bool StartRequest(Source* source,
[email protected]4f1633f2013-03-09 14:26:2465 const std::string& name,
[email protected]74e21e72012-07-09 21:20:5366 int request_id,
67 bool has_callback,
68 bool for_io_thread,
69 base::ListValue* value_args);
70
lazyboyee4adef2016-05-24 00:55:1671 // Sends the IPC to extension host for the API function that is described
72 // in |params|.
73 virtual void SendRequest(content::RenderFrame* render_frame,
74 bool for_io_thread,
75 ExtensionHostMsg_Request_Params& params);
76
[email protected]74e21e72012-07-09 21:20:5377 // Handles responses from the extension host to calls made by StartRequest().
78 void HandleResponse(int request_id,
79 bool success,
80 const base::ListValue& response,
81 const std::string& error);
82
[email protected]ec47b082013-03-29 14:46:1783 // Notifies this that a request source is no longer valid.
[email protected]4f1633f2013-03-09 14:26:2484 // TODO(kalman): Do this in a generic/safe way.
[email protected]ec47b082013-03-29 14:46:1785 void InvalidateSource(Source* source);
[email protected]f7885d42012-04-03 08:30:0486
87 private:
[email protected]eb7ef5f2014-02-06 09:59:1988 friend class ScopedTabID;
lazyboy4cbdfe0a2016-09-02 05:32:3289 using PendingRequestMap = std::map<int, std::unique_ptr<PendingRequest>>;
[email protected]f7885d42012-04-03 08:30:0490
lazyboy4cbdfe0a2016-09-02 05:32:3291 void InsertRequest(int request_id,
92 std::unique_ptr<PendingRequest> pending_request);
93 std::unique_ptr<PendingRequest> RemoveRequest(int request_id);
[email protected]f7885d42012-04-03 08:30:0494
[email protected]21776be2012-07-09 03:53:3295 PendingRequestMap pending_requests_;
96
[email protected]8fe74bf2012-08-07 21:08:4297 DISALLOW_COPY_AND_ASSIGN(RequestSender);
[email protected]f7885d42012-04-03 08:30:0498};
99
[email protected]8fe74bf2012-08-07 21:08:42100} // namespace extensions
101
[email protected]f55c90ee62014-04-12 00:50:03102#endif // EXTENSIONS_RENDERER_REQUEST_SENDER_H_