Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(48)

Side by Side Diff: chrome/browser/printing/print_view_manager.cc

Issue 6783005: Print Preview: Set a print job's title and url correctly. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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_view_manager.h" 5 #include "chrome/browser/printing/print_view_manager.h"
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/utf_string_conversions.h" 8 #include "base/utf_string_conversions.h"
9 #include "chrome/browser/browser_process.h" 9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/printing/print_job.h" 10 #include "chrome/browser/printing/print_job.h"
11 #include "chrome/browser/printing/print_job_manager.h" 11 #include "chrome/browser/printing/print_job_manager.h"
12 #include "chrome/browser/printing/print_preview_tab_controller.h"
12 #include "chrome/browser/printing/printer_query.h" 13 #include "chrome/browser/printing/printer_query.h"
14 #include "chrome/browser/ui/webui/print_preview_ui.h"
13 #include "chrome/common/print_messages.h" 15 #include "chrome/common/print_messages.h"
14 #include "content/browser/renderer_host/render_view_host.h" 16 #include "content/browser/renderer_host/render_view_host.h"
15 #include "content/browser/tab_contents/navigation_entry.h" 17 #include "content/browser/tab_contents/navigation_entry.h"
16 #include "content/browser/tab_contents/tab_contents.h" 18 #include "content/browser/tab_contents/tab_contents.h"
17 #include "content/common/notification_details.h" 19 #include "content/common/notification_details.h"
18 #include "content/common/notification_source.h" 20 #include "content/common/notification_source.h"
19 #include "grit/generated_resources.h" 21 #include "grit/generated_resources.h"
20 #include "printing/native_metafile.h" 22 #include "printing/native_metafile.h"
21 #include "printing/native_metafile_factory.h" 23 #include "printing/native_metafile_factory.h"
22 #include "printing/printed_document.h" 24 #include "printing/printed_document.h"
23 #include "ui/base/l10n/l10n_util.h" 25 #include "ui/base/l10n/l10n_util.h"
24 26
25 using base::TimeDelta; 27 using base::TimeDelta;
26 28
29 namespace {
30
31 string16 GenerateRenderSourceName(TabContents* tab_contents) {
32 string16 name(tab_contents->GetTitle());
33 if (name.empty())
34 name = l10n_util::GetStringUTF16(IDS_DEFAULT_PRINT_DOCUMENT_TITLE);
35 return name;
36 }
37
38 GURL GenerateRenderSourceUrl(TabContents* tab_contents) {
39 NavigationEntry* entry = tab_contents->controller().GetActiveEntry();
40 if (entry)
41 return entry->virtual_url();
42 else
stuartmorgan 2011/04/01 22:34:14 else is unnecessary here.
Lei Zhang 2011/04/04 22:21:03 Copy + Paste. In any case, I'm reverting this part
43 return GURL();
44 }
45
46 } // namespace
47
27 namespace printing { 48 namespace printing {
28 49
29 PrintViewManager::PrintViewManager(TabContents* tab_contents) 50 PrintViewManager::PrintViewManager(TabContents* tab_contents)
30 : TabContentsObserver(tab_contents), 51 : TabContentsObserver(tab_contents),
31 number_pages_(0), 52 number_pages_(0),
32 waiting_to_print_(false), 53 waiting_to_print_(false),
33 printing_succeeded_(false), 54 printing_succeeded_(false),
34 inside_inner_message_loop_(false) { 55 inside_inner_message_loop_(false),
56 overridden_title_url_(false) {
35 #if defined(OS_POSIX) && !defined(OS_MACOSX) 57 #if defined(OS_POSIX) && !defined(OS_MACOSX)
36 expecting_first_page_ = true; 58 expecting_first_page_ = true;
37 #endif 59 #endif
38 } 60 }
39 61
40 PrintViewManager::~PrintViewManager() { 62 PrintViewManager::~PrintViewManager() {
41 DisconnectFromCurrentPrintJob(); 63 DisconnectFromCurrentPrintJob();
42 } 64 }
43 65
44 void PrintViewManager::StopNavigation() { 66 void PrintViewManager::StopNavigation() {
45 // Cancel the current job, wait for the worker to finish. 67 // Cancel the current job, wait for the worker to finish.
46 TerminatePrintJob(true); 68 TerminatePrintJob(true);
47 } 69 }
48 70
49 void PrintViewManager::RenderViewGone() { 71 void PrintViewManager::RenderViewGone() {
50 if (!print_job_.get()) 72 if (!print_job_.get())
51 return; 73 return;
52 74
53 scoped_refptr<PrintedDocument> document(print_job_->document()); 75 scoped_refptr<PrintedDocument> document(print_job_->document());
54 if (document) { 76 if (document) {
55 // If IsComplete() returns false, the document isn't completely rendered. 77 // If IsComplete() returns false, the document isn't completely rendered.
56 // Since our renderer is gone, there's nothing to do, cancel it. Otherwise, 78 // Since our renderer is gone, there's nothing to do, cancel it. Otherwise,
57 // the print job may finish without problem. 79 // the print job may finish without problem.
58 TerminatePrintJob(!document->IsComplete()); 80 TerminatePrintJob(!document->IsComplete());
59 } 81 }
60 } 82 }
61 83
84 void PrintViewManager::OverrideTitleAndUrl(TabContents* tab_contents) {
85 overridden_title_url_ = true;
86 overridden_title_ = GenerateRenderSourceName(tab_contents);
87 overridden_url_ = GenerateRenderSourceUrl(tab_contents);
88 }
89
62 string16 PrintViewManager::RenderSourceName() { 90 string16 PrintViewManager::RenderSourceName() {
63 string16 name(tab_contents()->GetTitle()); 91 if (overridden_title_url_)
64 if (name.empty()) 92 return overridden_title_;
65 name = l10n_util::GetStringUTF16(IDS_DEFAULT_PRINT_DOCUMENT_TITLE); 93 return GenerateRenderSourceName(tab_contents());
66 return name;
67 } 94 }
68 95
69 GURL PrintViewManager::RenderSourceUrl() { 96 GURL PrintViewManager::RenderSourceUrl() {
70 NavigationEntry* entry = tab_contents()->controller().GetActiveEntry(); 97 if (overridden_title_url_)
71 if (entry) 98 return overridden_url_;
72 return entry->virtual_url(); 99 return GenerateRenderSourceUrl(tab_contents());
73 else
74 return GURL();
75 } 100 }
76 101
77 void PrintViewManager::OnDidGetPrintedPagesCount(int cookie, int number_pages) { 102 void PrintViewManager::OnDidGetPrintedPagesCount(int cookie, int number_pages) {
78 DCHECK_GT(cookie, 0); 103 DCHECK_GT(cookie, 0);
79 DCHECK_GT(number_pages, 0); 104 DCHECK_GT(number_pages, 0);
80 number_pages_ = number_pages; 105 number_pages_ = number_pages;
81 if (!OpportunisticallyCreatePrintJob(cookie)) 106 if (!OpportunisticallyCreatePrintJob(cookie))
82 return; 107 return;
83 108
84 PrintedDocument* document = print_job_->document(); 109 PrintedDocument* document = print_job_->document();
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 return false; 453 return false;
429 } 454 }
430 455
431 // Settings are already loaded. Go ahead. This will set 456 // Settings are already loaded. Go ahead. This will set
432 // print_job_->is_job_pending() to true. 457 // print_job_->is_job_pending() to true.
433 print_job_->StartPrinting(); 458 print_job_->StartPrinting();
434 return true; 459 return true;
435 } 460 }
436 461
437 } // namespace printing 462 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698