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/files/file_path.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/weak_ptr.h" 15 #include "chrome/browser/extensions/extension_installer.h" 16 17 class ExtensionService; 18 19 namespace extensions { 20 21 class Extension; 22 class RequirementsChecker; 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 static scoped_refptr<UnpackedInstaller> Create( 33 ExtensionService* extension_service); 34 35 // Loads the extension from the directory |extension_path|, which is 36 // the top directory of a specific extension where its manifest file lives. 37 // Errors are reported through ExtensionErrorReporter. On success, 38 // ExtensionService::AddExtension() is called. 39 void Load(const base::FilePath& extension_path); 40 41 // Loads the extension from the directory |extension_path|; 42 // for use with command line switch --load-extension=path or 43 // --load-and-launch-app=path. 44 // This is equivalent to Load, except that it reads the extension from 45 // |extension_path| synchronously. 46 // The return value indicates whether the installation has begun successfully. 47 // The id of the extension being loaded is returned in |extension_id|. 48 bool LoadFromCommandLine(const base::FilePath& extension_path, 49 std::string* extension_id); 50 51 // Allows prompting for plugins to be disabled; intended for testing only. 52 bool prompt_for_plugins() { return prompt_for_plugins_; } 53 void set_prompt_for_plugins(bool val) { prompt_for_plugins_ = val; } 54 55 // Allows overriding of whether modern manifest versions are required; 56 // intended for testing. 57 bool require_modern_manifest_version() const { 58 return require_modern_manifest_version_; 59 } 60 void set_require_modern_manifest_version(bool val) { 61 require_modern_manifest_version_ = val; 62 } 63 64 private: 65 friend class base::RefCountedThreadSafe<UnpackedInstaller>; 66 67 explicit UnpackedInstaller(ExtensionService* extension_service); 68 virtual ~UnpackedInstaller(); 69 70 // Must be called from the UI thread. 71 void ShowInstallPrompt(); 72 73 // Calls CheckRequirements. 74 void CallCheckRequirements(); 75 76 // Callback from RequirementsChecker. 77 void OnRequirementsChecked(std::vector<std::string> requirement_errors); 78 79 // Verifies if loading unpacked extensions is allowed. 80 bool IsLoadingUnpackedAllowed() const; 81 82 // We change the input extension path to an absolute path, on the file thread. 83 // Then we need to check the file access preference, which needs 84 // to happen back on the UI thread, so it posts CheckExtensionFileAccess on 85 // the UI thread. In turn, once that gets the pref, it goes back to the 86 // file thread with LoadWithFileAccess. 87 // TODO(yoz): It would be nice to remove this ping-pong, but we need to know 88 // what file access flags to pass to extension_file_util::LoadExtension. 89 void GetAbsolutePath(); 90 void CheckExtensionFileAccess(); 91 void LoadWithFileAccess(int flags); 92 93 // Notify the frontend that there was an error loading an extension. 94 void ReportExtensionLoadError(const std::string& error); 95 96 // Called when an unpacked extension has been loaded and installed. 97 void ConfirmInstall(); 98 99 // Helper to get the Extension::CreateFlags for the installing extension. 100 int GetFlags(); 101 102 // The service we will report results back to. 103 base::WeakPtr<ExtensionService> service_weak_; 104 105 // The pathname of the directory to load from, which is an absolute path 106 // after GetAbsolutePath has been called. 107 base::FilePath extension_path_; 108 109 // If true and the extension contains plugins, we prompt the user before 110 // loading. 111 bool prompt_for_plugins_; 112 113 // Whether to require the extension installed to have a modern manifest 114 // version. 115 bool require_modern_manifest_version_; 116 117 // Gives access to common methods and data of an extension installer. 118 ExtensionInstaller installer_; 119 120 DISALLOW_COPY_AND_ASSIGN(UnpackedInstaller); 121 }; 122 123 } // namespace extensions 124 125 #endif // CHROME_BROWSER_EXTENSIONS_UNPACKED_INSTALLER_H_ 126