session_preferences.cc 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright (c) 2017 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/session_preferences.h"
  5. #include "base/logging.h"
  6. #include "base/memory/ptr_util.h"
  7. #include "content/public/browser/browser_context.h"
  8. namespace electron {
  9. // static
  10. int SessionPreferences::kLocatorKey = 0;
  11. // static
  12. void SessionPreferences::CreateForBrowserContext(
  13. content::BrowserContext* context) {
  14. DCHECK(context);
  15. context->SetUserData(&kLocatorKey,
  16. base::WrapUnique(new SessionPreferences{}));
  17. }
  18. SessionPreferences::SessionPreferences() = default;
  19. SessionPreferences::~SessionPreferences() = default;
  20. // static
  21. SessionPreferences* SessionPreferences::FromBrowserContext(
  22. content::BrowserContext* context) {
  23. return static_cast<SessionPreferences*>(context->GetUserData(&kLocatorKey));
  24. }
  25. // static
  26. std::vector<base::FilePath> SessionPreferences::GetValidPreloads(
  27. content::BrowserContext* context) {
  28. std::vector<base::FilePath> result;
  29. if (auto* self = FromBrowserContext(context)) {
  30. for (const auto& preload : self->preloads()) {
  31. if (preload.IsAbsolute()) {
  32. result.emplace_back(preload);
  33. } else {
  34. LOG(ERROR) << "preload script must have absolute path: " << preload;
  35. }
  36. }
  37. }
  38. return result;
  39. }
  40. } // namespace electron