172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved. 272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// Use of this source code is governed by a BSD-style license that can be 372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// found in the LICENSE file. 472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#ifndef CHROME_BROWSER_EXTENSIONS_EXTERNAL_EXTENSION_LOADER_H_ 672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#define CHROME_BROWSER_EXTENSIONS_EXTERNAL_EXTENSION_LOADER_H_ 772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#pragma once 872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#include "base/file_path.h" 10ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/memory/ref_counted.h" 11ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/memory/scoped_ptr.h" 1272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 1372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenclass DictionaryValue; 1472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenclass ExternalExtensionProviderImpl; 1572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 1672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// Base class for gathering a list of external extensions. Subclasses 1772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// implement loading from registry, JSON file, policy. 1872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// Instances are owned by ExternalExtensionProviderImpl objects. 1972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// Instances are created on the UI thread and expect public method calls from 2072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// the UI thread. Some subclasses introduce new methods that are executed on the 2172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// FILE thread. 2272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// The sequence of loading the extension list: 2372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// 1.) StartLoading() - checks if a loading task is already running 2472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// 2.) Load() - implemented in subclasses 2572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// 3.) FinishLoading() 2672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// 4.) owner_->SetPrefs() 2772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenclass ExternalExtensionLoader 2872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen : public base::RefCountedThreadSafe<ExternalExtensionLoader> { 2972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen public: 3072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen explicit ExternalExtensionLoader(); 3172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 3272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Specifies the provider that owns this object. 3372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen void Init(ExternalExtensionProviderImpl* owner); 3472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 3572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Called by the owner before it gets deleted. 3672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen void OwnerShutdown(); 3772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 3872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Initiates the possibly asynchronous loading of extension list. 3972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // It is the responsibility of the caller to ensure that calls to 4072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // this method do not overlap with each other. 4172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Implementations of this method should save the loaded results 4272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // in prefs_ and then call LoadFinished. 4372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen virtual void StartLoading() = 0; 4472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 4572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Some external providers allow relative file paths to local CRX files. 4672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Subclasses that want this behavior should override this method to 4772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // return the absolute path from which relative paths should be resolved. 4872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // By default, return an empty path, which indicates that relative paths 4972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // are not allowed. 5072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen virtual const FilePath GetBaseCrxFilePath(); 5172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 5272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen protected: 5372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen virtual ~ExternalExtensionLoader(); 5472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 5572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Notifies the provider that the list of extensions has been loaded. 5672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen void LoadFinished(); 5772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 5872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Used for passing the list of extensions from the method that loads them 5972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // to |LoadFinished|. To ensure thread safety, the rules are the following: 6072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // if this value is written on another thread than the UI, then it should 6172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // only be written in a task that was posted from |StartLoading|. After that, 6272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // this task should invoke |LoadFinished| with a PostTask. This scheme of 6372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // posting tasks will avoid concurrent access and imply the necessary memory 6472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // barriers. 6572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen scoped_ptr<DictionaryValue> prefs_; 6672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 6772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen private: 6872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen friend class base::RefCountedThreadSafe<ExternalExtensionLoader>; 6972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 7072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen ExternalExtensionProviderImpl* owner_; // weak 7172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 7272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Set to true if loading the extensions is already running. New requests 7372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // are ignored while this is set true. 7472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen bool running_; 7572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 7672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen DISALLOW_COPY_AND_ASSIGN(ExternalExtensionLoader); 7772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen}; 7872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 7972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#endif // CHROME_BROWSER_EXTENSIONS_EXTERNAL_EXTENSION_LOADER_H_ 80