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 "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), intent.blob_length); | |
| 63 data_obj = v8::Local<v8::Value>::New(web_blob.toV8Value()); | |
|
michaeln
2012/03/29 20:51:57
i have no clue if wrapping the return value of toV
Greg Billock
2012/03/29 21:04:45
Do I need to store this WebBlob in the object? Tha
| |
| 56 } | 64 } |
| 57 | 65 |
| 58 data_val_.reset(new CppVariant); | 66 data_val_.reset(new CppVariant); |
| 59 WebBindings::toNPVariant(data_obj, frame->windowObject(), data_val_.get()); | 67 WebBindings::toNPVariant(data_obj, frame->windowObject(), data_val_.get()); |
| 60 | 68 |
| 61 BindGetterCallback("action", base::Bind(&BoundDeliveredIntent::getAction, | 69 BindGetterCallback("action", base::Bind(&BoundDeliveredIntent::GetAction, |
| 62 base::Unretained(this))); | 70 base::Unretained(this))); |
| 63 BindGetterCallback("type", base::Bind(&BoundDeliveredIntent::getType, | 71 BindGetterCallback("type", base::Bind(&BoundDeliveredIntent::GetType, |
| 64 base::Unretained(this))); | 72 base::Unretained(this))); |
| 65 BindGetterCallback("data", base::Bind(&BoundDeliveredIntent::getData, | 73 BindGetterCallback("data", base::Bind(&BoundDeliveredIntent::GetData, |
| 66 base::Unretained(this))); | 74 base::Unretained(this))); |
| 67 BindCallback("postResult", base::Bind(&BoundDeliveredIntent::postResult, | 75 BindCallback("getExtra", base::Bind(&BoundDeliveredIntent::GetExtra, |
| 76 base::Unretained(this))); | |
| 77 BindCallback("postResult", base::Bind(&BoundDeliveredIntent::PostResult, | |
| 68 base::Unretained(this))); | 78 base::Unretained(this))); |
| 69 BindCallback("postFailure", base::Bind(&BoundDeliveredIntent::postFailure, | 79 BindCallback("postFailure", base::Bind(&BoundDeliveredIntent::PostFailure, |
| 70 base::Unretained(this))); | 80 base::Unretained(this))); |
| 71 } | 81 } |
| 72 | 82 |
| 73 virtual ~BoundDeliveredIntent() { | 83 virtual ~BoundDeliveredIntent() { |
| 74 } | 84 } |
| 75 | 85 |
| 76 WebString SerializeCppVariant(const CppVariant& val) { | 86 WebString SerializeCppVariant(const CppVariant& val) { |
| 77 v8::HandleScope scope; | 87 v8::HandleScope scope; |
| 78 v8::Handle<v8::Value> v8obj = WebBindings::toV8Value(&val); | 88 v8::Handle<v8::Value> v8obj = WebBindings::toV8Value(&val); |
| 79 | 89 |
| 80 WebSerializedScriptValue ssv = | 90 WebSerializedScriptValue ssv = |
| 81 WebSerializedScriptValue::serialize(v8obj); | 91 WebSerializedScriptValue::serialize(v8obj); |
| 82 if (ssv.isNull()) | 92 if (ssv.isNull()) |
| 83 return WebKit::WebString(); | 93 return WebKit::WebString(); |
| 84 | 94 |
| 85 return ssv.toString(); | 95 return ssv.toString(); |
| 86 } | 96 } |
| 87 | 97 |
| 88 void postResult(const CppArgumentList& args, CppVariant* retval) { | 98 void PostResult(const CppArgumentList& args, CppVariant* retval) { |
| 89 if (args.size() != 1) { | 99 if (args.size() != 1) { |
| 90 WebBindings::setException(NULL, "Must pass one argument to postResult"); | 100 WebBindings::setException(NULL, "Must pass one argument to postResult"); |
| 91 return; | 101 return; |
| 92 } | 102 } |
| 93 | 103 |
| 94 WebString str = SerializeCppVariant(args[0]); | 104 WebString str = SerializeCppVariant(args[0]); |
| 95 parent_->OnResult(str); | 105 parent_->OnResult(str); |
| 96 } | 106 } |
| 97 | 107 |
| 98 void postFailure(const CppArgumentList& args, CppVariant* retval) { | 108 void PostFailure(const CppArgumentList& args, CppVariant* retval) { |
| 99 if (args.size() != 1) { | 109 if (args.size() != 1) { |
| 100 WebBindings::setException(NULL, "Must pass one argument to postFailure"); | 110 WebBindings::setException(NULL, "Must pass one argument to postFailure"); |
| 101 return; | 111 return; |
| 102 } | 112 } |
| 103 | 113 |
| 104 WebString str = SerializeCppVariant(args[0]); | 114 WebString str = SerializeCppVariant(args[0]); |
| 105 parent_->OnFailure(str); | 115 parent_->OnFailure(str); |
| 106 } | 116 } |
| 107 | 117 |
| 108 void getAction(CppVariant* result) { | 118 void GetAction(CppVariant* result) { |
| 109 std::string action; | 119 std::string action; |
| 110 action.assign(action_.data(), action_.length()); | 120 action.assign(action_.data(), action_.length()); |
| 111 result->Set(action); | 121 result->Set(action); |
| 112 } | 122 } |
| 113 | 123 |
| 114 void getType(CppVariant* result) { | 124 void GetType(CppVariant* result) { |
| 115 std::string type; | 125 std::string type; |
| 116 type.assign(type_.data(), type_.length()); | 126 type.assign(type_.data(), type_.length()); |
| 117 result->Set(type); | 127 result->Set(type); |
| 118 } | 128 } |
| 119 | 129 |
| 120 void getData(CppVariant* result) { | 130 void GetData(CppVariant* result) { |
| 121 result->Set(*data_val_.get()); | 131 result->Set(*data_val_.get()); |
| 122 } | 132 } |
| 123 | 133 |
| 134 void GetExtra(const CppArgumentList& args, CppVariant* result) { | |
| 135 if (args.size() != 1) { | |
| 136 WebBindings::setException(NULL, "Must pass one argument to getExtra"); | |
| 137 return; | |
| 138 } | |
| 139 | |
| 140 WebString str = SerializeCppVariant(args[0]); | |
| 141 std::map<string16, string16>::const_iterator iter = extra_data_.find(str); | |
| 142 if (iter == extra_data_.end()) { | |
| 143 result->SetNull(); | |
| 144 return; | |
| 145 } | |
| 146 std::string val = UTF16ToUTF8(iter->second); | |
| 147 result->Set(val); | |
| 148 } | |
| 149 | |
| 124 private: | 150 private: |
| 125 // Intent data suitable for surfacing to Javascript callers. | 151 // Intent data suitable for surfacing to Javascript callers. |
| 126 WebCString action_; | 152 WebCString action_; |
| 127 WebCString type_; | 153 WebCString type_; |
| 154 std::map<string16, string16> extra_data_; | |
| 128 scoped_ptr<CppVariant> data_val_; | 155 scoped_ptr<CppVariant> data_val_; |
| 129 | 156 |
| 130 // The dispatcher object, for forwarding postResult/postFailure calls. | 157 // The dispatcher object, for forwarding postResult/postFailure calls. |
| 131 WebIntentsHost* parent_; | 158 WebIntentsHost* parent_; |
| 132 }; | 159 }; |
| 133 | 160 |
| 134 WebIntentsHost::WebIntentsHost(RenderViewImpl* render_view) | 161 WebIntentsHost::WebIntentsHost(RenderViewImpl* render_view) |
| 135 : content::RenderViewObserver(render_view), | 162 : content::RenderViewObserver(render_view), |
| 136 id_counter_(0) { | 163 id_counter_(0) { |
| 137 } | 164 } |
| (...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 | 222 // sub-frames. TODO(gbillock): This policy needs to be fine-tuned and |
| 196 // documented. | 223 // documented. |
| 197 void WebIntentsHost::DidClearWindowObject(WebFrame* frame) { | 224 void WebIntentsHost::DidClearWindowObject(WebFrame* frame) { |
| 198 if (intent_.get() == NULL || frame->top() != frame) | 225 if (intent_.get() == NULL || frame->top() != frame) |
| 199 return; | 226 return; |
| 200 | 227 |
| 201 delivered_intent_.reset( | 228 delivered_intent_.reset( |
| 202 new BoundDeliveredIntent(*(intent_.get()), this, frame)); | 229 new BoundDeliveredIntent(*(intent_.get()), this, frame)); |
| 203 delivered_intent_->BindToJavascript(frame, "intent"); | 230 delivered_intent_->BindToJavascript(frame, "intent"); |
| 204 } | 231 } |
| OLD | NEW |