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

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

Issue 2343983004: Add PPDProvider barebones implementation and associated cache skeleton. (Closed)
Patch Set: Initial PPDProvider/PPDCache implementation. Also, add associated unittests. This doesn't plumb th… Created 4 years, 1 month 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
« no previous file with comments | « chromeos/printing/ppd_cache.h ('k') | chromeos/printing/ppd_cache_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chromeos/printing/ppd_cache.h"
6
7 #include <vector>
8
9 #include "base/files/file_util.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_util.h"
13 #include "crypto/sha2.h"
14 #include "net/base/io_buffer.h"
15 #include "net/filter/filter.h"
16 #include "net/filter/gzip_header.h"
17
18 using base::FilePath;
19 using std::string;
20
21 namespace chromeos {
22 namespace printing {
23 namespace {
24
25 // Return true if it looks like contents is already gzipped, false otherwise.
26 bool IsGZipped(const string& contents) {
27 const char* ignored;
28 net::GZipHeader header;
29 return header.ReadMore(contents.data(), contents.size(), &ignored) ==
30 net::GZipHeader::COMPLETE_HEADER;
31 }
32
33 class PpdCacheImpl : public PpdCache {
34 public:
35 explicit PpdCacheImpl(const FilePath& cache_base_dir)
36 : cache_base_dir_(cache_base_dir) {}
37 ~PpdCacheImpl() override {}
38
39 // Public API functions.
40 base::Optional<FilePath> Find(
41 const Printer::PpdReference& reference) const override {
42 base::Optional<FilePath> ret;
43
44 // We can't know here if we have a gzipped or un-gzipped version, so just
45 // look for both.
46 FilePath contents_path_base = GetCachePathBase(reference);
47 for (const string& extension : {".ppd", ".ppd.gz"}) {
48 FilePath contents_path = contents_path_base.AddExtension(extension);
49 if (base::PathExists(contents_path)) {
50 ret = contents_path;
51 break;
52 }
53 }
54 return ret;
55 }
56
57 base::Optional<FilePath> Store(const Printer::PpdReference& reference,
58 const string& ppd_contents) override {
59 base::Optional<FilePath> ret;
60 FilePath contents_path = GetCachePathBase(reference).AddExtension(".ppd");
61 if (IsGZipped(ppd_contents)) {
62 contents_path = contents_path.AddExtension(".gz");
63 }
64 if (base::WriteFile(contents_path, ppd_contents.data(),
65 ppd_contents.size()) ==
66 static_cast<int>(ppd_contents.size())) {
67 ret = contents_path;
68 } else {
69 LOG(ERROR) << "Failed to write " << contents_path.LossyDisplayName();
70 // Try to clean up the file, as it may have partial contents. Note that
71 // DeleteFile(nonexistant file) should return true, so failure here means
72 // something is exceptionally hosed.
73 if (!base::DeleteFile(contents_path, false)) {
74 LOG(ERROR) << "Failed to cleanup partially-written file "
75 << contents_path.LossyDisplayName();
76 return ret;
77 }
78 }
79 return ret;
80 }
81
82 private:
83 // Get the file path at which we expect to find a PPD if it's cached.
84 //
85 // This is, ultimately, just a hash function. It's extremely infrequently
86 // used (called once when trying to look up information on a printer or store
87 // a PPD), and should be stable, as changing the function will make previously
88 // cached entries unfindable, causing resolve logic to be reinvoked
89 // unnecessarily.
90 //
91 // There's also a faint possibility that a bad actor might try to do something
92 // nefarious by intentionally causing a cache collision that makes the wrong
93 // PPD be used for a printer. There's no obvious attack vector, but
94 // there's also no real cost to being paranoid here, so we use SHA-256 as the
95 // underlying hash function, and inject fixed field prefixes to prevent
96 // field-substitution spoofing. This also buys us hash function stability at
97 // the same time.
98 //
99 // Also, care should be taken to preserve the existing hash values if new
100 // fields are added to PpdReference -- that is, if a new field F is added
101 // to PpdReference, a PpdReference with a default F value should hash to
102 // the same thing as a PpdReference that predates the addition of F to the
103 // structure.
104 //
105 // Note this function expects that the caller will append ".ppd", or ".ppd.gz"
106 // to the output as needed.
107 FilePath GetCachePathBase(const Printer::PpdReference& ref) const {
108 std::vector<string> pieces;
109 if (!ref.user_supplied_ppd_url.empty()) {
110 pieces.push_back("user_supplied_ppd_url:");
111 pieces.push_back(ref.user_supplied_ppd_url);
112 } else if (!ref.effective_manufacturer.empty() &&
113 !ref.effective_model.empty()) {
114 pieces.push_back("manufacturer:");
115 pieces.push_back(ref.effective_manufacturer);
116 pieces.push_back("model:");
117 pieces.push_back(ref.effective_model);
118 } else {
119 NOTREACHED() << "PpdCache hashing empty PpdReference";
120 }
121 // The separator here is not needed, but makes debug output more readable.
122 string full_key = base::JoinString(pieces, "|");
123 string hashed_key = crypto::SHA256HashString(full_key);
124 string ascii_hash = base::HexEncode(hashed_key.data(), hashed_key.size());
125 VLOG(3) << "PPD Cache key is " << full_key << " which hashes to "
126 << ascii_hash;
127
128 return cache_base_dir_.Append(ascii_hash);
129 }
130
131 const FilePath cache_base_dir_;
132
133 DISALLOW_COPY_AND_ASSIGN(PpdCacheImpl);
134 };
135
136 } // namespace
137
138 // static
139 std::unique_ptr<PpdCache> PpdCache::Create(const FilePath& cache_base_dir) {
140 return base::MakeUnique<PpdCacheImpl>(cache_base_dir);
141 }
142
143 } // namespace printing
144 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/printing/ppd_cache.h ('k') | chromeos/printing/ppd_cache_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698