summaryrefslogtreecommitdiffstats
path: root/ios/web/web_state/web_state_observer.mm
blob: dcb02044e679dd49b95f3fed1f7d158eb6de3693 (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
// Copyright 2014 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.

#include "ios/web/public/web_state/web_state_observer.h"

#include "ios/web/public/load_committed_details.h"
#include "ios/web/public/web_state/web_state.h"

namespace web {

int WebStateObserver::kInvalidFormKeyCode = -1;

WebStateObserver::WebStateObserver(WebState* web_state) : web_state_(nullptr) {
  Observe(web_state);
}

WebStateObserver::WebStateObserver() : web_state_(nullptr) {}

WebStateObserver::~WebStateObserver() {
  if (web_state_)
    web_state_->RemoveObserver(this);
}

void WebStateObserver::Observe(WebState* web_state) {
  if (web_state == web_state_) {
    // Early exit to avoid infinite loops if we're in the middle of a callback.
    return;
  }
  if (web_state_)
    web_state_->RemoveObserver(this);
  web_state_ = web_state;
  if (web_state_)
    web_state_->AddObserver(this);
}

void WebStateObserver::ResetWebState() {
  web_state_->RemoveObserver(this);
  web_state_ = nullptr;
}

}  // namespace web