blob: 618a9678967adfd3660460912c142c28a09f4d5d [file] [log] [blame]
[email protected]f7885d42012-04-03 08:30:041// 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
[email protected]8fe74bf2012-08-07 21:08:425#ifndef CHROME_RENDERER_EXTENSIONS_REQUEST_SENDER_H_
6#define CHROME_RENDERER_EXTENSIONS_REQUEST_SENDER_H_
[email protected]f7885d42012-04-03 08:30:047
8#include <string>
9#include <map>
10
11#include "base/memory/linked_ptr.h"
12#include "v8/include/v8.h"
13
[email protected]f7885d42012-04-03 08:30:0414namespace base {
15class ListValue;
16}
17
[email protected]8fe74bf2012-08-07 21:08:4218namespace extensions {
[email protected]4f1633f2013-03-09 14:26:2419class ChromeV8Context;
[email protected]8fe74bf2012-08-07 21:08:4220class Dispatcher;
21
[email protected]f7885d42012-04-03 08:30:0422struct PendingRequest;
23
24// Responsible for sending requests for named extension API functions to the
25// extension host and routing the responses back to the caller.
[email protected]8fe74bf2012-08-07 21:08:4226class RequestSender {
[email protected]f7885d42012-04-03 08:30:0427 public:
[email protected]ec47b082013-03-29 14:46:1728 // Source represents a user of RequestSender. Every request is associated with
29 // a Source object, which will be notified when the corresponding response
30 // arrives. When a Source object is going away and there are pending requests,
31 // it should call InvalidateSource() to make sure no notifications are sent to
32 // it later.
33 class Source {
34 public:
35 virtual ~Source() {}
36
37 virtual ChromeV8Context* GetContext() = 0;
38 virtual void OnResponseReceived(const std::string& name,
39 int request_id,
40 bool success,
41 const base::ListValue& response,
42 const std::string& error) = 0;
43 };
44
[email protected]4f1633f2013-03-09 14:26:2445 explicit RequestSender(Dispatcher* dispatcher);
[email protected]8fe74bf2012-08-07 21:08:4246 ~RequestSender();
[email protected]f7885d42012-04-03 08:30:0447
[email protected]ec47b082013-03-29 14:46:1748 // In order to avoid collision, all |request_id|s passed into StartRequest()
49 // should be generated by this method.
50 int GetNextRequestId() const;
51
[email protected]f7885d42012-04-03 08:30:0452 // Makes a call to the API function |name| that is to be handled by the
[email protected]74e21e72012-07-09 21:20:5353 // extension host. The response to this request will be received in
54 // HandleResponse().
55 // TODO(koz): Remove |request_id| and generate that internally.
[email protected]4f1633f2013-03-09 14:26:2456 // There are multiple of these per render view though, so we'll
57 // need to vend the IDs centrally.
[email protected]ec47b082013-03-29 14:46:1758 void StartRequest(Source* source,
[email protected]4f1633f2013-03-09 14:26:2459 const std::string& name,
[email protected]74e21e72012-07-09 21:20:5360 int request_id,
61 bool has_callback,
62 bool for_io_thread,
63 base::ListValue* value_args);
64
65 // Handles responses from the extension host to calls made by StartRequest().
66 void HandleResponse(int request_id,
67 bool success,
68 const base::ListValue& response,
69 const std::string& error);
70
[email protected]ec47b082013-03-29 14:46:1771 // Notifies this that a request source is no longer valid.
[email protected]4f1633f2013-03-09 14:26:2472 // TODO(kalman): Do this in a generic/safe way.
[email protected]ec47b082013-03-29 14:46:1773 void InvalidateSource(Source* source);
[email protected]f7885d42012-04-03 08:30:0474
75 private:
[email protected]74e21e72012-07-09 21:20:5376 typedef std::map<int, linked_ptr<PendingRequest> > PendingRequestMap;
[email protected]f7885d42012-04-03 08:30:0477
78 void InsertRequest(int request_id, PendingRequest* pending_request);
79 linked_ptr<PendingRequest> RemoveRequest(int request_id);
80
[email protected]8fe74bf2012-08-07 21:08:4281 Dispatcher* dispatcher_;
[email protected]21776be2012-07-09 03:53:3282 PendingRequestMap pending_requests_;
83
[email protected]8fe74bf2012-08-07 21:08:4284 DISALLOW_COPY_AND_ASSIGN(RequestSender);
[email protected]f7885d42012-04-03 08:30:0485};
86
[email protected]8fe74bf2012-08-07 21:08:4287} // namespace extensions
88
89#endif // CHROME_RENDERER_EXTENSIONS_REQUEST_SENDER_H_