| 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 |
| 21 using WebKit::WebBindings; |
| 20 using WebKit::WebCString; | 22 using WebKit::WebCString; |
| 23 using WebKit::WebFrame; |
| 24 using WebKit::WebIntentRequest; |
| 21 using WebKit::WebString; | 25 using WebKit::WebString; |
| 26 using WebKit::WebSerializedScriptValue; |
| 22 | 27 |
| 23 // This class encapsulates the API the Intent object will expose to Javascript. | 28 // 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 | 29 // It is made available to the Javascript runtime in the service page using |
| 25 // NPAPI methods as with plugin/Javascript interaction objects and other | 30 // NPAPI methods as with plugin/Javascript interaction objects and other |
| 26 // browser-provided Javascript API objects on |window|. | 31 // browser-provided Javascript API objects on |window|. |
| 27 class WebIntentsHost::BoundDeliveredIntent : public CppBoundClass { | 32 class WebIntentsHost::BoundDeliveredIntent : public CppBoundClass { |
| 28 public: | 33 public: |
| 29 BoundDeliveredIntent(const string16& action, | 34 BoundDeliveredIntent(const string16& action, |
| 30 const string16& type, | 35 const string16& type, |
| 31 const string16& data, | 36 const string16& data, |
| 32 WebIntentsHost* parent, | 37 WebIntentsHost* parent, |
| 33 WebKit::WebFrame* frame) { | 38 WebFrame* frame) { |
| 34 action_ = WebString(action).utf8(); | 39 action_ = WebString(action).utf8(); |
| 35 type_ = WebString(type).utf8(); | 40 type_ = WebString(type).utf8(); |
| 36 parent_ = parent; | 41 parent_ = parent; |
| 37 | 42 |
| 38 v8::HandleScope scope; | 43 v8::HandleScope scope; |
| 39 v8::Local<v8::Context> ctx = frame->mainWorldScriptContext(); | 44 v8::Local<v8::Context> ctx = frame->mainWorldScriptContext(); |
| 40 v8::Context::Scope cscope(ctx); | 45 v8::Context::Scope cscope(ctx); |
| 41 WebKit::WebSerializedScriptValue ssv = | 46 WebSerializedScriptValue ssv = |
| 42 WebKit::WebSerializedScriptValue::fromString(WebString(data)); | 47 WebSerializedScriptValue::fromString(WebString(data)); |
| 43 // TODO(gbillock): use an exception handler instead? Need to | 48 // TODO(gbillock): use an exception handler instead? Need to |
| 44 // pass back error state to caller? This is a pretty unexpected | 49 // pass back error state to caller? This is a pretty unexpected |
| 45 // internal error... | 50 // internal error... |
| 46 CHECK(!ssv.isNull()); | 51 CHECK(!ssv.isNull()); |
| 47 v8::Local<v8::Value> data_obj = | 52 v8::Local<v8::Value> data_obj = |
| 48 v8::Local<v8::Value>::New(ssv.deserialize()); | 53 v8::Local<v8::Value>::New(ssv.deserialize()); |
| 49 | 54 |
| 50 data_val_.reset(new CppVariant); | 55 data_val_.reset(new CppVariant); |
| 51 WebKit::WebBindings::toNPVariant(data_obj, | 56 WebBindings::toNPVariant(data_obj, frame->windowObject(), data_val_.get()); |
| 52 frame->windowObject(), | |
| 53 data_val_.get()); | |
| 54 | 57 |
| 55 BindGetterCallback("action", base::Bind(&BoundDeliveredIntent::getAction, | 58 BindGetterCallback("action", base::Bind(&BoundDeliveredIntent::getAction, |
| 56 base::Unretained(this))); | 59 base::Unretained(this))); |
| 57 BindGetterCallback("type", base::Bind(&BoundDeliveredIntent::getType, | 60 BindGetterCallback("type", base::Bind(&BoundDeliveredIntent::getType, |
| 58 base::Unretained(this))); | 61 base::Unretained(this))); |
| 59 BindGetterCallback("data", base::Bind(&BoundDeliveredIntent::getData, | 62 BindGetterCallback("data", base::Bind(&BoundDeliveredIntent::getData, |
| 60 base::Unretained(this))); | 63 base::Unretained(this))); |
| 61 BindCallback("postResult", base::Bind(&BoundDeliveredIntent::postResult, | 64 BindCallback("postResult", base::Bind(&BoundDeliveredIntent::postResult, |
| 62 base::Unretained(this))); | 65 base::Unretained(this))); |
| 63 BindCallback("postFailure", base::Bind(&BoundDeliveredIntent::postFailure, | 66 BindCallback("postFailure", base::Bind(&BoundDeliveredIntent::postFailure, |
| 64 base::Unretained(this))); | 67 base::Unretained(this))); |
| 65 } | 68 } |
| 66 | 69 |
| 67 virtual ~BoundDeliveredIntent() { | 70 virtual ~BoundDeliveredIntent() { |
| 68 } | 71 } |
| 69 | 72 |
| 70 WebKit::WebString SerializeCppVariant(const CppVariant& val) { | 73 WebString SerializeCppVariant(const CppVariant& val) { |
| 71 v8::HandleScope scope; | 74 v8::HandleScope scope; |
| 72 v8::Handle<v8::Value> v8obj = WebKit::WebBindings::toV8Value(&val); | 75 v8::Handle<v8::Value> v8obj = WebBindings::toV8Value(&val); |
| 73 | 76 |
| 74 WebKit::WebSerializedScriptValue ssv = | 77 WebSerializedScriptValue ssv = |
| 75 WebKit::WebSerializedScriptValue::serialize(v8obj); | 78 WebSerializedScriptValue::serialize(v8obj); |
| 76 if (ssv.isNull()) | 79 if (ssv.isNull()) |
| 77 return WebKit::WebString(); | 80 return WebKit::WebString(); |
| 78 | 81 |
| 79 return ssv.toString(); | 82 return ssv.toString(); |
| 80 } | 83 } |
| 81 | 84 |
| 82 void postResult(const CppArgumentList& args, CppVariant* retval) { | 85 void postResult(const CppArgumentList& args, CppVariant* retval) { |
| 83 if (args.size() != 1) { | 86 if (args.size() != 1) { |
| 84 WebKit::WebBindings::setException( | 87 WebBindings::setException(NULL, "Must pass one argument to postResult"); |
| 85 NULL, "Must pass one argument to postResult"); | |
| 86 return; | 88 return; |
| 87 } | 89 } |
| 88 | 90 |
| 89 WebString str = SerializeCppVariant(args[0]); | 91 WebString str = SerializeCppVariant(args[0]); |
| 90 parent_->OnResult(str); | 92 parent_->OnResult(str); |
| 91 } | 93 } |
| 92 | 94 |
| 93 void postFailure(const CppArgumentList& args, CppVariant* retval) { | 95 void postFailure(const CppArgumentList& args, CppVariant* retval) { |
| 94 if (args.size() != 1) { | 96 if (args.size() != 1) { |
| 95 WebKit::WebBindings::setException( | 97 WebBindings::setException(NULL, "Must pass one argument to postFailure"); |
| 96 NULL, "Must pass one argument to postFailure"); | |
| 97 return; | 98 return; |
| 98 } | 99 } |
| 99 | 100 |
| 100 WebString str = SerializeCppVariant(args[0]); | 101 WebString str = SerializeCppVariant(args[0]); |
| 101 parent_->OnFailure(str); | 102 parent_->OnFailure(str); |
| 102 } | 103 } |
| 103 | 104 |
| 104 void getAction(CppVariant* result) { | 105 void getAction(CppVariant* result) { |
| 105 std::string action; | 106 std::string action; |
| 106 action.assign(action_.data(), action_.length()); | 107 action.assign(action_.data(), action_.length()); |
| (...skipping 14 matching lines...) Expand all 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 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, WebIntentRequest>::iterator request = |
| 165 intent_requests_.find(intent_id); |
| 166 if (request == intent_requests_.end()) |
| 167 return; |
| 168 WebIntentRequest intent_request = request->second; |
| 169 intent_requests_.erase(request); |
| 170 WebSerializedScriptValue value = |
| 171 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(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 |