OLD | NEW |
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_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" |
10 #include "chrome/browser/printing/print_job.h" | 12 #include "chrome/browser/printing/print_job.h" |
11 #include "chrome/common/notification_service.h" | 13 #include "chrome/common/notification_service.h" |
12 #include "printing/printed_document.h" | 14 #include "printing/printed_document.h" |
13 #include "printing/printed_page.h" | 15 #include "printing/printed_page.h" |
14 | 16 |
15 namespace printing { | 17 namespace printing { |
16 | 18 |
17 class PrintJobWorker::NotificationTask : public Task { | 19 class PrintJobWorker::NotificationTask : public Task { |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 BrowserThread::UI, FROM_HERE, | 93 BrowserThread::UI, FROM_HERE, |
92 NewRunnableMethod(this, &PrintJobWorker::GetSettingsWithUI, | 94 NewRunnableMethod(this, &PrintJobWorker::GetSettingsWithUI, |
93 parent_view, document_page_count, | 95 parent_view, document_page_count, |
94 has_selection)); | 96 has_selection)); |
95 } else { | 97 } else { |
96 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 98 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
97 NewRunnableMethod(this, &PrintJobWorker::UseDefaultSettings)); | 99 NewRunnableMethod(this, &PrintJobWorker::UseDefaultSettings)); |
98 } | 100 } |
99 } | 101 } |
100 | 102 |
| 103 void PrintJobWorker::SetSettings(const std::string& new_settings) { |
| 104 DCHECK_EQ(message_loop(), MessageLoop::current()); |
| 105 DCHECK_EQ(page_number_, PageNumber::npos()); |
| 106 |
| 107 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 108 NewRunnableMethod(this, &PrintJobWorker::UpdatePrintSettings, |
| 109 new_settings)); |
| 110 } |
| 111 |
| 112 void PrintJobWorker::UpdatePrintSettings(const std::string& new_settings) { |
| 113 scoped_ptr<Value> parsed_value(base::JSONReader::Read(new_settings, false)); |
| 114 if (!parsed_value.get() || !parsed_value->IsType(Value::TYPE_DICTIONARY)) { |
| 115 NOTREACHED() << "Unable to parse print params"; |
| 116 return; |
| 117 } |
| 118 DictionaryValue* settings = |
| 119 static_cast<DictionaryValue*>(parsed_value.get()); |
| 120 |
| 121 // Create new PageRanges based on |new_settings|. |
| 122 PageRanges new_ranges; |
| 123 ListValue* pageRangeArray = NULL; |
| 124 if (settings->GetList("pageRange", &pageRangeArray)) { |
| 125 for (size_t index = 0; index < pageRangeArray->GetSize(); index++) { |
| 126 DictionaryValue* dict = NULL; |
| 127 pageRangeArray->GetDictionary(index, &dict); |
| 128 if (dict) { |
| 129 int printFrom = 0; |
| 130 int printTo = 0; |
| 131 if (dict->GetInteger("from", &printFrom) && |
| 132 dict->GetInteger("to", &printTo)) { |
| 133 PageRange range; |
| 134 // Page numbers are 0-based. |
| 135 range.from = printFrom - 1; |
| 136 range.to = printTo - 1; |
| 137 new_ranges.push_back(range); |
| 138 } |
| 139 } |
| 140 } |
| 141 } |
| 142 PrintingContext::Result result = |
| 143 printing_context_->UpdatePrintSettings(new_ranges); |
| 144 GetSettingsDone(result); |
| 145 } |
| 146 |
101 void PrintJobWorker::GetSettingsDone(PrintingContext::Result result) { | 147 void PrintJobWorker::GetSettingsDone(PrintingContext::Result result) { |
102 // Most PrintingContext functions may start a message loop and process | 148 // Most PrintingContext functions may start a message loop and process |
103 // message recursively, so disable recursive task processing. | 149 // message recursively, so disable recursive task processing. |
104 // TODO(thestig): see above comment. SetNestableTasksAllowed(false) needs to | 150 // TODO(thestig): see above comment. SetNestableTasksAllowed(false) needs to |
105 // be called on the same thread as the previous call. See | 151 // be called on the same thread as the previous call. See |
106 // http://crbug.com/73466 | 152 // http://crbug.com/73466 |
107 // MessageLoop::current()->SetNestableTasksAllowed(false); | 153 // MessageLoop::current()->SetNestableTasksAllowed(false); |
108 | 154 |
109 // We can't use OnFailure() here since owner_ may not support notifications. | 155 // We can't use OnFailure() here since owner_ may not support notifications. |
110 | 156 |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 printing::PrintJobWorker* obj) { | 362 printing::PrintJobWorker* obj) { |
317 DCHECK(!owner_.get()); | 363 DCHECK(!owner_.get()); |
318 owner_ = obj->owner_; | 364 owner_ = obj->owner_; |
319 } | 365 } |
320 | 366 |
321 void RunnableMethodTraits<printing::PrintJobWorker>::ReleaseCallee( | 367 void RunnableMethodTraits<printing::PrintJobWorker>::ReleaseCallee( |
322 printing::PrintJobWorker* obj) { | 368 printing::PrintJobWorker* obj) { |
323 DCHECK_EQ(owner_, obj->owner_); | 369 DCHECK_EQ(owner_, obj->owner_); |
324 owner_ = NULL; | 370 owner_ = NULL; |
325 } | 371 } |
OLD | NEW |