1 // Copyright (c) 2012 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_EXTENSIONS_UNPACKED_INSTALLER_H_ 6 #define CHROME_BROWSER_EXTENSIONS_UNPACKED_INSTALLER_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/bind.h" 12 #include "base/files/file_path.h" 13 #include "base/memory/ref_counted.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/weak_ptr.h" 16 #include "chrome/browser/extensions/extension_install_checker.h" 17 18 class ExtensionService; 19 20 namespace extensions { 21 22 class Extension; 23 24 // Installs and loads an unpacked extension. Because internal state needs to be 25 // held about the instalation process, only one call to Load*() should be made 26 // per UnpackedInstaller. 27 // TODO(erikkay): It might be useful to be able to load a packed extension 28 // (presumably into memory) without installing it. 29 class UnpackedInstaller 30 : public base::RefCountedThreadSafe<UnpackedInstaller> { 31 public: 32 typedef base::Callback<void(const base::FilePath&, const std::string&)> 33 OnFailureCallback; 34 35 static scoped_refptr<UnpackedInstaller> Create( 36 ExtensionService* extension_service); 37 38 // Loads the extension from the directory |extension_path|, which is 39 // the top directory of a specific extension where its manifest file lives. 40 // Errors are reported through ExtensionErrorReporter. On success, 41 // ExtensionService::AddExtension() is called. 42 void Load(const base::FilePath& extension_path); 43 44 // Loads the extension from the directory |extension_path|; 45 // for use with command line switch --load-extension=path or 46 // --load-and-launch-app=path. 47 // This is equivalent to Load, except that it reads the extension from 48 // |extension_path| synchronously. 49 // The return value indicates whether the installation has begun successfully. 50 // The id of the extension being loaded is returned in |extension_id|. 51 bool LoadFromCommandLine(const base::FilePath& extension_path, 52 std::string* extension_id); 53 54 // Allows prompting for plugins to be disabled; intended for testing only. prompt_for_plugins()55 bool prompt_for_plugins() { return prompt_for_plugins_; } set_prompt_for_plugins(bool val)56 void set_prompt_for_plugins(bool val) { prompt_for_plugins_ = val; } 57 58 // Allows overriding of whether modern manifest versions are required; 59 // intended for testing. require_modern_manifest_version()60 bool require_modern_manifest_version() const { 61 return require_modern_manifest_version_; 62 } set_require_modern_manifest_version(bool val)63 void set_require_modern_manifest_version(bool val) { 64 require_modern_manifest_version_ = val; 65 } 66 set_be_noisy_on_failure(bool be_noisy_on_failure)67 void set_be_noisy_on_failure(bool be_noisy_on_failure) { 68 be_noisy_on_failure_ = be_noisy_on_failure; 69 } 70 71 private: 72 friend class base::RefCountedThreadSafe<UnpackedInstaller>; 73 74 explicit UnpackedInstaller(ExtensionService* extension_service); 75 virtual ~UnpackedInstaller(); 76 77 // Must be called from the UI thread. 78 void ShowInstallPrompt(); 79 80 // Begin management policy and requirements checks. 81 void StartInstallChecks(); 82 83 // Callback from ExtensionInstallChecker. 84 void OnInstallChecksComplete(int failed_checks); 85 86 // Verifies if loading unpacked extensions is allowed. 87 bool IsLoadingUnpackedAllowed() const; 88 89 // We change the input extension path to an absolute path, on the file thread. 90 // Then we need to check the file access preference, which needs 91 // to happen back on the UI thread, so it posts CheckExtensionFileAccess on 92 // the UI thread. In turn, once that gets the pref, it goes back to the 93 // file thread with LoadWithFileAccess. 94 // TODO(yoz): It would be nice to remove this ping-pong, but we need to know 95 // what file access flags to pass to file_util::LoadExtension. 96 void GetAbsolutePath(); 97 void CheckExtensionFileAccess(); 98 void LoadWithFileAccess(int flags); 99 100 // Notify the frontend that an attempt to retry will not be necessary. 101 void UnregisterLoadRetryListener(); 102 103 // Notify the frontend that there was an error loading an extension. 104 void ReportExtensionLoadError(const std::string& error); 105 106 // Passes the extension onto extension service. 107 void InstallExtension(); 108 109 // Helper to get the Extension::CreateFlags for the installing extension. 110 int GetFlags(); 111 extension()112 const Extension* extension() { return install_checker_.extension().get(); } 113 114 // The service we will report results back to. 115 base::WeakPtr<ExtensionService> service_weak_; 116 117 // The pathname of the directory to load from, which is an absolute path 118 // after GetAbsolutePath has been called. 119 base::FilePath extension_path_; 120 121 // If true and the extension contains plugins, we prompt the user before 122 // loading. 123 bool prompt_for_plugins_; 124 125 // Whether to require the extension installed to have a modern manifest 126 // version. 127 bool require_modern_manifest_version_; 128 129 // Whether or not to be noisy (show a dialog) on failure. Defaults to true. 130 bool be_noisy_on_failure_; 131 132 // Checks management policies and requirements before the extension can be 133 // installed. 134 ExtensionInstallChecker install_checker_; 135 136 DISALLOW_COPY_AND_ASSIGN(UnpackedInstaller); 137 }; 138 139 } // namespace extensions 140 141 #endif // CHROME_BROWSER_EXTENSIONS_UNPACKED_INSTALLER_H_ 142