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

Side by Side Diff: chromeos/printing/ppd_cache.cc

Issue 2939373003: Convert PpdCache and PpdProvider to TaskScheduler. (Closed)
Patch Set: remove extra imports Created 3 years, 6 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
OLDNEW
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_cache.h" 5 #include "chromeos/printing/ppd_cache.h"
6 6
7 #include <utility> 7 #include <utility>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
12 #include "base/json/json_parser.h" 12 #include "base/json/json_parser.h"
13 #include "base/json/json_writer.h" 13 #include "base/json/json_writer.h"
14 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
15 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/string_util.h" 16 #include "base/strings/string_util.h"
17 #include "base/synchronization/lock.h" 17 #include "base/synchronization/lock.h"
18 #include "base/task_scheduler/post_task.h"
18 #include "base/threading/sequenced_task_runner_handle.h" 19 #include "base/threading/sequenced_task_runner_handle.h"
19 #include "base/threading/thread_restrictions.h" 20 #include "base/threading/thread_restrictions.h"
20 #include "base/time/time.h" 21 #include "base/time/time.h"
21 #include "base/values.h" 22 #include "base/values.h"
22 #include "chromeos/printing/printing_constants.h" 23 #include "chromeos/printing/printing_constants.h"
23 #include "crypto/sha2.h" 24 #include "crypto/sha2.h"
24 #include "net/base/io_buffer.h" 25 #include "net/base/io_buffer.h"
25 #include "net/filter/gzip_header.h" 26 #include "net/filter/gzip_header.h"
26 27
27 namespace chromeos { 28 namespace chromeos {
28 namespace printing { 29 namespace printing {
29 namespace { 30 namespace {
30 31
31 class PpdCacheImpl : public PpdCache { 32 class PpdCacheImpl : public PpdCache {
32 public: 33 public:
33 PpdCacheImpl(const base::FilePath& cache_base_dir, 34 explicit PpdCacheImpl(const base::FilePath& cache_base_dir)
34 const scoped_refptr<base::SequencedTaskRunner>& disk_task_runner) 35 : cache_base_dir_(cache_base_dir),
35 : cache_base_dir_(cache_base_dir), disk_task_runner_(disk_task_runner) {} 36 fetch_task_runner_(base::CreateSequencedTaskRunnerWithTraits(
37 {base::TaskPriority::USER_VISIBLE,
38 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN})),
39 store_task_runner_(base::CreateSequencedTaskRunnerWithTraits(
40 {base::TaskPriority::BACKGROUND,
41 base::TaskShutdownBehavior::BLOCK_SHUTDOWN})) {}
36 42
37 // Public API functions. 43 // Public API functions.
38 void Find(const std::string& key, const FindCallback& cb) override { 44 void Find(const std::string& key, const FindCallback& cb) override {
39 // Ensure the cache lives until the op is over. 45 // Ensure the cache lives until the op is over.
40 AddRef(); 46 AddRef();
41 ++inflight_ops_; 47 ++inflight_ops_;
42 disk_task_runner_->PostTask( 48 fetch_task_runner_->PostTask(
43 FROM_HERE, base::Bind(&PpdCacheImpl::FindImpl, this, key, 49 FROM_HERE, base::Bind(&PpdCacheImpl::FindImpl, this, key,
44 base::SequencedTaskRunnerHandle::Get(), cb)); 50 base::SequencedTaskRunnerHandle::Get(), cb));
45 } 51 }
46 52
47 // Store the given contents at the given key. If cb is non-null, it will 53 // Store the given contents at the given key. If cb is non-null, it will
48 // be invoked on completion. 54 // be invoked on completion.
49 void Store(const std::string& key, 55 void Store(const std::string& key,
50 const std::string& contents, 56 const std::string& contents,
51 const base::Callback<void()>& cb) override { 57 const base::Callback<void()>& cb) override {
52 AddRef(); 58 AddRef();
53 ++inflight_ops_; 59 ++inflight_ops_;
54 disk_task_runner_->PostTask( 60 store_task_runner_->PostTask(
gab 2017/06/19 15:13:21 It's weird to run read/write on different sequence
skau 2017/06/19 23:31:08 I was able to convert the tasks to anonymous metho
55 FROM_HERE, base::Bind(&PpdCacheImpl::StoreImpl, this, key, contents, 61 FROM_HERE, base::Bind(&PpdCacheImpl::StoreImpl, this, key, contents,
56 base::SequencedTaskRunnerHandle::Get(), cb)); 62 base::SequencedTaskRunnerHandle::Get(), cb));
57 } 63 }
58 64
59 bool Idle() const override { return inflight_ops_ == 0; } 65 bool Idle() const override { return inflight_ops_ == 0; }
60 66
61 private: 67 private:
62 ~PpdCacheImpl() override {} 68 ~PpdCacheImpl() override {}
63 69
64 // If the cache doesn't already exist, create it. 70 // If the cache doesn't already exist, create it.
65 void MaybeCreateCache() { 71 void MaybeCreateCache() {
66 if (!base::PathExists(cache_base_dir_)) { 72 if (!base::PathExists(cache_base_dir_)) {
67 base::CreateDirectory(cache_base_dir_); 73 base::CreateDirectory(cache_base_dir_);
68 } 74 }
69 } 75 }
70 76
71 // Find implementation, runs on the i/o thread. 77 // Find implementation, runs on the i/o thread.
gab 2017/06/19 15:13:22 Update comment here and below
skau 2017/06/19 23:31:08 Done.
72 void FindImpl(const std::string& key, 78 void FindImpl(const std::string& key,
73 const scoped_refptr<base::SequencedTaskRunner>& callback_runner, 79 const scoped_refptr<base::SequencedTaskRunner>& callback_runner,
74 const FindCallback& cb) { 80 const FindCallback& cb) {
75 base::ThreadRestrictions::AssertIOAllowed(); 81 base::ThreadRestrictions::AssertIOAllowed();
76 FindResult result; 82 FindResult result;
77 result.success = false; 83 result.success = false;
78 MaybeCreateCache(); 84 MaybeCreateCache();
79 base::File file(FilePathForKey(key), 85 base::File file(FilePathForKey(key),
80 base::File::FLAG_OPEN | base::File::FLAG_READ); 86 base::File::FLAG_OPEN | base::File::FLAG_READ);
81 87
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 // Return the (full) path to the file we expect to find the given key at. 158 // Return the (full) path to the file we expect to find the given key at.
153 base::FilePath FilePathForKey(const std::string& key) { 159 base::FilePath FilePathForKey(const std::string& key) {
154 std::string hashed_key = crypto::SHA256HashString(key); 160 std::string hashed_key = crypto::SHA256HashString(key);
155 return cache_base_dir_.Append( 161 return cache_base_dir_.Append(
156 base::HexEncode(hashed_key.data(), hashed_key.size())); 162 base::HexEncode(hashed_key.data(), hashed_key.size()));
157 } 163 }
158 164
159 int inflight_ops_ = 0; 165 int inflight_ops_ = 0;
160 166
161 base::FilePath cache_base_dir_; 167 base::FilePath cache_base_dir_;
162 scoped_refptr<base::SequencedTaskRunner> disk_task_runner_; 168 scoped_refptr<base::SequencedTaskRunner> fetch_task_runner_;
169 scoped_refptr<base::SequencedTaskRunner> store_task_runner_;
163 170
164 DISALLOW_COPY_AND_ASSIGN(PpdCacheImpl); 171 DISALLOW_COPY_AND_ASSIGN(PpdCacheImpl);
165 }; 172 };
166 173
167 } // namespace 174 } // namespace
168 175
169 // static 176 // static
170 scoped_refptr<PpdCache> PpdCache::Create( 177 scoped_refptr<PpdCache> PpdCache::Create(const base::FilePath& cache_base_dir) {
171 const base::FilePath& cache_base_dir, 178 return scoped_refptr<PpdCache>(new PpdCacheImpl(cache_base_dir));
172 scoped_refptr<base::SequencedTaskRunner> disk_task_runner) {
173 return scoped_refptr<PpdCache>(
174 new PpdCacheImpl(cache_base_dir, disk_task_runner));
175 } 179 }
176 180
177 } // namespace printing 181 } // namespace printing
178 } // namespace chromeos 182 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698