1 // Copyright 2019 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 
5 #include "content/public/test/back_forward_cache_util.h"
6 
7 #include <map>
8 #include <set>
9 
10 #include "content/browser/renderer_host/back_forward_cache_impl.h"
11 #include "content/browser/renderer_host/render_frame_host_impl.h"
12 #include "content/public/browser/global_routing_id.h"
13 #include "content/public/browser/web_contents.h"
14 
15 namespace content {
16 
17 class BackForwardCacheDisabledTester::Impl
18     : public BackForwardCacheTestDelegate {
19  public:
IsDisabledForFrameWithReason(GlobalFrameRoutingId id,base::StringPiece reason)20   bool IsDisabledForFrameWithReason(GlobalFrameRoutingId id,
21                                     base::StringPiece reason) {
22     return disable_reasons_[id].count(std::string(reason)) != 0;
23   }
24 
OnDisabledForFrameWithReason(GlobalFrameRoutingId id,base::StringPiece reason)25   void OnDisabledForFrameWithReason(GlobalFrameRoutingId id,
26                                     base::StringPiece reason) override {
27     disable_reasons_[id].insert(std::string(reason));
28   }
29 
30  private:
31   std::map<GlobalFrameRoutingId, std::set<std::string>> disable_reasons_;
32 };
33 
BackForwardCacheDisabledTester()34 BackForwardCacheDisabledTester::BackForwardCacheDisabledTester()
35     : impl_(std::make_unique<Impl>()) {}
36 
~BackForwardCacheDisabledTester()37 BackForwardCacheDisabledTester::~BackForwardCacheDisabledTester() {}
38 
IsDisabledForFrameWithReason(int process_id,int frame_routing_id,base::StringPiece reason)39 bool BackForwardCacheDisabledTester::IsDisabledForFrameWithReason(
40     int process_id,
41     int frame_routing_id,
42     base::StringPiece reason) {
43   return impl_->IsDisabledForFrameWithReason(
44       GlobalFrameRoutingId{process_id, frame_routing_id}, reason);
45 }
46 
DisableBackForwardCacheForTesting(WebContents * web_contents,BackForwardCache::DisableForTestingReason reason)47 void DisableBackForwardCacheForTesting(
48     WebContents* web_contents,
49     BackForwardCache::DisableForTestingReason reason) {
50   // Used by tests. Disables BackForwardCache for a given WebContents.
51   web_contents->GetController().GetBackForwardCache().DisableForTesting(reason);
52 }
53 
54 }  // namespace content
55