Skip to content

Commit

Permalink
Linux Printing: Move file open operation to file thread.
Browse files Browse the repository at this point in the history
Also delete the temp file when we're done with it (usually).

BUG=22097
TEST=printing still works

Review URL: http://codereview.chromium.org/215015

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@26531 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
estade@chromium.org committed Sep 18, 2009
1 parent 767e997 commit a51b060
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 46 deletions.
3 changes: 3 additions & 0 deletions chrome/browser/printing/print_dialog_gtk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <gtk/gtkprintunixdialog.h>
#include <gtk/gtkpagesetupunixdialog.h>

#include "base/file_util.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "chrome/browser/browser_list.h"
Expand Down Expand Up @@ -134,5 +135,7 @@ void PrintDialogGtk::OnJobCompleted(GtkPrintJob* job, GError* error) {
if (job)
g_object_unref(job);

file_util::Delete(path_to_pdf_, false);

delete this;
}
4 changes: 2 additions & 2 deletions chrome/browser/renderer_host/resource_message_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@ bool ResourceMessageFilter::OnMessageReceived(const IPC::Message& msg) {
IPC_MESSAGE_HANDLER(ViewHostMsg_DuplicateSection, OnDuplicateSection)
#endif
#if defined(OS_LINUX)
IPC_MESSAGE_HANDLER(ViewHostMsg_AllocateTempFileForPrinting,
OnAllocateTempFileForPrinting)
IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_AllocateTempFileForPrinting,
OnAllocateTempFileForPrinting)
IPC_MESSAGE_HANDLER(ViewHostMsg_TempFileForPrintingWritten,
OnTempFileForPrintingWritten)
#endif
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/renderer_host/resource_message_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,7 @@ class ResourceMessageFilter : public IPC::ChannelProxy::MessageFilter,
#if defined(OS_LINUX)
// Used to ask the browser allocate a temporary file for the renderer
// to fill in resulting PDF in renderer.
void OnAllocateTempFileForPrinting(base::FileDescriptor* temp_file_fd,
int* fd_in_browser);
void OnAllocateTempFileForPrinting(IPC::Message* reply_msg);
void OnTempFileForPrintingWritten(int fd_in_browser);
#endif

Expand Down Expand Up @@ -270,6 +269,7 @@ class ResourceMessageFilter : public IPC::ChannelProxy::MessageFilter,
void DoOnClipboardReadAsciiText(Clipboard::Buffer buffer,
IPC::Message* reply_msg);
void DoOnClipboardReadHTML(Clipboard::Buffer buffer, IPC::Message* reply_msg);
void DoOnAllocateTempFileForPrinting(IPC::Message* reply_msg);
#endif

bool CheckBenchmarkingEnabled();
Expand Down
98 changes: 56 additions & 42 deletions chrome/browser/renderer_host/resource_message_filter_gtk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void ResourceMessageFilter::DoOnGetScreenInfo(gfx::NativeViewId view,

ChromeThread::GetMessageLoop(ChromeThread::IO)->PostTask(
FROM_HERE, NewRunnableMethod(
this, &ResourceMessageFilter::SendDelayedReply, reply_msg));
this, &ResourceMessageFilter::SendDelayedReply, reply_msg));
}

// Called on the BACKGROUND_X11 thread.
Expand All @@ -80,7 +80,7 @@ void ResourceMessageFilter::DoOnGetWindowRect(gfx::NativeViewId view,

ChromeThread::GetMessageLoop(ChromeThread::IO)->PostTask(
FROM_HERE, NewRunnableMethod(
this, &ResourceMessageFilter::SendDelayedReply, reply_msg));
this, &ResourceMessageFilter::SendDelayedReply, reply_msg));
}

// Return the top-level parent of the given window. Called on the
Expand Down Expand Up @@ -119,7 +119,7 @@ void ResourceMessageFilter::DoOnGetRootWindowRect(gfx::NativeViewId view,

ChromeThread::GetMessageLoop(ChromeThread::IO)->PostTask(
FROM_HERE, NewRunnableMethod(
this, &ResourceMessageFilter::SendDelayedReply, reply_msg));
this, &ResourceMessageFilter::SendDelayedReply, reply_msg));
}

