Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / extensions / browser / content_verify_job.h
1 // Copyright 2014 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 #ifndef EXTENSIONS_BROWSER_CONTENT_VERIFY_JOB_H_
6 #define EXTENSIONS_BROWSER_CONTENT_VERIFY_JOB_H_
7
8 #include <string>
9
10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/threading/thread_checker.h"
14
15 namespace base {
16 class FilePath;
17 }
18
19 namespace crypto {
20 class SecureHash;
21 }
22
23 namespace extensions {
24
25 class ContentHashReader;
26
27 // Objects of this class are responsible for verifying that the actual content
28 // read from an extension file matches an expected set of hashes. This class
29 // can be created on any thread but the rest of the methods should be called
30 // from only one thread.
31 class ContentVerifyJob : public base::RefCountedThreadSafe<ContentVerifyJob> {
32  public:
33   enum FailureReason {
34     // No failure.
35     NONE,
36
37     // Failed because there were no expected hashes at all (eg they haven't
38     // been fetched yet).
39     MISSING_ALL_HASHES,
40
41     // Failed because this file wasn't found in the list of expected hashes.
42     NO_HASHES_FOR_FILE,
43
44     // Some of the content read did not match the expected hash.
45     HASH_MISMATCH
46   };
47   typedef base::Callback<void(FailureReason)> FailureCallback;
48
49   // The |failure_callback| will be called at most once if there was a failure.
50   ContentVerifyJob(ContentHashReader* hash_reader,
51                    const FailureCallback& failure_callback);
52
53   // This begins the process of getting expected hashes, so it should be called
54   // as early as possible.
55   void Start();
56
57   // Call this to add more bytes to verify. If at any point the read bytes
58   // don't match the expected hashes, this will dispatch the failure
59   // callback. The failure callback will only be run once even if more bytes
60   // are read. Make sure to call DoneReading so that any final bytes that were
61   // read that didn't align exactly on a block size boundary get their hash
62   // checked as well.
63   void BytesRead(int count, const char* data);
64
65   // Call once when finished adding bytes via BytesRead.
66   void DoneReading();
67
68   class TestDelegate {
69    public:
70     // These methods will be called inside BytesRead/DoneReading respectively.
71     // If either return something other than NONE, then the failure callback
72     // will be dispatched with that reason.
73     virtual FailureReason BytesRead(const std::string& extension_id,
74                                     int count,
75                                     const char* data) = 0;
76     virtual FailureReason DoneReading(const std::string& extension_id) = 0;
77   };
78
79   static void SetDelegateForTests(TestDelegate* delegate);
80
81  private:
82   DISALLOW_COPY_AND_ASSIGN(ContentVerifyJob);
83
84   virtual ~ContentVerifyJob();
85   friend class base::RefCountedThreadSafe<ContentVerifyJob>;
86
87   // Called each time we're done adding bytes for the current block, and are
88   // ready to finish the hash operation for those bytes and make sure it
89   // matches what was expected for that block. Returns true if everything is
90   // still ok so far, or false if a mismatch was detected.
91   bool FinishBlock();
92
93   // Dispatches the failure callback with the given reason.
94   void DispatchFailureCallback(FailureReason reason);
95
96   // Called when our ContentHashReader has finished initializing.
97   void OnHashesReady(bool success);
98
99   // Indicates whether the caller has told us they are done calling BytesRead.
100   bool done_reading_;
101
102   // Set to true once hash_reader_ has read its expected hashes.
103   bool hashes_ready_;
104
105   // While we're waiting for the callback from the ContentHashReader, we need
106   // to queue up bytes any bytes that are read.
107   std::string queue_;
108
109   // The total bytes we've read.
110   int64 total_bytes_read_;
111
112   // The index of the block we're currently on.
113   int current_block_;
114
115   // The hash we're building up for the bytes of |current_block_|.
116   scoped_ptr<crypto::SecureHash> current_hash_;
117
118   // The number of bytes we've already input into |current_hash_|.
119   int current_hash_byte_count_;
120
121   scoped_refptr<ContentHashReader> hash_reader_;
122
123   base::TimeDelta time_spent_;
124
125   // Called once if verification fails.
126   FailureCallback failure_callback_;
127
128   // Set to true if we detected a mismatch and called the failure callback.
129   bool failed_;
130
131   // For ensuring methods on called on the right thread.
132   base::ThreadChecker thread_checker_;
133 };
134
135 }  // namespace extensions
136
137 #endif  // EXTENSIONS_BROWSER_CONTENT_VERIFY_JOB_H_