Commit 5b5f180b authored by teravest@chromium.org's avatar teravest@chromium.org

Pepper: Use base::Callback in RequestNexeFd.

GetNexeFd() in ppb_nacl_private_impl will change soon to be an internal method,
not one exposed through the PPB_NaCl_Private interface. From inside Chromium,
base::Callback is much easier to use than PP_CompletionCallback or
TrackedCallback.

This is one of a series of changes to refactor the pexe downloading logic in
the trusted plugin.

BUG=370556

Review URL: https://codereview.chromium.org/380773002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@282112 0039d316-1c4b-4281-b951-d872f2087c98
parent a84eb296
......@@ -8,17 +8,8 @@
#include "ppapi/c/pp_errors.h"
#include "ppapi/shared_impl/ppapi_globals.h"
using ppapi::TrackedCallback;
using ppapi::PpapiGlobals;
PnaclTranslationResourceHost::CacheRequestInfo::CacheRequestInfo(
PP_Bool* hit,
PP_FileHandle* handle,
scoped_refptr<TrackedCallback> cb)
: is_hit(hit), file_handle(handle), callback(cb) {}
PnaclTranslationResourceHost::CacheRequestInfo::~CacheRequestInfo() {}
PnaclTranslationResourceHost::PnaclTranslationResourceHost(
const scoped_refptr<base::MessageLoopProxy>& io_message_loop)
: io_message_loop_(io_message_loop), sender_(NULL) {}
......@@ -58,9 +49,7 @@ void PnaclTranslationResourceHost::RequestNexeFd(
int render_view_id,
PP_Instance instance,
const nacl::PnaclCacheInfo& cache_info,
PP_Bool* is_hit,
PP_FileHandle* file_handle,
scoped_refptr<TrackedCallback> callback) {
RequestNexeFdCallback callback) {
DCHECK(PpapiGlobals::Get()->
GetMainThreadMessageLoop()->BelongsToCurrentThread());
io_message_loop_->PostTask(
......@@ -70,8 +59,6 @@ void PnaclTranslationResourceHost::RequestNexeFd(
render_view_id,
instance,
cache_info,
is_hit,
file_handle,
callback));
return;
}
......@@ -80,21 +67,19 @@ void PnaclTranslationResourceHost::SendRequestNexeFd(
int render_view_id,
PP_Instance instance,
const nacl::PnaclCacheInfo& cache_info,
PP_Bool* is_hit,
PP_FileHandle* file_handle,
scoped_refptr<TrackedCallback> callback) {
RequestNexeFdCallback callback) {
DCHECK(io_message_loop_->BelongsToCurrentThread());
if (!sender_ || !sender_->Send(new NaClHostMsg_NexeTempFileRequest(
render_view_id, instance, cache_info))) {
PpapiGlobals::Get()->GetMainThreadMessageLoop()
->PostTask(FROM_HERE,
base::Bind(&TrackedCallback::Run,
callback,
static_cast<int32_t>(PP_ERROR_FAILED)));
PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
FROM_HERE,
base::Bind(callback,
static_cast<int32_t>(PP_ERROR_FAILED),
false,
PP_kInvalidFileHandle));
return;
}
pending_cache_requests_.insert(std::make_pair(
instance, CacheRequestInfo(is_hit, file_handle, callback)));
pending_cache_requests_.insert(std::make_pair(instance, callback));
}
void PnaclTranslationResourceHost::ReportTranslationFinished(
......@@ -133,10 +118,9 @@ void PnaclTranslationResourceHost::OnNexeTempFileReply(
CacheRequestInfoMap::iterator it = pending_cache_requests_.find(instance);
int32_t status = PP_ERROR_FAILED;
// Handle the expected successful case first.
if (it != pending_cache_requests_.end() && base_file.IsValid() &&
TrackedCallback::IsPending(it->second.callback)) {
*it->second.is_hit = PP_FromBool(is_hit);
*it->second.file_handle = base_file.TakePlatformFile();
PP_FileHandle file_handle = PP_kInvalidFileHandle;
if (it != pending_cache_requests_.end() && base_file.IsValid()) {
file_handle = base_file.TakePlatformFile();
status = PP_OK;
}
if (it == pending_cache_requests_.end()) {
......@@ -144,7 +128,7 @@ void PnaclTranslationResourceHost::OnNexeTempFileReply(
} else {
PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
FROM_HERE,
base::Bind(&TrackedCallback::Run, it->second.callback, status));
base::Bind(it->second, status, is_hit, file_handle));
pending_cache_requests_.erase(it);
}
if (!base_file.IsValid()) {
......@@ -157,7 +141,12 @@ void PnaclTranslationResourceHost::CleanupCacheRequests() {
for (CacheRequestInfoMap::iterator it = pending_cache_requests_.begin();
it != pending_cache_requests_.end();
++it) {
it->second.callback->PostAbort();
PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
FROM_HERE,
base::Bind(it->second,
static_cast<int32_t>(PP_ERROR_ABORTED),
false,
PP_kInvalidFileHandle));
}
pending_cache_requests_.clear();
}
......@@ -7,10 +7,13 @@
#include <map>
#include "base/callback.h"
#include "base/message_loop/message_loop_proxy.h"
#include "ipc/ipc_platform_file.h"
#include "ipc/message_filter.h"
#include "ppapi/c/pp_bool.h"
#include "ppapi/c/pp_instance.h"
#include "ppapi/c/private/pp_file_handle.h"
#include "ppapi/shared_impl/tracked_callback.h"
namespace nacl {
struct PnaclCacheInfo;
......@@ -24,36 +27,25 @@ struct PnaclCacheInfo;
// needs from the browser since "Resource" is a Pepper thing...
class PnaclTranslationResourceHost : public IPC::MessageFilter {
public:
typedef base::Callback<void(int32_t, bool, PP_FileHandle)>
RequestNexeFdCallback;
explicit PnaclTranslationResourceHost(
const scoped_refptr<base::MessageLoopProxy>& io_message_loop);
void RequestNexeFd(int render_view_id,
PP_Instance instance,
const nacl::PnaclCacheInfo& cache_info,
PP_Bool* is_hit,
PP_FileHandle* file_handle,
scoped_refptr<ppapi::TrackedCallback> callback);
RequestNexeFdCallback callback);
void ReportTranslationFinished(PP_Instance instance, PP_Bool success);
protected:
virtual ~PnaclTranslationResourceHost();
private:
class CacheRequestInfo {
public:
CacheRequestInfo(PP_Bool* hit,
PP_FileHandle* handle,
scoped_refptr<ppapi::TrackedCallback> cb);
~CacheRequestInfo();
PP_Bool* is_hit;
PP_FileHandle* file_handle;
scoped_refptr<ppapi::TrackedCallback> callback;
};
// Maps the instance with an outstanding cache request to the info
// about that request.
typedef std::map<PP_Instance, CacheRequestInfo> CacheRequestInfoMap;
typedef std::map<PP_Instance, RequestNexeFdCallback> CacheRequestInfoMap;
// IPC::MessageFilter implementation.
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
virtual void OnFilterAdded(IPC::Sender* sender) OVERRIDE;
......@@ -63,9 +55,7 @@ class PnaclTranslationResourceHost : public IPC::MessageFilter {
void SendRequestNexeFd(int render_view_id,
PP_Instance instance,
const nacl::PnaclCacheInfo& cache_info,
PP_Bool* is_hit,
PP_FileHandle* file_handle,
scoped_refptr<ppapi::TrackedCallback> callback);
RequestNexeFdCallback callback);
void SendReportTranslationFinished(PP_Instance instance,
PP_Bool success);
void OnNexeTempFileReply(PP_Instance instance,
......
......@@ -587,6 +587,19 @@ PP_Bool PPIsNonSFIModeEnabled() {
return PP_FromBool(IsNonSFIModeEnabled());
}
void GetNexeFdContinuation(scoped_refptr<ppapi::TrackedCallback> callback,
PP_Bool* out_is_hit,
PP_FileHandle* out_handle,
int32_t pp_error,
bool is_hit,
PP_FileHandle handle) {
if (pp_error == PP_OK) {
*out_is_hit = PP_FromBool(is_hit);
*out_handle = handle;
}
callback->PostRun(pp_error);
}
int32_t GetNexeFd(PP_Instance instance,
const char* pexe_url,
uint32_t abi_version,
......@@ -645,9 +658,7 @@ int32_t GetNexeFd(PP_Instance instance,
GetRoutingID(instance),
instance,
cache_info,
is_hit,
handle,
enter.callback());
base::Bind(&GetNexeFdContinuation, enter.callback(), is_hit, handle));
return enter.SetResult(PP_OK_COMPLETIONPENDING);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment