#ifndef CHROME_RENDERER_EXTENSIONS_REQUEST_SENDER_H_
#define CHROME_RENDERER_EXTENSIONS_REQUEST_SENDER_H_
#include <string>
#include <map>
#include "base/memory/linked_ptr.h"
#include "v8/include/v8.h"
namespace base {
class ListValue;
}
namespace extensions {
class ChromeV8Context;
class Dispatcher;
struct PendingRequest;
class RequestSender {
public:
class Source {
public:
virtual ~Source() {}
virtual ChromeV8Context* GetContext() = 0;
virtual void OnResponseReceived(const std::string& name,
int request_id,
bool success,
const base::ListValue& response,
const std::string& error) = 0;
};
class ScopedTabID {
public:
ScopedTabID(RequestSender* request_sender, int tab_id);
~ScopedTabID();
private:
RequestSender* const request_sender_;
const int tab_id_;
const int previous_tab_id_;
DISALLOW_COPY_AND_ASSIGN(ScopedTabID);
};
explicit RequestSender(Dispatcher* dispatcher);
~RequestSender();
int GetNextRequestId() const;
void StartRequest(Source* source,
const std::string& name,
int request_id,
bool has_callback,
bool for_io_thread,
base::ListValue* value_args);
void HandleResponse(int request_id,
bool success,
const base::ListValue& response,
const std::string& error);
void InvalidateSource(Source* source);
private:
friend class ScopedTabID;
typedef std::map<int, linked_ptr<PendingRequest> > PendingRequestMap;
void InsertRequest(int request_id, PendingRequest* pending_request);
linked_ptr<PendingRequest> RemoveRequest(int request_id);
Dispatcher* dispatcher_;
PendingRequestMap pending_requests_;
int source_tab_id_;
DISALLOW_COPY_AND_ASSIGN(RequestSender);
};
}
#endif