flutter_flutter/lib/ui/plugins/callback_cache.cc
Siva d90223fadf
Roll Dart to version 1be785ae2ddb1754a184cd638ab719e94d86b4e9 (#5985)
* Roll Dart to version 1be785ae2ddb1754a184cd638ab719e94d86b4e9

This rolls includes the following changes :

1be785ae2d Clean up some dead code
7b9fb893d2 [vm] Add a service method for 'invoke' as the dual to 'eval'.
099f1504fa Mention -O flag in CHANGELOG
79f69abace [vm/compiler] breakage fix: add missing break
96a1e9985f Fix the pkg bot
7818db20a7 Add analysis hint for invalid use of @visibleForTemplate code.
71d96019d1 [vm/compiler] Introduce 64-bit NEGATE - all archs.
5013a2ccc4 Remove spurious line.
ef2f777625 Mark some analyzer tests as being flaky
cf560fe17b Fix a couple of the tests failing on the analyzer with fasta parser bot
63c11693e6 [gardening] Fix language_2/type_variable_promotion_test.
d0f28884ff [VM] Fix expression evaluation implementation: Never register temporary/unused classes with the system.
e2a1807fc2 [gardening] Update status for io/compile_all_test in PRODUCT AOT mode
f7ff739448 Insert date of 2.0.0 release in Changelog
bcabad6014 [vm] Fix SIMARM64 build on Windows.
af02ccae83 [infra] Fix filesets for new vm-kernel-precomp builders
8e2f28e264 Update homebrew to drop the @2 tab and fix for 2.0
ba119d7292 [VM] Remove "$compiler == precompiler" sections from language_2_precompiler.status
f3a2c0e28f [release] Prepare changelog for 2.1.0-dev.0.0
88cba7d860 [vm/kernel/bytecode] Fix arguments descriptor for List._fromLiteral call in bytecode
bd45ec0c4b [vm, gc] Refactor PageSpace::CollectGarbage to ensure the safepoint and task count remain well-scoped even with early exits.
8195fd8c64 Repair dart2js/string_interpolation_test
a0b335ac6c Try no implicit casts in pkg/analyzer_cli.
3d25d3761b Update pub - leave packages directories alone
327db5e9ab [vm] Fix kernel_isolate use_field_guard flag for kbc
a1ca88f554 Resolve invocation arguments to parameters.

* Address source format error.
2018-08-09 16:24:16 -07:00

187 lines
5.6 KiB
C++

// Copyright 2018 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <fstream>
#include <iterator>
#include "flutter/fml/build_config.h"
#include "flutter/fml/logging.h"
#include "flutter/fml/paths.h"
#include "flutter/lib/ui/plugins/callback_cache.h"
#include "third_party/rapidjson/rapidjson/document.h"
#include "third_party/rapidjson/rapidjson/stringbuffer.h"
#include "third_party/rapidjson/rapidjson/writer.h"
#include "third_party/tonic/converter/dart_converter.h"
using rapidjson::Document;
using rapidjson::StringBuffer;
using rapidjson::Writer;
using tonic::ToDart;
namespace blink {
static const char* kHandleKey = "handle";
static const char* kRepresentationKey = "representation";
static const char* kNameKey = "name";
static const char* kClassNameKey = "class_name";
static const char* kLibraryPathKey = "library_path";
static const char* kCacheName = "flutter_callback_cache.json";
std::mutex DartCallbackCache::mutex_;
std::string DartCallbackCache::cache_path_;
std::map<int64_t, DartCallbackRepresentation> DartCallbackCache::cache_;
void DartCallbackCache::SetCachePath(const std::string& path) {
cache_path_ = fml::paths::JoinPaths({path, kCacheName});
}
Dart_Handle DartCallbackCache::GetCallback(int64_t handle) {
std::lock_guard<std::mutex> lock(mutex_);
auto iterator = cache_.find(handle);
if (iterator != cache_.end()) {
DartCallbackRepresentation cb = iterator->second;
return LookupDartClosure(cb.name, cb.class_name, cb.library_path);
}
return Dart_Null();
}
int64_t DartCallbackCache::GetCallbackHandle(const std::string& name,
const std::string& class_name,
const std::string& library_path) {
std::lock_guard<std::mutex> lock(mutex_);
std::hash<std::string> hasher;
int64_t hash = hasher(name);
hash += hasher(class_name);
hash += hasher(library_path);
if (cache_.find(hash) == cache_.end()) {
cache_[hash] = {name, class_name, library_path};
SaveCacheToDisk();
}
return hash;
}
std::unique_ptr<DartCallbackRepresentation>
DartCallbackCache::GetCallbackInformation(int64_t handle) {
std::lock_guard<std::mutex> lock(mutex_);
auto iterator = cache_.find(handle);
if (iterator != cache_.end()) {
return std::make_unique<DartCallbackRepresentation>(iterator->second);
}
return nullptr;
}
void DartCallbackCache::SaveCacheToDisk() {
// Cache JSON format
// [
// {
// "hash": 42,
// "representation": {
// "name": "...",
// "class_name": "...",
// "library_path": "..."
// }
// },
// {
// ...
// }
// ]
StringBuffer s;
Writer<StringBuffer> writer(s);
writer.StartArray();
for (auto iterator = cache_.begin(); iterator != cache_.end(); ++iterator) {
int64_t hash = iterator->first;
DartCallbackRepresentation cb = iterator->second;
writer.StartObject();
writer.Key(kHandleKey);
writer.Int64(hash);
writer.Key(kRepresentationKey);
writer.StartObject();
writer.Key(kNameKey);
writer.String(cb.name.c_str());
writer.Key(kClassNameKey);
writer.String(cb.class_name.c_str());
writer.Key(kLibraryPathKey);
writer.String(cb.library_path.c_str());
writer.EndObject();
writer.EndObject();
}
writer.EndArray();
std::ofstream output(cache_path_);
output << s.GetString();
output.close();
}
void DartCallbackCache::LoadCacheFromDisk() {
std::lock_guard<std::mutex> lock(mutex_);
// Don't reload the cache if it's already populated.
if (!cache_.empty()) {
return;
}
std::ifstream input(cache_path_);
if (!input) {
return;
}
std::string cache_contents{std::istreambuf_iterator<char>(input),
std::istreambuf_iterator<char>()};
Document d;
d.Parse(cache_contents.c_str());
if (d.HasParseError() || !d.IsArray()) {
FML_LOG(WARNING) << "Could not parse callback cache, aborting restore";
// TODO(bkonyi): log and bail (delete cache?)
return;
}
const auto entries = d.GetArray();
for (auto it = entries.begin(); it != entries.end(); ++it) {
const auto root_obj = it->GetObject();
const auto representation = root_obj[kRepresentationKey].GetObject();
const int64_t hash = root_obj[kHandleKey].GetInt64();
DartCallbackRepresentation cb;
cb.name = representation[kNameKey].GetString();
cb.class_name = representation[kClassNameKey].GetString();
cb.library_path = representation[kLibraryPathKey].GetString();
cache_[hash] = cb;
}
}
Dart_Handle DartCallbackCache::LookupDartClosure(
const std::string& name,
const std::string& class_name,
const std::string& library_path) {
Dart_Handle closure_name = ToDart(name);
Dart_Handle library_name =
library_path.empty() ? Dart_Null() : ToDart(library_path);
Dart_Handle cls_name = class_name.empty() ? Dart_Null() : ToDart(class_name);
DART_CHECK_VALID(closure_name);
DART_CHECK_VALID(library_name);
DART_CHECK_VALID(cls_name);
Dart_Handle library;
if (library_name == Dart_Null()) {
library = Dart_RootLibrary();
} else {
library = Dart_LookupLibrary(library_name);
}
DART_CHECK_VALID(library);
Dart_Handle closure;
if (Dart_IsNull(cls_name)) {
closure = Dart_GetField(library, closure_name);
} else {
Dart_Handle cls = Dart_GetClass(library, cls_name);
DART_CHECK_VALID(cls);
if (Dart_IsNull(cls)) {
closure = Dart_Null();
} else {
closure = Dart_GetStaticMethodClosure(library, cls, closure_name);
}
}
DART_CHECK_VALID(closure);
return closure;
}
} // namespace blink