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

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

Issue 6533006: Print Preview: Hook up the print button to initiate printing without displaying a print dialog. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: '' Created 9 years, 10 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) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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/json/json_reader.h"
7 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/values.h"
8 #include "chrome/browser/browser_process.h" 10 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/browser_thread.h" 11 #include "chrome/browser/browser_thread.h"
12 #include "chrome/browser/dom_ui/web_ui_util.h"
10 #include "chrome/browser/printing/print_job.h" 13 #include "chrome/browser/printing/print_job.h"
11 #include "chrome/common/notification_service.h" 14 #include "chrome/common/notification_service.h"
12 #include "printing/printed_document.h" 15 #include "printing/printed_document.h"
13 #include "printing/printed_page.h" 16 #include "printing/printed_page.h"
14 17
15 namespace printing { 18 namespace printing {
16 19
17 class PrintJobWorker::NotificationTask : public Task { 20 class PrintJobWorker::NotificationTask : public Task {
18 public: 21 public:
19 NotificationTask() : print_job_(NULL), details_(NULL) { 22 NotificationTask() : print_job_(NULL), details_(NULL) {
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 BrowserThread::UI, FROM_HERE, 91 BrowserThread::UI, FROM_HERE,
89 NewRunnableMethod(this, &PrintJobWorker::GetSettingsWithUI, 92 NewRunnableMethod(this, &PrintJobWorker::GetSettingsWithUI,
90 parent_view, document_page_count, 93 parent_view, document_page_count,
91 has_selection)); 94 has_selection));
92 } else { 95 } else {
93 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 96 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
94 NewRunnableMethod(this, &PrintJobWorker::UseDefaultSettings)); 97 NewRunnableMethod(this, &PrintJobWorker::UseDefaultSettings));
95 } 98 }
96 } 99 }
97 100
101 void PrintJobWorker::SetSettings(const std::string& new_settings) {
102 DCHECK_EQ(message_loop(), MessageLoop::current());
103 DCHECK_EQ(page_number_, PageNumber::npos());
104
105 // Disable recursive task processing.
106 MessageLoop::current()->SetNestableTasksAllowed(false);
107
108 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
109 NewRunnableMethod(this, &PrintJobWorker::UpdatePrintSettings,
110 new_settings));
111 }
112
113 void PrintJobWorker::UpdatePrintSettings(const std::string& new_settings) {
114 scoped_ptr<Value> parsed_value(base::JSONReader::Read(new_settings, false));
115 if (!parsed_value.get() || !parsed_value->IsType(Value::TYPE_DICTIONARY)) {
116 NOTREACHED() << "Unable to parse print params";
117 return;
118 }
119 DictionaryValue* settings =
120 static_cast<DictionaryValue*>(parsed_value.get());
121
122 // Create new PageRanges based on |new_settings|.
123 PageRanges new_ranges;
124 ListValue* pageRangeArray = NULL;
125 if (settings->GetList("pageRange", &pageRangeArray)) {
126 for (size_t index = 0; index < pageRangeArray->GetSize(); index++) {
127 std::string json_str(
128 web_ui_util::GetJsonResponseFromArgumentList(pageRangeArray, index));
129 if (json_str.empty())
130 continue;
131
132 scoped_ptr<Value> parsed_val(base::JSONReader::Read(json_str, false));
133 if (!parsed_val.get() || !parsed_val->IsType(Value::TYPE_DICTIONARY)) {
134 NOTREACHED() << "Unable to parse page range params";
135 continue;
136 }
137 DictionaryValue* dict = static_cast<DictionaryValue*>(parsed_val.get());
138 if (dict) {
139 int printFrom = 0;
140 int printTo = 0;
141 if (dict->GetInteger("from", &printFrom) &&
142 dict->GetInteger("to", &printTo)) {
143 if (!(printFrom < 1) && !(printTo < 1)) {
144 PageRange range;
145 range.from = printFrom - 1;
146 range.to = printTo - 1;
147 new_ranges.push_back(range);
148 }
149 }
150 }
151 }
152 }
153 PrintingContext::Result result =
154 printing_context_->UpdatePrintSettings(new_settings, new_ranges);
155 GetSettingsDone(result);
156 }
157
98 void PrintJobWorker::GetSettingsDone(PrintingContext::Result result) { 158 void PrintJobWorker::GetSettingsDone(PrintingContext::Result result) {
99 // Most PrintingContext functions may start a message loop and process 159 // Most PrintingContext functions may start a message loop and process
100 // message recursively, so disable recursive task processing. 160 // message recursively, so disable recursive task processing.
101 MessageLoop::current()->SetNestableTasksAllowed(false); 161 MessageLoop::current()->SetNestableTasksAllowed(false);
102 162
103 // We can't use OnFailure() here since owner_ may not support notifications. 163 // We can't use OnFailure() here since owner_ may not support notifications.
104 164
105 // PrintJob will create the new PrintedDocument. 165 // PrintJob will create the new PrintedDocument.
106 owner_->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( 166 owner_->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
107 owner_, 167 owner_,
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 printing::PrintJobWorker* obj) { 366 printing::PrintJobWorker* obj) {
307 DCHECK(!owner_.get()); 367 DCHECK(!owner_.get());
308 owner_ = obj->owner_; 368 owner_ = obj->owner_;
309 } 369 }
310 370
311 void RunnableMethodTraits<printing::PrintJobWorker>::ReleaseCallee( 371 void RunnableMethodTraits<printing::PrintJobWorker>::ReleaseCallee(
312 printing::PrintJobWorker* obj) { 372 printing::PrintJobWorker* obj) {
313 DCHECK_EQ(owner_, obj->owner_); 373 DCHECK_EQ(owner_, obj->owner_);
314 owner_ = NULL; 374 owner_ = NULL;
315 } 375 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698