| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "content/renderer/web_intents_host.h" | 5 #include "content/renderer/web_intents_host.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "content/common/intents_messages.h" | 9 #include "content/common/intents_messages.h" |
| 10 #include "content/renderer/render_view_impl.h" | 10 #include "content/renderer/render_view_impl.h" |
| 11 #include "ipc/ipc_message.h" | 11 #include "ipc/ipc_message.h" |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" | 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebIntentRequest.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h
" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h
" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSerialize
dScriptValue.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSerialize
dScriptValue.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
| 17 #include "v8/include/v8.h" | 18 #include "v8/include/v8.h" |
| 18 #include "webkit/glue/cpp_bound_class.h" | 19 #include "webkit/glue/cpp_bound_class.h" |
| 19 | 20 |
| 20 using WebKit::WebCString; | 21 using WebKit::WebCString; |
| 21 using WebKit::WebString; | 22 using WebKit::WebString; |
| 22 | 23 |
| 23 // This class encapsulates the API the Intent object will expose to Javascript. | 24 // This class encapsulates the API the Intent object will expose to Javascript. |
| 24 // It is made available to the Javascript runtime in the service page using | 25 // It is made available to the Javascript runtime in the service page using |
| 25 // NPAPI methods as with plugin/Javascript interaction objects and other | 26 // NPAPI methods as with plugin/Javascript interaction objects and other |
| 26 // browser-provided Javascript API objects on |window|. | 27 // browser-provided Javascript API objects on |window|. |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 // Intent data suitable for surfacing to Javascript callers. | 122 // Intent data suitable for surfacing to Javascript callers. |
| 122 WebCString action_; | 123 WebCString action_; |
| 123 WebCString type_; | 124 WebCString type_; |
| 124 scoped_ptr<CppVariant> data_val_; | 125 scoped_ptr<CppVariant> data_val_; |
| 125 | 126 |
| 126 // The dispatcher object, for forwarding postResult/postFailure calls. | 127 // The dispatcher object, for forwarding postResult/postFailure calls. |
| 127 WebIntentsHost* parent_; | 128 WebIntentsHost* parent_; |
| 128 }; | 129 }; |
| 129 | 130 |
| 130 WebIntentsHost::WebIntentsHost(RenderViewImpl* render_view) | 131 WebIntentsHost::WebIntentsHost(RenderViewImpl* render_view) |
| 131 : content::RenderViewObserver(render_view) { | 132 : content::RenderViewObserver(render_view), |
| 133 id_counter_(0) { |
| 132 } | 134 } |
| 133 | 135 |
| 134 WebIntentsHost::~WebIntentsHost() {} | 136 WebIntentsHost::~WebIntentsHost() { |
| 137 } |
| 138 |
| 139 int WebIntentsHost::RegisterWebIntent( |
| 140 const WebKit::WebIntentRequest& request) { |
| 141 int id = id_counter_++; |
| 142 intent_requests_[id] = request; |
| 143 return id; |
| 144 } |
| 135 | 145 |
| 136 bool WebIntentsHost::OnMessageReceived(const IPC::Message& message) { | 146 bool WebIntentsHost::OnMessageReceived(const IPC::Message& message) { |
| 137 bool handled = true; | 147 bool handled = true; |
| 138 IPC_BEGIN_MESSAGE_MAP(WebIntentsHost, message) | 148 IPC_BEGIN_MESSAGE_MAP(WebIntentsHost, message) |
| 139 IPC_MESSAGE_HANDLER(IntentsMsg_SetWebIntentData, OnSetIntent) | 149 IPC_MESSAGE_HANDLER(IntentsMsg_SetWebIntentData, OnSetIntent) |
| 140 IPC_MESSAGE_HANDLER(IntentsMsg_WebIntentReply, OnWebIntentReply); | 150 IPC_MESSAGE_HANDLER(IntentsMsg_WebIntentReply, OnWebIntentReply); |
| 141 IPC_MESSAGE_UNHANDLED(handled = false) | 151 IPC_MESSAGE_UNHANDLED(handled = false) |
| 142 IPC_END_MESSAGE_MAP() | 152 IPC_END_MESSAGE_MAP() |
| 143 return handled; | 153 return handled; |
| 144 } | 154 } |
| 145 | 155 |
| 146 void WebIntentsHost::OnSetIntent(const webkit_glue::WebIntentData& intent) { | 156 void WebIntentsHost::OnSetIntent(const webkit_glue::WebIntentData& intent) { |
| 147 intent_.reset(new webkit_glue::WebIntentData(intent)); | 157 intent_.reset(new webkit_glue::WebIntentData(intent)); |
| 148 } | 158 } |
| 149 | 159 |
| 150 void WebIntentsHost::OnWebIntentReply( | 160 void WebIntentsHost::OnWebIntentReply( |
| 151 webkit_glue::WebIntentReplyType reply_type, | 161 webkit_glue::WebIntentReplyType reply_type, |
| 152 const WebKit::WebString& data, | 162 const WebKit::WebString& data, |
| 153 int intent_id) { | 163 int intent_id) { |
| 164 std::map<int, WebKit::WebIntentRequest>::iterator request = |
| 165 intent_requests_.find(intent_id); |
| 166 if (request == intent_requests_.end()) |
| 167 return; |
| 168 WebKit::WebIntentRequest intent_request = request->second; |
| 169 intent_requests_.erase(request); |
| 170 WebKit::WebSerializedScriptValue value = |
| 171 WebKit::WebSerializedScriptValue::fromString(data); |
| 154 | 172 |
| 155 if (reply_type == webkit_glue::WEB_INTENT_REPLY_SUCCESS) { | 173 if (reply_type == webkit_glue::WEB_INTENT_REPLY_SUCCESS) { |
| 156 render_view()->GetWebView()->mainFrame()->handleIntentResult( | 174 intent_request.postResult(value); |
| 157 intent_id, data); | |
| 158 } else { | 175 } else { |
| 159 render_view()->GetWebView()->mainFrame()->handleIntentFailure( | 176 intent_request.postFailure(value); |
| 160 intent_id, data); | |
| 161 } | 177 } |
| 162 } | 178 } |
| 163 | 179 |
| 164 void WebIntentsHost::OnResult(const WebKit::WebString& data) { | 180 void WebIntentsHost::OnResult(const WebKit::WebString& data) { |
| 165 Send(new IntentsHostMsg_WebIntentReply( | 181 Send(new IntentsHostMsg_WebIntentReply( |
| 166 routing_id(), webkit_glue::WEB_INTENT_REPLY_SUCCESS, data)); | 182 routing_id(), webkit_glue::WEB_INTENT_REPLY_SUCCESS, data)); |
| 167 } | 183 } |
| 168 | 184 |
| 169 void WebIntentsHost::OnFailure(const WebKit::WebString& data) { | 185 void WebIntentsHost::OnFailure(const WebKit::WebString& data) { |
| 170 Send(new IntentsHostMsg_WebIntentReply( | 186 Send(new IntentsHostMsg_WebIntentReply( |
| 171 routing_id(), webkit_glue::WEB_INTENT_REPLY_FAILURE, data)); | 187 routing_id(), webkit_glue::WEB_INTENT_REPLY_FAILURE, data)); |
| 172 } | 188 } |
| 173 | 189 |
| 174 // We set the intent payload into all top-level frame window objects. This | 190 // We set the intent payload into all top-level frame window objects. This |
| 175 // should persist the data through redirects, and not deliver it to any | 191 // should persist the data through redirects, and not deliver it to any |
| 176 // sub-frames. TODO(gbillock): This policy needs to be fine-tuned and | 192 // sub-frames. TODO(gbillock): This policy needs to be fine-tuned and |
| 177 // documented. | 193 // documented. |
| 178 void WebIntentsHost::DidClearWindowObject(WebKit::WebFrame* frame) { | 194 void WebIntentsHost::DidClearWindowObject(WebKit::WebFrame* frame) { |
| 179 if (intent_.get() == NULL || frame->top() != frame) | 195 if (intent_.get() == NULL || frame->top() != frame) |
| 180 return; | 196 return; |
| 181 | 197 |
| 182 delivered_intent_.reset(new BoundDeliveredIntent( | 198 delivered_intent_.reset(new BoundDeliveredIntent( |
| 183 intent_->action, intent_->type, intent_->data, this, frame)); | 199 intent_->action, intent_->type, intent_->data, this, frame)); |
| 184 delivered_intent_->BindToJavascript(frame, "intent"); | 200 delivered_intent_->BindToJavascript(frame, "intent"); |
| 185 } | 201 } |
| OLD | NEW |