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 #include "ios/web/webui/web_ui_ios_impl.h"
7 #include "base/json/json_writer.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/values.h"
11 #include "ios/public/provider/web/web_ui_ios_controller.h"
12 #include "ios/public/provider/web/web_ui_ios_controller_factory.h"
13 #include "ios/public/provider/web/web_ui_ios_message_handler.h"
14 #include "ios/web/web_state/web_state_impl.h"
16 using web::WebUIIOSController;
21 base::string16 WebUIIOS::GetJavascriptCall(
22 const std::string& function_name,
23 const std::vector<const base::Value*>& arg_list) {
24 base::string16 parameters;
26 for (size_t i = 0; i < arg_list.size(); ++i) {
28 parameters += base::char16(',');
30 base::JSONWriter::Write(*arg_list[i], &json);
31 parameters += base::UTF8ToUTF16(json);
33 return base::ASCIIToUTF16(function_name) + base::char16('(') + parameters +
34 base::char16(')') + base::char16(';');
37 WebUIIOSImpl::WebUIIOSImpl(WebStateImpl* web_state) : web_state_(web_state) {
41 WebUIIOSImpl::~WebUIIOSImpl() {
45 // WebUIIOSImpl, public:
46 // ----------------------------------------------------------
48 WebState* WebUIIOSImpl::GetWebState() const {
52 WebUIIOSController* WebUIIOSImpl::GetController() const {
53 return controller_.get();
56 void WebUIIOSImpl::SetController(WebUIIOSController* controller) {
57 controller_.reset(controller);
60 void WebUIIOSImpl::CallJavascriptFunction(const std::string& function_name) {
61 DCHECK(base::IsStringASCII(function_name));
62 base::string16 javascript = base::ASCIIToUTF16(function_name + "();");
63 ExecuteJavascript(javascript);
66 void WebUIIOSImpl::CallJavascriptFunction(const std::string& function_name,
67 const base::Value& arg) {
68 DCHECK(base::IsStringASCII(function_name));
69 std::vector<const base::Value*> args;
71 ExecuteJavascript(GetJavascriptCall(function_name, args));
74 void WebUIIOSImpl::CallJavascriptFunction(const std::string& function_name,
75 const base::Value& arg1,
76 const base::Value& arg2) {
77 DCHECK(base::IsStringASCII(function_name));
78 std::vector<const base::Value*> args;
79 args.push_back(&arg1);
80 args.push_back(&arg2);
81 ExecuteJavascript(GetJavascriptCall(function_name, args));
84 void WebUIIOSImpl::CallJavascriptFunction(const std::string& function_name,
85 const base::Value& arg1,
86 const base::Value& arg2,
87 const base::Value& arg3) {
88 DCHECK(base::IsStringASCII(function_name));
89 std::vector<const base::Value*> args;
90 args.push_back(&arg1);
91 args.push_back(&arg2);
92 args.push_back(&arg3);
93 ExecuteJavascript(GetJavascriptCall(function_name, args));
96 void WebUIIOSImpl::CallJavascriptFunction(const std::string& function_name,
97 const base::Value& arg1,
98 const base::Value& arg2,
99 const base::Value& arg3,
100 const base::Value& arg4) {
101 DCHECK(base::IsStringASCII(function_name));
102 std::vector<const base::Value*> args;
103 args.push_back(&arg1);
104 args.push_back(&arg2);
105 args.push_back(&arg3);
106 args.push_back(&arg4);
107 ExecuteJavascript(GetJavascriptCall(function_name, args));
110 void WebUIIOSImpl::CallJavascriptFunction(
111 const std::string& function_name,
112 const std::vector<const base::Value*>& args) {
113 DCHECK(base::IsStringASCII(function_name));
114 ExecuteJavascript(GetJavascriptCall(function_name, args));
117 void WebUIIOSImpl::RegisterMessageCallback(const std::string& message,
118 const MessageCallback& callback) {
119 message_callbacks_.insert(std::make_pair(message, callback));
122 void WebUIIOSImpl::ProcessWebUIIOSMessage(const GURL& source_url,
123 const std::string& message,
124 const base::ListValue& args) {
125 if (controller_->OverrideHandleWebUIIOSMessage(source_url, message, args))
128 // Look up the callback for this message.
129 MessageCallbackMap::const_iterator callback =
130 message_callbacks_.find(message);
131 if (callback != message_callbacks_.end()) {
132 // Forward this message and content on.
133 callback->second.Run(&args);
137 // WebUIIOSImpl, protected:
138 // -------------------------------------------------------
140 void WebUIIOSImpl::AddMessageHandler(WebUIIOSMessageHandler* handler) {
141 DCHECK(!handler->web_ui());
142 handler->set_web_ui(this);
143 handler->RegisterMessages();
144 handlers_.push_back(handler);
147 void WebUIIOSImpl::ExecuteJavascript(const base::string16& javascript) {
148 web_state_->ExecuteJavaScriptAsync(javascript);