// Called on the UI thread.
Expand All @@ -132,7 +132,7 @@ void ResourceMessageFilter::DoOnClipboardIsFormatAvailable(

ChromeThread::GetMessageLoop(ChromeThread::IO)->PostTask(
FROM_HERE, NewRunnableMethod(
this, &ResourceMessageFilter::SendDelayedReply, reply_msg));
this, &ResourceMessageFilter::SendDelayedReply, reply_msg));
}

// Called on the UI thread.
Expand All @@ -145,7 +145,7 @@ void ResourceMessageFilter::DoOnClipboardReadText(Clipboard::Buffer buffer,

ChromeThread::GetMessageLoop(ChromeThread::IO)->PostTask(
FROM_HERE, NewRunnableMethod(
this, &ResourceMessageFilter::SendDelayedReply, reply_msg));
this, &ResourceMessageFilter::SendDelayedReply, reply_msg));
}

// Called on the UI thread.
Expand All @@ -158,7 +158,7 @@ void ResourceMessageFilter::DoOnClipboardReadAsciiText(

ChromeThread::GetMessageLoop(ChromeThread::IO)->PostTask(
FROM_HERE, NewRunnableMethod(
this, &ResourceMessageFilter::SendDelayedReply, reply_msg));
this, &ResourceMessageFilter::SendDelayedReply, reply_msg));
}

// Called on the UI thread.
Expand All @@ -173,92 +173,106 @@ void ResourceMessageFilter::DoOnClipboardReadHTML(Clipboard::Buffer buffer,

ChromeThread::GetMessageLoop(ChromeThread::IO)->PostTask(
FROM_HERE, NewRunnableMethod(
this, &ResourceMessageFilter::SendDelayedReply, reply_msg));
this, &ResourceMessageFilter::SendDelayedReply, reply_msg));
}

// Called on the FILE thread.
void ResourceMessageFilter::DoOnAllocateTempFileForPrinting(
IPC::Message* reply_msg) {
base::FileDescriptor temp_file_fd;
int fd_in_browser;
temp_file_fd.fd = fd_in_browser = -1;
temp_file_fd.auto_close = false;

FilePath path;
if (file_util::CreateTemporaryFile(&path)) {
int fd = open(path.value().c_str(), O_WRONLY);
if (fd >= 0) {
FdMap* map = &Singleton<PrintingFileDescriptorMap>::get()->map;
FdMap::iterator it = map->find(fd);
if (it != map->end()) {
NOTREACHED() << "The file descriptor is in use. fd=" << fd;
} else {
(*map)[fd] = path;
temp_file_fd.fd = fd_in_browser = fd;
temp_file_fd.auto_close = true;
}
}
}

ViewHostMsg_AllocateTempFileForPrinting::WriteReplyParams(
reply_msg, temp_file_fd, fd_in_browser);

ChromeThread::GetMessageLoop(ChromeThread::IO)->PostTask(
FROM_HERE, NewRunnableMethod(
this, &ResourceMessageFilter::SendDelayedReply, reply_msg));
}

// Called on the IO thread.
void ResourceMessageFilter::OnGetScreenInfo(gfx::NativeViewId view,
IPC::Message* reply_msg) {
ChromeThread::GetMessageLoop(ChromeThread::BACKGROUND_X11)->PostTask(
FROM_HERE, NewRunnableMethod(
this, &ResourceMessageFilter::DoOnGetScreenInfo, view, reply_msg));
this, &ResourceMessageFilter::DoOnGetScreenInfo, view, reply_msg));
}

