Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(426)

Side by Side Diff: chrome/browser/extensions/extensions_service.h

Issue 160311: Pull CrxInstaller out of ExtensionsService. (Closed)
Patch Set: Fix leak of SandboxedExtensionUnpacker Created 11 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSIONS_SERVICE_H_ 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSIONS_SERVICE_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSIONS_SERVICE_H_ 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSIONS_SERVICE_H_
7 7
8 #include <map> 8 #include <map>
9 #include <set> 9 #include <set>
10 #include <string> 10 #include <string>
(...skipping 19 matching lines...) Expand all
30 class ExtensionUpdater; 30 class ExtensionUpdater;
31 class GURL; 31 class GURL;
32 class MessageLoop; 32 class MessageLoop;
33 class PrefService; 33 class PrefService;
34 class Profile; 34 class Profile;
35 class ResourceDispatcherHost; 35 class ResourceDispatcherHost;
36 class SiteInstance; 36 class SiteInstance;
37 37
38 typedef std::vector<Extension*> ExtensionList; 38 typedef std::vector<Extension*> ExtensionList;
39 39
40 // A callback for when installs finish. If the Extension* parameter is
41 // null then the install failed.
42 typedef Callback2<const FilePath&, Extension*>::Type ExtensionInstallCallback;
43
44 // This is an interface class to encapsulate the dependencies that 40 // This is an interface class to encapsulate the dependencies that
45 // ExtensionUpdater has on ExtensionsService. This allows easy mocking. 41 // ExtensionUpdater has on ExtensionsService. This allows easy mocking.
46 class ExtensionUpdateService { 42 class ExtensionUpdateService {
47 public: 43 public:
48 virtual ~ExtensionUpdateService() {} 44 virtual ~ExtensionUpdateService() {}
49 virtual const ExtensionList* extensions() const = 0; 45 virtual const ExtensionList* extensions() const = 0;
50 virtual void UpdateExtension(const std::string& id, 46 virtual void UpdateExtension(const std::string& id, const FilePath& path) = 0;
51 const FilePath& path,
52 bool alert_on_error,
53 ExtensionInstallCallback* callback) = 0;
54 virtual Extension* GetExtensionById(const std::string& id) = 0; 47 virtual Extension* GetExtensionById(const std::string& id) = 0;
55 }; 48 };
56 49
57 // Manages installed and running Chromium extensions. 50 // Manages installed and running Chromium extensions.
58 class ExtensionsService 51 class ExtensionsService
59 : public ExtensionUpdateService, 52 : public ExtensionUpdateService,
60 public base::RefCountedThreadSafe<ExtensionsService> { 53 public base::RefCountedThreadSafe<ExtensionsService> {
61 public: 54 public:
62 55
63 // The name of the directory inside the profile where extensions are 56 // The name of the directory inside the profile where extensions are
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 // immediately loaded. 98 // immediately loaded.
106 void InstallExtension(const FilePath& extension_path); 99 void InstallExtension(const FilePath& extension_path);
107 100
108 // XXX Hack: This is a temporary nasty hack to get theme installation working 101 // XXX Hack: This is a temporary nasty hack to get theme installation working
109 // without a dialog. Will be fixed by making ExtensionsService more modular. 102 // without a dialog. Will be fixed by making ExtensionsService more modular.
110 void InstallExtension(const FilePath& extension_path, 103 void InstallExtension(const FilePath& extension_path,
111 const GURL& download_url, 104 const GURL& download_url,
112 const GURL& referrer_url); 105 const GURL& referrer_url);
113 106
114 // Updates a currently-installed extension with the contents from 107 // Updates a currently-installed extension with the contents from
115 // |extension_path|. The |alert_on_error| parameter controls whether the 108 // |extension_path|.
116 // user will be notified in the event of failure. If |callback| is non-null,
117 // it will be called back when the update is finished (in success or failure).
118 // This is useful to know when the service is done using |extension_path|.
119 // Also, this takes ownership of |callback| if it's non-null.
120 virtual void UpdateExtension(const std::string& id, 109 virtual void UpdateExtension(const std::string& id,
121 const FilePath& extension_path, 110 const FilePath& extension_path);
122 bool alert_on_error,
123 ExtensionInstallCallback* callback);
124 111
125 // Uninstalls the specified extension. Callers should only call this method 112 // Uninstalls the specified extension. Callers should only call this method
126 // with extensions that exist. |external_uninstall| is a magical parameter 113 // with extensions that exist. |external_uninstall| is a magical parameter
127 // that is only used to send information to ExtensionPrefs, which external 114 // that is only used to send information to ExtensionPrefs, which external
128 // callers should never set to true. 115 // callers should never set to true.
129 // TODO(aa): Remove |external_uninstall| -- this information should be passed 116 // TODO(aa): Remove |external_uninstall| -- this information should be passed
130 // to ExtensionPrefs some other way. 117 // to ExtensionPrefs some other way.
131 void UninstallExtension(const std::string& extension_id, 118 void UninstallExtension(const std::string& extension_id,
132 bool external_uninstall); 119 bool external_uninstall);
133 120
(...skipping 25 matching lines...) Expand all
159 Extension* GetExtensionByURL(const GURL& url); 146 Extension* GetExtensionByURL(const GURL& url);
160 147
161 // Clear all ExternalExtensionProviders. 148 // Clear all ExternalExtensionProviders.
162 void ClearProvidersForTesting(); 149 void ClearProvidersForTesting();
163 150
164 // Sets an ExternalExtensionProvider for the service to use during testing. 151 // Sets an ExternalExtensionProvider for the service to use during testing.
165 // |location| specifies what type of provider should be added. 152 // |location| specifies what type of provider should be added.
166 void SetProviderForTesting(Extension::Location location, 153 void SetProviderForTesting(Extension::Location location,
167 ExternalExtensionProvider* test_provider); 154 ExternalExtensionProvider* test_provider);
168 155
169 void SetExtensionsEnabled(bool enabled); 156 // Called by the backend when the initial extension load has completed.
157 void OnLoadedInstalledExtensions();
158
159 // Called by the backend when extensions have been loaded.
160 void OnExtensionsLoaded(ExtensionList* extensions);
161
162 // Called by the backend when an extension has been installed.
163 void OnExtensionInstalled(Extension* extension);
164
165 // Called by the backend when an attempt was made to reinstall the same
166 // version of an existing extension.
167 void OnExtensionOverinstallAttempted(const std::string& id);
168
169 // Called by the backend when an external extension is found.
170 void OnExternalExtensionFound(const std::string& id,
171 const std::string& version,
172 const FilePath& path,
173 Extension::Location location);
174
175 void set_extensions_enabled(bool enabled) { extensions_enabled_ = enabled; }
170 bool extensions_enabled() { return extensions_enabled_; } 176 bool extensions_enabled() { return extensions_enabled_; }
171 177
172 void set_show_extensions_prompts(bool enabled) { 178 void set_show_extensions_prompts(bool enabled) {
173 show_extensions_prompts_ = enabled; 179 show_extensions_prompts_ = enabled;
174 } 180 }
175 181
176 bool show_extensions_prompts() { 182 bool show_extensions_prompts() {
177 return show_extensions_prompts_; 183 return show_extensions_prompts_;
178 } 184 }
179 185
180 // Profile calls this when it is destroyed so that we know not to call it. 186 // Profile calls this when it is destroyed so that we know not to call it.
181 void ProfileDestroyed() { profile_ = NULL; } 187 void ProfileDestroyed() { profile_ = NULL; }
182 188
183 ExtensionPrefs* extension_prefs() { return extension_prefs_.get(); } 189 ExtensionPrefs* extension_prefs() { return extension_prefs_.get(); }
184 190
185 // Whether the extension service is ready. 191 // Whether the extension service is ready.
186 bool is_ready() { return ready_; } 192 bool is_ready() { return ready_; }
187 193
188 private: 194 private:
189 // For OnExtensionLoaded, OnExtensionInstalled, and
190 // OnExtensionVersionReinstalled.
191 friend class ExtensionsServiceBackend;
192
193 // Called by the backend when the initial extension load has completed.
194 void OnLoadedInstalledExtensions();
195
196 // Called by the backend when extensions have been loaded.
197 void OnExtensionsLoaded(ExtensionList* extensions);
198
199 // Called by the backend when an extension has been installed.
200 void OnExtensionInstalled(const FilePath& path, Extension* extension,
201 Extension::InstallType install_type);
202
203 // Calls and then removes any registered install callback for |path|.
204 void FireInstallCallback(const FilePath& path, Extension* extension);
205
206 // Called by the backend when there was an error installing an extension.
207 void OnExtenionInstallError(const FilePath& path);
208
209 // Called by the backend when an attempt was made to reinstall the same
210 // version of an existing extension.
211 void OnExtensionOverinstallAttempted(const std::string& id,
212 const FilePath& path);
213
214 // Show a confirm installation infobar on the currently active tab. 195 // Show a confirm installation infobar on the currently active tab.
215 // TODO(aa): This should be moved up into the UI and attached to the tab it 196 // TODO(aa): This should be moved up into the UI and attached to the tab it
216 // actually occured in. This requires some modularization of 197 // actually occured in. This requires some modularization of
217 // ExtensionsService. 198 // ExtensionsService.
218 bool ShowThemePreviewInfobar(Extension* extension); 199 bool ShowThemePreviewInfobar(Extension* extension);
219 200
220 // The profile this ExtensionsService is part of. 201 // The profile this ExtensionsService is part of.
221 Profile* profile_; 202 Profile* profile_;
222 203
223 // Preferences for the owning profile. 204 // Preferences for the owning profile.
(...skipping 10 matching lines...) Expand all
234 215
235 // Whether or not extensions are enabled. 216 // Whether or not extensions are enabled.
236 bool extensions_enabled_; 217 bool extensions_enabled_;
237 218
238 // Whether to notify users when they attempt to install an extension. 219 // Whether to notify users when they attempt to install an extension.
239 bool show_extensions_prompts_; 220 bool show_extensions_prompts_;
240 221
241 // The backend that will do IO on behalf of this instance. 222 // The backend that will do IO on behalf of this instance.
242 scoped_refptr<ExtensionsServiceBackend> backend_; 223 scoped_refptr<ExtensionsServiceBackend> backend_;
243 224
244 // Stores data we'll need to do callbacks as installs complete.
245 typedef std::map<FilePath, linked_ptr<ExtensionInstallCallback> > CallbackMap;
246 CallbackMap install_callbacks_;
247
248 // Is the service ready to go? 225 // Is the service ready to go?
249 bool ready_; 226 bool ready_;
250 227
251 // Our extension updater, if updates are turned on. 228 // Our extension updater, if updates are turned on.
252 scoped_refptr<ExtensionUpdater> updater_; 229 scoped_refptr<ExtensionUpdater> updater_;
253 230
254 DISALLOW_COPY_AND_ASSIGN(ExtensionsService); 231 DISALLOW_COPY_AND_ASSIGN(ExtensionsService);
255 }; 232 };
256 233
257 // Implements IO for the ExtensionsService. 234 // Implements IO for the ExtensionsService.
258 // TODO(aa): This can probably move into the .cc file. 235 // TODO(aa): This can probably move into the .cc file.
259 class ExtensionsServiceBackend 236 class ExtensionsServiceBackend
260 : public base::RefCountedThreadSafe<ExtensionsServiceBackend>, 237 : public base::RefCountedThreadSafe<ExtensionsServiceBackend>,
261 public ExternalExtensionProvider::Visitor { 238 public ExternalExtensionProvider::Visitor {
262 public: 239 public:
263 // |rdh| can be NULL in the case of test environment. 240 // |rdh| can be NULL in the case of test environment.
264 // |extension_prefs| contains a dictionary value that points to the extension 241 // |extension_prefs| contains a dictionary value that points to the extension
265 // preferences. 242 // preferences.
266 ExtensionsServiceBackend(const FilePath& install_directory, 243 ExtensionsServiceBackend(const FilePath& install_directory,
267 ResourceDispatcherHost* rdh, 244 MessageLoop* frontend_loop);
268 MessageLoop* frontend_loop,
269 bool extensions_enabled);
270 245
271 virtual ~ExtensionsServiceBackend(); 246 virtual ~ExtensionsServiceBackend();
272 247
273 void set_extensions_enabled(bool enabled) { extensions_enabled_ = enabled; }
274
275 // Loads the installed extensions. 248 // Loads the installed extensions.
276 // Errors are reported through ExtensionErrorReporter. On completion, 249 // Errors are reported through ExtensionErrorReporter. On completion,
277 // OnExtensionsLoaded() is called with any successfully loaded extensions. 250 // OnExtensionsLoaded() is called with any successfully loaded extensions.
278 void LoadInstalledExtensions(scoped_refptr<ExtensionsService> frontend, 251 void LoadInstalledExtensions(scoped_refptr<ExtensionsService> frontend,
279 InstalledExtensions* installed); 252 InstalledExtensions* installed);
280 253
281 // Loads a single extension from |path| where |path| is the top directory of 254 // Loads a single extension from |path| where |path| is the top directory of
282 // a specific extension where its manifest file lives. 255 // a specific extension where its manifest file lives.
283 // Errors are reported through ExtensionErrorReporter. On completion, 256 // Errors are reported through ExtensionErrorReporter. On completion,
284 // OnExtensionsLoadedFromDirectory() is called with any successfully loaded 257 // OnExtensionsLoadedFromDirectory() is called with any successfully loaded
285 // extensions. 258 // extensions.
286 // TODO(erikkay): It might be useful to be able to load a packed extension 259 // TODO(erikkay): It might be useful to be able to load a packed extension
287 // (presumably into memory) without installing it. 260 // (presumably into memory) without installing it.
288 void LoadSingleExtension(const FilePath &path, 261 void LoadSingleExtension(const FilePath &path,
289 scoped_refptr<ExtensionsService> frontend); 262 scoped_refptr<ExtensionsService> frontend);
290 263
291 // Install the extension file at |extension_path|. Errors are reported through
292 // ExtensionErrorReporter. OnExtensionInstalled is called in the frontend on
293 // success.
294 void InstallExtension(const FilePath& extension_path, bool from_gallery,
295 scoped_refptr<ExtensionsService> frontend);
296
297 // Similar to InstallExtension, but |extension_path| must be an updated
298 // version of an installed extension with id of |id|.
299 void UpdateExtension(const std::string& id,
300 const FilePath& extension_path,
301 bool alert_on_error,
302 scoped_refptr<ExtensionsService> frontend);
303
304 // Check externally updated extensions for updates and install if necessary. 264 // Check externally updated extensions for updates and install if necessary.
305 // Errors are reported through ExtensionErrorReporter. Succcess is not 265 // Errors are reported through ExtensionErrorReporter. Succcess is not
306 // reported. 266 // reported.
307 void CheckForExternalUpdates(std::set<std::string> ids_to_ignore, 267 void CheckForExternalUpdates(std::set<std::string> ids_to_ignore,
308 scoped_refptr<ExtensionsService> frontend); 268 scoped_refptr<ExtensionsService> frontend);
309 269
310 // Clear all ExternalExtensionProviders. 270 // Clear all ExternalExtensionProviders.
311 void ClearProvidersForTesting(); 271 void ClearProvidersForTesting();
312 272
313 // Sets an ExternalExtensionProvider for the service to use during testing. 273 // Sets an ExternalExtensionProvider for the service to use during testing.
314 // |location| specifies what type of provider should be added. 274 // |location| specifies what type of provider should be added.
315 void SetProviderForTesting(Extension::Location location, 275 void SetProviderForTesting(Extension::Location location,
316 ExternalExtensionProvider* test_provider); 276 ExternalExtensionProvider* test_provider);
317 277
318 // ExternalExtensionProvider::Visitor implementation. 278 // ExternalExtensionProvider::Visitor implementation.
319 virtual void OnExternalExtensionFound(const std::string& id, 279 virtual void OnExternalExtensionFound(const std::string& id,
320 const Version* version, 280 const Version* version,
321 const FilePath& path); 281 const FilePath& path,
282 Extension::Location location);
322 private: 283 private:
323 class UnpackerClient;
324 friend class UnpackerClient;
325
326 // Loads a single installed extension. 284 // Loads a single installed extension.
327 void LoadInstalledExtension(const std::string& id, const FilePath& path, 285 void LoadInstalledExtension(const std::string& id, const FilePath& path,
328 Extension::Location location); 286 Extension::Location location);
329 287
330 // Install a crx file at |extension_path|. If |expected_id| is not empty, it's
331 // verified against the extension's manifest before installation. If the
332 // extension is already installed, install the new version only if its version
333 // number is greater than the current installed version. If |silent| is true,
334 // the confirmation dialog will not pop up.
335 void InstallOrUpdateExtension(const FilePath& extension_path,
336 bool from_gallery,
337 const std::string& expected_id, bool silent);
338
339 // Finish installing the extension in |crx_path| after it has been unpacked to 288 // Finish installing the extension in |crx_path| after it has been unpacked to
340 // |unpacked_path|. If |expected_id| is not empty, it's verified against the 289 // |unpacked_path|. If |expected_id| is not empty, it's verified against the
341 // extension's manifest before installation. If |silent| is true, there will 290 // extension's manifest before installation. If |silent| is true, there will
342 // be no install confirmation dialog. |from_gallery| indicates whether the 291 // be no install confirmation dialog. |from_gallery| indicates whether the
343 // crx was installed from our gallery, which results in different UI. 292 // crx was installed from our gallery, which results in different UI.
344 // 293 //
345 // Note: We take ownership of |extension|. 294 // Note: We take ownership of |extension|.
346 void OnExtensionUnpacked( 295 void OnExtensionUnpacked(
347 const FilePath& crx_path, 296 const FilePath& crx_path,
348 const FilePath& unpacked_path, 297 const FilePath& unpacked_path,
349 Extension* extension, 298 Extension* extension,
350 const std::string expected_id, 299 const std::string expected_id);
351 bool silent,
352 bool from_gallery);
353 300
354 // Notify the frontend that there was an error loading an extension. 301 // Notify the frontend that there was an error loading an extension.
355 void ReportExtensionLoadError(const FilePath& extension_path, 302 void ReportExtensionLoadError(const FilePath& extension_path,
356 const std::string& error); 303 const std::string& error);
357 304
358 // Notify the frontend that extensions were loaded. 305 // Notify the frontend that extensions were loaded.
359 void ReportExtensionsLoaded(ExtensionList* extensions); 306 void ReportExtensionsLoaded(ExtensionList* extensions);
360 307
361 // Notify the frontend that there was an error installing an extension. 308 // Notify the frontend that there was an error installing an extension.
362 void ReportExtensionInstallError(const FilePath& extension_path, 309 void ReportExtensionInstallError(const FilePath& extension_path,
363 const std::string& error); 310 const std::string& error);
364 311
365 // Notify the frontend that an attempt was made (but not carried out) to
366 // install the same version of an existing extension.
367 void ReportExtensionOverinstallAttempted(const std::string& id,
368 const FilePath& path);
369
370 // Lookup an external extension by |id| by going through all registered 312 // Lookup an external extension by |id| by going through all registered
371 // external extension providers until we find a provider that contains an 313 // external extension providers until we find a provider that contains an
372 // extension that matches. If |version| is not NULL, the extension version 314 // extension that matches. If |version| is not NULL, the extension version
373 // will be returned (caller is responsible for deleting that pointer). 315 // will be returned (caller is responsible for deleting that pointer).
374 // |location| can also be null, if not needed. Returns true if extension is 316 // |location| can also be null, if not needed. Returns true if extension is
375 // found, false otherwise. 317 // found, false otherwise.
376 bool LookupExternalExtension(const std::string& id, 318 bool LookupExternalExtension(const std::string& id,
377 Version** version, 319 Version** version,
378 Extension::Location* location); 320 Extension::Location* location);
379 321
380 // For the extension in |version_path| with |id|, check to see if it's an 322 // For the extension in |version_path| with |id|, check to see if it's an
381 // externally managed extension. If so return true if it should be 323 // externally managed extension. If so return true if it should be
382 // uninstalled. 324 // uninstalled.
383 bool CheckExternalUninstall(const std::string& id, 325 bool CheckExternalUninstall(const std::string& id,
384 Extension::Location location); 326 Extension::Location location);
385 327
386 // This is a naked pointer which is set by each entry point. 328 // This is a naked pointer which is set by each entry point.
387 // The entry point is responsible for ensuring lifetime. 329 // The entry point is responsible for ensuring lifetime.
388 ExtensionsService* frontend_; 330 ExtensionsService* frontend_;
389 331
390 // The top-level extensions directory being installed to. 332 // The top-level extensions directory being installed to.
391 FilePath install_directory_; 333 FilePath install_directory_;
392 334
393 // We only need a pointer to this to pass along to other interfaces.
394 ResourceDispatcherHost* resource_dispatcher_host_;
395
396 // Whether errors result in noisy alerts. 335 // Whether errors result in noisy alerts.
397 bool alert_on_error_; 336 bool alert_on_error_;
398 337
399 // The message loop to use to call the frontend. 338 // The message loop to use to call the frontend.
400 MessageLoop* frontend_loop_; 339 MessageLoop* frontend_loop_;
401 340
402 // Whether non-theme extensions are enabled (themes and externally registered
403 // extensions are always enabled).
404 bool extensions_enabled_;
405
406 // A map of all external extension providers. 341 // A map of all external extension providers.
407 typedef std::map<Extension::Location, 342 typedef std::map<Extension::Location,
408 linked_ptr<ExternalExtensionProvider> > ProviderMap; 343 linked_ptr<ExternalExtensionProvider> > ProviderMap;
409 ProviderMap external_extension_providers_; 344 ProviderMap external_extension_providers_;
410 345
411 DISALLOW_COPY_AND_ASSIGN(ExtensionsServiceBackend); 346 DISALLOW_COPY_AND_ASSIGN(ExtensionsServiceBackend);
412 }; 347 };
413 348
414 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSIONS_SERVICE_H_ 349 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSIONS_SERVICE_H_
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_updater_unittest.cc ('k') | chrome/browser/extensions/extensions_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698