Roll src/third_party/WebKit a3b4a2e:7441784 (svn 202551:202552)
[chromium-blink-merge.git] / extensions / browser / content_verifier.h
blob8d6f47b5c64b6c635f647911edbbdd1f63c1cce7
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.
5 #ifndef EXTENSIONS_BROWSER_CONTENT_VERIFIER_H_
6 #define EXTENSIONS_BROWSER_CONTENT_VERIFIER_H_
8 #include <set>
9 #include <string>
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/scoped_observer.h"
15 #include "base/version.h"
16 #include "extensions/browser/content_verifier_delegate.h"
17 #include "extensions/browser/content_verify_job.h"
18 #include "extensions/browser/extension_registry_observer.h"
20 namespace base {
21 class FilePath;
24 namespace content {
25 class BrowserContext;
28 namespace extensions {
30 class Extension;
31 class ContentHashFetcher;
32 class ContentVerifierIOData;
34 // Used for managing overall content verification - both fetching content
35 // hashes as needed, and supplying job objects to verify file contents as they
36 // are read.
37 class ContentVerifier : public base::RefCountedThreadSafe<ContentVerifier>,
38 public ExtensionRegistryObserver {
39 public:
40 class TestObserver {
41 public:
42 virtual void OnFetchComplete(const std::string& extension_id,
43 bool success) = 0;
45 static void SetObserverForTests(TestObserver* observer);
47 // Takes ownership of |delegate|.
48 ContentVerifier(content::BrowserContext* context,
49 ContentVerifierDelegate* delegate);
50 void Start();
51 void Shutdown();
53 // Call this before reading a file within an extension. The caller owns the
54 // returned job.
55 ContentVerifyJob* CreateJobFor(const std::string& extension_id,
56 const base::FilePath& extension_root,
57 const base::FilePath& relative_path);
59 // Called (typically by a verification job) to indicate that verification
60 // failed while reading some file in |extension_id|.
61 void VerifyFailed(const std::string& extension_id,
62 ContentVerifyJob::FailureReason reason);
64 // ExtensionRegistryObserver interface
65 void OnExtensionLoaded(content::BrowserContext* browser_context,
66 const Extension* extension) override;
67 void OnExtensionUnloaded(content::BrowserContext* browser_context,
68 const Extension* extension,
69 UnloadedExtensionInfo::Reason reason) override;
71 private:
72 DISALLOW_COPY_AND_ASSIGN(ContentVerifier);
74 friend class base::RefCountedThreadSafe<ContentVerifier>;
75 ~ContentVerifier() override;
77 void OnFetchComplete(const std::string& extension_id,
78 bool success,
79 bool was_force_check,
80 const std::set<base::FilePath>& hash_mismatch_paths);
82 void OnFetchCompleteHelper(const std::string& extension_id,
83 bool shouldVerifyAnyPathsResult);
85 // Returns true if any of the paths in |relative_paths| *should* have their
86 // contents verified. (Some files get transcoded during the install process,
87 // so we don't want to verify their contents because they are expected not
88 // to match).
89 bool ShouldVerifyAnyPaths(const std::string& extension_id,
90 const base::FilePath& extension_root,
91 const std::set<base::FilePath>& relative_paths);
93 // Set to true once we've begun shutting down.
94 bool shutdown_;
96 content::BrowserContext* context_;
98 scoped_ptr<ContentVerifierDelegate> delegate_;
100 // For fetching content hash signatures.
101 scoped_ptr<ContentHashFetcher> fetcher_;
103 // For observing the ExtensionRegistry.
104 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> observer_;
106 // Data that should only be used on the IO thread.
107 scoped_refptr<ContentVerifierIOData> io_data_;
110 } // namespace extensions
112 #endif // EXTENSIONS_BROWSER_CONTENT_VERIFIER_H_