Fix breakages in https://codereview.chromium.org/1155713003/
[chromium-blink-merge.git] / ios / web / web_state / web_controller_observer_bridge.mm
blob35e3b08f2663414cb5aa3ce03fac291c4113be2d
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
3 // found in the LICENSE file.
5 #import "ios/web/web_state/web_controller_observer_bridge.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/strings/sys_string_conversions.h"
10 #import "ios/web/public/web_state/crw_web_controller_observer.h"
11 #include "ios/web/public/web_state/web_state.h"
13 namespace web {
15 WebControllerObserverBridge::WebControllerObserverBridge(
16     id<CRWWebControllerObserver> web_controller_observer,
17     WebState* web_state,
18     CRWWebController* web_controller)
19     : WebStateObserver(web_state),
20       web_controller_observer_(web_controller_observer),
21       web_controller_(web_controller) {
22   DCHECK(web_controller_observer_);
23   DCHECK(web_controller_);
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   }
37 WebControllerObserverBridge::~WebControllerObserverBridge() {
38   if (!script_command_callback_prefix_.empty())
39     web_state()->RemoveScriptCommandCallback(script_command_callback_prefix_);
42 void WebControllerObserverBridge::PageLoaded(
43     PageLoadCompletionStatus load_completion_status) {
44   if (load_completion_status == PageLoadCompletionStatus::SUCCESS &&
45       [web_controller_observer_ respondsToSelector:@selector(pageLoaded:)])
46     [web_controller_observer_ pageLoaded:web_controller_];
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];
60 }  // namespace web