1 // Copyright (c) 2013 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 CHROME_BROWSER_CHROMEOS_EXTENSIONS_EXTERNAL_CACHE_H_
6 #define CHROME_BROWSER_CHROMEOS_EXTENSIONS_EXTERNAL_CACHE_H_
10 #include "base/basictypes.h"
11 #include "base/callback_forward.h"
12 #include "base/files/file_path.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/sequenced_task_runner.h"
18 #include "chrome/browser/extensions/updater/extension_downloader_delegate.h"
19 #include "chrome/browser/extensions/updater/local_extension_cache.h"
20 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h"
24 class DictionaryValue;
27 namespace extensions {
28 class ExtensionDownloader;
32 class URLRequestContextGetter;
37 // The ExternalCache manages a cache for external extensions.
38 class ExternalCache : public content::NotificationObserver,
39 public extensions::ExtensionDownloaderDelegate {
43 virtual ~Delegate() {}
44 // Caller owns |prefs|.
45 virtual void OnExtensionListsUpdated(
46 const base::DictionaryValue* prefs) = 0;
47 // Called after extension with |id| is loaded in cache.
48 virtual void OnExtensionLoadedInCache(const std::string& id) {}
49 // Called when extension with |id| is failed with downloading for |error|.
50 virtual void OnExtensionDownloadFailed(
51 const std::string& id,
52 extensions::ExtensionDownloaderDelegate::Error error) {}
54 // Cache needs to provide already installed extensions otherwise they
55 // will be removed. Cache calls this function to get version of installed
56 // extension or empty string if not installed.
57 virtual std::string GetInstalledExtensionVersion(const std::string& id);
60 // The |request_context| is used for update checks. All file I/O is done via
61 // the |backend_task_runner|. If |always_check_updates| is |false|, update
62 // checks are performed for extensions that have an |external_update_url|
63 // only. If |wait_for_cache_initialization| is |true|, the cache contents will
64 // not be read until a flag file appears in the cache directory, signaling
65 // that the cache is ready.
66 ExternalCache(const base::FilePath& cache_dir,
67 net::URLRequestContextGetter* request_context,
68 const scoped_refptr<base::SequencedTaskRunner>&
71 bool always_check_updates,
72 bool wait_for_cache_initialization);
73 virtual ~ExternalCache();
75 // Returns already cached extensions.
76 const base::DictionaryValue* cached_extensions() {
77 return cached_extensions_.get();
80 // Implementation of content::NotificationObserver:
81 virtual void Observe(int type,
82 const content::NotificationSource& source,
83 const content::NotificationDetails& details) OVERRIDE;
85 // Implementation of ExtensionDownloaderDelegate:
86 virtual void OnExtensionDownloadFailed(
87 const std::string& id,
89 const PingResult& ping_result,
90 const std::set<int>& request_ids) OVERRIDE;
92 virtual void OnExtensionDownloadFinished(
93 const std::string& id,
94 const base::FilePath& path,
95 bool file_ownership_passed,
96 const GURL& download_url,
97 const std::string& version,
98 const PingResult& ping_result,
99 const std::set<int>& request_ids) OVERRIDE;
101 virtual bool IsExtensionPending(const std::string& id) OVERRIDE;
103 virtual bool GetExtensionExistingVersion(const std::string& id,
104 std::string* version) OVERRIDE;
106 // Shut down the cache. The |callback| will be invoked when the cache has shut
107 // down completely and there are no more pending file I/O operations.
108 void Shutdown(const base::Closure& callback);
110 // Replace the list of extensions to cache with |prefs| and perform update
112 void UpdateExtensionsList(scoped_ptr<base::DictionaryValue> prefs);
114 // If a user of one of the ExternalCache's extensions detects that
115 // the extension is damaged then this method can be used to remove it from
116 // the cache and retry to download it after a restart.
117 void OnDamagedFileDetected(const base::FilePath& path);
119 // Removes extensions listed in |ids| from external cache, corresponding crx
120 // files will be removed from disk too.
121 void RemoveExtensions(const std::vector<std::string>& ids);
124 // Notifies the that the cache has been updated, providing
125 // extensions loader with an updated list of extensions.
126 void UpdateExtensionLoader();
128 // Checks the cache contents and initiate download if needed.
131 // Invoked on the UI thread when a new entry has been installed in the cache.
132 void OnPutExtension(const std::string& id,
133 const base::FilePath& file_path,
134 bool file_ownership_passed);
136 extensions::LocalExtensionCache local_cache_;
138 // Request context used by the |downloader_|.
139 scoped_refptr<net::URLRequestContextGetter> request_context_;
141 // Task runner for executing file I/O tasks.
142 const scoped_refptr<base::SequencedTaskRunner> backend_task_runner_;
144 // Delegate that would like to get notifications about cache updates.
147 // Updates needs to be check for the extensions with external_crx too.
148 bool always_check_updates_;
150 // Set to true if cache should wait for initialization flag file.
151 bool wait_for_cache_initialization_;
153 // This is the list of extensions currently configured.
154 scoped_ptr<base::DictionaryValue> extensions_;
156 // This contains extensions that are both currently configured
157 // and that have a valid crx in the cache.
158 scoped_ptr<base::DictionaryValue> cached_extensions_;
160 // Used to download the extensions and to check for updates.
161 scoped_ptr<extensions::ExtensionDownloader> downloader_;
163 // Observes failures to install CRX files.
164 content::NotificationRegistrar notification_registrar_;
166 // Weak factory for callbacks.
167 base::WeakPtrFactory<ExternalCache> weak_ptr_factory_;
169 DISALLOW_COPY_AND_ASSIGN(ExternalCache);
172 } // namespace chromeos
174 #endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_EXTERNAL_CACHE_H_