process_util.cc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2021 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/browser/process_util.h"
  5. #include "base/check.h"
  6. #include "content/public/browser/browser_context.h"
  7. #include "extensions/browser/extension_host.h"
  8. #include "extensions/browser/extension_util.h"
  9. #include "extensions/browser/extensions_browser_client.h"
  10. #include "extensions/browser/process_manager.h"
  11. #include "extensions/common/extension.h"
  12. #include "extensions/common/manifest_handlers/background_info.h"
  13. #include "extensions/common/manifest_handlers/incognito_info.h"
  14. namespace extensions {
  15. namespace process_util {
  16. PersistentBackgroundPageState GetPersistentBackgroundPageState(
  17. const Extension& extension,
  18. content::BrowserContext* browser_context) {
  19. // If the extension doesn't have a persistent background page, it can never
  20. // be ready.
  21. if (!BackgroundInfo::HasPersistentBackgroundPage(&extension))
  22. return PersistentBackgroundPageState::kInvalid;
  23. content::BrowserContext* browser_context_to_use = browser_context;
  24. if (browser_context->IsOffTheRecord()) {
  25. // Sanity checks: First check that the extension supports running in
  26. // incognito, according to its manifest.
  27. DCHECK(IncognitoInfo::IsIncognitoAllowed(&extension))
  28. << "Can't use an incognito browser context for an extension that "
  29. "doesn't suppport incognito!";
  30. // Then, check that the user enabled the extension in incognito.
  31. DCHECK(util::IsIncognitoEnabled(extension.id(), browser_context))
  32. << "Can't use an incognito browser context for an extension that isn't "
  33. "allowed to run in incognito!";
  34. // If the extension runs in spanning mode, the background page will be
  35. // associated with the on-the-record context.
  36. if (!IncognitoInfo::IsSplitMode(&extension)) {
  37. browser_context_to_use =
  38. ExtensionsBrowserClient::Get()->GetOriginalContext(browser_context);
  39. }
  40. }
  41. ProcessManager* process_manager = ProcessManager::Get(browser_context_to_use);
  42. DCHECK(process_manager);
  43. ExtensionHost* background_host =
  44. process_manager->GetBackgroundHostForExtension(extension.id());
  45. if (!background_host || !background_host->document_element_available())
  46. return PersistentBackgroundPageState::kNotReady;
  47. return PersistentBackgroundPageState::kReady;
  48. }
  49. } // namespace process_util
  50. } // namespace extensions