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/local_extension_cache.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
21 #include "extensions/browser/updater/extension_downloader_delegate.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
{
41 typedef base::Callback
<void(const std::string
& id
, bool success
)>
42 PutExternalExtensionCallback
;
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.
51 virtual void OnExtensionLoadedInCache(const std::string
& id
) {}
52 // Called when extension with |id| is failed with downloading for |error|.
53 virtual void OnExtensionDownloadFailed(
54 const std::string
& id
,
55 extensions::ExtensionDownloaderDelegate::Error error
) {}
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
);
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
>&
74 bool always_check_updates
,
75 bool wait_for_cache_initialization
);
76 ~ExternalCache() override
;
78 // Returns already cached extensions.
79 const base::DictionaryValue
* cached_extensions() {
80 return cached_extensions_
.get();
83 // Implementation of content::NotificationObserver:
84 void Observe(int type
,
85 const content::NotificationSource
& source
,
86 const content::NotificationDetails
& details
) override
;
88 // Implementation of ExtensionDownloaderDelegate:
89 void OnExtensionDownloadFailed(const std::string
& id
,
91 const PingResult
& ping_result
,
92 const std::set
<int>& request_ids
) override
;
94 void OnExtensionDownloadFinished(const extensions::CRXFileInfo
& file
,
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
,
100 const InstallCallback
& callback
) override
;
102 bool IsExtensionPending(const std::string
& id
) override
;
104 bool GetExtensionExistingVersion(const std::string
& id
,
105 std::string
* version
) override
;
107 // Shut down the cache. The |callback| will be invoked when the cache has shut
108 // down completely and there are no more pending file I/O operations.
109 void Shutdown(const base::Closure
& callback
);
111 // Replace the list of extensions to cache with |prefs| and perform update
113 void UpdateExtensionsList(scoped_ptr
<base::DictionaryValue
> prefs
);
115 // If a user of one of the ExternalCache's extensions detects that
116 // the extension is damaged then this method can be used to remove it from
117 // the cache and retry to download it after a restart.
118 void OnDamagedFileDetected(const base::FilePath
& path
);
120 // Removes extensions listed in |ids| from external cache, corresponding crx
121 // files will be removed from disk too.
122 void RemoveExtensions(const std::vector
<std::string
>& ids
);
124 // If extension with |id| exists in the cache, returns |true|, |file_path| and
125 // |version| for the extension. Extension will be marked as used with current
127 bool GetExtension(const std::string
& id
,
128 base::FilePath
* file_path
,
129 std::string
* version
);
131 // Puts the external |crx_file_path| into |local_cache_| for extension with
133 void PutExternalExtension(const std::string
& id
,
134 const base::FilePath
& crx_file_path
,
135 const std::string
& version
,
136 const PutExternalExtensionCallback
& callback
);
139 // Notifies the that the cache has been updated, providing
140 // extensions loader with an updated list of extensions.
141 void UpdateExtensionLoader();
143 // Checks the cache contents and initiate download if needed.
146 // Invoked on the UI thread when a new entry has been installed in the cache.
147 void OnPutExtension(const std::string
& id
,
148 const base::FilePath
& file_path
,
149 bool file_ownership_passed
);
151 // Invoked on the UI thread when the external extension has been installed
152 // in the local cache by calling PutExternalExtension.
153 void OnPutExternalExtension(const std::string
& id
,
154 const PutExternalExtensionCallback
& callback
,
155 const base::FilePath
& file_path
,
156 bool file_ownership_passed
);
158 extensions::LocalExtensionCache local_cache_
;
160 // Request context used by the |downloader_|.
161 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
163 // Task runner for executing file I/O tasks.
164 const scoped_refptr
<base::SequencedTaskRunner
> backend_task_runner_
;
166 // Delegate that would like to get notifications about cache updates.
169 // Updates needs to be check for the extensions with external_crx too.
170 bool always_check_updates_
;
172 // Set to true if cache should wait for initialization flag file.
173 bool wait_for_cache_initialization_
;
175 // This is the list of extensions currently configured.
176 scoped_ptr
<base::DictionaryValue
> extensions_
;
178 // This contains extensions that are both currently configured
179 // and that have a valid crx in the cache.
180 scoped_ptr
<base::DictionaryValue
> cached_extensions_
;
182 // Used to download the extensions and to check for updates.
183 scoped_ptr
<extensions::ExtensionDownloader
> downloader_
;
185 // Observes failures to install CRX files.
186 content::NotificationRegistrar notification_registrar_
;
188 // Weak factory for callbacks.
189 base::WeakPtrFactory
<ExternalCache
> weak_ptr_factory_
;
191 DISALLOW_COPY_AND_ASSIGN(ExternalCache
);
194 } // namespace chromeos
196 #endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_EXTERNAL_CACHE_H_