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;
48 // Cache needs to provide already installed extensions otherwise they
49 // will be removed. Cache calls this function to get version of installed
50 // extension or empty string if not installed.
51 virtual std::string
GetInstalledExtensionVersion(const std::string
& id
);
54 // The |request_context| is used for update checks. All file I/O is done via
55 // the |backend_task_runner|. If |always_check_updates| is |false|, update
56 // checks are performed for extensions that have an |external_update_url|
57 // only. If |wait_for_cache_initialization| is |true|, the cache contents will
58 // not be read until a flag file appears in the cache directory, signaling
59 // that the cache is ready.
60 ExternalCache(const base::FilePath
& cache_dir
,
61 net::URLRequestContextGetter
* request_context
,
62 const scoped_refptr
<base::SequencedTaskRunner
>&
65 bool always_check_updates
,
66 bool wait_for_cache_initialization
);
67 virtual ~ExternalCache();
69 // Returns already cached extensions.
70 const base::DictionaryValue
* cached_extensions() {
71 return cached_extensions_
.get();
74 // Implementation of content::NotificationObserver:
75 virtual void Observe(int type
,
76 const content::NotificationSource
& source
,
77 const content::NotificationDetails
& details
) OVERRIDE
;
79 // Implementation of ExtensionDownloaderDelegate:
80 virtual void OnExtensionDownloadFailed(
81 const std::string
& id
,
83 const PingResult
& ping_result
,
84 const std::set
<int>& request_ids
) OVERRIDE
;
86 virtual void OnExtensionDownloadFinished(
87 const std::string
& id
,
88 const base::FilePath
& path
,
89 const GURL
& download_url
,
90 const std::string
& version
,
91 const PingResult
& ping_result
,
92 const std::set
<int>& request_ids
) OVERRIDE
;
94 virtual bool IsExtensionPending(const std::string
& id
) OVERRIDE
;
96 virtual bool GetExtensionExistingVersion(const std::string
& id
,
97 std::string
* version
) OVERRIDE
;
99 // Shut down the cache. The |callback| will be invoked when the cache has shut
100 // down completely and there are no more pending file I/O operations.
101 void Shutdown(const base::Closure
& callback
);
103 // Replace the list of extensions to cache with |prefs| and perform update
105 void UpdateExtensionsList(scoped_ptr
<base::DictionaryValue
> prefs
);
107 // If a user of one of the ExternalCache's extensions detects that
108 // the extension is damaged then this method can be used to remove it from
109 // the cache and retry to download it after a restart.
110 void OnDamagedFileDetected(const base::FilePath
& path
);
113 // Notifies the that the cache has been updated, providing
114 // extensions loader with an updated list of extensions.
115 void UpdateExtensionLoader();
117 // Checks the cache contents and initiate download if needed.
120 // Invoked on the UI thread when a new entry has been installed in the cache.
121 void CacheEntryInstalled(const std::string
& id
);
123 extensions::LocalExtensionCache local_cache_
;
125 // Request context used by the |downloader_|.
126 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
128 // Delegate that would like to get notifications about cache updates.
131 // Updates needs to be check for the extensions with external_crx too.
132 bool always_check_updates_
;
134 // Set to true if cache should wait for initialization flag file.
135 bool wait_for_cache_initialization_
;
137 // This is the list of extensions currently configured.
138 scoped_ptr
<base::DictionaryValue
> extensions_
;
140 // This contains extensions that are both currently configured
141 // and that have a valid crx in the cache.
142 scoped_ptr
<base::DictionaryValue
> cached_extensions_
;
144 // Used to download the extensions and to check for updates.
145 scoped_ptr
<extensions::ExtensionDownloader
> downloader_
;
147 // Observes failures to install CRX files.
148 content::NotificationRegistrar notification_registrar_
;
150 // Weak factory for callbacks.
151 base::WeakPtrFactory
<ExternalCache
> weak_ptr_factory_
;
153 DISALLOW_COPY_AND_ASSIGN(ExternalCache
);
156 } // namespace chromeos
158 #endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_EXTERNAL_CACHE_H_