Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2011 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 CHROME_BROWSER_EXTENSIONS_EXTERNAL_EXTENSION_LOADER_H_
      6 #define CHROME_BROWSER_EXTENSIONS_EXTERNAL_EXTENSION_LOADER_H_
      7 #pragma once
      8 
      9 #include "base/file_path.h"
     10 #include "base/memory/ref_counted.h"
     11 #include "base/memory/scoped_ptr.h"
     12 
     13 class DictionaryValue;
     14 class ExternalExtensionProviderImpl;
     15 
     16 // Base class for gathering a list of external extensions. Subclasses
     17 // implement loading from registry, JSON file, policy.
     18 // Instances are owned by ExternalExtensionProviderImpl objects.
     19 // Instances are created on the UI thread and expect public method calls from
     20 // the UI thread. Some subclasses introduce new methods that are executed on the
     21 // FILE thread.
     22 // The sequence of loading the extension list:
     23 // 1.) StartLoading() - checks if a loading task is already running
     24 // 2.) Load() - implemented in subclasses
     25 // 3.) FinishLoading()
     26 // 4.) owner_->SetPrefs()
     27 class ExternalExtensionLoader
     28     : public base::RefCountedThreadSafe<ExternalExtensionLoader> {
     29  public:
     30   explicit ExternalExtensionLoader();
     31 
     32   // Specifies the provider that owns this object.
     33   void Init(ExternalExtensionProviderImpl* owner);
     34 
     35   // Called by the owner before it gets deleted.
     36   void OwnerShutdown();
     37 
     38   // Initiates the possibly asynchronous loading of extension list.
     39   // It is the responsibility of the caller to ensure that calls to
     40   // this method do not overlap with each other.
     41   // Implementations of this method should save the loaded results
     42   // in prefs_ and then call LoadFinished.
     43   virtual void StartLoading() = 0;
     44 
     45   // Some external providers allow relative file paths to local CRX files.
     46   // Subclasses that want this behavior should override this method to
     47   // return the absolute path from which relative paths should be resolved.
     48   // By default, return an empty path, which indicates that relative paths
     49   // are not allowed.
     50   virtual const FilePath GetBaseCrxFilePath();
     51 
     52  protected:
     53   virtual ~ExternalExtensionLoader();
     54 
     55   // Notifies the provider that the list of extensions has been loaded.
     56   void LoadFinished();
     57 
     58   // Used for passing the list of extensions from the method that loads them
     59   // to |LoadFinished|. To ensure thread safety, the rules are the following:
     60   // if this value is written on another thread than the UI, then it should
     61   // only be written in a task that was posted from |StartLoading|. After that,
     62   // this task should invoke |LoadFinished| with a PostTask. This scheme of
     63   // posting tasks will avoid concurrent access and imply the necessary memory
     64   // barriers.
     65   scoped_ptr<DictionaryValue> prefs_;
     66 
     67  private:
     68   friend class base::RefCountedThreadSafe<ExternalExtensionLoader>;
     69 
     70   ExternalExtensionProviderImpl* owner_;  // weak
     71 
     72   // Set to true if loading the extensions is already running. New requests
     73   // are ignored while this is set true.
     74   bool running_;
     75 
     76   DISALLOW_COPY_AND_ASSIGN(ExternalExtensionLoader);
     77 };
     78 
     79 #endif  // CHROME_BROWSER_EXTENSIONS_EXTERNAL_EXTENSION_LOADER_H_
     80