Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/printing/print_job_worker.h" | 5 #include "chrome/browser/printing/print_job_worker.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/chrome_notification_types.h" | 14 #include "chrome/browser/chrome_notification_types.h" |
| 15 #include "chrome/browser/printing/print_job.h" | 15 #include "chrome/browser/printing/print_job.h" |
| 16 #include "chrome/browser/printing/printing_ui_web_contents_observer.h" | |
| 17 #include "chrome/grit/generated_resources.h" | 16 #include "chrome/grit/generated_resources.h" |
| 18 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
| 20 #include "content/public/browser/render_view_host.h" | 19 #include "content/public/browser/render_view_host.h" |
| 21 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
| 22 #include "printing/print_job_constants.h" | 21 #include "printing/print_job_constants.h" |
| 23 #include "printing/printed_document.h" | 22 #include "printing/printed_document.h" |
| 24 #include "printing/printed_page.h" | 23 #include "printing/printed_page.h" |
| 25 #include "printing/printing_utils.h" | 24 #include "printing/printing_utils.h" |
| 26 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 39 | 38 |
| 40 class PrintingContextDelegate : public PrintingContext::Delegate { | 39 class PrintingContextDelegate : public PrintingContext::Delegate { |
| 41 public: | 40 public: |
| 42 PrintingContextDelegate(int render_process_id, int render_view_id); | 41 PrintingContextDelegate(int render_process_id, int render_view_id); |
| 43 virtual ~PrintingContextDelegate(); | 42 virtual ~PrintingContextDelegate(); |
| 44 | 43 |
| 45 virtual gfx::NativeView GetParentView() OVERRIDE; | 44 virtual gfx::NativeView GetParentView() OVERRIDE; |
| 46 virtual std::string GetAppLocale() OVERRIDE; | 45 virtual std::string GetAppLocale() OVERRIDE; |
| 47 | 46 |
| 48 private: | 47 private: |
| 49 void InitOnUiThread(int render_process_id, int render_view_id); | 48 int render_process_id_; |
| 50 | 49 int render_view_id_; |
| 51 scoped_ptr<PrintingUIWebContentsObserver> web_contents_observer_; | |
| 52 base::WeakPtrFactory<PrintingContextDelegate> weak_ptr_factory_; | 50 base::WeakPtrFactory<PrintingContextDelegate> weak_ptr_factory_; |
| 53 }; | 51 }; |
| 54 | 52 |
| 55 PrintingContextDelegate::PrintingContextDelegate(int render_process_id, | 53 PrintingContextDelegate::PrintingContextDelegate(int render_process_id, |
| 56 int render_view_id) | 54 int render_view_id) |
| 57 : weak_ptr_factory_(this) { | 55 : weak_ptr_factory_(this), |
| 58 InitOnUiThread(render_process_id, render_view_id); | 56 render_process_id_(render_process_id), |
| 57 render_view_id_(render_view_id) { | |
| 59 } | 58 } |
| 60 | 59 |
| 61 PrintingContextDelegate::~PrintingContextDelegate() { | 60 PrintingContextDelegate::~PrintingContextDelegate() { |
| 62 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
|
Noam Samuel
2014/08/26 21:09:30
Have DCHECK for new thread if it matters?
Vitaly Buka (NO REVIEWS)
2014/08/26 21:12:24
web_contents_observer_ was the reason we wanted de
| |
| 63 } | 61 } |
| 64 | 62 |
| 65 gfx::NativeView PrintingContextDelegate::GetParentView() { | 63 gfx::NativeView PrintingContextDelegate::GetParentView() { |
| 66 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 64 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 67 if (!web_contents_observer_) | 65 content::RenderViewHost* view = |
| 66 content::RenderViewHost::FromID(render_process_id_, render_view_id_); | |
| 67 if (!view) | |
| 68 return NULL; | 68 return NULL; |
| 69 return web_contents_observer_->GetParentView(); | 69 content::WebContents* wc = content::WebContents::FromRenderViewHost(view); |
| 70 return wc ? wc->GetNativeView() : NULL; | |
| 70 } | 71 } |
| 71 | 72 |
| 72 std::string PrintingContextDelegate::GetAppLocale() { | 73 std::string PrintingContextDelegate::GetAppLocale() { |
| 73 return g_browser_process->GetApplicationLocale(); | 74 return g_browser_process->GetApplicationLocale(); |
| 74 } | 75 } |
| 75 | 76 |
| 76 void PrintingContextDelegate::InitOnUiThread(int render_process_id, | |
| 77 int render_view_id) { | |
| 78 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 79 // All data initialized here should be accessed on UI thread. Because object | |
| 80 // is being constructed now, anything that is going to access | |
| 81 // PrintingContextDelegate on UI thread would be scheduled after the tasks | |
| 82 // below. | |
| 83 BrowserThread::PostTask(BrowserThread::UI, | |
| 84 FROM_HERE, | |
| 85 base::Bind(&PrintingContextDelegate::InitOnUiThread, | |
| 86 weak_ptr_factory_.GetWeakPtr(), | |
| 87 render_process_id, | |
| 88 render_view_id)); | |
| 89 return; | |
| 90 } | |
| 91 content::RenderViewHost* view = | |
| 92 content::RenderViewHost::FromID(render_process_id, render_view_id); | |
| 93 if (!view) | |
| 94 return; | |
| 95 content::WebContents* wc = content::WebContents::FromRenderViewHost(view); | |
| 96 if (!wc) | |
| 97 return; | |
| 98 web_contents_observer_.reset(new PrintingUIWebContentsObserver(wc)); | |
| 99 } | |
| 100 | |
| 101 void NotificationCallback(PrintJobWorkerOwner* print_job, | 77 void NotificationCallback(PrintJobWorkerOwner* print_job, |
| 102 JobEventDetails::Type detail_type, | 78 JobEventDetails::Type detail_type, |
| 103 PrintedDocument* document, | 79 PrintedDocument* document, |
| 104 PrintedPage* page) { | 80 PrintedPage* page) { |
| 105 JobEventDetails* details = new JobEventDetails(detail_type, document, page); | 81 JobEventDetails* details = new JobEventDetails(detail_type, document, page); |
| 106 content::NotificationService::current()->Notify( | 82 content::NotificationService::current()->Notify( |
| 107 chrome::NOTIFICATION_PRINT_JOB_EVENT, | 83 chrome::NOTIFICATION_PRINT_JOB_EVENT, |
| 108 // We know that is is a PrintJob object in this circumstance. | 84 // We know that is is a PrintJob object in this circumstance. |
| 109 content::Source<PrintJob>(static_cast<PrintJob*>(print_job)), | 85 content::Source<PrintJob>(static_cast<PrintJob*>(print_job)), |
| 110 content::Details<JobEventDetails>(details)); | 86 content::Details<JobEventDetails>(details)); |
| (...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 448 document_, | 424 document_, |
| 449 scoped_refptr<PrintedPage>())); | 425 scoped_refptr<PrintedPage>())); |
| 450 Cancel(); | 426 Cancel(); |
| 451 | 427 |
| 452 // Makes sure the variables are reinitialized. | 428 // Makes sure the variables are reinitialized. |
| 453 document_ = NULL; | 429 document_ = NULL; |
| 454 page_number_ = PageNumber::npos(); | 430 page_number_ = PageNumber::npos(); |
| 455 } | 431 } |
| 456 | 432 |
| 457 } // namespace printing | 433 } // namespace printing |
| OLD | NEW |