session_preferences.cc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 "atom/browser/session_preferences.h"
  5. #include "atom/common/options_switches.h"
  6. #include "base/command_line.h"
  7. #include "base/memory/ptr_util.h"
  8. namespace atom {
  9. namespace {
  10. #if defined(OS_WIN)
  11. const base::FilePath::CharType kPathDelimiter = FILE_PATH_LITERAL(';');
  12. #else
  13. const base::FilePath::CharType kPathDelimiter = FILE_PATH_LITERAL(':');
  14. #endif
  15. } // namespace
  16. // static
  17. int SessionPreferences::kLocatorKey = 0;
  18. SessionPreferences::SessionPreferences(content::BrowserContext* context) {
  19. context->SetUserData(&kLocatorKey, base::WrapUnique(this));
  20. }
  21. SessionPreferences::~SessionPreferences() {}
  22. // static
  23. SessionPreferences* SessionPreferences::FromBrowserContext(
  24. content::BrowserContext* context) {
  25. return static_cast<SessionPreferences*>(context->GetUserData(&kLocatorKey));
  26. }
  27. // static
  28. void SessionPreferences::AppendExtraCommandLineSwitches(
  29. content::BrowserContext* context,
  30. base::CommandLine* command_line) {
  31. SessionPreferences* self = FromBrowserContext(context);
  32. if (!self)
  33. return;
  34. base::FilePath::StringType preloads;
  35. for (const auto& preload : self->preloads()) {
  36. if (!base::FilePath(preload).IsAbsolute()) {
  37. LOG(ERROR) << "preload script must have absolute path: " << preload;
  38. continue;
  39. }
  40. if (preloads.empty())
  41. preloads = preload;
  42. else
  43. preloads += kPathDelimiter + preload;
  44. }
  45. if (!preloads.empty())
  46. command_line->AppendSwitchNative(switches::kPreloadScripts, preloads);
  47. }
  48. } // namespace atom