gaia_config.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2020 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. #ifndef GOOGLE_APIS_GAIA_GAIA_CONFIG_H_
  5. #define GOOGLE_APIS_GAIA_GAIA_CONFIG_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/gtest_prod_util.h"
  9. #include "base/strings/string_piece_forward.h"
  10. #include "base/values.h"
  11. #include "google_apis/google_api_keys.h"
  12. class GURL;
  13. namespace base {
  14. class CommandLine;
  15. class FilePath;
  16. } // namespace base
  17. // Class representing a configuration for Gaia URLs and Google API keys.
  18. // Parses a JSON config file specified by |switches::kGaiaConfigPath| or
  19. // |switches::kGaiaConfigContents| and provides convenient getters for reading
  20. // this config.
  21. //
  22. // The config is represented by a JSON object with the following structure:
  23. // {
  24. // "urls": {
  25. // "gaia_url": {
  26. // "url": "https://accounts.example.com"
  27. // },
  28. // ...
  29. // },
  30. // "api_keys": {
  31. // "GOOGLE_CLIENT_ID_MAIN": "example_key",
  32. // ...
  33. // }
  34. // }
  35. class GaiaConfig {
  36. public:
  37. // Returns a global instance of GaiaConfig.
  38. // This may return nullptr if the config file was not specified by a command
  39. // line parameter.
  40. static GaiaConfig* GetInstance();
  41. // Constructs a new GaiaConfig from a parsed JSON dictionary.
  42. // Prefer GetInstance() over this constructor.
  43. explicit GaiaConfig(base::Value parsed_config);
  44. GaiaConfig(const GaiaConfig&) = delete;
  45. GaiaConfig& operator=(const GaiaConfig&) = delete;
  46. ~GaiaConfig();
  47. // Searches for a URL by |key|.
  48. // Returns true if |key| exists and contains a valid URL. |out_url| will be
  49. // set to that URL.
  50. // Otherwise, returns false. |out_url| will be unmodified.
  51. bool GetURLIfExists(base::StringPiece key, GURL* out_url);
  52. // Searches for an API key, OAuth2 client ID or secret by |key|.
  53. // Returns true if |key| exists and contains a valid string.
  54. // |out_api_key| will be set to that string.
  55. // Otherwise, returns false. |out_api_key| will be unmodified.
  56. bool GetAPIKeyIfExists(base::StringPiece key, std::string* out_api_key);
  57. // Serializes the state of |this| into |command_line|, in a way that
  58. // GaiaConfig::GetInstance() would honor. Internally, it uses switch
  59. // |kGaiaConfigContents| for this purpose, which is appended to
  60. // |*command_line|.
  61. void SerializeContentsToCommandLineSwitch(
  62. base::CommandLine* command_line) const;
  63. // Instantiates this object given |base::CommandLine|, used in tests.
  64. static std::unique_ptr<GaiaConfig> CreateFromCommandLineForTesting(
  65. const base::CommandLine* command_line);
  66. private:
  67. friend class GaiaUrlsTest;
  68. FRIEND_TEST_ALL_PREFIXES(GoogleAPIKeysTest, OverrideAllKeysUsingConfig);
  69. static std::unique_ptr<GaiaConfig>* GetGlobalConfig();
  70. static std::unique_ptr<GaiaConfig> ReadConfigFromString(
  71. const std::string& config_contents);
  72. static std::unique_ptr<GaiaConfig> ReadConfigFromDisk(
  73. const base::FilePath& config_path);
  74. static std::unique_ptr<GaiaConfig> ReadConfigFromCommandLineSwitches(
  75. const base::CommandLine* command_line);
  76. // Re-reads the config from disk and resets the global instance of GaiaConfig.
  77. static void ResetInstanceForTesting();
  78. base::Value parsed_config_;
  79. };
  80. #endif // GOOGLE_APIS_GAIA_GAIA_CONFIG_H_