| 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" |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 base::Bind(&PrintJobWorker::UseDefaultSettings, | 113 base::Bind(&PrintJobWorker::UseDefaultSettings, |
| 114 base::Unretained(this)))); | 114 base::Unretained(this)))); |
| 115 } | 115 } |
| 116 } | 116 } |
| 117 | 117 |
| 118 void PrintJobWorker::SetSettings( | 118 void PrintJobWorker::SetSettings( |
| 119 const base::DictionaryValue* const new_settings) { | 119 const base::DictionaryValue* const new_settings) { |
| 120 DCHECK_EQ(message_loop(), base::MessageLoop::current()); | 120 DCHECK_EQ(message_loop(), base::MessageLoop::current()); |
| 121 | 121 |
| 122 BrowserThread::PostTask( | 122 BrowserThread::PostTask( |
| 123 BrowserThread::UI, FROM_HERE, | 123 BrowserThread::UI, |
| 124 base::Bind(&HoldRefCallback, make_scoped_refptr(owner_), | 124 FROM_HERE, |
| 125 base::Bind(&HoldRefCallback, |
| 126 make_scoped_refptr(owner_), |
| 125 base::Bind(&PrintJobWorker::UpdatePrintSettings, | 127 base::Bind(&PrintJobWorker::UpdatePrintSettings, |
| 126 base::Unretained(this), new_settings))); | 128 base::Unretained(this), |
| 129 base::Owned(new_settings)))); |
| 127 } | 130 } |
| 128 | 131 |
| 129 void PrintJobWorker::UpdatePrintSettings( | 132 void PrintJobWorker::UpdatePrintSettings( |
| 130 const base::DictionaryValue* const new_settings) { | 133 const base::DictionaryValue* const new_settings) { |
| 131 // Create new PageRanges based on |new_settings|. | |
| 132 PageRanges new_ranges; | |
| 133 const base::ListValue* page_range_array; | |
| 134 if (new_settings->GetList(kSettingPageRange, &page_range_array)) { | |
| 135 for (size_t index = 0; index < page_range_array->GetSize(); ++index) { | |
| 136 const base::DictionaryValue* dict; | |
| 137 if (!page_range_array->GetDictionary(index, &dict)) | |
| 138 continue; | |
| 139 | |
| 140 PageRange range; | |
| 141 if (!dict->GetInteger(kSettingPageRangeFrom, &range.from) || | |
| 142 !dict->GetInteger(kSettingPageRangeTo, &range.to)) { | |
| 143 continue; | |
| 144 } | |
| 145 | |
| 146 // Page numbers are 1-based in the dictionary. | |
| 147 // Page numbers are 0-based for the printing context. | |
| 148 range.from--; | |
| 149 range.to--; | |
| 150 new_ranges.push_back(range); | |
| 151 } | |
| 152 } | |
| 153 PrintingContext::Result result = | 134 PrintingContext::Result result = |
| 154 printing_context_->UpdatePrintSettings(*new_settings, new_ranges); | 135 printing_context_->UpdatePrintSettings(*new_settings); |
| 155 delete new_settings; | |
| 156 GetSettingsDone(result); | 136 GetSettingsDone(result); |
| 157 } | 137 } |
| 158 | 138 |
| 159 void PrintJobWorker::GetSettingsDone(PrintingContext::Result result) { | 139 void PrintJobWorker::GetSettingsDone(PrintingContext::Result result) { |
| 160 // Most PrintingContext functions may start a message loop and process | 140 // Most PrintingContext functions may start a message loop and process |
| 161 // message recursively, so disable recursive task processing. | 141 // message recursively, so disable recursive task processing. |
| 162 // TODO(thestig): See above comment. SetNestableTasksAllowed(false) needs to | 142 // TODO(thestig): See above comment. SetNestableTasksAllowed(false) needs to |
| 163 // be called on the same thread as the previous call. See | 143 // be called on the same thread as the previous call. See |
| 164 // http://crbug.com/73466 | 144 // http://crbug.com/73466 |
| 165 // MessageLoop::current()->SetNestableTasksAllowed(false); | 145 // MessageLoop::current()->SetNestableTasksAllowed(false); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 } | 245 } |
| 266 // We have enough information to initialize page_number_. | 246 // We have enough information to initialize page_number_. |
| 267 page_number_.Init(document_->settings(), page_count); | 247 page_number_.Init(document_->settings(), page_count); |
| 268 if (destination_.get() != NULL) | 248 if (destination_.get() != NULL) |
| 269 destination_->SetPageCount(page_count); | 249 destination_->SetPageCount(page_count); |
| 270 } | 250 } |
| 271 DCHECK_NE(page_number_, PageNumber::npos()); | 251 DCHECK_NE(page_number_, PageNumber::npos()); |
| 272 | 252 |
| 273 while (true) { | 253 while (true) { |
| 274 // Is the page available? | 254 // Is the page available? |
| 275 scoped_refptr<PrintedPage> page; | 255 scoped_refptr<PrintedPage> page = document_->GetPage(page_number_.ToInt()); |
| 276 if (!document_->GetPage(page_number_.ToInt(), &page)) { | 256 if (!page) { |
| 277 // We need to wait for the page to be available. | 257 // We need to wait for the page to be available. |
| 278 base::MessageLoop::current()->PostDelayedTask( | 258 base::MessageLoop::current()->PostDelayedTask( |
| 279 FROM_HERE, | 259 FROM_HERE, |
| 280 base::Bind(&PrintJobWorker::OnNewPage, weak_factory_.GetWeakPtr()), | 260 base::Bind(&PrintJobWorker::OnNewPage, weak_factory_.GetWeakPtr()), |
| 281 base::TimeDelta::FromMilliseconds(500)); | 261 base::TimeDelta::FromMilliseconds(500)); |
| 282 break; | 262 break; |
| 283 } | 263 } |
| 284 // The page is there, print it. | 264 // The page is there, print it. |
| 285 SpoolPage(page.get()); | 265 SpoolPage(page.get()); |
| 286 ++page_number_; | 266 ++page_number_; |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 JobEventDetails::FAILED, document_, | 358 JobEventDetails::FAILED, document_, |
| 379 scoped_refptr<PrintedPage>())); | 359 scoped_refptr<PrintedPage>())); |
| 380 Cancel(); | 360 Cancel(); |
| 381 | 361 |
| 382 // Makes sure the variables are reinitialized. | 362 // Makes sure the variables are reinitialized. |
| 383 document_ = NULL; | 363 document_ = NULL; |
| 384 page_number_ = PageNumber::npos(); | 364 page_number_ = PageNumber::npos(); |
| 385 } | 365 } |
| 386 | 366 |
| 387 } // namespace printing | 367 } // namespace printing |
| OLD | NEW |