diff options
author | John Abd-El-Malek <jam@chromium.org> | 2015-04-02 10:29:35 -0700 |
---|---|---|
committer | John Abd-El-Malek <jam@chromium.org> | 2015-04-02 17:31:11 +0000 |
commit | 537a670451020f4764d511cbdf8e30ec91ef897c (patch) | |
tree | d2868da2b0d33dc7ed8c8e709ae4a7f5bd5aefd8 /mojo/common | |
parent | 83653dd1da59dfa7ddd9e48d4cd507a11cefd968 (diff) | |
download | chromium_src-537a670451020f4764d511cbdf8e30ec91ef897c.zip chromium_src-537a670451020f4764d511cbdf8e30ec91ef897c.tar.gz chromium_src-537a670451020f4764d511cbdf8e30ec91ef897c.tar.bz2 |
Get mojo_shell building inside chromium checkout.
This brings in mojo_shell and the necessary services to make html_viewer work.
This is copied from the Mojo repo at 272fbba5887d66fc0111e2ab44c1edf67b7f23e0.
R=scottmg@chromium.org
Review URL: https://codereview.chromium.org/1049993002
Cr-Commit-Position: refs/heads/master@{#323528}
Diffstat (limited to 'mojo/common')
-rw-r--r-- | mojo/common/BUILD.gn | 21 | ||||
-rw-r--r-- | mojo/common/DEPS | 9 | ||||
-rw-r--r-- | mojo/common/data_pipe_file_utils.cc | 79 | ||||
-rw-r--r-- | mojo/common/data_pipe_utils.cc | 31 | ||||
-rw-r--r-- | mojo/common/data_pipe_utils.h | 17 | ||||
-rw-r--r-- | mojo/common/trace_controller_impl.cc | 49 | ||||
-rw-r--r-- | mojo/common/trace_controller_impl.h | 39 | ||||
-rw-r--r-- | mojo/common/tracing_impl.cc | 30 | ||||
-rw-r--r-- | mojo/common/tracing_impl.h | 35 | ||||
-rw-r--r-- | mojo/common/weak_binding_set.h | 106 | ||||
-rw-r--r-- | mojo/common/weak_interface_ptr_set.h | 88 |
11 files changed, 504 insertions, 0 deletions
diff --git a/mojo/common/BUILD.gn b/mojo/common/BUILD.gn index 8044698..8f5dfaa 100644 --- a/mojo/common/BUILD.gn +++ b/mojo/common/BUILD.gn @@ -18,6 +18,7 @@ component("common_base") { sources = [ "common_type_converters.cc", "common_type_converters.h", + "data_pipe_file_utils.cc", "data_pipe_utils.cc", "data_pipe_utils.h", "handle_watcher.cc", @@ -27,6 +28,8 @@ component("common_base") { "message_pump_mojo_handler.h", "time_helper.cc", "time_helper.h", + "weak_binding_set.h", + "weak_interface_ptr_set.h", ] defines = [ "MOJO_COMMON_IMPLEMENTATION" ] @@ -76,3 +79,21 @@ test("mojo_common_unittests") { "message_pump_mojo_unittest.cc", ] } + +if (!is_component_build) { + source_set("tracing_impl") { + sources = [ + "trace_controller_impl.cc", + "trace_controller_impl.h", + "tracing_impl.cc", + "tracing_impl.h", + ] + + deps = [ + "//base", + "//third_party/mojo/src/mojo/public/cpp/application", + "//third_party/mojo/src/mojo/public/cpp/bindings", + "//mojo/services/tracing:bindings", + ] + } +} diff --git a/mojo/common/DEPS b/mojo/common/DEPS index f3ee1c8..81bd86c 100644 --- a/mojo/common/DEPS +++ b/mojo/common/DEPS @@ -4,3 +4,12 @@ include_rules = [ "+mojo/common", "+third_party/mojo/src/mojo/public", ] + +specific_include_rules = {
+ "trace_controller_impl\.h": [
+ "+mojo/services/tracing/tracing.mojom.h"
+ ],
+ "tracing_impl\.h": [
+ "+mojo/services/tracing/tracing.mojom.h"
+ ],
+}
diff --git a/mojo/common/data_pipe_file_utils.cc b/mojo/common/data_pipe_file_utils.cc new file mode 100644 index 0000000..4bc2686 --- /dev/null +++ b/mojo/common/data_pipe_file_utils.cc @@ -0,0 +1,79 @@ +// Copyright 2014 The Chromium 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 "mojo/common/data_pipe_utils.h" + +#include "base/files/file_path.h" +#include "base/files/file_util.h" +#include "base/files/scoped_file.h" +#include "base/location.h" +#include "base/task_runner_util.h" + +namespace mojo { +namespace common { +namespace { + +bool BlockingCopyFromFile(const base::FilePath& source, + ScopedDataPipeProducerHandle destination, + uint32_t skip) { + base::File file(source, base::File::FLAG_OPEN | base::File::FLAG_READ); + if (!file.IsValid()) + return false; + if (file.Seek(base::File::FROM_BEGIN, skip) != skip) { + LOG(ERROR) << "Seek of " << skip << " in " << source.value() << " failed"; + return false; + } + for (;;) { + void* buffer = nullptr; + uint32_t buffer_num_bytes = 0; + MojoResult result = + BeginWriteDataRaw(destination.get(), &buffer, &buffer_num_bytes, + MOJO_WRITE_DATA_FLAG_NONE); + if (result == MOJO_RESULT_OK) { + int bytes_read = + file.ReadAtCurrentPos(static_cast<char*>(buffer), buffer_num_bytes); + if (bytes_read >= 0) { + EndWriteDataRaw(destination.get(), bytes_read); + if (bytes_read == 0) { + // eof + return true; + } + } else { + // error + EndWriteDataRaw(destination.get(), 0); + return false; + } + } else if (result == MOJO_RESULT_SHOULD_WAIT) { + result = Wait(destination.get(), MOJO_HANDLE_SIGNAL_WRITABLE, + MOJO_DEADLINE_INDEFINITE, nullptr); + if (result != MOJO_RESULT_OK) { + // If the consumer handle was closed, then treat as EOF. + return result == MOJO_RESULT_FAILED_PRECONDITION; + } + } else { + // If the consumer handle was closed, then treat as EOF. + return result == MOJO_RESULT_FAILED_PRECONDITION; + } + } +#if !defined(OS_WIN) + NOTREACHED(); + return false; +#endif +} + +} // namespace + +void CopyFromFile(const base::FilePath& source, + ScopedDataPipeProducerHandle destination, + uint32_t skip, + base::TaskRunner* task_runner, + const base::Callback<void(bool)>& callback) { + base::PostTaskAndReplyWithResult(task_runner, FROM_HERE, + base::Bind(&BlockingCopyFromFile, source, + base::Passed(&destination), skip), + callback); +} + +} // namespace common +} // namespace mojo diff --git a/mojo/common/data_pipe_utils.cc b/mojo/common/data_pipe_utils.cc index c99c666..a280dad 100644 --- a/mojo/common/data_pipe_utils.cc +++ b/mojo/common/data_pipe_utils.cc @@ -71,6 +71,37 @@ bool BlockingCopyToString(ScopedDataPipeConsumerHandle source, source.Pass(), base::Bind(&CopyToStringHelper, result)); } +bool MOJO_COMMON_EXPORT BlockingCopyFromString( + const std::string& source, + const ScopedDataPipeProducerHandle& destination) { + auto it = source.begin(); + for (;;) { + void* buffer = nullptr; + uint32_t buffer_num_bytes = 0; + MojoResult result = + BeginWriteDataRaw(destination.get(), &buffer, &buffer_num_bytes, + MOJO_WRITE_DATA_FLAG_NONE); + if (result == MOJO_RESULT_OK) { + char* char_buffer = static_cast<char*>(buffer); + uint32_t byte_index = 0; + while (it != source.end() && byte_index < buffer_num_bytes) { + char_buffer[byte_index++] = *it++; + } + EndWriteDataRaw(destination.get(), byte_index); + } else if (result == MOJO_RESULT_SHOULD_WAIT) { + result = Wait(destination.get(), MOJO_HANDLE_SIGNAL_WRITABLE, + MOJO_DEADLINE_INDEFINITE, nullptr); + if (result != MOJO_RESULT_OK) { + // If the consumer handle was closed, then treat as EOF. + return result == MOJO_RESULT_FAILED_PRECONDITION; + } + } else { + // If the consumer handle was closed, then treat as EOF. + return result == MOJO_RESULT_FAILED_PRECONDITION; + } + } +} + bool BlockingCopyToFile(ScopedDataPipeConsumerHandle source, const base::FilePath& destination) { base::ScopedFILE fp(base::OpenFile(destination, "wb")); diff --git a/mojo/common/data_pipe_utils.h b/mojo/common/data_pipe_utils.h index 65a0b8b..5b32cfd 100644 --- a/mojo/common/data_pipe_utils.h +++ b/mojo/common/data_pipe_utils.h @@ -28,6 +28,13 @@ void MOJO_COMMON_EXPORT CopyToFile( base::TaskRunner* task_runner, const base::Callback<void(bool /*success*/)>& callback); +void MOJO_COMMON_EXPORT +CopyFromFile(const base::FilePath& source, + ScopedDataPipeProducerHandle destination, + uint32_t skip, + base::TaskRunner* task_runner, + const base::Callback<void(bool /*success*/)>& callback); + // Copies the data from |source| into |contents| and returns true on success and // false on error. In case of I/O error, |contents| holds the data that could // be read from source before the error occurred. @@ -35,6 +42,16 @@ bool MOJO_COMMON_EXPORT BlockingCopyToString( ScopedDataPipeConsumerHandle source, std::string* contents); +bool MOJO_COMMON_EXPORT BlockingCopyFromString( + const std::string& source, + const ScopedDataPipeProducerHandle& destination); + +// Synchronously copies data from source to the destination file returning true +// on success and false on error. In case of an error, |destination| holds the +// data that could be read from the source before the error occured. +bool MOJO_COMMON_EXPORT BlockingCopyToFile(ScopedDataPipeConsumerHandle source, + const base::FilePath& destination); + } // namespace common } // namespace mojo diff --git a/mojo/common/trace_controller_impl.cc b/mojo/common/trace_controller_impl.cc new file mode 100644 index 0000000..178f4cd --- /dev/null +++ b/mojo/common/trace_controller_impl.cc @@ -0,0 +1,49 @@ +// Copyright 2015 The Chromium 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 "mojo/common/trace_controller_impl.h" + +#include "base/trace_event/trace_event.h" +#include "third_party/mojo/src/mojo/public/cpp/application/application_connection.h" +#include "third_party/mojo/src/mojo/public/cpp/application/application_impl.h" + +namespace mojo { + +TraceControllerImpl::TraceControllerImpl( + InterfaceRequest<tracing::TraceController> request) + : binding_(this, request.Pass()) { +} + +TraceControllerImpl::~TraceControllerImpl() { +} + +void TraceControllerImpl::StartTracing( + const String& categories, + tracing::TraceDataCollectorPtr collector) { + DCHECK(!collector_.get()); + collector_ = collector.Pass(); + std::string categories_str = categories.To<std::string>(); + base::trace_event::TraceLog::GetInstance()->SetEnabled( + base::trace_event::CategoryFilter(categories_str), + base::trace_event::TraceLog::RECORDING_MODE, + base::trace_event::TraceOptions(base::trace_event::RECORD_UNTIL_FULL)); +} + +void TraceControllerImpl::StopTracing() { + base::trace_event::TraceLog::GetInstance()->SetDisabled(); + + base::trace_event::TraceLog::GetInstance()->Flush( + base::Bind(&TraceControllerImpl::SendChunk, base::Unretained(this))); +} + +void TraceControllerImpl::SendChunk( + const scoped_refptr<base::RefCountedString>& events_str, + bool has_more_events) { + collector_->DataCollected(mojo::String(events_str->data())); + if (!has_more_events) { + collector_.reset(); + } +} + +} // namespace mojo diff --git a/mojo/common/trace_controller_impl.h b/mojo/common/trace_controller_impl.h new file mode 100644 index 0000000..3f9225a --- /dev/null +++ b/mojo/common/trace_controller_impl.h @@ -0,0 +1,39 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef MOJO_COMMON_TRACING_CONTROLLER_IMPL_H_ +#define MOJO_COMMON_TRACING_CONTROLLER_IMPL_H_ + +#include "base/memory/ref_counted_memory.h" +#include "mojo/services/tracing/tracing.mojom.h" +#include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h" +#include "third_party/mojo/src/mojo/public/cpp/bindings/strong_binding.h" + +namespace mojo { + +class TraceControllerImpl : public tracing::TraceController { + public: + explicit TraceControllerImpl( + InterfaceRequest<tracing::TraceController> request); + + ~TraceControllerImpl() override; + + private: + // tracing::TraceController implementation: + void StartTracing(const String& categories, + tracing::TraceDataCollectorPtr collector) override; + void StopTracing() override; + + void SendChunk(const scoped_refptr<base::RefCountedString>& events_str, + bool has_more_events); + + tracing::TraceDataCollectorPtr collector_; + StrongBinding<tracing::TraceController> binding_; + + DISALLOW_COPY_AND_ASSIGN(TraceControllerImpl); +}; + +} // namespace mojo + +#endif // MOJO_COMMON_TRACING_CONTROLLER_IMPL_H_ diff --git a/mojo/common/tracing_impl.cc b/mojo/common/tracing_impl.cc new file mode 100644 index 0000000..420a475 --- /dev/null +++ b/mojo/common/tracing_impl.cc @@ -0,0 +1,30 @@ +// Copyright 2014 The Chromium 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 "mojo/common/tracing_impl.h" + +#include "base/trace_event/trace_event.h" +#include "mojo/common/trace_controller_impl.h" +#include "third_party/mojo/src/mojo/public/cpp/application/application_connection.h" +#include "third_party/mojo/src/mojo/public/cpp/application/application_impl.h" + +namespace mojo { + +TracingImpl::TracingImpl() { +} + +TracingImpl::~TracingImpl() { +} + +void TracingImpl::Initialize(ApplicationImpl* app) { + ApplicationConnection* connection = app->ConnectToApplication("mojo:tracing"); + connection->AddService(this); +} + +void TracingImpl::Create(ApplicationConnection* connection, + InterfaceRequest<tracing::TraceController> request) { + new TraceControllerImpl(request.Pass()); +} + +} // namespace mojo diff --git a/mojo/common/tracing_impl.h b/mojo/common/tracing_impl.h new file mode 100644 index 0000000..c9118e2 --- /dev/null +++ b/mojo/common/tracing_impl.h @@ -0,0 +1,35 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef MOJO_COMMON_TRACING_IMPL_H_ +#define MOJO_COMMON_TRACING_IMPL_H_ + +#include "base/macros.h" +#include "mojo/services/tracing/tracing.mojom.h" +#include "third_party/mojo/src/mojo/public/cpp/application/interface_factory.h" + +namespace mojo { + +class ApplicationImpl; + +class TracingImpl : public InterfaceFactory<tracing::TraceController> { + public: + TracingImpl(); + ~TracingImpl() override; + + // This connects to the tracing service and registers ourselves to provide + // tracing data on demand. + void Initialize(ApplicationImpl* app); + + private: + // InterfaceFactory<tracing::TraceController> implementation. + void Create(ApplicationConnection* connection, + InterfaceRequest<tracing::TraceController> request) override; + + DISALLOW_COPY_AND_ASSIGN(TracingImpl); +}; + +} // namespace mojo + +#endif // MOJO_COMMON_TRACING_IMPL_H_ diff --git a/mojo/common/weak_binding_set.h b/mojo/common/weak_binding_set.h new file mode 100644 index 0000000..8738388 --- /dev/null +++ b/mojo/common/weak_binding_set.h @@ -0,0 +1,106 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef MOJO_COMMON_WEAK_BINDING_SET_H_ +#define MOJO_COMMON_WEAK_BINDING_SET_H_ + +#include <algorithm> +#include <vector> + +#include "base/memory/weak_ptr.h" +#include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h" + +namespace mojo { + +template <typename Interface> +class WeakBinding; + +// Use this class to manage a set of weak pointers to bindings each of which is +// owned by the pipe they are bound to. +template <typename Interface> +class WeakBindingSet : public ErrorHandler { + public: + WeakBindingSet() : error_handler_(nullptr) {} + ~WeakBindingSet() { CloseAllBindings(); } + + void set_error_handler(ErrorHandler* error_handler) { + error_handler_ = error_handler; + } + + void AddBinding(Interface* impl, InterfaceRequest<Interface> request) { + auto binding = new WeakBinding<Interface>(impl, request.Pass()); + binding->set_error_handler(this); + bindings_.push_back(binding->GetWeakPtr()); + } + + void CloseAllBindings() { + for (const auto& it : bindings_) { + if (it) + it->Close(); + } + bindings_.clear(); + } + + private: + // ErrorHandler implementation. + void OnConnectionError() override { + // Clear any deleted bindings. + bindings_.erase( + std::remove_if(bindings_.begin(), bindings_.end(), + [](const base::WeakPtr<WeakBinding<Interface>>& p) { + return p.get() == nullptr; + }), + bindings_.end()); + + if (error_handler_) + error_handler_->OnConnectionError(); + } + + ErrorHandler* error_handler_; + std::vector<base::WeakPtr<WeakBinding<Interface>>> bindings_; + + DISALLOW_COPY_AND_ASSIGN(WeakBindingSet); +}; + +template <typename Interface> +class WeakBinding : public ErrorHandler { + public: + WeakBinding(Interface* impl, InterfaceRequest<Interface> request) + : binding_(impl, request.Pass()), + error_handler_(nullptr), + weak_ptr_factory_(this) { + binding_.set_error_handler(this); + } + + ~WeakBinding() override {} + + void set_error_handler(ErrorHandler* error_handler) { + error_handler_ = error_handler; + } + + base::WeakPtr<WeakBinding> GetWeakPtr() { + return weak_ptr_factory_.GetWeakPtr(); + } + + void Close() { binding_.Close(); } + + // ErrorHandler implementation. + void OnConnectionError() override { + ErrorHandler* error_handler = error_handler_; + delete this; + if (error_handler) + error_handler->OnConnectionError(); + } + + private: + mojo::Binding<Interface> binding_; + ErrorHandler* error_handler_; + base::WeakPtrFactory<WeakBinding> weak_ptr_factory_; + + DISALLOW_COPY_AND_ASSIGN(WeakBinding); +}; + +} // namespace mojo + +#endif // MOJO_COMMON_WEAK_BINDING_SET_H_ diff --git a/mojo/common/weak_interface_ptr_set.h b/mojo/common/weak_interface_ptr_set.h new file mode 100644 index 0000000..e2e88a5 --- /dev/null +++ b/mojo/common/weak_interface_ptr_set.h @@ -0,0 +1,88 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef MOJO_COMMON_WEAK_INTERFACE_PTR_SET_H_ +#define MOJO_COMMON_WEAK_INTERFACE_PTR_SET_H_ + +#include <vector> + +#include "base/memory/weak_ptr.h" +#include "third_party/mojo/src/mojo/public/cpp/bindings/interface_ptr.h" + +namespace mojo { + +template <typename Interface> +class WeakInterfacePtr; + +template <typename Interface> +class WeakInterfacePtrSet { + public: + WeakInterfacePtrSet() {} + ~WeakInterfacePtrSet() { CloseAll(); } + + void AddInterfacePtr(InterfacePtr<Interface> ptr) { + auto weak_interface_ptr = new WeakInterfacePtr<Interface>(ptr.Pass()); + ptrs_.push_back(weak_interface_ptr->GetWeakPtr()); + ClearNullInterfacePtrs(); + } + + template <typename FunctionType> + void ForAllPtrs(FunctionType function) { + for (const auto& it : ptrs_) { + if (it) + function(it->get()); + } + ClearNullInterfacePtrs(); + } + + void CloseAll() { + for (const auto& it : ptrs_) { + if (it) + it->Close(); + } + ptrs_.clear(); + } + + private: + using WPWIPI = base::WeakPtr<WeakInterfacePtr<Interface>>; + + void ClearNullInterfacePtrs() { + ptrs_.erase(std::remove_if(ptrs_.begin(), ptrs_.end(), [](const WPWIPI& p) { + return p.get() == nullptr; + }), ptrs_.end()); + } + + std::vector<WPWIPI> ptrs_; +}; + +template <typename Interface> +class WeakInterfacePtr : public ErrorHandler { + public: + explicit WeakInterfacePtr(InterfacePtr<Interface> ptr) + : ptr_(ptr.Pass()), weak_ptr_factory_(this) { + ptr_.set_error_handler(this); + } + ~WeakInterfacePtr() override {} + + void Close() { ptr_.reset(); } + + Interface* get() { return ptr_.get(); } + + base::WeakPtr<WeakInterfacePtr> GetWeakPtr() { + return weak_ptr_factory_.GetWeakPtr(); + } + + private: + // ErrorHandler implementation + void OnConnectionError() override { delete this; } + + InterfacePtr<Interface> ptr_; + base::WeakPtrFactory<WeakInterfacePtr> weak_ptr_factory_; + + DISALLOW_COPY_AND_ASSIGN(WeakInterfacePtr); +}; + +} // namespace mojo + +#endif // MOJO_COMMON_WEAK_INTERFACE_PTR_SET_H_ |