| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "chromeos/printing/ppd_provider.h" | 5 #include "chromeos/printing/ppd_provider.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/json/json_parser.h" | 10 #include "base/json/json_parser.h" |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 void Resolve(const Printer::PpdReference& ppd_reference, | 78 void Resolve(const Printer::PpdReference& ppd_reference, |
| 79 const PpdProvider::ResolveCallback& cb) override { | 79 const PpdProvider::ResolveCallback& cb) override { |
| 80 CHECK(!cb.is_null()); | 80 CHECK(!cb.is_null()); |
| 81 CHECK(resolve_sequence_checker_.CalledOnValidSequence()); | 81 CHECK(resolve_sequence_checker_.CalledOnValidSequence()); |
| 82 CHECK(base::SequencedTaskRunnerHandle::IsSet()) | 82 CHECK(base::SequencedTaskRunnerHandle::IsSet()) |
| 83 << "Resolve must be called from a SequencedTaskRunner context"; | 83 << "Resolve must be called from a SequencedTaskRunner context"; |
| 84 CHECK(!resolve_inflight_) | 84 CHECK(!resolve_inflight_) |
| 85 << "Can't have concurrent PpdProvider Resolve calls"; | 85 << "Can't have concurrent PpdProvider Resolve calls"; |
| 86 resolve_inflight_ = true; | 86 resolve_inflight_ = true; |
| 87 auto cache_result = base::MakeUnique<base::Optional<base::FilePath>>(); | 87 auto cache_result = base::MakeUnique<base::Optional<base::FilePath>>(); |
| 88 auto* raw_cache_result_ptr = cache_result.get(); |
| 88 bool post_result = io_task_runner_->PostTaskAndReply( | 89 bool post_result = io_task_runner_->PostTaskAndReply( |
| 89 FROM_HERE, base::Bind(&PpdProviderImpl::ResolveDoCacheLookup, | 90 FROM_HERE, base::Bind(&PpdProviderImpl::ResolveDoCacheLookup, |
| 90 weak_factory_.GetWeakPtr(), ppd_reference, | 91 weak_factory_.GetWeakPtr(), ppd_reference, |
| 91 cache_result.get()), | 92 raw_cache_result_ptr), |
| 92 base::Bind(&PpdProviderImpl::ResolveCacheLookupDone, | 93 base::Bind(&PpdProviderImpl::ResolveCacheLookupDone, |
| 93 weak_factory_.GetWeakPtr(), ppd_reference, cb, | 94 weak_factory_.GetWeakPtr(), ppd_reference, cb, |
| 94 std::move(cache_result))); | 95 std::move(cache_result))); |
| 95 DCHECK(post_result); | 96 DCHECK(post_result); |
| 96 } | 97 } |
| 97 | 98 |
| 98 void QueryAvailable(const QueryAvailableCallback& cb) override { | 99 void QueryAvailable(const QueryAvailableCallback& cb) override { |
| 99 CHECK(!cb.is_null()); | 100 CHECK(!cb.is_null()); |
| 100 CHECK(base::SequencedTaskRunnerHandle::IsSet()) | 101 CHECK(base::SequencedTaskRunnerHandle::IsSet()) |
| 101 << "QueryAvailable() must be called from a SequencedTaskRunner context"; | 102 << "QueryAvailable() must be called from a SequencedTaskRunner context"; |
| 102 auto cache_result = base::MakeUnique< | 103 auto cache_result = base::MakeUnique< |
| 103 base::Optional<const PpdProvider::AvailablePrintersMap*>>(); | 104 base::Optional<const PpdProvider::AvailablePrintersMap*>>(); |
| 104 CHECK(!query_inflight_) | 105 CHECK(!query_inflight_) |
| 105 << "Can't have concurrent PpdProvider QueryAvailable calls"; | 106 << "Can't have concurrent PpdProvider QueryAvailable calls"; |
| 106 query_inflight_ = true; | 107 query_inflight_ = true; |
| 108 auto* raw_cache_result_ptr = cache_result.get(); |
| 107 CHECK(io_task_runner_->PostTaskAndReply( | 109 CHECK(io_task_runner_->PostTaskAndReply( |
| 108 FROM_HERE, base::Bind(&PpdProviderImpl::QueryAvailableDoCacheLookup, | 110 FROM_HERE, base::Bind(&PpdProviderImpl::QueryAvailableDoCacheLookup, |
| 109 weak_factory_.GetWeakPtr(), cache_result.get()), | 111 weak_factory_.GetWeakPtr(), raw_cache_result_ptr), |
| 110 base::Bind(&PpdProviderImpl::QueryAvailableCacheLookupDone, | 112 base::Bind(&PpdProviderImpl::QueryAvailableCacheLookupDone, |
| 111 weak_factory_.GetWeakPtr(), cb, std::move(cache_result)))); | 113 weak_factory_.GetWeakPtr(), cb, std::move(cache_result)))); |
| 112 } | 114 } |
| 113 | 115 |
| 114 bool CachePpd(const Printer::PpdReference& ppd_reference, | 116 bool CachePpd(const Printer::PpdReference& ppd_reference, |
| 115 const base::FilePath& ppd_path) override { | 117 const base::FilePath& ppd_path) override { |
| 116 std::string buf; | 118 std::string buf; |
| 117 if (!base::ReadFileToStringWithMaxSize(ppd_path, &buf, | 119 if (!base::ReadFileToStringWithMaxSize(ppd_path, &buf, |
| 118 options_.max_ppd_contents_size_)) { | 120 options_.max_ppd_contents_size_)) { |
| 119 return false; | 121 return false; |
| 120 } | 122 } |
| 121 return static_cast<bool>(cache_->Store(ppd_reference, buf)); | 123 return static_cast<bool>(cache_->Store(ppd_reference, buf)); |
| 122 } | 124 } |
| 123 | 125 |
| 124 private: | 126 private: |
| 125 // Trivial wrappers around PpdCache::Find() and | 127 // Trivial wrappers around PpdCache::Find() and |
| 126 // PpdCache::FindAvailablePrinters(). We need these wrappers both because we | 128 // PpdCache::FindAvailablePrinters(). We need these wrappers both because we |
| 127 // use weak_ptrs to manage lifetime, and so we need to bind callbacks to | 129 // use weak_ptrs to manage lifetime, and so we need to bind callbacks to |
| 128 // *this*, and because weak_ptr's preclude return values in posted tasks, so | 130 // *this*, and because weak_ptr's preclude return values in posted tasks, so |
| 129 // we have to use a parameter to return state. | 131 // we have to use a parameter to return state. |
| 130 void ResolveDoCacheLookup( | 132 void ResolveDoCacheLookup( |
| 131 const Printer::PpdReference& reference, | 133 const Printer::PpdReference& reference, |
| 132 base::Optional<base::FilePath>* cache_result) const { | 134 base::Optional<base::FilePath>* cache_result) const { |
| 133 *cache_result = cache_->Find(reference); | 135 *cache_result = cache_->Find(reference); |
| 134 } | 136 } |
| 135 | 137 |
| 136 void QueryAvailableDoCacheLookup( | 138 void QueryAvailableDoCacheLookup( |
| 137 base::Optional<const PpdProvider::AvailablePrintersMap*>* cache_result) | 139 base::Optional<const PpdProvider::AvailablePrintersMap*>* cache_result) |
| 138 const { | 140 const { |
| 141 DCHECK(cache_result); |
| 139 auto tmp = cache_->FindAvailablePrinters(); | 142 auto tmp = cache_->FindAvailablePrinters(); |
| 140 if (tmp != nullptr) { | 143 if (tmp != nullptr) { |
| 141 *cache_result = tmp; | 144 *cache_result = tmp; |
| 142 } else { | 145 } else { |
| 143 *cache_result = base::nullopt; | 146 *cache_result = base::nullopt; |
| 144 } | 147 } |
| 145 } | 148 } |
| 146 | 149 |
| 147 // Callback that happens when the Resolve() cache lookup completes. If the | 150 // Callback that happens when the Resolve() cache lookup completes. If the |
| 148 // cache satisfied the request, finish the Resolve, otherwise start a URL | 151 // cache satisfied the request, finish the Resolve, otherwise start a URL |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 scoped_refptr<net::URLRequestContextGetter> url_context_getter, | 419 scoped_refptr<net::URLRequestContextGetter> url_context_getter, |
| 417 scoped_refptr<base::SequencedTaskRunner> io_task_runner, | 420 scoped_refptr<base::SequencedTaskRunner> io_task_runner, |
| 418 std::unique_ptr<PpdCache> cache, | 421 std::unique_ptr<PpdCache> cache, |
| 419 const PpdProvider::Options& options) { | 422 const PpdProvider::Options& options) { |
| 420 return base::MakeUnique<PpdProviderImpl>( | 423 return base::MakeUnique<PpdProviderImpl>( |
| 421 api_key, url_context_getter, io_task_runner, std::move(cache), options); | 424 api_key, url_context_getter, io_task_runner, std::move(cache), options); |
| 422 } | 425 } |
| 423 | 426 |
| 424 } // namespace printing | 427 } // namespace printing |
| 425 } // namespace chromeos | 428 } // namespace chromeos |
| OLD | NEW |