Simplify the pdf-data handled code.

This commit is contained in:
Haojian Wu 2015-06-13 15:47:32 +08:00
parent 894f9c0cb0
commit 93bbc6c810
2 changed files with 15 additions and 24 deletions

View file

@ -48,20 +48,6 @@ void StopWorker(int document_cookie) {
} }
} }
base::RefCountedBytes* GetDataFromHandle(base::SharedMemoryHandle handle,
uint32 data_size) {
scoped_ptr<base::SharedMemory> shared_buf(
new base::SharedMemory(handle, true));
if (!shared_buf->Map(data_size)) {
NOTREACHED();
return NULL;
}
unsigned char* data_begin = static_cast<unsigned char*>(shared_buf->memory());
std::vector<unsigned char> data(data_begin, data_begin + data_size);
return base::RefCountedBytes::TakeVector(&data);
}
} // namespace } // namespace
namespace printing { namespace printing {
@ -86,15 +72,22 @@ void PrintPreviewMessageHandler::OnMetafileReadyForPrinting(
return; return;
} }
base::RefCountedBytes *data = ( scoped_ptr<base::SharedMemory> shared_buf(
GetDataFromHandle(params.metafile_data_handle, params.data_size)); new base::SharedMemory(params.metafile_data_handle, true));
RunPrintToPDFCallback(params.preview_request_id, data); if (!shared_buf->Map(params.data_size)) {
RunPrintToPDFCallback(params.preview_request_id, nullptr, 0);
return;
}
RunPrintToPDFCallback(params.preview_request_id,
static_cast<char*>(shared_buf->memory()),
params.data_size);
} }
void PrintPreviewMessageHandler::OnPrintPreviewFailed(int document_cookie, void PrintPreviewMessageHandler::OnPrintPreviewFailed(int document_cookie,
int request_id) { int request_id) {
StopWorker(document_cookie); StopWorker(document_cookie);
RunPrintToPDFCallback(request_id, nullptr); RunPrintToPDFCallback(request_id, nullptr, 0);
} }
bool PrintPreviewMessageHandler::OnMessageReceived( bool PrintPreviewMessageHandler::OnMessageReceived(
@ -122,15 +115,13 @@ void PrintPreviewMessageHandler::PrintToPDF(
} }
void PrintPreviewMessageHandler::RunPrintToPDFCallback( void PrintPreviewMessageHandler::RunPrintToPDFCallback(
int request_id, base::RefCountedBytes* data) { int request_id, char* data, uint32 data_size) {
v8::Isolate* isolate = v8::Isolate::GetCurrent(); v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Locker locker(isolate); v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate); v8::HandleScope handle_scope(isolate);
if (data) { if (data) {
v8::Local<v8::Value> buffer = node::Buffer::Use( v8::Local<v8::Value> buffer = node::Buffer::New(isolate,
isolate, data, static_cast<size_t>(data_size));
const_cast<char*>(reinterpret_cast<const char*>(data->front())),
data->size());
print_to_pdf_callback_map_[request_id].Run(v8::Null(isolate), buffer); print_to_pdf_callback_map_[request_id].Run(v8::Null(isolate), buffer);
} else { } else {
v8::Local<v8::String> error_message = v8::String::NewFromUtf8(isolate, v8::Local<v8::String> error_message = v8::String::NewFromUtf8(isolate,

View file

@ -48,7 +48,7 @@ class PrintPreviewMessageHandler
void OnPrintPreviewFailed(int document_cookie, int request_id); void OnPrintPreviewFailed(int document_cookie, int request_id);
void RunPrintToPDFCallback( void RunPrintToPDFCallback(
int request_id, base::RefCountedBytes* data); int request_id, char* data, uint32 data_size);
PrintToPDFCallbackMap print_to_pdf_callback_map_; PrintToPDFCallbackMap print_to_pdf_callback_map_;