• 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   class Delegate {
42    public:
~Delegate()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.
OnExtensionLoadedInCache(const std::string & id)48     virtual void OnExtensionLoadedInCache(const std::string& id) {}
49     // Called when extension with |id| is failed with downloading for |error|.
OnExtensionDownloadFailed(const std::string & id,extensions::ExtensionDownloaderDelegate::Error error)50     virtual void OnExtensionDownloadFailed(
51         const std::string& id,
52         extensions::ExtensionDownloaderDelegate::Error error) {}
53 
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);
58   };
59 
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>&
69                     backend_task_runner,
70                 Delegate* delegate,
71                 bool always_check_updates,
72                 bool wait_for_cache_initialization);
73   virtual ~ExternalCache();
74 
75   // Returns already cached extensions.
cached_extensions()76   const base::DictionaryValue* cached_extensions() {
77     return cached_extensions_.get();
78   }
79 
80   // Implementation of content::NotificationObserver:
81   virtual void Observe(int type,
82                        const content::NotificationSource& source,
83                        const content::NotificationDetails& details) OVERRIDE;
84 
85   // Implementation of ExtensionDownloaderDelegate:
86   virtual void OnExtensionDownloadFailed(
87       const std::string& id,
88       Error error,
89       const PingResult& ping_result,
90       const std::set<int>& request_ids) OVERRIDE;
91 
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;
100 
101   virtual bool IsExtensionPending(const std::string& id) OVERRIDE;
102 
103   virtual bool GetExtensionExistingVersion(const std::string& id,
104                                            std::string* version) OVERRIDE;
105 
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);
109 
110   // Replace the list of extensions to cache with |prefs| and perform update
111   // checks for these.
112   void UpdateExtensionsList(scoped_ptr<base::DictionaryValue> prefs);
113 
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);
118 
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);
122 
123   // If extension with |id| exists in the cache, returns |true|, |file_path| and
124   // |version| for the extension. Extension will be marked as used with current
125   // timestamp.
126   bool GetExtension(const std::string& id,
127                     base::FilePath* file_path,
128                     std::string* version);
129 
130  private:
131   // Notifies the that the cache has been updated, providing
132   // extensions loader with an updated list of extensions.
133   void UpdateExtensionLoader();
134 
135   // Checks the cache contents and initiate download if needed.
136   void CheckCache();
137 
138   // Invoked on the UI thread when a new entry has been installed in the cache.
139   void OnPutExtension(const std::string& id,
140                       const base::FilePath& file_path,
141                       bool file_ownership_passed);
142 
143   extensions::LocalExtensionCache local_cache_;
144 
145   // Request context used by the |downloader_|.
146   scoped_refptr<net::URLRequestContextGetter> request_context_;
147 
148   // Task runner for executing file I/O tasks.
149   const scoped_refptr<base::SequencedTaskRunner> backend_task_runner_;
150 
151   // Delegate that would like to get notifications about cache updates.
152   Delegate* delegate_;
153 
154   // Updates needs to be check for the extensions with external_crx too.
155   bool always_check_updates_;
156 
157   // Set to true if cache should wait for initialization flag file.
158   bool wait_for_cache_initialization_;
159 
160   // This is the list of extensions currently configured.
161   scoped_ptr<base::DictionaryValue> extensions_;
162 
163   // This contains extensions that are both currently configured
164   // and that have a valid crx in the cache.
165   scoped_ptr<base::DictionaryValue> cached_extensions_;
166 
167   // Used to download the extensions and to check for updates.
168   scoped_ptr<extensions::ExtensionDownloader> downloader_;
169 
170   // Observes failures to install CRX files.
171   content::NotificationRegistrar notification_registrar_;
172 
173   // Weak factory for callbacks.
174   base::WeakPtrFactory<ExternalCache> weak_ptr_factory_;
175 
176   DISALLOW_COPY_AND_ASSIGN(ExternalCache);
177 };
178 
179 }  // namespace chromeos
180 
181 #endif  // CHROME_BROWSER_CHROMEOS_EXTENSIONS_EXTERNAL_CACHE_H_
182