• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CHROME_BROWSER_CHROMEOS_EXTENSIONS_EXTERNAL_CACHE_H_
6 #define CHROME_BROWSER_CHROMEOS_EXTENSIONS_EXTERNAL_CACHE_H_
7 
8 #include <string>
9 
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"
22 
23 namespace base {
24 class DictionaryValue;
25 }
26 
27 namespace extensions {
28 class ExtensionDownloader;
29 }
30 
31 namespace net {
32 class URLRequestContextGetter;
33 }
34 
35 namespace chromeos {
36 
37 // The ExternalCache manages a cache for external extensions.
38 class ExternalCache : public content::NotificationObserver,
39                       public extensions::ExtensionDownloaderDelegate {
40  public:
41   typedef base::Callback<void(const std::string& id, bool success)>
42       PutExternalExtensionCallback;
43 
44   class Delegate {
45    public:
~Delegate()46     virtual ~Delegate() {}
47     // Caller owns |prefs|.
48     virtual void OnExtensionListsUpdated(
49         const base::DictionaryValue* prefs) = 0;
50     // Called after extension with |id| is loaded in cache.
OnExtensionLoadedInCache(const std::string & id)51     virtual void OnExtensionLoadedInCache(const std::string& id) {}
52     // Called when extension with |id| is failed with downloading for |error|.
OnExtensionDownloadFailed(const std::string & id,extensions::ExtensionDownloaderDelegate::Error error)53     virtual void OnExtensionDownloadFailed(
54         const std::string& id,
55         extensions::ExtensionDownloaderDelegate::Error error) {}
56 
57     // Cache needs to provide already installed extensions otherwise they
58     // will be removed. Cache calls this function to get version of installed
59     // extension or empty string if not installed.
60     virtual std::string GetInstalledExtensionVersion(const std::string& id);
61   };
62 
63   // The |request_context| is used for update checks. All file I/O is done via
64   // the |backend_task_runner|. If |always_check_updates| is |false|, update
65   // checks are performed for extensions that have an |external_update_url|
66   // only. If |wait_for_cache_initialization| is |true|, the cache contents will
67   // not be read until a flag file appears in the cache directory, signaling
68   // that the cache is ready.
69   ExternalCache(const base::FilePath& cache_dir,
70                 net::URLRequestContextGetter* request_context,
71                 const scoped_refptr<base::SequencedTaskRunner>&
72                     backend_task_runner,
73                 Delegate* delegate,
74                 bool always_check_updates,
75                 bool wait_for_cache_initialization);
76   virtual ~ExternalCache();
77 
78   // Returns already cached extensions.
cached_extensions()79   const base::DictionaryValue* cached_extensions() {
80     return cached_extensions_.get();
81   }
82 
83   // Implementation of content::NotificationObserver:
84   virtual void Observe(int type,
85                        const content::NotificationSource& source,
86                        const content::NotificationDetails& details) OVERRIDE;
87 
88   // Implementation of ExtensionDownloaderDelegate:
89   virtual void OnExtensionDownloadFailed(
90       const std::string& id,
91       Error error,
92       const PingResult& ping_result,
93       const std::set<int>& request_ids) OVERRIDE;
94 
95   virtual void OnExtensionDownloadFinished(
96       const std::string& id,
97       const base::FilePath& path,
98       bool file_ownership_passed,
99       const GURL& download_url,
100       const std::string& version,
101       const PingResult& ping_result,
102       const std::set<int>& request_ids) OVERRIDE;
103 
104   virtual bool IsExtensionPending(const std::string& id) OVERRIDE;
105 
106   virtual bool GetExtensionExistingVersion(const std::string& id,
107                                            std::string* version) OVERRIDE;
108 
109   // Shut down the cache. The |callback| will be invoked when the cache has shut
110   // down completely and there are no more pending file I/O operations.
111   void Shutdown(const base::Closure& callback);
112 
113   // Replace the list of extensions to cache with |prefs| and perform update
114   // checks for these.
115   void UpdateExtensionsList(scoped_ptr<base::DictionaryValue> prefs);
116 
117   // If a user of one of the ExternalCache's extensions detects that
118   // the extension is damaged then this method can be used to remove it from
119   // the cache and retry to download it after a restart.
120   void OnDamagedFileDetected(const base::FilePath& path);
121 
122   // Removes extensions listed in |ids| from external cache, corresponding crx
123   // files will be removed from disk too.
124   void RemoveExtensions(const std::vector<std::string>& ids);
125 
126   // If extension with |id| exists in the cache, returns |true|, |file_path| and
127   // |version| for the extension. Extension will be marked as used with current
128   // timestamp.
129   bool GetExtension(const std::string& id,
130                     base::FilePath* file_path,
131                     std::string* version);
132 
133   // Puts the external |crx_file_path| into |local_cache_| for extension with
134   // |id|.
135   void PutExternalExtension(const std::string& id,
136                             const base::FilePath& crx_file_path,
137                             const std::string& version,
138                             const PutExternalExtensionCallback& callback);
139 
140  private:
141   // Notifies the that the cache has been updated, providing
142   // extensions loader with an updated list of extensions.
143   void UpdateExtensionLoader();
144 
145   // Checks the cache contents and initiate download if needed.
146   void CheckCache();
147 
148   // Invoked on the UI thread when a new entry has been installed in the cache.
149   void OnPutExtension(const std::string& id,
150                       const base::FilePath& file_path,
151                       bool file_ownership_passed);
152 
153   // Invoked on the UI thread when the external extension has been installed
154   // in the local cache by calling PutExternalExtension.
155   void OnPutExternalExtension(const std::string& id,
156                               const PutExternalExtensionCallback& callback,
157                               const base::FilePath& file_path,
158                               bool file_ownership_passed);
159 
160   extensions::LocalExtensionCache local_cache_;
161 
162   // Request context used by the |downloader_|.
163   scoped_refptr<net::URLRequestContextGetter> request_context_;
164 
165   // Task runner for executing file I/O tasks.
166   const scoped_refptr<base::SequencedTaskRunner> backend_task_runner_;
167 
168   // Delegate that would like to get notifications about cache updates.
169   Delegate* delegate_;
170 
171   // Updates needs to be check for the extensions with external_crx too.
172   bool always_check_updates_;
173 
174   // Set to true if cache should wait for initialization flag file.
175   bool wait_for_cache_initialization_;
176 
177   // This is the list of extensions currently configured.
178   scoped_ptr<base::DictionaryValue> extensions_;
179 
180   // This contains extensions that are both currently configured
181   // and that have a valid crx in the cache.
182   scoped_ptr<base::DictionaryValue> cached_extensions_;
183 
184   // Used to download the extensions and to check for updates.
185   scoped_ptr<extensions::ExtensionDownloader> downloader_;
186 
187   // Observes failures to install CRX files.
188   content::NotificationRegistrar notification_registrar_;
189 
190   // Weak factory for callbacks.
191   base::WeakPtrFactory<ExternalCache> weak_ptr_factory_;
192 
193   DISALLOW_COPY_AND_ASSIGN(ExternalCache);
194 };
195 
196 }  // namespace chromeos
197 
198 #endif  // CHROME_BROWSER_CHROMEOS_EXTENSIONS_EXTERNAL_CACHE_H_
199