session_preferences.cc 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. }
  23. // static
  24. SessionPreferences* SessionPreferences::FromBrowserContext(
  25. content::BrowserContext* context) {
  26. return static_cast<SessionPreferences*>(context->GetUserData(&kLocatorKey));
  27. }
  28. // static
  29. void SessionPreferences::AppendExtraCommandLineSwitches(
  30. content::BrowserContext* context, 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