Home | History | Annotate | Download | only in browser
      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_VERIFIER_H_
      6 #define EXTENSIONS_BROWSER_CONTENT_VERIFIER_H_
      7 
      8 #include <set>
      9 #include <string>
     10 
     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"
     19 
     20 namespace base {
     21 class FilePath;
     22 }
     23 
     24 namespace content {
     25 class BrowserContext;
     26 }
     27 
     28 namespace extensions {
     29 
     30 class Extension;
     31 class ContentHashFetcher;
     32 class ContentVerifierIOData;
     33 
     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   // Takes ownership of |delegate|.
     41   ContentVerifier(content::BrowserContext* context,
     42                   ContentVerifierDelegate* delegate);
     43   void Start();
     44   void Shutdown();
     45 
     46   // Call this before reading a file within an extension. The caller owns the
     47   // returned job.
     48   ContentVerifyJob* CreateJobFor(const std::string& extension_id,
     49                                  const base::FilePath& extension_root,
     50                                  const base::FilePath& relative_path);
     51 
     52   // Called (typically by a verification job) to indicate that verification
     53   // failed while reading some file in |extension_id|.
     54   void VerifyFailed(const std::string& extension_id,
     55                     ContentVerifyJob::FailureReason reason);
     56 
     57   // ExtensionRegistryObserver interface
     58   virtual void OnExtensionLoaded(content::BrowserContext* browser_context,
     59                                  const Extension* extension) OVERRIDE;
     60   virtual void OnExtensionUnloaded(
     61       content::BrowserContext* browser_context,
     62       const Extension* extension,
     63       UnloadedExtensionInfo::Reason reason) OVERRIDE;
     64 
     65  private:
     66   DISALLOW_COPY_AND_ASSIGN(ContentVerifier);
     67 
     68   friend class base::RefCountedThreadSafe<ContentVerifier>;
     69   virtual ~ContentVerifier();
     70 
     71   void OnFetchComplete(const std::string& extension_id,
     72                        bool success,
     73                        bool was_force_check,
     74                        const std::set<base::FilePath>& hash_mismatch_paths);
     75 
     76   void OnFetchCompleteHelper(const std::string& extension_id,
     77                              bool shouldVerifyAnyPathsResult);
     78 
     79   // Returns true if any of the paths in |relative_paths| *should* have their
     80   // contents verified. (Some files get transcoded during the install process,
     81   // so we don't want to verify their contents because they are expected not
     82   // to match).
     83   bool ShouldVerifyAnyPaths(const std::string& extension_id,
     84                             const base::FilePath& extension_root,
     85                             const std::set<base::FilePath>& relative_paths);
     86 
     87   // Set to true once we've begun shutting down.
     88   bool shutdown_;
     89 
     90   content::BrowserContext* context_;
     91 
     92   scoped_ptr<ContentVerifierDelegate> delegate_;
     93 
     94   // For fetching content hash signatures.
     95   scoped_ptr<ContentHashFetcher> fetcher_;
     96 
     97   // For observing the ExtensionRegistry.
     98   ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> observer_;
     99 
    100   // Data that should only be used on the IO thread.
    101   scoped_refptr<ContentVerifierIOData> io_data_;
    102 };
    103 
    104 }  // namespace extensions
    105 
    106 #endif  // EXTENSIONS_BROWSER_CONTENT_VERIFIER_H_
    107