| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/renderer/intents_dispatcher.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "content/common/intents_messages.h" | |
| 10 #include "content/renderer/render_view_impl.h" | |
| 11 #include "ipc/ipc_message.h" | |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" | |
| 13 #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" | |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
| 17 #include "v8/include/v8.h" | |
| 18 #include "webkit/glue/cpp_bound_class.h" | |
| 19 | |
| 20 using WebKit::WebCString; | |
| 21 using WebKit::WebString; | |
| 22 | |
| 23 // 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 // NPAPI methods as with plugin/Javascript interaction objects and other | |
| 26 // browser-provided Javascript API objects on |window|. | |
| 27 class IntentsDispatcher::BoundDeliveredIntent : public CppBoundClass { | |
| 28 public: | |
| 29 BoundDeliveredIntent(const string16& action, | |
| 30 const string16& type, | |
| 31 const string16& data, | |
| 32 IntentsDispatcher* parent, | |
| 33 WebKit::WebFrame* frame) { | |
| 34 action_ = WebString(action).utf8(); | |
| 35 type_ = WebString(type).utf8(); | |
| 36 parent_ = parent; | |
| 37 | |
| 38 v8::HandleScope scope; | |
| 39 v8::Local<v8::Context> ctx = frame->mainWorldScriptContext(); | |
| 40 v8::Context::Scope cscope(ctx); | |
| 41 WebKit::WebSerializedScriptValue ssv = | |
| 42 WebKit::WebSerializedScriptValue::fromString(WebString(data)); | |
| 43 // TODO(gbillock): use an exception handler instead? Need to | |
| 44 // pass back error state to caller? This is a pretty unexpected | |
| 45 // internal error... | |
| 46 CHECK(!ssv.isNull()); | |
| 47 v8::Local<v8::Value> data_obj = | |
| 48 v8::Local<v8::Value>::New(ssv.deserialize()); | |
| 49 | |
| 50 data_val_.reset(new CppVariant); | |
| 51 WebKit::WebBindings::toNPVariant(data_obj, | |
| 52 frame->windowObject(), | |
| 53 data_val_.get()); | |
| 54 | |
| 55 BindGetterCallback("action", base::Bind(&BoundDeliveredIntent::getAction, | |
| 56 base::Unretained(this))); | |
| 57 BindGetterCallback("type", base::Bind(&BoundDeliveredIntent::getType, | |
| 58 base::Unretained(this))); | |
| 59 BindGetterCallback("data", base::Bind(&BoundDeliveredIntent::getData, | |
| 60 base::Unretained(this))); | |
| 61 BindCallback("postResult", base::Bind(&BoundDeliveredIntent::postResult, | |
| 62 base::Unretained(this))); | |
| 63 BindCallback("postFailure", base::Bind(&BoundDeliveredIntent::postFailure, | |
| 64 base::Unretained(this))); | |
| 65 } | |
| 66 | |
| 67 virtual ~BoundDeliveredIntent() { | |
| 68 } | |
| 69 | |
| 70 WebKit::WebString SerializeCppVariant(const CppVariant& val) { | |
| 71 v8::HandleScope scope; | |
| 72 v8::Handle<v8::Value> v8obj = WebKit::WebBindings::toV8Value(&val); | |
| 73 | |
| 74 WebKit::WebSerializedScriptValue ssv = | |
| 75 WebKit::WebSerializedScriptValue::serialize(v8obj); | |
| 76 if (ssv.isNull()) | |
| 77 return WebKit::WebString(); | |
| 78 | |
| 79 return ssv.toString(); | |
| 80 } | |
| 81 | |
| 82 void postResult(const CppArgumentList& args, CppVariant* retval) { | |
| 83 if (args.size() != 1) { | |
| 84 WebKit::WebBindings::setException( | |
| 85 NULL, "Must pass one argument to postResult"); | |
| 86 return; | |
| 87 } | |
| 88 | |
| 89 WebString str = SerializeCppVariant(args[0]); | |
| 90 parent_->OnResult(str); | |
| 91 } | |
| 92 | |
| 93 void postFailure(const CppArgumentList& args, CppVariant* retval) { | |
| 94 if (args.size() != 1) { | |
| 95 WebKit::WebBindings::setException( | |
| 96 NULL, "Must pass one argument to postFailure"); | |
| 97 return; | |
| 98 } | |
| 99 | |
| 100 WebString str = SerializeCppVariant(args[0]); | |
| 101 parent_->OnFailure(str); | |
| 102 } | |
| 103 | |
| 104 void getAction(CppVariant* result) { | |
| 105 std::string action; | |
| 106 action.assign(action_.data(), action_.length()); | |
| 107 result->Set(action); | |
| 108 } | |
| 109 | |
| 110 void getType(CppVariant* result) { | |
| 111 std::string type; | |
| 112 type.assign(type_.data(), type_.length()); | |
| 113 result->Set(type); | |
| 114 } | |
| 115 | |
| 116 void getData(CppVariant* result) { | |
| 117 result->Set(*data_val_.get()); | |
| 118 } | |
| 119 | |
| 120 private: | |
| 121 // Intent data suitable for surfacing to Javascript callers. | |
| 122 WebCString action_; | |
| 123 WebCString type_; | |
| 124 scoped_ptr<CppVariant> data_val_; | |
| 125 | |
| 126 // The dispatcher object, for forwarding postResult/postFailure calls. | |
| 127 IntentsDispatcher* parent_; | |
| 128 }; | |
| 129 | |
| 130 IntentsDispatcher::IntentsDispatcher(RenderViewImpl* render_view) | |
| 131 : content::RenderViewObserver(render_view) { | |
| 132 } | |
| 133 | |
| 134 IntentsDispatcher::~IntentsDispatcher() {} | |
| 135 | |
| 136 bool IntentsDispatcher::OnMessageReceived(const IPC::Message& message) { | |
| 137 bool handled = true; | |
| 138 IPC_BEGIN_MESSAGE_MAP(IntentsDispatcher, message) | |
| 139 IPC_MESSAGE_HANDLER(IntentsMsg_SetWebIntentData, OnSetIntent) | |
| 140 IPC_MESSAGE_HANDLER(IntentsMsg_WebIntentReply, OnWebIntentReply); | |
| 141 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 142 IPC_END_MESSAGE_MAP() | |
| 143 return handled; | |
| 144 } | |
| 145 | |
| 146 void IntentsDispatcher::OnSetIntent(const webkit_glue::WebIntentData& intent) { | |
| 147 intent_.reset(new webkit_glue::WebIntentData(intent)); | |
| 148 } | |
| 149 | |
| 150 void IntentsDispatcher::OnWebIntentReply( | |
| 151 webkit_glue::WebIntentReplyType reply_type, | |
| 152 const WebKit::WebString& data, | |
| 153 int intent_id) { | |
| 154 | |
| 155 if (reply_type == webkit_glue::WEB_INTENT_REPLY_SUCCESS) { | |
| 156 render_view()->GetWebView()->mainFrame()->handleIntentResult( | |
| 157 intent_id, data); | |
| 158 } else { | |
| 159 render_view()->GetWebView()->mainFrame()->handleIntentFailure( | |
| 160 intent_id, data); | |
| 161 } | |
| 162 } | |
| 163 | |
| 164 void IntentsDispatcher::OnResult(const WebKit::WebString& data) { | |
| 165 Send(new IntentsHostMsg_WebIntentReply( | |
| 166 routing_id(), webkit_glue::WEB_INTENT_REPLY_SUCCESS, data)); | |
| 167 } | |
| 168 | |
| 169 void IntentsDispatcher::OnFailure(const WebKit::WebString& data) { | |
| 170 Send(new IntentsHostMsg_WebIntentReply( | |
| 171 routing_id(), webkit_glue::WEB_INTENT_REPLY_FAILURE, data)); | |
| 172 } | |
| 173 | |
| 174 // 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 | |
| 176 // sub-frames. TODO(gbillock): This policy needs to be fine-tuned and | |
| 177 // documented. | |
| 178 void IntentsDispatcher::DidClearWindowObject(WebKit::WebFrame* frame) { | |
| 179 if (intent_.get() == NULL || frame->top() != frame) | |
| 180 return; | |
| 181 | |
| 182 delivered_intent_.reset(new BoundDeliveredIntent( | |
| 183 intent_->action, intent_->type, intent_->data, this, frame)); | |
| 184 delivered_intent_->BindToJavascript(frame, "intent"); | |
| 185 } | |
| OLD | NEW |