File: fake_cws.h

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (127 lines) | stat: -rw-r--r-- 4,855 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_ASH_APP_MODE_FAKE_CWS_H_
#define CHROME_BROWSER_ASH_APP_MODE_FAKE_CWS_H_

#include <map>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <vector>

#include "base/auto_reset.h"
#include "base/functional/callback_forward.h"
#include "extensions/browser/scoped_ignore_content_verifier_for_test.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "url/gurl.h"

namespace ash {

// Simple fake CWS update check request handler that returns a fixed update
// check response. The response is created either from SetUpdateCrx() or
// SetNoUpdate().
class FakeCWS {
 public:
  FakeCWS();
  FakeCWS(const FakeCWS&) = delete;
  FakeCWS& operator=(const FakeCWS&) = delete;
  ~FakeCWS();

  // Initializes as CWS request handler and overrides app gallery command line
  // switches.
  void Init(net::EmbeddedTestServer* embedded_test_server);

  // Initializes a handler in `embedded_test_server` for a private store using
  // the given `web_store_url` and `update_check_end_point`.
  void InitAsPrivateStore(net::EmbeddedTestServer* embedded_test_server,
                          const GURL& web_store_url,
                          std::string_view update_check_end_point);

  // Sets up the update check response with has_update template.
  void SetUpdateCrx(std::string_view app_id,
                    std::string_view crx_file,
                    std::string_view version);

  // Sets up the update check response with no_update template.
  void SetNoUpdate(std::string_view app_id);

  // Set the details to be returned via Chrome Web Store details query.
  void SetAppDetails(std::string_view app_id,
                     std::string localized_name,
                     std::string icon_url,
                     std::string manifest_json);

  // Returns the current `update_check_count_` and resets it.
  int GetUpdateCheckCountAndReset();

 private:
  enum class GalleryUpdateMode {
    kOnlyCommandLine,
    kModifyExtensionsClient,
  };

  struct AppDetails {
    std::string localized_name;
    std::string icon_url;
    std::string manifest_json;
  };

  void SetupWebStoreURL(const GURL& test_server_url);
  void OverrideGalleryCommandlineSwitches();

  bool GetUpdateCheckContent(const std::vector<std::string>& ids,
                             std::string* update_check_content,
                             bool use_json);

  // Creates serialized protobuf string of an item snippet API response. Returns
  // nullopt if the `app_id` is not in `id_to_details_map_`.
  std::optional<std::string> CreateItemSnippetStringForApp(
      const std::string& app_id);

  // Request handler for kiosk app update server.
  std::unique_ptr<net::test_server::HttpResponse> HandleRequest(
      const net::test_server::HttpRequest& request);

  GURL web_store_url_;

  // Used to override the item snippets API URL for the embedded test server.
  GURL item_snippets_url_;
  std::optional<base::AutoReset<const GURL*>> item_snippets_url_override_;

  bool use_private_store_templates_;
  std::string update_check_end_point_;

  // Map keyed by app_id to partially-bound functions that can generate the
  // app's update content.
  std::map<std::string, base::RepeatingCallback<std::string(bool, bool)>>
      id_to_update_check_content_map_;
  int update_check_count_;

  // Map keyed by app_id to app details. These are details returned via a
  // special request to Chrome Web Store and normally used to render app's item
  // in the kiosk app menu. Since test which use them don't rely on these
  // details so far, only two necessary ones are supported at the moment (see
  // the AppDetails struct).
  std::map<std::string, AppDetails> id_to_details_map_;

  // FakeCWS overrides Chrome Web Store URLs, so extensions it provides in tests
  // are considered as extensions from Chrome Web Store. ContentVerifier assumes
  // that Chrome Web Store provides signed hashes for its extensions' resources
  // (a.k.a. verified_contents.json). FakeCWS currently doesn't provide such
  // hashes, so content verification fails with MISSING_ALL_HASHES, which is
  // considered as a corruption. In order to not false-positively detect
  // corruption in test extensions, disable content verification.
  // TODO(https://crbug.com/1051560) Make FakeCWS support this feature of the
  // real Chrome Web Store and remove this scoped ignore.
  std::unique_ptr<extensions::ScopedIgnoreContentVerifierForTest>
      scoped_ignore_content_verifier_;
};

}  // namespace ash

#endif  // CHROME_BROWSER_ASH_APP_MODE_FAKE_CWS_H_