content_hash_fetcher.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2014 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 EXTENSIONS_BROWSER_CONTENT_HASH_FETCHER_H_
  5. #define EXTENSIONS_BROWSER_CONTENT_HASH_FETCHER_H_
  6. #include <map>
  7. #include <set>
  8. #include <string>
  9. #include <utility>
  10. #include "base/callback.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "extensions/browser/content_verifier/content_hash.h"
  13. #include "extensions/common/extension_id.h"
  14. namespace base {
  15. class SequencedTaskRunner;
  16. }
  17. namespace network {
  18. class SimpleURLLoader;
  19. } // namespace network
  20. namespace extensions {
  21. namespace internals {
  22. // This class is responsible for getting signed expected hashes for use in
  23. // extension content verification.
  24. //
  25. // This class takes care of doing the network I/O work to ensure we
  26. // have the contents of verified_contents.json files from the webstore.
  27. //
  28. // Note: This class manages its own lifetime. It deletes itself when
  29. // Start() completes at OnSimpleLoaderComplete().
  30. //
  31. // Note: This class is an internal implementation detail of ContentHash and is
  32. // not be used independently.
  33. // TODO(lazyboy): Consider changing BUILD rules to enforce the above, yet
  34. // keeping the class unit testable.
  35. class ContentHashFetcher {
  36. public:
  37. // A callback for when fetch is complete.
  38. // The response contents is passed through std::unique_ptr<std::string>. In
  39. // case of failure the error code is passed as a last argument.
  40. using HashFetcherCallback =
  41. base::OnceCallback<void(ContentHash::FetchKey,
  42. std::unique_ptr<std::string>,
  43. ContentHash::FetchErrorCode)>;
  44. ContentHashFetcher(ContentHash::FetchKey fetch_key);
  45. ContentHashFetcher(const ContentHashFetcher&) = delete;
  46. ContentHashFetcher& operator=(const ContentHashFetcher&) = delete;
  47. // Note: |this| is deleted once OnSimpleLoaderComplete() completes.
  48. void Start(HashFetcherCallback hash_fetcher_callback);
  49. private:
  50. friend class base::RefCounted<ContentHashFetcher>;
  51. ~ContentHashFetcher();
  52. void OnSimpleLoaderComplete(std::unique_ptr<std::string> response_body);
  53. ContentHash::FetchKey fetch_key_;
  54. HashFetcherCallback hash_fetcher_callback_;
  55. scoped_refptr<base::SequencedTaskRunner> response_task_runner_;
  56. // Alive when url fetch is ongoing.
  57. std::unique_ptr<network::SimpleURLLoader> simple_loader_;
  58. SEQUENCE_CHECKER(sequence_checker_);
  59. };
  60. } // namespace internals
  61. } // namespace extensions
  62. #endif // EXTENSIONS_BROWSER_CONTENT_HASH_FETCHER_H_