// Called on the IO thread.
void ResourceMessageFilter::OnGetWindowRect(gfx::NativeViewId view,
IPC::Message* reply_msg) {
ChromeThread::GetMessageLoop(ChromeThread::BACKGROUND_X11)->PostTask(
FROM_HERE, NewRunnableMethod(
this, &ResourceMessageFilter::DoOnGetWindowRect, view, reply_msg));
this, &ResourceMessageFilter::DoOnGetWindowRect, view, reply_msg));
}

// Called on the IO thread.
void ResourceMessageFilter::OnGetRootWindowRect(gfx::NativeViewId view,
IPC::Message* reply_msg) {
ChromeThread::GetMessageLoop(ChromeThread::BACKGROUND_X11)->PostTask(
FROM_HERE, NewRunnableMethod(
this, &ResourceMessageFilter::DoOnGetRootWindowRect, view, reply_msg));
this, &ResourceMessageFilter::DoOnGetRootWindowRect, view,
reply_msg));
}

// Called on the IO thread.
void ResourceMessageFilter::OnClipboardIsFormatAvailable(
Clipboard::FormatType format, Clipboard::Buffer buffer,
IPC::Message* reply_msg) {
ui_loop()->PostTask(FROM_HERE, NewRunnableMethod(
this, &ResourceMessageFilter::DoOnClipboardIsFormatAvailable, format,
buffer, reply_msg));
this, &ResourceMessageFilter::DoOnClipboardIsFormatAvailable, format,
buffer, reply_msg));
}

// Called on the IO thread.
void ResourceMessageFilter::OnClipboardReadText(Clipboard::Buffer buffer,
IPC::Message* reply_msg) {
ui_loop()->PostTask(FROM_HERE, NewRunnableMethod(
this, &ResourceMessageFilter::DoOnClipboardReadText, buffer,
reply_msg));
this, &ResourceMessageFilter::DoOnClipboardReadText, buffer,
reply_msg));
}

// Called on the IO thread.
void ResourceMessageFilter::OnClipboardReadAsciiText(Clipboard::Buffer buffer,
IPC::Message* reply_msg) {
ui_loop()->PostTask(FROM_HERE, NewRunnableMethod(
this, &ResourceMessageFilter::DoOnClipboardReadAsciiText, buffer,
reply_msg));
this, &ResourceMessageFilter::DoOnClipboardReadAsciiText, buffer,
reply_msg));
}

// Called on the IO thread.
void ResourceMessageFilter::OnClipboardReadHTML(Clipboard::Buffer buffer,
IPC::Message* reply_msg) {
ui_loop()->PostTask(FROM_HERE, NewRunnableMethod(
this, &ResourceMessageFilter::DoOnClipboardReadHTML, buffer,
reply_msg));
this, &ResourceMessageFilter::DoOnClipboardReadHTML, buffer,
reply_msg));
}

// Called on the IO thread.
void ResourceMessageFilter::OnAllocateTempFileForPrinting(
base::FileDescriptor* temp_file_fd, int* fd_in_browser) {
temp_file_fd->fd = *fd_in_browser = -1;

FilePath path;
if (!file_util::CreateTemporaryFile(&path))
return;

int fd = open(path.value().c_str(), O_WRONLY);
if (fd < 0)
return;

// We need to remember the FilePath of the temporary file because we need
// it when we want to rename/move it, and more importantly, to print it
// when we print by using gtk_print_job_set_source_file().
FdMap* map = &Singleton<PrintingFileDescriptorMap>::get()->map;
FdMap::iterator it = map->find(fd);
if (it != map->end()) {
NOTREACHED() << "The file descriptor is in use. fd=" << fd;
return;
}

(*map)[fd] = path;
temp_file_fd->fd = *fd_in_browser = fd;
temp_file_fd->auto_close = true;
IPC::Message* reply_msg) {
ChromeThread::GetMessageLoop(ChromeThread::FILE)->PostTask(
FROM_HERE, NewRunnableMethod(
this, &ResourceMessageFilter::DoOnAllocateTempFileForPrinting,
reply_msg));
}

// Called on the IO thread.
Expand Down

0 comments on commit a51b060

Please sign in to comment.