Skip to content

Commit

Permalink
Initial modified libraries support (flutter#2845)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmccutchan authored Aug 1, 2016
1 parent e849e09 commit b23f07e
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 21 deletions.
2 changes: 1 addition & 1 deletion DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ vars = {

# Note: When updating the Dart revision, ensure that all entries that are
# dependencies of dart are also updated
'dart_revision': 'd37ea681f5d80a3ab150b709baaeb1fef7a12cae',
'dart_revision': '59e67cc36d0877949719160de8959a11db4a8027',
'dart_boringssl_revision': 'daeafc22c66ad48f6b32fc8d3362eb9ba31b774e',
'dart_observatory_packages_revision': 'e5e1e543bea10d4bed95b22ad3e7aa2b20a23584',
'dart_root_certificates_revision': 'aed07942ce98507d2be28cbd29e879525410c7fc',
Expand Down
19 changes: 15 additions & 4 deletions flutter/tonic/dart_isolate_reloader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ class DartIsolateReloader::LoadResult {
LoadResult(intptr_t tag,
const std::string& url,
const std::string& library_url,
const std::string& resolved_url,
std::vector<uint8_t> payload)
: success_(true),
tag_(tag),
url_(url),
library_url_(library_url),
resolved_url_(resolved_url),
payload_(std::move(payload)) {
DCHECK(success());
}
Expand All @@ -58,6 +60,10 @@ class DartIsolateReloader::LoadResult {
return StdStringToDart(error_);
}
Dart_Handle uri = StdStringToDart(url_);
Dart_Handle resolved_uri = Dart_Null();
if (!resolved_url_.empty()) {
resolved_uri = StdStringToDart(resolved_url_);
}
Dart_Handle library = Dart_Null();
if (!library_url_.empty()) {
library = Dart_LookupLibrary(StdStringToDart(library_url_));
Expand All @@ -67,13 +73,13 @@ class DartIsolateReloader::LoadResult {
Dart_Handle result = Dart_Null();
switch (tag_) {
case Dart_kImportTag:
result = Dart_LoadLibrary(uri, source, 0, 0);
result = Dart_LoadLibrary(uri, resolved_uri, source, 0, 0);
break;
case Dart_kSourceTag:
result = Dart_LoadSource(library, uri, source, 0, 0);
result = Dart_LoadSource(library, uri, resolved_uri, source, 0, 0);
break;
case Dart_kScriptTag:
result = Dart_LoadScript(uri, source, 0, 0);
result = Dart_LoadScript(uri, resolved_uri, source, 0, 0);
break;
}

Expand All @@ -88,6 +94,7 @@ class DartIsolateReloader::LoadResult {
intptr_t tag_;
std::string url_;
std::string library_url_;
std::string resolved_url_;
std::string error_;
std::vector<uint8_t> payload_;
};
Expand Down Expand Up @@ -153,7 +160,8 @@ class DartIsolateReloader::LoadRequest : public DataPipeDrainer::Client {
}

protected:
void OnStreamAvailable(mojo::ScopedDataPipeConsumerHandle pipe) {
void OnStreamAvailable(mojo::ScopedDataPipeConsumerHandle pipe,
const std::string& resolved_url) {
if (!pipe.is_valid()) {
std::unique_ptr<DartIsolateReloader::LoadResult> result(
new DartIsolateReloader::LoadResult(
Expand All @@ -167,6 +175,7 @@ class DartIsolateReloader::LoadRequest : public DataPipeDrainer::Client {
delete this;
return;
}
resolved_url_ = resolved_url;
drainer_.reset(new DataPipeDrainer(this, pipe.Pass()));
}

Expand All @@ -182,6 +191,7 @@ class DartIsolateReloader::LoadRequest : public DataPipeDrainer::Client {
new DartIsolateReloader::LoadResult(tag_,
url_,
library_url_,
resolved_url_,
std::move(buffer_)));
isolate_reloader_->PostResult(std::move(result));
// We are finished with this request.
Expand All @@ -193,6 +203,7 @@ class DartIsolateReloader::LoadRequest : public DataPipeDrainer::Client {
intptr_t tag_;
std::string url_;
std::string library_url_;
std::string resolved_url_;
std::vector<uint8_t> buffer_;
std::unique_ptr<DataPipeDrainer> drainer_;

Expand Down
15 changes: 13 additions & 2 deletions flutter/tonic/dart_library_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "flutter/tonic/dart_library_loader.h"

#include "base/callback.h"
#include "base/strings/string_util.h"
#include "base/files/file_util.h"
#include "base/trace_event/trace_event.h"
#include "flutter/tonic/dart_api_scope.h"
#include "flutter/tonic/dart_converter.h"
Expand Down Expand Up @@ -46,18 +48,23 @@ class DartLibraryLoader::Job : public DartDependency,

const std::string& name() const { return name_; }

const std::string& resolved_url() const { return resolved_url_; }

protected:
DartLibraryLoader* loader_;
// TODO(abarth): Should we be using SharedBuffer to buffer the data?
std::vector<uint8_t> buffer_;

private:
void OnStreamAvailable(mojo::ScopedDataPipeConsumerHandle pipe) {
void OnStreamAvailable(mojo::ScopedDataPipeConsumerHandle pipe,
const std::string& resolved_url) {
if (!pipe.is_valid()) {
loader_->DidFailJob(this);
return;
}
drainer_ = std::unique_ptr<DataPipeDrainer>(new DataPipeDrainer(this, pipe.Pass()));
resolved_url_ = resolved_url;
drainer_ = std::unique_ptr<DataPipeDrainer>(
new DataPipeDrainer(this, pipe.Pass()));
}

// DataPipeDrainer::Client
Expand All @@ -68,6 +75,7 @@ class DartLibraryLoader::Job : public DartDependency,
// Subclasses must implement OnDataComplete.

std::string name_;
std::string resolved_url_;
std::unique_ptr<DataPipeDrainer> drainer_;

base::WeakPtrFactory<Job> weak_factory_;
Expand Down Expand Up @@ -291,11 +299,13 @@ void DartLibraryLoader::DidCompleteImportJob(
if (job->should_load_as_script()) {
result = Dart_LoadScript(
StdStringToDart(job->name()),
StdStringToDart(job->resolved_url()),
Dart_NewStringFromUTF8(buffer.data(), buffer.size()),
0, 0);
} else {
result = Dart_LoadLibrary(
StdStringToDart(job->name()),
StdStringToDart(job->resolved_url()),
Dart_NewStringFromUTF8(buffer.data(), buffer.size()),
0, 0);
}
Expand All @@ -319,6 +329,7 @@ void DartLibraryLoader::DidCompleteSourceJob(
Dart_Handle result = Dart_LoadSource(
Dart_HandleFromPersistent(job->library()),
StdStringToDart(job->name()),
StdStringToDart(job->resolved_url()),
Dart_NewStringFromUTF8(buffer.data(), buffer.size()), 0, 0);

if (Dart_IsError(result)) {
Expand Down
2 changes: 1 addition & 1 deletion flutter/tonic/dart_library_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class DartLibraryLoader {
~DartLibraryLoader();

// TODO(dart): This can be called both on the main thread from application
// solates or from the handle watcher isolate thread.
// isolates or from the handle watcher isolate thread.
static Dart_Handle HandleLibraryTag(Dart_LibraryTag tag,
Dart_Handle library,
Dart_Handle url);
Expand Down
3 changes: 2 additions & 1 deletion flutter/tonic/dart_library_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

namespace blink {

typedef base::Callback<void(mojo::ScopedDataPipeConsumerHandle)>
typedef base::Callback<void(mojo::ScopedDataPipeConsumerHandle,
const std::string&)>
DataPipeConsumerCallback;

class DartLibraryProvider {
Expand Down
25 changes: 25 additions & 0 deletions sky/engine/core/script/dart_init.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,29 @@ void IsolateShutdownCallback(void* callback_data) {
delete dart_state;
}

bool DartFileModifiedCallback(const char* source_url,
int64_t since_ms) {
std::string url(source_url);
if (!base::StartsWithASCII(url, "file:", true)) {
// Assume modified.
return true;
}
base::ReplaceFirstSubstringAfterOffset(&url, 0, "file:", "");
base::FilePath path(url);
base::File::Info file_info;
if (!base::GetFileInfo(path, &file_info)) {
// Assume modified.
return true;
}
int64_t since_seconds = since_ms / base::Time::kMillisecondsPerSecond;
int64_t since_milliseconds =
since_ms - (since_seconds * base::Time::kMillisecondsPerSecond);
base::Time since_time = base::Time::FromTimeT(since_seconds) +
base::TimeDelta::FromMilliseconds(since_milliseconds);
// TODO(johnmccutchan): Remove 'true ||' after Todd's fix has landed.
return true || file_info.last_modified > since_time;
}

void ThreadExitCallback() {
#ifdef OS_ANDROID
DartJni::OnThreadExit();
Expand Down Expand Up @@ -564,6 +587,8 @@ void InitDartVM() {
Dart_SetEmbedderTimelineCallbacks(&EmbedderTimelineStartRecording,
&EmbedderTimelineStopRecording);

Dart_SetFileModifiedCallback(&DartFileModifiedCallback);

{
TRACE_EVENT0("flutter", "Dart_Initialize");
char* init_error = Dart_Initialize(
Expand Down
6 changes: 3 additions & 3 deletions sky/engine/core/script/dart_service_isolate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,13 @@ Dart_Handle DartServiceIsolate::GetSource(const char* name) {
Dart_Handle DartServiceIsolate::LoadScript(const char* name) {
Dart_Handle url = Dart_NewStringFromCString("dart:vmservice_sky");
Dart_Handle source = GetSource(name);
return Dart_LoadScript(url, source, 0, 0);
return Dart_LoadScript(url, Dart_Null(), source, 0, 0);
}

Dart_Handle DartServiceIsolate::LoadSource(Dart_Handle library, const char* name) {
Dart_Handle url = Dart_NewStringFromCString(name);
Dart_Handle source = GetSource(name);
return Dart_LoadSource(library, url, source, 0, 0);
return Dart_LoadSource(library, url, Dart_Null(), source, 0, 0);
}

Dart_Handle DartServiceIsolate::LoadResource(Dart_Handle library,
Expand Down Expand Up @@ -282,7 +282,7 @@ Dart_Handle DartServiceIsolate::LibraryTagHandler(Dart_LibraryTag tag,
if (Dart_IsError(source)) {
return source;
}
return Dart_LoadSource(library, url, source, 0, 0);
return Dart_LoadSource(library, url, Dart_Null(), source, 0, 0);
}


Expand Down
3 changes: 2 additions & 1 deletion sky/shell/dart/dart_library_provider_files.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ void DartLibraryProviderFiles::GetLibraryAsStream(
const std::string& name,
blink::DataPipeConsumerCallback callback) {
mojo::DataPipe pipe;
callback.Run(pipe.consumer_handle.Pass());
base::FilePath path = GetFilePathForURL(name);
base::FilePath source = base::MakeAbsoluteFilePath(path);
std::string source_url = "file://" + source.value();
callback.Run(pipe.consumer_handle.Pass(), source_url);
scoped_refptr<base::TaskRunner> runner =
base::WorkerPool::GetTaskRunner(true);
mojo::common::CopyFromFile(source, pipe.producer_handle.Pass(), 0,
Expand Down
1 change: 1 addition & 0 deletions sky/shell/diagnostic/diagnostic_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ void DiagnosticServer::Start() {

Dart_Handle diagnostic_library = Dart_LoadLibrary(
Dart_NewStringFromCString("dart:diagnostic_server"),
Dart_Null(),
Dart_NewStringFromUTF8(reinterpret_cast<const uint8_t*>(source),
source_length),
0, 0);
Expand Down
30 changes: 22 additions & 8 deletions sky/tools/sky_snapshot/loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Loader {
base::FilePath GetFilePathForURL(std::string url);
base::FilePath GetFilePathForPackageURL(std::string url);
base::FilePath GetFilePathForFileURL(std::string url);
std::string Fetch(const std::string& url);
std::string Fetch(const std::string& url, std::string* resolved_url);
Dart_Handle Import(Dart_Handle url);
Dart_Handle Source(Dart_Handle library, Dart_Handle url);

Expand Down Expand Up @@ -159,10 +159,13 @@ base::FilePath Loader::GetFilePathForFileURL(std::string url) {
return base::FilePath(url);
}

std::string Loader::Fetch(const std::string& url) {
std::string Loader::Fetch(const std::string& url,
std::string* resolved_url) {
base::FilePath path = GetFilePathForURL(url);
base::FilePath absolute_path = base::MakeAbsoluteFilePath(path);
*resolved_url = "file://" + absolute_path.value();
std::string source;
if (!base::ReadFileToString(base::MakeAbsoluteFilePath(path), &source)) {
if (!base::ReadFileToString(absolute_path, &source)) {
fprintf(stderr, "error: Unable to find Dart library '%s'.\n", url.c_str());
exit(1);
}
Expand All @@ -171,15 +174,22 @@ std::string Loader::Fetch(const std::string& url) {
}

Dart_Handle Loader::Import(Dart_Handle url) {
Dart_Handle source = StringToDart(Fetch(StringFromDart(url)));
Dart_Handle result = Dart_LoadLibrary(url, source, 0, 0);
std::string resolved_url;
Dart_Handle source = StringToDart(Fetch(StringFromDart(url), &resolved_url));
Dart_Handle result = Dart_LoadLibrary(url,
StringToDart(resolved_url),
source, 0, 0);
LogIfError(result);
return result;
}

Dart_Handle Loader::Source(Dart_Handle library, Dart_Handle url) {
Dart_Handle source = StringToDart(Fetch(StringFromDart(url)));
Dart_Handle result = Dart_LoadSource(library, url, source, 0, 0);
std::string resolved_url;
Dart_Handle source = StringToDart(Fetch(StringFromDart(url), &resolved_url));
Dart_Handle result = Dart_LoadSource(library,
url,
StringToDart(resolved_url),
source, 0, 0);
LogIfError(result);
return result;
}
Expand Down Expand Up @@ -226,8 +236,12 @@ Dart_Handle HandleLibraryTag(Dart_LibraryTag tag,
}

void LoadScript(const std::string& url) {
std::string resolved_url;
Dart_Handle source = StringToDart(GetLoader().Fetch(url, &resolved_url));
LogIfError(
Dart_LoadScript(StringToDart(url), StringToDart(GetLoader().Fetch(url)),
Dart_LoadScript(StringToDart(url),
StringToDart(resolved_url),
source,
0, 0));
}

Expand Down

0 comments on commit b23f07e

Please sign in to comment.