Chromium Code Reviews| 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 "base/utf_string_conversions.h" |
| 10 #include "content/common/intents_messages.h" | 10 #include "content/common/intents_messages.h" |
| 11 #include "content/public/renderer/v8_value_converter.h" | |
| 11 #include "content/renderer/render_view_impl.h" | 12 #include "content/renderer/render_view_impl.h" |
| 12 #include "ipc/ipc_message.h" | 13 #include "ipc/ipc_message.h" |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBlob.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBlob.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDeliveredIntentCli ent.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDeliveredIntentCli ent.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebIntent.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebIntent.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebIntentRequest.h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebIntentRequest.h" |
| 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 21 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h " | 22 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h " |
| 22 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebFileSyste m.h" | 23 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebFileSyste m.h" |
| 23 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSerialize dScriptValue.h" | 24 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSerialize dScriptValue.h" |
| 24 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | 25 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
| 25 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" | 26 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" |
| 26 #include "v8/include/v8.h" | |
| 27 #include "webkit/fileapi/file_system_util.h" | 27 #include "webkit/fileapi/file_system_util.h" |
| 28 #include "webkit/glue/cpp_bound_class.h" | 28 #include "webkit/glue/cpp_bound_class.h" |
| 29 | 29 |
| 30 using WebKit::WebBindings; | 30 using WebKit::WebBindings; |
| 31 using WebKit::WebBlob; | 31 using WebKit::WebBlob; |
| 32 using WebKit::WebCString; | 32 using WebKit::WebCString; |
| 33 using WebKit::WebDeliveredIntentClient; | 33 using WebKit::WebDeliveredIntentClient; |
| 34 using WebKit::WebFrame; | 34 using WebKit::WebFrame; |
| 35 using WebKit::WebIntent; | 35 using WebKit::WebIntent; |
| 36 using WebKit::WebIntentRequest; | 36 using WebKit::WebIntentRequest; |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 113 } | 113 } |
| 114 | 114 |
| 115 void WebIntentsHost::OnFailure(const WebKit::WebString& data) { | 115 void WebIntentsHost::OnFailure(const WebKit::WebString& data) { |
| 116 Send(new IntentsHostMsg_WebIntentReply( | 116 Send(new IntentsHostMsg_WebIntentReply( |
| 117 routing_id(), webkit_glue::WEB_INTENT_REPLY_FAILURE, data)); | 117 routing_id(), webkit_glue::WEB_INTENT_REPLY_FAILURE, data)); |
| 118 } | 118 } |
| 119 | 119 |
| 120 // We set the intent payload into all top-level frame window objects. This | 120 // We set the intent payload into all top-level frame window objects. This |
| 121 // should persist the data through redirects, and not deliver it to any | 121 // should persist the data through redirects, and not deliver it to any |
| 122 // sub-frames. | 122 // sub-frames. |
| 123 // TODO(gbillock): match to spec to double-check registration match before | 123 void WebIntentsHost::DidCreateScriptContext(WebKit::WebFrame* frame, |
| 124 // delivery. | 124 v8::Handle<v8::Context> ctx, |
| 125 void WebIntentsHost::DidClearWindowObject(WebFrame* frame) { | 125 int extension_group, |
| 126 int world_id) { | |
| 126 if (intent_.get() == NULL || frame->top() != frame) | 127 if (intent_.get() == NULL || frame->top() != frame) |
| 127 return; | 128 return; |
| 128 | 129 |
| 129 if (!delivered_intent_client_.get()) { | 130 if (!delivered_intent_client_.get()) { |
| 130 delivered_intent_client_.reset(new DeliveredIntentClientImpl(this)); | 131 delivered_intent_client_.reset(new DeliveredIntentClientImpl(this)); |
| 131 } | 132 } |
| 132 | 133 |
| 133 WebVector<WebString> extras_keys(intent_->extra_data.size()); | 134 v8::HandleScope scope; |
| 134 WebVector<WebString> extras_values(intent_->extra_data.size()); | 135 v8::Context::Scope cscope(ctx); |
|
abarth-chromium
2012/10/12 19:59:09
Do we need either of these things any more? We sh
Greg Billock
2012/10/12 21:32:39
Yes. Just checked and this gets called within thes
| |
| 135 std::map<string16, string16>::iterator iter; | 136 WebIntent web_intent = CreateWebIntent(frame, *(intent_.get())); |
| 136 int i; | 137 |
| 137 for (i = 0, iter = intent_->extra_data.begin(); | 138 if (!web_intent.action().isEmpty()) |
| 138 iter != intent_->extra_data.end(); | 139 frame->deliverIntent(web_intent, NULL, delivered_intent_client_.get()); |
| 140 } | |
| 141 | |
| 142 WebIntent WebIntentsHost::CreateWebIntent( | |
| 143 WebFrame* frame, const webkit_glue::WebIntentData& intent_data) { | |
| 144 // Must be called with v8 scope held. | |
| 145 DCHECK(!v8::Context::GetCurrent().IsEmpty()); | |
|
abarth-chromium
2012/10/12 19:59:09
Why not call v8::Context::InContext() ? That's th
Greg Billock
2012/10/12 21:32:39
Done.
| |
| 146 | |
| 147 // TODO(gbillock): Remove this block when we get rid of |extras|. | |
| 148 WebVector<WebString> extras_keys(intent_data.extra_data.size()); | |
| 149 WebVector<WebString> extras_values(intent_data.extra_data.size()); | |
| 150 std::map<string16, string16>::const_iterator iter; | |
| 151 size_t i; | |
| 152 for (i = 0, iter = intent_data.extra_data.begin(); | |
| 153 iter != intent_data.extra_data.end(); | |
| 139 ++i, ++iter) { | 154 ++i, ++iter) { |
| 140 extras_keys[i] = iter->first; | 155 extras_keys[i] = iter->first; |
| 141 extras_values[i] = iter->second; | 156 extras_values[i] = iter->second; |
| 142 } | 157 } |
| 143 | 158 |
| 144 v8::HandleScope scope; | 159 if (intent_data.data_type == webkit_glue::WebIntentData::SERIALIZED) { |
| 145 v8::Local<v8::Context> ctx = frame->mainWorldScriptContext(); | 160 return WebIntent::create(intent_data.action, intent_data.type, |
| 146 v8::Context::Scope cscope(ctx); | 161 intent_data.data, |
| 147 WebIntent web_intent; | 162 extras_keys, extras_values); |
| 148 | 163 } else if (intent_data.data_type == |
| 149 if (intent_->data_type == webkit_glue::WebIntentData::SERIALIZED) { | 164 webkit_glue::WebIntentData::UNSERIALIZED) { |
| 150 web_intent = WebIntent::create(intent_->action, intent_->type, | |
| 151 intent_->data, | |
| 152 extras_keys, extras_values); | |
| 153 } else if (intent_->data_type == webkit_glue::WebIntentData::UNSERIALIZED) { | |
| 154 v8::Local<v8::String> dataV8 = v8::String::New( | 165 v8::Local<v8::String> dataV8 = v8::String::New( |
| 155 reinterpret_cast<const uint16_t*>(intent_->unserialized_data.data()), | 166 reinterpret_cast<const uint16_t*>(intent_data.unserialized_data.data()), |
|
abarth-chromium
2012/10/12 19:59:09
Is this safe? Do we need to worry about unpaired
Greg Billock
2012/10/12 21:32:39
This is for text data prepared safely in the brows
| |
| 156 static_cast<int>(intent_->unserialized_data.length())); | 167 static_cast<int>(intent_data.unserialized_data.length())); |
| 157 WebSerializedScriptValue serialized_data = | 168 WebSerializedScriptValue serialized_data = |
| 158 WebSerializedScriptValue::serialize(dataV8); | 169 WebSerializedScriptValue::serialize(dataV8); |
| 159 | 170 |
| 160 web_intent = WebIntent::create(intent_->action, intent_->type, | 171 return WebIntent::create(intent_data.action, intent_data.type, |
| 161 serialized_data.toString(), | 172 serialized_data.toString(), |
| 162 extras_keys, extras_values); | 173 extras_keys, extras_values); |
| 163 } else if (intent_->data_type == webkit_glue::WebIntentData::BLOB) { | 174 } else if (intent_data.data_type == webkit_glue::WebIntentData::BLOB) { |
| 164 DCHECK(intent_->data_type == webkit_glue::WebIntentData::BLOB); | |
| 165 web_blob_ = WebBlob::createFromFile( | 175 web_blob_ = WebBlob::createFromFile( |
| 166 WebString::fromUTF8(intent_->blob_file.AsUTF8Unsafe()), | 176 WebString::fromUTF8(intent_data.blob_file.AsUTF8Unsafe()), |
|
abarth-chromium
2012/10/12 19:59:09
What if the blob_file cannot be represented as UTF
Greg Billock
2012/10/12 21:32:39
This refers to the name not having encoding inform
| |
| 167 intent_->blob_length); | 177 intent_data.blob_length); |
| 168 WebSerializedScriptValue serialized_data = | 178 WebSerializedScriptValue serialized_data = |
| 169 WebSerializedScriptValue::serialize(web_blob_.toV8Value()); | 179 WebSerializedScriptValue::serialize(web_blob_.toV8Value()); |
| 170 web_intent = WebIntent::create(intent_->action, intent_->type, | 180 return WebIntent::create(intent_data.action, intent_data.type, |
| 171 serialized_data.toString(), | 181 serialized_data.toString(), |
| 172 extras_keys, extras_values); | 182 extras_keys, extras_values); |
| 173 } else if (intent_->data_type == webkit_glue::WebIntentData::FILESYSTEM) { | 183 } else if (intent_data.data_type == webkit_glue::WebIntentData::FILESYSTEM) { |
| 174 const GURL origin = GURL(frame->document().securityOrigin().toString()); | 184 const GURL origin = GURL(frame->document().securityOrigin().toString()); |
| 175 const GURL root_url = | 185 const GURL root_url = |
| 176 fileapi::GetFileSystemRootURI(origin, fileapi::kFileSystemTypeIsolated); | 186 fileapi::GetFileSystemRootURI(origin, fileapi::kFileSystemTypeIsolated); |
| 177 const std::string url = base::StringPrintf( | 187 const std::string url = base::StringPrintf( |
| 178 "%s%s/%s/", | 188 "%s%s/%s/", |
| 179 root_url.spec().c_str(), | 189 root_url.spec().c_str(), |
| 180 intent_->filesystem_id.c_str(), | 190 intent_data.filesystem_id.c_str(), |
| 181 intent_->root_name.c_str()); | 191 intent_data.root_name.c_str()); |
| 182 // TODO(kmadhusu): This is a temporary hack to create a serializable file | 192 // TODO(kmadhusu): This is a temporary hack to create a serializable file |
| 183 // system. Once we have a better way to create a serializable file system, | 193 // system. Once we have a better way to create a serializable file system, |
| 184 // remove this hack. | 194 // remove this hack. |
| 185 v8::Handle<v8::Value> filesystem_V8 = frame->createSerializableFileSystem( | 195 v8::Handle<v8::Value> filesystem_V8 = frame->createSerializableFileSystem( |
| 186 WebKit::WebFileSystem::TypeIsolated, | 196 WebKit::WebFileSystem::TypeIsolated, |
| 187 WebKit::WebString::fromUTF8(intent_->root_name), | 197 WebKit::WebString::fromUTF8(intent_data.root_name), |
| 188 WebKit::WebString::fromUTF8(url)); | 198 WebKit::WebString::fromUTF8(url)); |
| 189 WebSerializedScriptValue serialized_data = | 199 WebSerializedScriptValue serialized_data = |
| 190 WebSerializedScriptValue::serialize(filesystem_V8); | 200 WebSerializedScriptValue::serialize(filesystem_V8); |
| 191 web_intent = WebIntent::create(intent_->action, intent_->type, | 201 return WebIntent::create(intent_data.action, intent_data.type, |
| 192 serialized_data.toString(), | 202 serialized_data.toString(), |
| 193 extras_keys, extras_values); | 203 extras_keys, extras_values); |
| 194 } else { | 204 } else if (intent_data.data_type == webkit_glue::WebIntentData::MIME_TYPE) { |
| 195 NOTREACHED(); | 205 scoped_ptr<content::V8ValueConverter> converter( |
| 206 content::V8ValueConverter::create()); | |
| 207 v8::Handle<v8::Value> valV8 = converter->ToV8Value( | |
| 208 &intent_data.mime_data, v8::Context::GetCurrent()); | |
| 209 | |
| 210 // If we are carrying blob data, convert it to v8 format and set it to | |
| 211 // the "blob" property of the first element in the array. | |
| 212 if (!intent_data.blob_file.empty() && valV8->IsArray()) { | |
| 213 v8::Local<v8::Array> valArray = v8::Array::Cast(*valV8); | |
| 214 v8::Local<v8::Value> firstVal = valArray->Get(0); | |
| 215 if (firstVal->IsObject()) { | |
| 216 web_blob_ = WebBlob::createFromFile( | |
| 217 WebString::fromUTF8(intent_data.blob_file.AsUTF8Unsafe()), | |
|
abarth-chromium
2012/10/12 19:59:09
Should we be concerned that we're calling a functi
Greg Billock
2012/10/12 21:32:39
See above. I think this is the best way to transla
| |
| 218 intent_data.blob_length); | |
| 219 firstVal->ToObject()->Set(v8::String::New("blob", 4), | |
| 220 web_blob_.toV8Value()); | |
|
abarth-chromium
2012/10/12 19:59:09
It's not clear to me why it is safe to call Set he
Greg Billock
2012/10/12 21:32:39
Because of the __setter possibility? I looked thro
| |
| 221 } | |
| 222 } | |
| 223 | |
| 224 WebSerializedScriptValue serialized_data = | |
| 225 WebSerializedScriptValue::serialize(valV8); | |
| 226 return WebIntent::create(intent_data.action, intent_data.type, | |
| 227 serialized_data.toString(), | |
| 228 WebVector<WebString>(), WebVector<WebString>()); | |
| 196 } | 229 } |
| 197 | 230 |
| 198 if (!web_intent.action().isEmpty()) | 231 NOTREACHED(); |
| 199 frame->deliverIntent(web_intent, NULL, delivered_intent_client_.get()); | 232 return WebIntent(); |
| 200 } | 233 } |
| OLD | NEW |