File: indexed_db_transaction_feature_observer_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (291 lines) | stat: -rw-r--r-- 10,705 bytes parent folder | download | duplicates (5)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/memory/raw_ptr.h"
#include "build/build_config.h"
#include "content/browser/feature_observer.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/feature_observer_client.h"
#include "content/public/common/content_client.h"
#include "content/public/test/back_forward_cache_util.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_content_browser_client.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

#if defined(USE_AURA)
#include "ui/aura/window.h"
#endif

namespace content {

namespace {

// IndexedDB detects that a client is inactive when it is frozen, or if it
// enters BFCache. Since this test suite is testing the freezing part, disable
// BFCache to ensure this test doesn't depend on that functionality to work.
void DisableBackForwardCache(Shell* tab) {
  DisableBackForwardCacheForTesting(tab->web_contents(),
                                    BackForwardCache::TEST_REQUIRES_NO_CACHING);
}

void HideAndFreezeTab(Shell* tab) {
#if defined(USE_AURA)
  // On platforms that use Aura, simply calling WebContentsImpl::WasHidden() is
  // not sufficient, because a navigation will force a recalculation of the
  // occlusion state of all windows. This would switch back the visibility state
  // of the WebContents to "shown" because its window is still visible.
  tab->window()->Hide();
#else
  tab->web_contents()->WasHidden();
#endif

  tab->web_contents()->SetPageFrozen(true);
}

ACTION_P(InvokeClosure, closure) {
  closure.Run();
}

class TestBrowserClient : public ContentBrowserTestContentBrowserClient {
 public:
  explicit TestBrowserClient(FeatureObserverClient* feature_observer_client)
      : feature_observer_client_(feature_observer_client) {}
  ~TestBrowserClient() override = default;

  TestBrowserClient(const TestBrowserClient&) = delete;
  TestBrowserClient& operator=(const TestBrowserClient&) = delete;

  // ContentBrowserClient:
  FeatureObserverClient* GetFeatureObserverClient() override {
    return feature_observer_client_;
  }

 private:
  raw_ptr<FeatureObserverClient> feature_observer_client_;
};

class MockObserverClient : public FeatureObserverClient {
 public:
  MockObserverClient() = default;
  ~MockObserverClient() override = default;

