Skip to content

Commit

Permalink
Added PrintingContext::Delegate to get parent view handle and applica…
Browse files Browse the repository at this point in the history
…tion locale.

BUG=374321

Committed: https://chromium.googlesource.com/chromium/src/+/ee8f4e4029c09ba77e7dbb8fddd85186642b3de8

R=jschuh@chromium.org, noamsml@chromium.org, thestig@chromium.org, yzshen@chromium.org

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

Cr-Commit-Position: refs/heads/master@{#291871}
  • Loading branch information
vitalybuka committed Aug 26, 2014
1 parent 4d18cc8 commit bd7c981
Show file tree
Hide file tree
Showing 27 changed files with 281 additions and 205 deletions.
7 changes: 5 additions & 2 deletions chrome/browser/printing/print_job_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ scoped_refptr<PrinterQuery> PrintQueriesQueue::PopPrinterQuery(
return NULL;
}

scoped_refptr<PrinterQuery> PrintQueriesQueue::CreatePrinterQuery() {
scoped_refptr<PrinterQuery> job = new printing::PrinterQuery;
scoped_refptr<PrinterQuery> PrintQueriesQueue::CreatePrinterQuery(
int render_process_id,
int render_view_id) {
scoped_refptr<PrinterQuery> job =
new printing::PrinterQuery(render_process_id, render_view_id);
base::AutoLock lock(lock_);
job->SetWorkerDestination(destination_);
return job;
Expand Down
4 changes: 3 additions & 1 deletion chrome/browser/printing/print_job_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/synchronization/lock.h"
#include "base/threading/non_thread_safe.h"
#include "content/public/browser/notification_observer.h"
Expand Down Expand Up @@ -39,7 +40,8 @@ class PrintQueriesQueue : public base::RefCountedThreadSafe<PrintQueriesQueue> {
scoped_refptr<PrinterQuery> PopPrinterQuery(int document_cookie);

// Creates new query.
scoped_refptr<PrinterQuery> CreatePrinterQuery();
scoped_refptr<PrinterQuery> CreatePrinterQuery(int render_process_id,
int render_view_id);

void Shutdown();

Expand Down
27 changes: 14 additions & 13 deletions chrome/browser/printing/print_job_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/printing/print_job.h"

#include "base/message_loop/message_loop.h"
#include "base/strings/string16.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/printing/print_job.h"
#include "chrome/browser/printing/print_job_worker.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/notification_service.h"
#include "content/public/common/child_process_host.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "printing/printed_pages_source.h"
#include "testing/gtest/include/gtest/gtest.h"

Expand All @@ -24,8 +27,9 @@ class TestSource : public printing::PrintedPagesSource {
class TestPrintJobWorker : public printing::PrintJobWorker {
public:
explicit TestPrintJobWorker(printing::PrintJobWorkerOwner* owner)
: printing::PrintJobWorker(owner) {
}
: printing::PrintJobWorker(content::ChildProcessHost::kInvalidUniqueID,
content::ChildProcessHost::kInvalidUniqueID,
owner) {}
friend class TestOwner;
};

Expand Down Expand Up @@ -82,18 +86,15 @@ class TestPrintNotifObserv : public content::NotificationObserver {

} // namespace

typedef testing::Test PrintJobTest;

TEST_F(PrintJobTest, SimplePrint) {
TEST(PrintJobTest, SimplePrint) {
// Test the multi-threaded nature of PrintJob to make sure we can use it with
// known lifetime.

// This message loop is actually never run.
base::MessageLoop current;

content::TestBrowserThreadBundle thread_bundle_;
content::NotificationRegistrar registrar_;
TestPrintNotifObserv observ;
registrar_.Add(&observ, content::NOTIFICATION_ALL,
registrar_.Add(&observ,
content::NOTIFICATION_ALL,
content::NotificationService::AllSources());
volatile bool check = false;
scoped_refptr<printing::PrintJob> job(new TestPrintJob(&check));
Expand All @@ -103,17 +104,17 @@ TEST_F(PrintJobTest, SimplePrint) {
job->Initialize(owner.get(), &source, 1);
job->Stop();
while (job->document()) {
current.RunUntilIdle();
base::MessageLoop::current()->RunUntilIdle();
}
EXPECT_FALSE(job->document());
job = NULL;
while (!check) {
current.RunUntilIdle();
base::MessageLoop::current()->RunUntilIdle();
}
EXPECT_TRUE(check);
}

TEST_F(PrintJobTest, SimplePrintLateInit) {
TEST(PrintJobTest, SimplePrintLateInit) {
volatile bool check = false;
base::MessageLoop current;
scoped_refptr<printing::PrintJob> job(new TestPrintJob(&check));
Expand Down
97 changes: 77 additions & 20 deletions chrome/browser/printing/print_job_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "chrome/grit/generated_resources.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "printing/print_job_constants.h"
#include "printing/printed_document.h"
#include "printing/printed_page.h"
Expand All @@ -35,7 +37,66 @@ void HoldRefCallback(const scoped_refptr<printing::PrintJobWorkerOwner>& owner,
callback.Run();
}

} // namespace
class PrintingContextDelegate : public PrintingContext::Delegate {
public:
PrintingContextDelegate(int render_process_id, int render_view_id);
virtual ~PrintingContextDelegate();

virtual gfx::NativeView GetParentView() OVERRIDE;
virtual std::string GetAppLocale() OVERRIDE;

private:
void InitOnUiThread(int render_process_id, int render_view_id);

scoped_ptr<PrintingUIWebContentsObserver> web_contents_observer_;
base::WeakPtrFactory<PrintingContextDelegate> weak_ptr_factory_;
};

PrintingContextDelegate::PrintingContextDelegate(int render_process_id,
int render_view_id)
: weak_ptr_factory_(this) {
InitOnUiThread(render_process_id, render_view_id);
}

PrintingContextDelegate::~PrintingContextDelegate() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
}

gfx::NativeView PrintingContextDelegate::GetParentView() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (!web_contents_observer_)
return NULL;
return web_contents_observer_->GetParentView();
}

std::string PrintingContextDelegate::GetAppLocale() {
return g_browser_process->GetApplicationLocale();
}

void PrintingContextDelegate::InitOnUiThread(int render_process_id,
int render_view_id) {
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
// All data initialized here should be accessed on UI thread. Because object
// is being constructed now, anything that is going to access
// PrintingContextDelegate on UI thread would be scheduled after the tasks
// below.
BrowserThread::PostTask(BrowserThread::UI,
FROM_HERE,
base::Bind(&PrintingContextDelegate::InitOnUiThread,
weak_ptr_factory_.GetWeakPtr(),
render_process_id,
render_view_id));
return;
}
content::RenderViewHost* view =
content::RenderViewHost::FromID(render_process_id, render_view_id);
if (!view)
return;
content::WebContents* wc = content::WebContents::FromRenderViewHost(view);
if (!wc)
return;
web_contents_observer_.reset(new PrintingUIWebContentsObserver(wc));
}

void NotificationCallback(PrintJobWorkerOwner* print_job,
JobEventDetails::Type detail_type,
Expand All @@ -49,13 +110,18 @@ void NotificationCallback(PrintJobWorkerOwner* print_job,
content::Details<JobEventDetails>(details));
}

PrintJobWorker::PrintJobWorker(PrintJobWorkerOwner* owner)
} // namespace

PrintJobWorker::PrintJobWorker(int render_process_id,
int render_view_id,
PrintJobWorkerOwner* owner)
: owner_(owner), thread_("Printing_Worker"), weak_factory_(this) {
// The object is created in the IO thread.
DCHECK(owner_->RunsTasksOnCurrentThread());

printing_context_.reset(PrintingContext::Create(
g_browser_process->GetApplicationLocale()));
printing_context_delegate_.reset(
new PrintingContextDelegate(render_process_id, render_view_id));
printing_context_ = PrintingContext::Create(printing_context_delegate_.get());
}

PrintJobWorker::~PrintJobWorker() {
Expand All @@ -78,7 +144,6 @@ void PrintJobWorker::SetPrintDestination(

void PrintJobWorker::GetSettings(
bool ask_user_for_settings,
scoped_ptr<PrintingUIWebContentsObserver> web_contents_observer,
int document_page_count,
bool has_selection,
MarginType margin_type) {
Expand All @@ -101,12 +166,9 @@ void PrintJobWorker::GetSettings(
base::Bind(&HoldRefCallback, make_scoped_refptr(owner_),
base::Bind(&PrintJobWorker::GetSettingsWithUI,
base::Unretained(this),
base::Passed(&web_contents_observer),
document_page_count,
has_selection)));
} else {
BrowserThread::DeleteSoon(
BrowserThread::UI, FROM_HERE, web_contents_observer.release());
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&HoldRefCallback, make_scoped_refptr(owner_),
Expand All @@ -131,6 +193,7 @@ void PrintJobWorker::SetSettings(

void PrintJobWorker::UpdatePrintSettings(
scoped_ptr<base::DictionaryValue> new_settings) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
PrintingContext::Result result =
printing_context_->UpdatePrintSettings(*new_settings);
GetSettingsDone(result);
Expand All @@ -155,18 +218,12 @@ void PrintJobWorker::GetSettingsDone(PrintingContext::Result result) {
}

void PrintJobWorker::GetSettingsWithUI(
scoped_ptr<PrintingUIWebContentsObserver> web_contents_observer,
int document_page_count,
bool has_selection) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);

gfx::NativeView parent_view = web_contents_observer->GetParentView();
if (!parent_view) {
GetSettingsWithUIDone(printing::PrintingContext::FAILED);
return;
}
printing_context_->AskUserForSettings(
parent_view, document_page_count, has_selection,
document_page_count,
has_selection,
base::Bind(&PrintJobWorker::GetSettingsWithUIDone,
base::Unretained(this)));
}
Expand Down Expand Up @@ -316,7 +373,7 @@ void PrintJobWorker::OnDocumentDone() {
}

owner_->PostTask(FROM_HERE,
base::Bind(NotificationCallback,
base::Bind(&NotificationCallback,
make_scoped_refptr(owner_),
JobEventDetails::DOC_DONE,
document_,
Expand All @@ -332,7 +389,7 @@ void PrintJobWorker::SpoolPage(PrintedPage* page) {

// Signal everyone that the page is about to be printed.
owner_->PostTask(FROM_HERE,
base::Bind(NotificationCallback,
base::Bind(&NotificationCallback,
make_scoped_refptr(owner_),
JobEventDetails::NEW_PAGE,
document_,
Expand Down Expand Up @@ -371,7 +428,7 @@ void PrintJobWorker::SpoolPage(PrintedPage* page) {

// Signal everyone that the page is printed.
owner_->PostTask(FROM_HERE,
base::Bind(NotificationCallback,
base::Bind(&NotificationCallback,
make_scoped_refptr(owner_),
JobEventDetails::PAGE_DONE,
document_,
Expand All @@ -385,7 +442,7 @@ void PrintJobWorker::OnFailure() {
scoped_refptr<PrintJobWorkerOwner> handle(owner_);

owner_->PostTask(FROM_HERE,
base::Bind(NotificationCallback,
base::Bind(&NotificationCallback,
make_scoped_refptr(owner_),
JobEventDetails::FAILED,
document_,
Expand Down
18 changes: 11 additions & 7 deletions chrome/browser/printing/print_job_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread.h"
#include "content/public/browser/browser_thread.h"
#include "printing/page_number.h"
#include "printing/print_destination_interface.h"
#include "printing/printing_context.h"
#include "printing/print_job_constants.h"
#include "printing/printing_context.h"

namespace base {
class DictionaryValue;
Expand All @@ -24,7 +25,6 @@ class PrintJob;
class PrintJobWorkerOwner;
class PrintedDocument;
class PrintedPage;
class PrintingUIWebContentsObserver;

// Worker thread code. It manages the PrintingContext, which can be blocking
// and/or run a message loop. This is the object that generates most
Expand All @@ -33,7 +33,9 @@ class PrintingUIWebContentsObserver;
// PrintJob always outlives its worker instance.
class PrintJobWorker {
public:
explicit PrintJobWorker(PrintJobWorkerOwner* owner);
PrintJobWorker(int render_process_id,
int render_view_id,
PrintJobWorkerOwner* owner);
virtual ~PrintJobWorker();

void SetNewOwner(PrintJobWorkerOwner* new_owner);
Expand All @@ -46,14 +48,12 @@ class PrintJobWorker {
// Print... dialog box will be shown to ask the user his preference.
void GetSettings(
bool ask_user_for_settings,
scoped_ptr<PrintingUIWebContentsObserver> web_contents_observer,
int document_page_count,
bool has_selection,
MarginType margin_type);

// Set the new print settings.
void SetSettings(
scoped_ptr<base::DictionaryValue> new_settings);
void SetSettings(scoped_ptr<base::DictionaryValue> new_settings);

// Starts the printing loop. Every pages are printed as soon as the data is
// available. Makes sure the new_document is the right one.
Expand Down Expand Up @@ -112,7 +112,6 @@ class PrintJobWorker {
// Required on Mac and Linux. Windows can display UI from non-main threads,
// but sticks with this for consistency.
void GetSettingsWithUI(
scoped_ptr<PrintingUIWebContentsObserver> web_contents_observer,
int document_page_count,
bool has_selection);

Expand All @@ -132,6 +131,11 @@ class PrintJobWorker {
// systems.
void UseDefaultSettings();

// Printing context delegate.
scoped_ptr<PrintingContext::Delegate,
content::BrowserThread::DeleteOnUIThread>
printing_context_delegate_;

// Information about the printer setting.
scoped_ptr<PrintingContext> printing_context_;

Expand Down
6 changes: 2 additions & 4 deletions chrome/browser/printing/printer_query.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

namespace printing {

PrinterQuery::PrinterQuery()
: worker_(new PrintJobWorker(this)),
PrinterQuery::PrinterQuery(int render_process_id, int render_view_id)
: worker_(new PrintJobWorker(render_process_id, render_view_id, this)),
is_print_dialog_box_shown_(false),
cookie_(PrintSettings::NewCookie()),
last_status_(PrintingContext::FAILED) {
Expand Down Expand Up @@ -66,7 +66,6 @@ int PrinterQuery::cookie() const {

void PrinterQuery::GetSettings(
GetSettingsAskParam ask_user_for_settings,
scoped_ptr<PrintingUIWebContentsObserver> web_contents_observer,
int expected_page_count,
bool has_selection,
MarginType margin_type,
Expand All @@ -82,7 +81,6 @@ void PrinterQuery::GetSettings(
base::Bind(&PrintJobWorker::GetSettings,
base::Unretained(worker_.get()),
is_print_dialog_box_shown_,
base::Passed(&web_contents_observer),
expected_page_count,
has_selection,
margin_type));
Expand Down
Loading

0 comments on commit bd7c981

Please sign in to comment.