| 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 #import "ios/web/web_state/web_controller_observer_bridge.h" | 5 #import "ios/web/web_state/web_controller_observer_bridge.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | 7 #include "base/logging.h" |
| 9 #include "base/strings/sys_string_conversions.h" | |
| 10 #import "ios/web/public/web_state/crw_web_controller_observer.h" | 8 #import "ios/web/public/web_state/crw_web_controller_observer.h" |
| 11 #include "ios/web/public/web_state/web_state.h" | |
| 12 | 9 |
| 13 namespace web { | 10 namespace web { |
| 14 | 11 |
| 15 WebControllerObserverBridge::WebControllerObserverBridge( | 12 WebControllerObserverBridge::WebControllerObserverBridge( |
| 16 id<CRWWebControllerObserver> web_controller_observer, | 13 id<CRWWebControllerObserver> web_controller_observer, |
| 17 WebState* web_state, | 14 WebState* web_state, |
| 18 CRWWebController* web_controller) | 15 CRWWebController* web_controller) |
| 19 : WebStateObserver(web_state), | 16 : WebStateObserver(web_state), |
| 20 web_controller_observer_(web_controller_observer), | 17 web_controller_observer_(web_controller_observer), |
| 21 web_controller_(web_controller) { | 18 web_controller_(web_controller) { |
| 22 DCHECK(web_controller_observer_); | 19 DCHECK(web_controller_observer_); |
| 23 DCHECK(web_controller_); | 20 DCHECK(web_controller_); |
| 24 | |
| 25 // Listen for script commands if needed. | |
| 26 if ([web_controller_observer_ respondsToSelector:@selector(commandPrefix)]) { | |
| 27 NSString* prefix = [web_controller_observer_ commandPrefix]; | |
| 28 DCHECK_GT([prefix length], 0u); | |
| 29 script_command_callback_prefix_ = base::SysNSStringToUTF8(prefix); | |
| 30 web_state->AddScriptCommandCallback( | |
| 31 base::Bind(&WebControllerObserverBridge::ScriptCommandReceived, | |
| 32 base::Unretained(this)), | |
| 33 script_command_callback_prefix_); | |
| 34 } | |
| 35 } | 21 } |
| 36 | 22 |
| 37 WebControllerObserverBridge::~WebControllerObserverBridge() { | 23 WebControllerObserverBridge::~WebControllerObserverBridge() { |
| 38 if (!script_command_callback_prefix_.empty()) | |
| 39 web_state()->RemoveScriptCommandCallback(script_command_callback_prefix_); | |
| 40 } | 24 } |
| 41 | 25 |
| 42 void WebControllerObserverBridge::PageLoaded( | 26 void WebControllerObserverBridge::PageLoaded( |
| 43 PageLoadCompletionStatus load_completion_status) { | 27 PageLoadCompletionStatus load_completion_status) { |
| 44 if (load_completion_status == PageLoadCompletionStatus::SUCCESS && | 28 if (load_completion_status == PageLoadCompletionStatus::SUCCESS && |
| 45 [web_controller_observer_ respondsToSelector:@selector(pageLoaded:)]) | 29 [web_controller_observer_ respondsToSelector:@selector(pageLoaded:)]) |
| 46 [web_controller_observer_ pageLoaded:web_controller_]; | 30 [web_controller_observer_ pageLoaded:web_controller_]; |
| 47 } | 31 } |
| 48 | 32 |
| 49 bool WebControllerObserverBridge::ScriptCommandReceived( | |
| 50 const base::DictionaryValue& value, | |
| 51 const GURL& url, | |
| 52 bool user_is_interacting) { | |
| 53 DCHECK(!script_command_callback_prefix_.empty()); | |
| 54 return [web_controller_observer_ handleCommand:value | |
| 55 webController:web_controller_ | |
| 56 userIsInteracting:user_is_interacting | |
| 57 originURL:url]; | |
| 58 } | |
| 59 | |
| 60 } // namespace web | 33 } // namespace web |
| OLD | NEW |