shell_nacl_browser_delegate.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2014 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. #include "extensions/shell/browser/shell_nacl_browser_delegate.h"
  5. #include <string>
  6. #include "base/base_paths.h"
  7. #include "base/command_line.h"
  8. #include "base/logging.h"
  9. #include "base/path_service.h"
  10. #include "base/strings/string_split.h"
  11. #include "base/strings/string_util.h"
  12. #include "build/build_config.h"
  13. #include "content/public/browser/browser_context.h"
  14. #include "content/public/browser/browser_thread.h"
  15. #include "content/public/browser/render_frame_host.h"
  16. #include "content/public/browser/site_instance.h"
  17. #include "extensions/browser/extension_registry.h"
  18. #include "extensions/browser/extension_system.h"
  19. #include "extensions/browser/process_manager.h"
  20. #include "extensions/common/constants.h"
  21. #include "extensions/common/extension.h"
  22. #include "extensions/common/url_pattern.h"
  23. #include "extensions/shell/common/version.h" // Generated file.
  24. #include "url/gurl.h"
  25. using content::BrowserContext;
  26. using content::BrowserThread;
  27. using content::BrowserPpapiHost;
  28. namespace extensions {
  29. ShellNaClBrowserDelegate::ShellNaClBrowserDelegate(BrowserContext* context)
  30. : browser_context_(context) {
  31. DCHECK(browser_context_);
  32. }
  33. ShellNaClBrowserDelegate::~ShellNaClBrowserDelegate() {
  34. }
  35. void ShellNaClBrowserDelegate::ShowMissingArchInfobar(int render_process_id,
  36. int render_frame_id) {
  37. // app_shell does not have infobars.
  38. LOG(ERROR) << "Missing architecture for pid " << render_process_id;
  39. }
  40. bool ShellNaClBrowserDelegate::DialogsAreSuppressed() {
  41. return false;
  42. }
  43. bool ShellNaClBrowserDelegate::GetCacheDirectory(base::FilePath* cache_dir) {
  44. // Just use the general cache directory, not a subdirectory like Chrome does.
  45. #if BUILDFLAG(IS_POSIX)
  46. return base::PathService::Get(base::DIR_CACHE, cache_dir);
  47. #elif BUILDFLAG(IS_WIN)
  48. // TODO(yoz): Find an appropriate persistent directory to use here.
  49. return base::PathService::Get(base::DIR_TEMP, cache_dir);
  50. #endif
  51. }
  52. bool ShellNaClBrowserDelegate::GetPluginDirectory(base::FilePath* plugin_dir) {
  53. // On Posix, plugins are in the module directory.
  54. return base::PathService::Get(base::DIR_MODULE, plugin_dir);
  55. }
  56. bool ShellNaClBrowserDelegate::GetPnaclDirectory(base::FilePath* pnacl_dir) {
  57. // On Posix, the pnacl directory is inside the plugin directory.
  58. base::FilePath plugin_dir;
  59. if (!GetPluginDirectory(&plugin_dir))
  60. return false;
  61. *pnacl_dir = plugin_dir.Append(FILE_PATH_LITERAL("pnacl"));
  62. return true;
  63. }
  64. bool ShellNaClBrowserDelegate::GetUserDirectory(base::FilePath* user_dir) {
  65. base::FilePath path = browser_context_->GetPath();
  66. if (!path.empty()) {
  67. *user_dir = path;
  68. return true;
  69. }
  70. return false;
  71. }
  72. std::string ShellNaClBrowserDelegate::GetVersionString() const {
  73. // A version change triggers an update of the NaCl validation caches.
  74. // Example version: "39.0.2129.0 (290550)".
  75. return PRODUCT_VERSION " (" LAST_CHANGE ")";
  76. }
  77. ppapi::host::HostFactory* ShellNaClBrowserDelegate::CreatePpapiHostFactory(
  78. content::BrowserPpapiHost* ppapi_host) {
  79. return NULL;
  80. }
  81. void ShellNaClBrowserDelegate::SetDebugPatterns(
  82. const std::string& debug_patterns) {
  83. // No debugger support. Developers should use Chrome for debugging.
  84. }
  85. bool ShellNaClBrowserDelegate::URLMatchesDebugPatterns(
  86. const GURL& manifest_url) {
  87. // No debugger support. Developers should use Chrome for debugging.
  88. return false;
  89. }
  90. // This function is security sensitive. Be sure to check with a security
  91. // person before you modify it.
  92. // TODO(jamescook): Refactor this code into the extensions module so it can
  93. // be shared with Chrome's NaClBrowserDelegateImpl. http://crbug.com/403017
  94. NaClBrowserDelegate::MapUrlToLocalFilePathCallback
  95. ShellNaClBrowserDelegate::GetMapUrlToLocalFilePathCallback(
  96. const base::FilePath& profile_directory) {
  97. auto extensions = std::make_unique<ExtensionSet>();
  98. extensions->InsertAll(
  99. ExtensionRegistry::Get(browser_context_)->enabled_extensions());
  100. return base::BindRepeating(
  101. [](const ExtensionSet* extensions, const GURL& file_url,
  102. bool use_blocking_api, base::FilePath* file_path) {
  103. // Check that the URL is recognized by the extension system.
  104. const Extension* extension =
  105. extensions->GetExtensionOrAppByURL(file_url);
  106. if (!extension)
  107. return false;
  108. // This is a short-cut which avoids calling a blocking file operation
  109. // (GetFilePath()), so that this can be called on the IO thread. It only
  110. // handles a subset of the urls.
  111. if (!use_blocking_api) {
  112. if (file_url.SchemeIs(kExtensionScheme)) {
  113. std::string path = file_url.path();
  114. base::TrimString(path, "/", &path); // Remove first slash
  115. *file_path = extension->path().AppendASCII(path);
  116. return true;
  117. }
  118. return false;
  119. }
  120. // Check that the URL references a resource in the extension.
  121. // NOTE: app_shell does not support shared modules.
  122. ExtensionResource resource = extension->GetResource(file_url.path());
  123. if (resource.empty())
  124. return false;
  125. // GetFilePath is a blocking function call.
  126. const base::FilePath resource_file_path = resource.GetFilePath();
  127. if (resource_file_path.empty())
  128. return false;
  129. *file_path = resource_file_path;
  130. return true;
  131. },
  132. base::Owned(std::move(extensions)));
  133. }
  134. } // namespace extensions