OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ios/web/public/web_state/web_state_observer.h" | 5 #include "ios/web/public/web_state/web_state_observer.h" |
6 | 6 |
7 #include "ios/web/public/load_committed_details.h" | 7 #include "ios/web/public/load_committed_details.h" |
8 #include "ios/web/public/web_state/web_state.h" | 8 #include "ios/web/public/web_state/web_state.h" |
9 | 9 |
10 namespace web { | 10 namespace web { |
11 | 11 |
12 int WebStateObserver::kInvalidFormKeyCode = -1; | |
13 | |
14 WebStateObserver::WebStateObserver(WebState* web_state) : web_state_(nullptr) { | 12 WebStateObserver::WebStateObserver(WebState* web_state) : web_state_(nullptr) { |
15 Observe(web_state); | 13 Observe(web_state); |
16 } | 14 } |
17 | 15 |
18 WebStateObserver::WebStateObserver() : web_state_(nullptr) {} | 16 WebStateObserver::WebStateObserver() : web_state_(nullptr) {} |
19 | 17 |
20 WebStateObserver::~WebStateObserver() { | 18 WebStateObserver::~WebStateObserver() { |
21 if (web_state_) | 19 if (web_state_) |
22 web_state_->RemoveObserver(this); | 20 web_state_->RemoveObserver(this); |
23 } | 21 } |
24 | 22 |
25 void WebStateObserver::Observe(WebState* web_state) { | 23 void WebStateObserver::Observe(WebState* web_state) { |
26 if (web_state == web_state_) { | 24 if (web_state == web_state_) { |
27 // Early exit to avoid infinite loops if we're in the middle of a callback. | 25 // Early exit to avoid infinite loops if we're in the middle of a callback. |
28 return; | 26 return; |
29 } | 27 } |
30 if (web_state_) | 28 if (web_state_) |
31 web_state_->RemoveObserver(this); | 29 web_state_->RemoveObserver(this); |
32 web_state_ = web_state; | 30 web_state_ = web_state; |
33 if (web_state_) | 31 if (web_state_) |
34 web_state_->AddObserver(this); | 32 web_state_->AddObserver(this); |
35 } | 33 } |
36 | 34 |
37 void WebStateObserver::ResetWebState() { | 35 void WebStateObserver::ResetWebState() { |
38 web_state_->RemoveObserver(this); | 36 web_state_->RemoveObserver(this); |
39 web_state_ = nullptr; | 37 web_state_ = nullptr; |
40 } | 38 } |
41 | 39 |
42 } // namespace web | 40 } // namespace web |
OLD | NEW |