File: request_sender.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (102 lines) | stat: -rw-r--r-- 3,446 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EXTENSIONS_RENDERER_REQUEST_SENDER_H_
#define EXTENSIONS_RENDERER_REQUEST_SENDER_H_

#include <map>
#include <string>

#include "base/macros.h"
#include "v8/include/v8.h"

struct ExtensionHostMsg_Request_Params;

namespace base {
class ListValue;
}

namespace content {
class RenderFrame;
}

namespace extensions {
class ScriptContext;

struct PendingRequest;

// Responsible for sending requests for named extension API functions to the
// extension host and routing the responses back to the caller.
class RequestSender {
 public:
  // Source represents a user of RequestSender. Every request is associated with
  // a Source object, which will be notified when the corresponding response
  // arrives. When a Source object is going away and there are pending requests,
  // it should call InvalidateSource() to make sure no notifications are sent to
  // it later.
  class Source {
   public:
    virtual ~Source() {}

    virtual ScriptContext* GetContext() = 0;
    virtual void OnResponseReceived(const std::string& name,
                                    int request_id,
                                    bool success,
                                    const base::ListValue& response,
                                    const std::string& error) = 0;
  };

  RequestSender();
  virtual ~RequestSender();

  // In order to avoid collision, all |request_id|s passed into StartRequest()
  // should be generated by this method.
  int GetNextRequestId() const;

  // Makes a call to the API function |name| that is to be handled by the
  // extension host. The response to this request will be received in
  // HandleResponse().
  // TODO(koz): Remove |request_id| and generate that internally.
  //            There are multiple of these per render view though, so we'll
  //            need to vend the IDs centrally.
  // Returns true if the request is successfully sent.
  bool StartRequest(Source* source,
                    const std::string& name,
                    int request_id,
                    bool has_callback,
                    bool for_io_thread,
                    base::ListValue* value_args);

  // Sends the IPC to extension host for the API function that is described
  // in |params|.
  virtual void SendRequest(content::RenderFrame* render_frame,
                           bool for_io_thread,
                           ExtensionHostMsg_Request_Params& params);

  // Handles responses from the extension host to calls made by StartRequest().
  void HandleResponse(int request_id,
                      bool success,
                      const base::ListValue& response,
                      const std::string& error);

  // Notifies this that a request source is no longer valid.
  // TODO(kalman): Do this in a generic/safe way.
  void InvalidateSource(Source* source);

 private:
  friend class ScopedTabID;
  using PendingRequestMap = std::map<int, std::unique_ptr<PendingRequest>>;

  void InsertRequest(int request_id,
                     std::unique_ptr<PendingRequest> pending_request);
  std::unique_ptr<PendingRequest> RemoveRequest(int request_id);

  PendingRequestMap pending_requests_;

  DISALLOW_COPY_AND_ASSIGN(RequestSender);
};

}  // namespace extensions

#endif  // EXTENSIONS_RENDERER_REQUEST_SENDER_H_