Delay app cache check until flag file indicates that it is ready
[chromium-blink-merge.git] / chrome / browser / chromeos / extensions / external_cache.h
blob8f42c42b00df8ac3f96f30240c98b0af3672d38e
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_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/files/file_path.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/threading/sequenced_worker_pool.h"
15 #include "chrome/browser/extensions/updater/extension_downloader_delegate.h"
16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
19 namespace base {
20 class DictionaryValue;
23 namespace extensions {
24 class ExtensionDownloader;
27 namespace net {
28 class URLRequestContextGetter;
31 namespace tracked_objects {
32 class Location;
35 namespace chromeos {
37 // The ExternalCache manages cache for external extensions.
38 class ExternalCache : public content::NotificationObserver,
39 public extensions::ExtensionDownloaderDelegate {
40 public:
41 class Delegate {
42 public:
43 virtual ~Delegate() {}
44 // Caller owns |prefs|.
45 virtual void OnExtensionListsUpdated(
46 const base::DictionaryValue* prefs) = 0;
49 // The |request_context| is used for the update checks.
50 // By default updates are checked for the extensions with external_update_url.
51 // If |always_check_webstore| set, updates will be check for external_crx too.
52 // If |wait_cache_initialization|, cache will wait for flag file in cache dir.
53 ExternalCache(const std::string& cache_dir,
54 net::URLRequestContextGetter* request_context,
55 Delegate* delegate,
56 bool always_check_updates,
57 bool wait_cache_initialization);
58 virtual ~ExternalCache();
60 // Returns already cached extensions.
61 const base::DictionaryValue* cached_extensions() {
62 return cached_extensions_.get();
65 // Update list of extensions in cache and force update check for them.
66 // ExternalCache gets ownership of |prefs|.
67 void UpdateExtensionsList(scoped_ptr<base::DictionaryValue> prefs);
69 // If a user of one of the ExternalCache's extensions detects that
70 // the extension is damaged then this method can be used to remove it from
71 // the cache and retry to download it after a restart.
72 void OnDamagedFileDetected(const base::FilePath& path);
74 protected:
75 // Implementation of content::NotificationObserver:
76 virtual void Observe(int type,
77 const content::NotificationSource& source,
78 const content::NotificationDetails& details) OVERRIDE;
80 // Implementation of ExtensionDownloaderDelegate:
81 virtual void OnExtensionDownloadFailed(
82 const std::string& id,
83 Error error,
84 const PingResult& ping_result,
85 const std::set<int>& request_ids) OVERRIDE;
87 virtual void OnExtensionDownloadFinished(
88 const std::string& id,
89 const base::FilePath& path,
90 const GURL& download_url,
91 const std::string& version,
92 const PingResult& ping_result,
93 const std::set<int>& request_ids) OVERRIDE;
95 virtual void OnBlacklistDownloadFinished(
96 const std::string& data,
97 const std::string& package_hash,
98 const std::string& version,
99 const PingResult& ping_result,
100 const std::set<int>& request_ids) OVERRIDE;
102 virtual bool IsExtensionPending(const std::string& id) OVERRIDE;
104 virtual bool GetExtensionExistingVersion(const std::string& id,
105 std::string* version) OVERRIDE;
107 // Starts a cache update check immediately.
108 void CheckCacheNow();
110 // Notifies the that the cache has been updated, providing
111 // extensions loader with an updated list of extensions.
112 void UpdateExtensionLoader();
114 // Performs a cache update check on the blocking pool. |external_cache| is
115 // used to reply in the UI thread. |prefs| contains the list extensions
116 // anything else is invalid, and should be removed from the cache.
117 // Ownership of |prefs| is transferred to this function.
118 static void BlockingCheckCache(
119 base::WeakPtr<ExternalCache> external_cache,
120 base::SequencedWorkerPool::SequenceToken token,
121 const std::string& app_cache_dir,
122 scoped_ptr<base::DictionaryValue> prefs,
123 bool wait_cache_initialization);
125 // Helper for BlockingCheckCache(), updates |prefs|.
126 static void BlockingCheckCacheInternal(
127 const std::string& app_cache_dir,
128 base::DictionaryValue* prefs);
130 // Invoked when the cache has been updated. |prefs| contains all the currently
131 // valid crx files in the cache, ownerships is transfered to this function.
132 void OnCacheUpdated(scoped_ptr<base::DictionaryValue> prefs);
134 // Invoked to install the downloaded crx file at |path| in the cache.
135 static void BlockingInstallCacheEntry(
136 base::WeakPtr<ExternalCache> external_cache,
137 const std::string& app_cache_dir,
138 const std::string& id,
139 const base::FilePath& path,
140 const std::string& version);
142 // Invoked on the UI thread when a new entry has been installed in the cache.
143 void OnCacheEntryInstalled(const std::string& id,
144 const std::string& path,
145 const std::string& version);
147 // Helper to post blocking IO tasks to the blocking pool.
148 void PostBlockingTask(const tracked_objects::Location& from_here,
149 const base::Closure& task);
151 // Path to the dir where apps cache is stored.
152 std::string cache_dir_;
154 // Request context used by the |downloader_|.
155 net::URLRequestContextGetter* request_context_;
157 // Delegate that would like to get notifications about cache updates.
158 Delegate* delegate_;
160 // Updates needs to be check for the extensions with external_crx too.
161 bool always_check_updates_;
163 // Set to true if cache should wait for initialization flag file.
164 bool wait_cache_initialization_;
166 // This is the list of extensions currently configured.
167 scoped_ptr<base::DictionaryValue> extensions_;
169 // This contains extensions that are both currently configured
170 // and that have a valid crx in the cache.
171 scoped_ptr<base::DictionaryValue> cached_extensions_;
173 // Used to download the extensions and to check for updates.
174 scoped_ptr<extensions::ExtensionDownloader> downloader_;
176 base::WeakPtrFactory<ExternalCache> weak_ptr_factory_;
178 // Observes failures to install CRX files.
179 content::NotificationRegistrar notification_registrar_;
181 // Unique sequence token so that tasks posted by the ExternalCache are
182 // executed sequentially in the blocking pool.
183 base::SequencedWorkerPool::SequenceToken worker_pool_token_;
185 DISALLOW_COPY_AND_ASSIGN(ExternalCache);
188 } // namespace chromeos
190 #endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_EXTERNAL_CACHE_H_