| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/utf_string_conversions.h" |
| 9 #include "content/common/intents_messages.h" | 10 #include "content/common/intents_messages.h" |
| 10 #include "content/renderer/render_view_impl.h" | 11 #include "content/renderer/render_view_impl.h" |
| 11 #include "ipc/ipc_message.h" | 12 #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/WebBindings.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBlob.h" |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebIntentRequest.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebIntentRequest.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h
" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h
" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSerialize
dScriptValue.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSerialize
dScriptValue.h" |
| 18 #include "v8/include/v8.h" | 20 #include "v8/include/v8.h" |
| 19 #include "webkit/glue/cpp_bound_class.h" | 21 #include "webkit/glue/cpp_bound_class.h" |
| 20 | 22 |
| 21 using WebKit::WebBindings; | 23 using WebKit::WebBindings; |
| 24 using WebKit::WebBlob; |
| 22 using WebKit::WebCString; | 25 using WebKit::WebCString; |
| 23 using WebKit::WebFrame; | 26 using WebKit::WebFrame; |
| 24 using WebKit::WebIntentRequest; | 27 using WebKit::WebIntentRequest; |
| 25 using WebKit::WebString; | 28 using WebKit::WebString; |
| 26 using WebKit::WebSerializedScriptValue; | 29 using WebKit::WebSerializedScriptValue; |
| 27 | 30 |
| 28 // This class encapsulates the API the Intent object will expose to Javascript. | 31 // This class encapsulates the API the Intent object will expose to Javascript. |
| 29 // It is made available to the Javascript runtime in the service page using | 32 // It is made available to the Javascript runtime in the service page using |
| 30 // NPAPI methods as with plugin/Javascript interaction objects and other | 33 // NPAPI methods as with plugin/Javascript interaction objects and other |
| 31 // browser-provided Javascript API objects on |window|. | 34 // browser-provided Javascript API objects on |window|. |
| 32 class WebIntentsHost::BoundDeliveredIntent : public CppBoundClass { | 35 class WebIntentsHost::BoundDeliveredIntent : public CppBoundClass { |
| 33 public: | 36 public: |
| 34 BoundDeliveredIntent(const webkit_glue::WebIntentData& intent, | 37 BoundDeliveredIntent(const webkit_glue::WebIntentData& intent, |
| 35 WebIntentsHost* parent, | 38 WebIntentsHost* parent, |
| 36 WebFrame* frame) { | 39 WebFrame* frame) { |
| 37 action_ = WebString(intent.action).utf8(); | 40 action_ = WebString(intent.action).utf8(); |
| 38 type_ = WebString(intent.type).utf8(); | 41 type_ = WebString(intent.type).utf8(); |
| 42 extra_data_ = intent.extra_data; |
| 39 parent_ = parent; | 43 parent_ = parent; |
| 40 | 44 |
| 41 v8::HandleScope scope; | 45 v8::HandleScope scope; |
| 42 v8::Local<v8::Context> ctx = frame->mainWorldScriptContext(); | 46 v8::Local<v8::Context> ctx = frame->mainWorldScriptContext(); |
| 43 v8::Context::Scope cscope(ctx); | 47 v8::Context::Scope cscope(ctx); |
| 44 v8::Local<v8::Value> data_obj; | 48 v8::Local<v8::Value> data_obj; |
| 45 | 49 |
| 46 if (intent.data_type == webkit_glue::WebIntentData::SERIALIZED) { | 50 if (intent.data_type == webkit_glue::WebIntentData::SERIALIZED) { |
| 47 WebSerializedScriptValue ssv = | 51 WebSerializedScriptValue ssv = |
| 48 WebSerializedScriptValue::fromString(WebString(intent.data)); | 52 WebSerializedScriptValue::fromString(WebString(intent.data)); |
| 49 DCHECK(!ssv.isNull()); | 53 DCHECK(!ssv.isNull()); |
| 50 data_obj = v8::Local<v8::Value>::New(ssv.deserialize()); | 54 data_obj = v8::Local<v8::Value>::New(ssv.deserialize()); |
| 51 } else { | 55 } else if (intent.data_type == webkit_glue::WebIntentData::UNSERIALIZED) { |
| 52 DCHECK(intent.data_type == webkit_glue::WebIntentData::UNSERIALIZED); | |
| 53 data_obj = v8::String::New( | 56 data_obj = v8::String::New( |
| 54 reinterpret_cast<const uint16_t*>(intent.unserialized_data.data()), | 57 reinterpret_cast<const uint16_t*>(intent.unserialized_data.data()), |
| 55 static_cast<int>(intent.unserialized_data.length())); | 58 static_cast<int>(intent.unserialized_data.length())); |
| 59 } else { |
| 60 DCHECK(intent.data_type == webkit_glue::WebIntentData::BLOB); |
| 61 WebBlob web_blob = WebBlob::createFromFile( |
| 62 WebString::fromUTF8(intent.blob_file.AsUTF8Unsafe()), |
| 63 intent.blob_length); |
| 64 data_obj = v8::Local<v8::Value>::New(web_blob.toV8Value()); |
| 56 } | 65 } |
| 57 | 66 |
| 58 data_val_.reset(new CppVariant); | 67 data_val_.reset(new CppVariant); |
| 59 WebBindings::toNPVariant(data_obj, frame->windowObject(), data_val_.get()); | 68 WebBindings::toNPVariant(data_obj, frame->windowObject(), data_val_.get()); |
| 60 | 69 |
| 61 BindGetterCallback("action", base::Bind(&BoundDeliveredIntent::getAction, | 70 BindGetterCallback("action", base::Bind(&BoundDeliveredIntent::GetAction, |
| 62 base::Unretained(this))); | 71 base::Unretained(this))); |
| 63 BindGetterCallback("type", base::Bind(&BoundDeliveredIntent::getType, | 72 BindGetterCallback("type", base::Bind(&BoundDeliveredIntent::GetType, |
| 64 base::Unretained(this))); | 73 base::Unretained(this))); |
| 65 BindGetterCallback("data", base::Bind(&BoundDeliveredIntent::getData, | 74 BindGetterCallback("data", base::Bind(&BoundDeliveredIntent::GetData, |
| 66 base::Unretained(this))); | 75 base::Unretained(this))); |
| 67 BindCallback("postResult", base::Bind(&BoundDeliveredIntent::postResult, | 76 BindCallback("getExtra", base::Bind(&BoundDeliveredIntent::GetExtra, |
| 77 base::Unretained(this))); |
| 78 BindCallback("postResult", base::Bind(&BoundDeliveredIntent::PostResult, |
| 68 base::Unretained(this))); | 79 base::Unretained(this))); |
| 69 BindCallback("postFailure", base::Bind(&BoundDeliveredIntent::postFailure, | 80 BindCallback("postFailure", base::Bind(&BoundDeliveredIntent::PostFailure, |
| 70 base::Unretained(this))); | 81 base::Unretained(this))); |
| 71 } | 82 } |
| 72 | 83 |
| 73 virtual ~BoundDeliveredIntent() { | 84 virtual ~BoundDeliveredIntent() { |
| 74 } | 85 } |
| 75 | 86 |
| 76 WebString SerializeCppVariant(const CppVariant& val) { | 87 WebString SerializeCppVariant(const CppVariant& val) { |
| 77 v8::HandleScope scope; | 88 v8::HandleScope scope; |
| 78 v8::Handle<v8::Value> v8obj = WebBindings::toV8Value(&val); | 89 v8::Handle<v8::Value> v8obj = WebBindings::toV8Value(&val); |
| 79 | 90 |
| 80 WebSerializedScriptValue ssv = | 91 WebSerializedScriptValue ssv = |
| 81 WebSerializedScriptValue::serialize(v8obj); | 92 WebSerializedScriptValue::serialize(v8obj); |
| 82 if (ssv.isNull()) | 93 if (ssv.isNull()) |
| 83 return WebKit::WebString(); | 94 return WebKit::WebString(); |
| 84 | 95 |
| 85 return ssv.toString(); | 96 return ssv.toString(); |
| 86 } | 97 } |
| 87 | 98 |
| 88 void postResult(const CppArgumentList& args, CppVariant* retval) { | 99 void PostResult(const CppArgumentList& args, CppVariant* retval) { |
| 89 if (args.size() != 1) { | 100 if (args.size() != 1) { |
| 90 WebBindings::setException(NULL, "Must pass one argument to postResult"); | 101 WebBindings::setException(NULL, "Must pass one argument to postResult"); |
| 91 return; | 102 return; |
| 92 } | 103 } |
| 93 | 104 |
| 94 WebString str = SerializeCppVariant(args[0]); | 105 WebString str = SerializeCppVariant(args[0]); |
| 95 parent_->OnResult(str); | 106 parent_->OnResult(str); |
| 96 } | 107 } |
| 97 | 108 |
| 98 void postFailure(const CppArgumentList& args, CppVariant* retval) { | 109 void PostFailure(const CppArgumentList& args, CppVariant* retval) { |
| 99 if (args.size() != 1) { | 110 if (args.size() != 1) { |
| 100 WebBindings::setException(NULL, "Must pass one argument to postFailure"); | 111 WebBindings::setException(NULL, "Must pass one argument to postFailure"); |
| 101 return; | 112 return; |
| 102 } | 113 } |
| 103 | 114 |
| 104 WebString str = SerializeCppVariant(args[0]); | 115 WebString str = SerializeCppVariant(args[0]); |
| 105 parent_->OnFailure(str); | 116 parent_->OnFailure(str); |
| 106 } | 117 } |
| 107 | 118 |
| 108 void getAction(CppVariant* result) { | 119 void GetAction(CppVariant* result) { |
| 109 std::string action; | 120 std::string action; |
| 110 action.assign(action_.data(), action_.length()); | 121 action.assign(action_.data(), action_.length()); |
| 111 result->Set(action); | 122 result->Set(action); |
| 112 } | 123 } |
| 113 | 124 |
| 114 void getType(CppVariant* result) { | 125 void GetType(CppVariant* result) { |
| 115 std::string type; | 126 std::string type; |
| 116 type.assign(type_.data(), type_.length()); | 127 type.assign(type_.data(), type_.length()); |
| 117 result->Set(type); | 128 result->Set(type); |
| 118 } | 129 } |
| 119 | 130 |
| 120 void getData(CppVariant* result) { | 131 void GetData(CppVariant* result) { |
| 121 result->Set(*data_val_.get()); | 132 result->Set(*data_val_.get()); |
| 122 } | 133 } |
| 123 | 134 |
| 135 void GetExtra(const CppArgumentList& args, CppVariant* result) { |
| 136 if (args.size() != 1) { |
| 137 WebBindings::setException(NULL, "Must pass one argument to getExtra"); |
| 138 return; |
| 139 } |
| 140 |
| 141 if (!args[0].isString()) { |
| 142 WebBindings::setException(NULL, "Argument to getExtra must be a string"); |
| 143 return; |
| 144 } |
| 145 |
| 146 std::string str = args[0].ToString(); |
| 147 std::map<string16, string16>::const_iterator iter = |
| 148 extra_data_.find(UTF8ToUTF16(str)); |
| 149 if (iter == extra_data_.end()) { |
| 150 result->SetNull(); |
| 151 return; |
| 152 } |
| 153 std::string val = UTF16ToUTF8(iter->second); |
| 154 result->Set(val); |
| 155 } |
| 156 |
| 124 private: | 157 private: |
| 125 // Intent data suitable for surfacing to Javascript callers. | 158 // Intent data suitable for surfacing to Javascript callers. |
| 126 WebCString action_; | 159 WebCString action_; |
| 127 WebCString type_; | 160 WebCString type_; |
| 161 std::map<string16, string16> extra_data_; |
| 128 scoped_ptr<CppVariant> data_val_; | 162 scoped_ptr<CppVariant> data_val_; |
| 129 | 163 |
| 130 // The dispatcher object, for forwarding postResult/postFailure calls. | 164 // The dispatcher object, for forwarding postResult/postFailure calls. |
| 131 WebIntentsHost* parent_; | 165 WebIntentsHost* parent_; |
| 132 }; | 166 }; |
| 133 | 167 |
| 134 WebIntentsHost::WebIntentsHost(RenderViewImpl* render_view) | 168 WebIntentsHost::WebIntentsHost(RenderViewImpl* render_view) |
| 135 : content::RenderViewObserver(render_view), | 169 : content::RenderViewObserver(render_view), |
| 136 id_counter_(0) { | 170 id_counter_(0) { |
| 137 } | 171 } |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 // sub-frames. TODO(gbillock): This policy needs to be fine-tuned and | 229 // sub-frames. TODO(gbillock): This policy needs to be fine-tuned and |
| 196 // documented. | 230 // documented. |
| 197 void WebIntentsHost::DidClearWindowObject(WebFrame* frame) { | 231 void WebIntentsHost::DidClearWindowObject(WebFrame* frame) { |
| 198 if (intent_.get() == NULL || frame->top() != frame) | 232 if (intent_.get() == NULL || frame->top() != frame) |
| 199 return; | 233 return; |
| 200 | 234 |
| 201 delivered_intent_.reset( | 235 delivered_intent_.reset( |
| 202 new BoundDeliveredIntent(*(intent_.get()), this, frame)); | 236 new BoundDeliveredIntent(*(intent_.get()), this, frame)); |
| 203 delivered_intent_->BindToJavascript(frame, "intent"); | 237 delivered_intent_->BindToJavascript(frame, "intent"); |
| 204 } | 238 } |
| OLD | NEW |