  // FeatureObserverClient implementation:
  MOCK_METHOD2(OnStartUsing,
               void(GlobalRenderFrameHostId id,
                    blink::mojom::ObservedFeatureType type));
  MOCK_METHOD2(OnStopUsing,
               void(GlobalRenderFrameHostId id,
                    blink::mojom::ObservedFeatureType type));
};

class IndexedDBTransactionFeatureObserverBrowserTest
    : public ContentBrowserTest {
 public:
  IndexedDBTransactionFeatureObserverBrowserTest() = default;
  ~IndexedDBTransactionFeatureObserverBrowserTest() override = default;

  IndexedDBTransactionFeatureObserverBrowserTest(
      const IndexedDBTransactionFeatureObserverBrowserTest&) = delete;
  IndexedDBTransactionFeatureObserverBrowserTest& operator=(
      const IndexedDBTransactionFeatureObserverBrowserTest&) = delete;

  RenderFrameHost* GetPrimaryMainFrame(Shell* shell) {
    return shell->web_contents()->GetPrimaryMainFrame();
  }

  MockObserverClient& mock_observer_client() { return mock_observer_client_; }

 private:
  void SetUpOnMainThread() override {
    ContentBrowserTest::SetUpOnMainThread();
    browser_client_ =
        std::make_unique<TestBrowserClient>(&mock_observer_client_);

    host_resolver()->AddRule("a.com", "127.0.0.1");
    server_.ServeFilesFromSourceDirectory(GetTestDataFilePath());
    ASSERT_TRUE(server_.Start());
  }

  testing::StrictMock<MockObserverClient> mock_observer_client_;
  std::unique_ptr<TestBrowserClient> browser_client_;
  net::EmbeddedTestServer server_{net::EmbeddedTestServer::TYPE_HTTPS};
};

}  // namespace

// Validates that we don't get kBlockingIndexedDBLock
// notification when the indexedDB transaction is not blocking.
IN_PROC_BROWSER_TEST_F(
    IndexedDBTransactionFeatureObserverBrowserTest,
    ObserveNoIndexedDBTransactionWhenTransactionIsNotBlocking) {
  ASSERT_TRUE(embedded_test_server()->Start());

  EXPECT_CALL(
      mock_observer_client(),
      OnStartUsing(testing::_,
                   blink::mojom::ObservedFeatureType::kBlockingIndexedDBLock))
      .Times(0);
  EXPECT_CALL(
      mock_observer_client(),
      OnStopUsing(testing::_,
                  blink::mojom::ObservedFeatureType::kBlockingIndexedDBLock))
      .Times(0);

  Shell* tab_holding_locks = shell();
  Shell* tab_acquiring_locks = CreateBrowser();

  DisableBackForwardCache(tab_holding_locks);
  DisableBackForwardCache(tab_acquiring_locks);

  // Navigate the tab holding locks to A and use IndexedDB.
  ASSERT_TRUE(NavigateToURL(
      tab_holding_locks,
      embedded_test_server()->GetURL(
          "a.com", "/back_forward_cache/page_with_indexedDB.html")));

  ASSERT_TRUE(ExecJs(tab_holding_locks, "setupIndexedDBConnection()"));

  // Navigate the tab waiting for locks to A as well and make it request
  // for the same lock.
  ASSERT_TRUE(NavigateToURL(
      tab_acquiring_locks,
      embedded_test_server()->GetURL(
          "a.com", "/back_forward_cache/page_with_indexedDB.html")));

  // There is no kBlockingIndexedDBLock notified to the observer
  ASSERT_TRUE(ExecJs(tab_acquiring_locks, "setupIndexedDBConnection()"));
  ASSERT_TRUE(ExecJs(tab_acquiring_locks, "startIndexedDBTransaction()"));

  // Close the tab holding locks
  tab_acquiring_locks->Close();
}

IN_PROC_BROWSER_TEST_F(IndexedDBTransactionFeatureObserverBrowserTest,
                       ObserveIndexedDBTransactionOnBlockingOthers) {
  ASSERT_TRUE(embedded_test_server()->Start());

  Shell* tab_holding_locks = shell();
  Shell* tab_waiting_for_locks = CreateBrowser();

  DisableBackForwardCache(tab_holding_locks);
  DisableBackForwardCache(tab_waiting_for_locks);

  // Navigate the tab holding locks to A and use IndexedDB.
  ASSERT_TRUE(NavigateToURL(
      tab_holding_locks,
      embedded_test_server()->GetURL(
          "a.com", "/back_forward_cache/page_with_indexedDB.html")));
  GlobalRenderFrameHostId document_holding_locks_id =
      GetPrimaryMainFrame(tab_holding_locks)->GetGlobalId();

  ASSERT_TRUE(ExecJs(tab_holding_locks, "setupIndexedDBConnection()"));

  // Make sure the page keeps holding the lock by running infinite tasks on the
  // object store.
  ASSERT_TRUE(
      ExecJs(tab_holding_locks, "runInfiniteIndexedDBTransactionLoop()"));

  // Freeze the page running the infinite transaction loop.
  HideAndFreezeTab(tab_holding_locks);

  // Navigate the tab waiting for locks to A as well and make it request
  // for the same lock. Since the other tab is holding the lock, this
  // tab will be blocked and waiting for the lock to be released.
  ASSERT_TRUE(NavigateToURL(
      tab_waiting_for_locks,
      embedded_test_server()->GetURL(
          "a.com", "/back_forward_cache/page_with_indexedDB.html")));

  ASSERT_TRUE(ExecJs(tab_waiting_for_locks, "setupIndexedDBConnection()"));

  {
    // Initiating the transaction on the waiting locks, which will raise the
    // OnStartUsing(kBlockingIndexedDBLock) event.
    base::RunLoop run_loop;
    EXPECT_CALL(
        mock_observer_client(),
        OnStartUsing(document_holding_locks_id,
                     blink::mojom::ObservedFeatureType::kBlockingIndexedDBLock))
        .WillOnce(InvokeClosure(run_loop.QuitClosure()));
    ASSERT_TRUE(ExecJs(tab_waiting_for_locks, "startIndexedDBTransaction()"));
    run_loop.Run();
  }

  {
    base::RunLoop run_loop;
    EXPECT_CALL(
        mock_observer_client(),
        OnStopUsing(document_holding_locks_id,
                    blink::mojom::ObservedFeatureType::kBlockingIndexedDBLock))
        .WillOnce(InvokeClosure(run_loop.QuitClosure()));

    // Close the tab holding the lock.
    tab_holding_locks->Close();

    run_loop.Run();
  }
}

IN_PROC_BROWSER_TEST_F(IndexedDBTransactionFeatureObserverBrowserTest,
                       ObserveIndexedDBTransactionOnVersionChange) {
  ASSERT_TRUE(embedded_test_server()->Start());

  Shell* tab_receiving_version_change = shell();
  Shell* tab_sending_version_change = CreateBrowser();

  DisableBackForwardCache(tab_receiving_version_change);
  DisableBackForwardCache(tab_sending_version_change);

  // Navigate the tab receiving version change to A and use IndexedDB.
  ASSERT_TRUE(NavigateToURL(
      tab_receiving_version_change,
      embedded_test_server()->GetURL(
          "a.com", "/back_forward_cache/page_with_indexedDB.html")));

  GlobalRenderFrameHostId document_receiving_version_change_id =
      GetPrimaryMainFrame(tab_receiving_version_change)->GetGlobalId();

  // Create two connection with the same version here
  ASSERT_TRUE(
      ExecJs(tab_receiving_version_change, "setupIndexedDBConnection()"));
  ASSERT_TRUE(ExecJs(tab_receiving_version_change,
                     "setupNewIndexedDBConnectionWithSameVersion()"));

  // Navigate the tab sending version change to the same page, and create a new
  // IndexedDB connection with a higher version.
  ASSERT_TRUE(NavigateToURL(
      tab_sending_version_change,
      embedded_test_server()->GetURL(
          "a.com", "/back_forward_cache/page_with_indexedDB.html")));

  // Running `setupNewIndexedDBConnectionWithHigherVersion()` will trigger the
  // `versionchange` event. This will raise kBlockingIndexedDBLock
  base::RunLoop run_loop;
  EXPECT_CALL(
      mock_observer_client(),
      OnStartUsing(document_receiving_version_change_id,
                   blink::mojom::ObservedFeatureType::kBlockingIndexedDBLock))
      .WillOnce(InvokeClosure(run_loop.QuitClosure()));
  ExecuteScriptAsync(tab_sending_version_change,
                     "setupNewIndexedDBConnectionWithHigherVersion()");
  run_loop.Run();

  // The RenderFrameHost release will notify the observer
  EXPECT_CALL(
      mock_observer_client(),
      OnStopUsing(document_receiving_version_change_id,
                  blink::mojom::ObservedFeatureType::kBlockingIndexedDBLock));

  tab_receiving_version_change->Close();
}

}  // namespace content