OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/browser/android/java/gin_java_method_invocation_helper.h" | 5 #include "content/browser/android/java/gin_java_method_invocation_helper.h" |
6 | 6 |
7 #include <unistd.h> | 7 #include <unistd.h> |
8 #include <cmath> | 8 #include <cmath> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 26 matching lines...) Expand all Loading... |
37 method_name_(method_name), | 37 method_name_(method_name), |
38 arguments_(arguments.DeepCopy()), | 38 arguments_(arguments.DeepCopy()), |
39 invocation_error_(kGinJavaBridgeNoError) {} | 39 invocation_error_(kGinJavaBridgeNoError) {} |
40 | 40 |
41 GinJavaMethodInvocationHelper::~GinJavaMethodInvocationHelper() {} | 41 GinJavaMethodInvocationHelper::~GinJavaMethodInvocationHelper() {} |
42 | 42 |
43 void GinJavaMethodInvocationHelper::Init(DispatcherDelegate* dispatcher) { | 43 void GinJavaMethodInvocationHelper::Init(DispatcherDelegate* dispatcher) { |
44 // Build on the UI thread a map of object_id -> WeakRef for Java objects from | 44 // Build on the UI thread a map of object_id -> WeakRef for Java objects from |
45 // |arguments_|. Then we can use this map on the background thread without | 45 // |arguments_|. Then we can use this map on the background thread without |
46 // accessing |dispatcher|. | 46 // accessing |dispatcher|. |
47 BuildObjectRefsFromListValue(dispatcher, arguments_.get()); | 47 BuildObjectRefsFromListValue(dispatcher, *arguments_); |
48 } | 48 } |
49 | 49 |
50 // As V8ValueConverter has finite recursion depth when serializing | 50 // As V8ValueConverter has finite recursion depth when serializing |
51 // JavaScript values, we don't bother about having a recursion threshold here. | 51 // JavaScript values, we don't bother about having a recursion threshold here. |
52 void GinJavaMethodInvocationHelper::BuildObjectRefsFromListValue( | 52 void GinJavaMethodInvocationHelper::BuildObjectRefsFromListValue( |
53 DispatcherDelegate* dispatcher, | 53 DispatcherDelegate* dispatcher, |
54 const base::Value* list_value) { | 54 const base::Value& list_value) { |
55 DCHECK(list_value->IsType(base::Value::TYPE_LIST)); | 55 DCHECK(list_value.IsType(base::Value::TYPE_LIST)); |
56 const base::ListValue* list; | 56 const base::ListValue* list; |
57 list_value->GetAsList(&list); | 57 list_value.GetAsList(&list); |
58 for (base::ListValue::const_iterator iter = list->begin(); | 58 for (const auto& entry : *list) { |
59 iter != list->end(); | 59 if (AppendObjectRef(dispatcher, *entry)) |
60 ++iter) { | |
61 if (AppendObjectRef(dispatcher, *iter)) | |
62 continue; | 60 continue; |
63 if ((*iter)->IsType(base::Value::TYPE_LIST)) { | 61 if (entry->IsType(base::Value::TYPE_LIST)) { |
64 BuildObjectRefsFromListValue(dispatcher, *iter); | 62 BuildObjectRefsFromListValue(dispatcher, *entry); |
65 } else if ((*iter)->IsType(base::Value::TYPE_DICTIONARY)) { | 63 } else if (entry->IsType(base::Value::TYPE_DICTIONARY)) { |
66 BuildObjectRefsFromDictionaryValue(dispatcher, *iter); | 64 BuildObjectRefsFromDictionaryValue(dispatcher, *entry); |
67 } | 65 } |
68 } | 66 } |
69 } | 67 } |
70 | 68 |
71 void GinJavaMethodInvocationHelper::BuildObjectRefsFromDictionaryValue( | 69 void GinJavaMethodInvocationHelper::BuildObjectRefsFromDictionaryValue( |
72 DispatcherDelegate* dispatcher, | 70 DispatcherDelegate* dispatcher, |
73 const base::Value* dict_value) { | 71 const base::Value& dict_value) { |
74 DCHECK(dict_value->IsType(base::Value::TYPE_DICTIONARY)); | 72 DCHECK(dict_value.IsType(base::Value::TYPE_DICTIONARY)); |
75 const base::DictionaryValue* dict; | 73 const base::DictionaryValue* dict; |
76 dict_value->GetAsDictionary(&dict); | 74 dict_value.GetAsDictionary(&dict); |
77 for (base::DictionaryValue::Iterator iter(*dict); | 75 for (base::DictionaryValue::Iterator iter(*dict); |
78 !iter.IsAtEnd(); | 76 !iter.IsAtEnd(); |
79 iter.Advance()) { | 77 iter.Advance()) { |
80 if (AppendObjectRef(dispatcher, &iter.value())) | 78 if (AppendObjectRef(dispatcher, iter.value())) |
81 continue; | 79 continue; |
82 if (iter.value().IsType(base::Value::TYPE_LIST)) { | 80 if (iter.value().IsType(base::Value::TYPE_LIST)) { |
83 BuildObjectRefsFromListValue(dispatcher, &iter.value()); | 81 BuildObjectRefsFromListValue(dispatcher, iter.value()); |
84 } else if (iter.value().IsType(base::Value::TYPE_DICTIONARY)) { | 82 } else if (iter.value().IsType(base::Value::TYPE_DICTIONARY)) { |
85 BuildObjectRefsFromDictionaryValue(dispatcher, &iter.value()); | 83 BuildObjectRefsFromDictionaryValue(dispatcher, iter.value()); |
86 } | 84 } |
87 } | 85 } |
88 } | 86 } |
89 | 87 |
90 bool GinJavaMethodInvocationHelper::AppendObjectRef( | 88 bool GinJavaMethodInvocationHelper::AppendObjectRef( |
91 DispatcherDelegate* dispatcher, | 89 DispatcherDelegate* dispatcher, |
92 const base::Value* raw_value) { | 90 const base::Value& raw_value) { |
93 if (!GinJavaBridgeValue::ContainsGinJavaBridgeValue(raw_value)) | 91 if (!GinJavaBridgeValue::ContainsGinJavaBridgeValue(&raw_value)) |
94 return false; | 92 return false; |
95 std::unique_ptr<const GinJavaBridgeValue> value( | 93 std::unique_ptr<const GinJavaBridgeValue> value( |
96 GinJavaBridgeValue::FromValue(raw_value)); | 94 GinJavaBridgeValue::FromValue(&raw_value)); |
97 if (!value->IsType(GinJavaBridgeValue::TYPE_OBJECT_ID)) | 95 if (!value->IsType(GinJavaBridgeValue::TYPE_OBJECT_ID)) |
98 return false; | 96 return false; |
99 GinJavaBoundObject::ObjectID object_id; | 97 GinJavaBoundObject::ObjectID object_id; |
100 if (value->GetAsObjectID(&object_id)) { | 98 if (value->GetAsObjectID(&object_id)) { |
101 ObjectRefs::iterator iter = object_refs_.find(object_id); | 99 ObjectRefs::iterator iter = object_refs_.find(object_id); |
102 if (iter == object_refs_.end()) { | 100 if (iter == object_refs_.end()) { |
103 JavaObjectWeakGlobalRef object_ref( | 101 JavaObjectWeakGlobalRef object_ref( |
104 dispatcher->GetObjectWeakRef(object_id)); | 102 dispatcher->GetObjectWeakRef(object_id)); |
105 if (!object_ref.is_empty()) { | 103 if (!object_ref.is_empty()) { |
106 object_refs_.insert(std::make_pair(object_id, object_ref)); | 104 object_refs_.insert(std::make_pair(object_id, object_ref)); |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 } | 334 } |
337 // This is for all cases except JavaType::TypeObject. | 335 // This is for all cases except JavaType::TypeObject. |
338 if (!base::android::ClearException(env)) { | 336 if (!base::android::ClearException(env)) { |
339 SetPrimitiveResult(result_wrapper); | 337 SetPrimitiveResult(result_wrapper); |
340 } else { | 338 } else { |
341 SetInvocationError(kGinJavaBridgeJavaExceptionRaised); | 339 SetInvocationError(kGinJavaBridgeJavaExceptionRaised); |
342 } | 340 } |
343 } | 341 } |
344 | 342 |
345 } // namespace content | 343 } // namespace content |
OLD | NEW |