summaryrefslogtreecommitdiffstats
path: root/ios/web/web_view_counter_impl.mm
blob: ebd9963619de101618b46d5e3b77cd2e4348d273 (plain)
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
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "ios/web/web_view_counter_impl.h"

#include <stddef.h>

#include "base/logging.h"
#include "ios/web/public/browser_state.h"
#include "ios/web/public/web_thread.h"

namespace web {

namespace {
// A key used to associate a WebViewCounter with a BrowserState.
const char kWebViewCounterKeyName[] = "web_view_counter";
}  // namespace

WebViewCounterImpl::WebViewCounterImpl() {
  DCHECK_CURRENTLY_ON(WebThread::UI);
}

WebViewCounterImpl::~WebViewCounterImpl() {
  DCHECK_CURRENTLY_ON(WebThread::UI);
}

// static
WebViewCounter* WebViewCounter::FromBrowserState(
    web::BrowserState* browser_state) {
  DCHECK_CURRENTLY_ON(WebThread::UI);
  DCHECK(browser_state);

  return WebViewCounterImpl::FromBrowserState(browser_state);
}

// static
WebViewCounterImpl* WebViewCounterImpl::FromBrowserState(
    web::BrowserState* browser_state) {
  DCHECK_CURRENTLY_ON(WebThread::UI);
  DCHECK(browser_state);

  if (!browser_state->GetUserData(kWebViewCounterKeyName)) {
    browser_state->SetUserData(kWebViewCounterKeyName,
                               new WebViewCounterImpl());
  }
  return static_cast<WebViewCounterImpl*>(
      browser_state->GetUserData(kWebViewCounterKeyName));
}

size_t WebViewCounterImpl::GetWKWebViewCount() {
  DCHECK_CURRENTLY_ON(WebThread::UI);
  return wk_web_view_counter_.Size();
}

void WebViewCounterImpl::InsertWKWebView(WKWebView* wk_web_view) {
  DCHECK_CURRENTLY_ON(WebThread::UI);
  DCHECK(wk_web_view);
  DCHECK([wk_web_view isKindOfClass:[WKWebView class]]);

  wk_web_view_counter_.Insert(wk_web_view);
}

}  // namespace web