Commit 0d388725 authored by ben@chromium.org's avatar ben@chromium.org

Launcher flow

R=sky@chromium.org

Review URL: https://codereview.chromium.org/337563002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@276892 0039d316-1c4b-4281-b951-d872f2087c98
parent fe2357b0
......@@ -99,7 +99,6 @@ ui::EventSource* WindowTreeHostMojo::GetEventSource() {
}
gfx::AcceleratedWidget WindowTreeHostMojo::GetAcceleratedWidget() {
NOTIMPLEMENTED() << "GetAcceleratedWidget";
return gfx::kNullAcceleratedWidget;
}
......
......@@ -9,19 +9,24 @@
#include "mojo/services/public/cpp/view_manager/view_manager_delegate.h"
#include "mojo/services/public/cpp/view_manager/view_observer.h"
#include "mojo/services/public/cpp/view_manager/view_tree_node.h"
#include "mojo/services/public/interfaces/launcher/launcher.mojom.h"
#include "mojo/views/native_widget_view_manager.h"
#include "mojo/views/views_init.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/controls/textfield/textfield_controller.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"
#include "url/gurl.h"
namespace mojo {
namespace examples {
// This is the basics of creating a views widget with a textfield.
// TODO: cleanup!
class Browser : public Application, public view_manager::ViewManagerDelegate {
class Browser : public Application,
public view_manager::ViewManagerDelegate,
public views::TextfieldController {
public:
Browser() : view_manager_(NULL), view_(NULL) {}
......@@ -34,10 +39,13 @@ class Browser : public Application, public view_manager::ViewManagerDelegate {
views_init_.reset(new ViewsInit);
view_manager::ViewManager::Create(this, this);
ConnectTo("mojo:mojo_launcher", &launcher_);
}
void CreateWidget() {
views::Textfield* textfield = new views::Textfield;
textfield->set_controller(this);
views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView;
widget_delegate->GetContentsView()->AddChildView(textfield);
......@@ -67,10 +75,22 @@ class Browser : public Application, public view_manager::ViewManagerDelegate {
view_manager::ViewTreeNode* root) OVERRIDE {
}
// views::TextfieldController:
virtual bool HandleKeyEvent(views::Textfield* sender,
const ui::KeyEvent& key_event) OVERRIDE {
if (key_event.key_code() == ui::VKEY_RETURN) {
GURL url(sender->text());
printf("User entered this URL: %s\n", url.spec().c_str());
launcher_->Launch(url.spec());
}
return false;
}
scoped_ptr<ViewsInit> views_init_;
view_manager::ViewManager* view_manager_;
view_manager::View* view_;
launcher::LauncherPtr launcher_;
DISALLOW_COPY_AND_ASSIGN(Browser);
};
......
// 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/public/cpp/application/application.h"
#include "mojo/services/public/interfaces/launcher/launcher.mojom.h"
namespace mojo {
namespace examples {
class HTMLViewer;
class LaunchableConnection : public InterfaceImpl<launcher::Launchable> {
public:
LaunchableConnection() {}
virtual ~LaunchableConnection() {}
private:
// Overridden from launcher::Launchable:
virtual void OnLaunch(
URLResponsePtr response,
ScopedDataPipeConsumerHandle response_body_stream) MOJO_OVERRIDE {
printf("In HTMLViewer, rendering url: %s\n", response->url.data());
printf("HTML: \n");
for (;;) {
char buf[512];
uint32_t num_bytes = sizeof(buf);
MojoResult result = ReadDataRaw(
response_body_stream.get(),
buf,
&num_bytes,
MOJO_READ_DATA_FLAG_NONE);
if (result == MOJO_RESULT_SHOULD_WAIT) {
Wait(response_body_stream.get(),
MOJO_WAIT_FLAG_READABLE,
MOJO_DEADLINE_INDEFINITE);
} else if (result == MOJO_RESULT_OK) {
fwrite(buf, num_bytes, 1, stdout);
} else {
break;
}
}
printf("\n>>>> EOF <<<<\n\n");
}
};
class HTMLViewer : public Application {
public:
HTMLViewer() {}
virtual ~HTMLViewer() {}
private:
// Overridden from Application:
virtual void Initialize() MOJO_OVERRIDE {
AddService<LaunchableConnection>();
}
};
}
// static
Application* Application::Create() {
return new examples::HTMLViewer;
}
}
......@@ -34,9 +34,11 @@
'mojo_common_unittests',
'mojo_cpp_bindings',
'mojo_geometry_lib',
'mojo_html_viewer',
'mojo_js',
'mojo_js_bindings',
'mojo_js_unittests',
'mojo_launcher',
'mojo_message_generator',
'mojo_native_viewport_service',
'mojo_network_service',
......
......@@ -91,6 +91,23 @@
},
'includes': [ 'build/package_app.gypi' ],
},
{
'target_name': 'mojo_html_viewer',
'type': 'shared_library',
'dependencies': [
'mojo_application',
'mojo_cpp_bindings',
'mojo_environment_standalone',
'mojo_network_bindings',
'mojo_launcher_bindings',
'mojo_system',
'mojo_utility',
],
'sources': [
'examples/html_viewer/html_viewer.cc',
'public/cpp/application/lib/mojo_main_standalone.cc',
],
},
{
'target_name': 'mojo_pepper_container_app',
'type': 'shared_library',
......@@ -247,6 +264,7 @@
'../ui/resources/ui_resources.gyp:ui_resources',
'../ui/resources/ui_resources.gyp:ui_test_pak',
'../ui/views/views.gyp:views',
'../url/url.gyp:url_lib',
'mojo_application',
'mojo_aura_support',
'mojo_common_lib',
......@@ -254,6 +272,7 @@
'mojo_geometry_bindings',
'mojo_geometry_lib',
'mojo_input_events_lib',
'mojo_launcher_bindings',
'mojo_system_impl',
'mojo_views_support',
'mojo_view_manager_bindings',
......
......@@ -245,6 +245,39 @@
'services/public/cpp/view_manager/view_manager_types.h',
],
},
{
'target_name': 'mojo_launcher_bindings',
'type': 'static_library',
'sources': [
'services/public/interfaces/launcher/launcher.mojom',
],
'includes': [ 'public/tools/bindings/mojom_bindings_generator.gypi' ],
'export_dependent_settings': [
'mojo_cpp_bindings',
],
'dependencies': [
'mojo_cpp_bindings',
'mojo_network_bindings',
],
},
{
'target_name': 'mojo_launcher',
'type': 'shared_library',
'dependencies': [
'../base/base.gyp:base',
'mojo_application',
'mojo_cpp_bindings',
'mojo_environment_chromium',
'mojo_launcher_bindings',
'mojo_network_bindings',
'mojo_system_impl',
'mojo_utility',
],
'sources': [
'services/launcher/launcher.cc',
'public/cpp/application/lib/mojo_main_chromium.cc',
],
},
{
'target_name': 'mojo_view_manager_bindings',
'type': 'static_library',
......
include_rules = [
"+mojo/services/public",
]
// 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 "base/compiler_specific.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string_tokenizer.h"
#include "mojo/public/cpp/application/application.h"
#include "mojo/services/public/interfaces/launcher/launcher.mojom.h"
#include "mojo/services/public/interfaces/network/network_service.mojom.h"
#include "mojo/services/public/interfaces/network/url_loader.mojom.h"
namespace mojo {
namespace launcher {
class LauncherApp;
class LauncherConnection : public InterfaceImpl<Launcher> {
public:
explicit LauncherConnection(LauncherApp* app) : app_(app) {}
virtual ~LauncherConnection() {}
private:
// Overridden from Launcher:
virtual void Launch(const String& url) OVERRIDE;
LauncherApp* app_;
DISALLOW_COPY_AND_ASSIGN(LauncherConnection);
};
class Launch : public URLLoaderClient {
public:
Launch(LauncherApp* app, const String& url);
virtual ~Launch() {}
private:
// Overridden from URLLoaderClient:
virtual void OnReceivedRedirect(URLResponsePtr response,
const String& new_url,
const String& new_method) OVERRIDE {
}
virtual void OnReceivedResponse(URLResponsePtr response) OVERRIDE;
virtual void OnReceivedError(NetworkErrorPtr error) OVERRIDE {
ScheduleDestroy();
}
virtual void OnReceivedEndOfResponseBody() OVERRIDE {
ScheduleDestroy();
}
std::string GetContentType(const Array<String>& headers) {
for (size_t i = 0; i < headers.size(); ++i) {
base::StringTokenizer t(headers[i], ": ;=");
while (t.GetNext()) {
if (!t.token_is_delim() && t.token() == "Content-Type") {
while (t.GetNext()) {
if (!t.token_is_delim())
return t.token();
}
}
}
}
return "";
}
void ScheduleDestroy() {
if (destroy_scheduled_)
return;
destroy_scheduled_ = true;
base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
}
LauncherApp* app_;
bool destroy_scheduled_;
URLLoaderPtr url_loader_;
ScopedDataPipeConsumerHandle response_body_stream_;
LaunchablePtr launchable_;
DISALLOW_COPY_AND_ASSIGN(Launch);
};
class LauncherApp : public Application {
public:
LauncherApp() {
handler_map_["text/html"] = "mojo:mojo_html_viewer";
handler_map_["image/png"] = "mojo:mojo_image_viewer";
}
virtual ~LauncherApp() {}
void LaunchURL(const String& url) {
new Launch(this, url);
}
URLLoaderPtr CreateURLLoader() {
URLLoaderPtr loader;
network_service_->CreateURLLoader(Get(&loader));
return loader.Pass();
}
std::string GetHandlerForContentType(const std::string& content_type) {
HandlerMap::const_iterator it = handler_map_.find(content_type);
return it != handler_map_.end() ? it->second : "";
}
private:
typedef std::map<std::string, std::string> HandlerMap;
// Overridden from Application:
virtual void Initialize() OVERRIDE {
AddService<LauncherConnection>(this);
ConnectTo("mojo:mojo_network_service", &network_service_);
}
HandlerMap handler_map_;
NetworkServicePtr network_service_;
DISALLOW_COPY_AND_ASSIGN(LauncherApp);
};
void LauncherConnection::Launch(const String& url) {
app_->LaunchURL(url);
}
Launch::Launch(LauncherApp* app, const String& url)
: app_(app),
destroy_scheduled_(false) {
url_loader_ = app_->CreateURLLoader();
url_loader_.set_client(this);
URLRequestPtr request(URLRequest::New());
request->url = url;
request->method = "GET";
request->auto_follow_redirects = true;
DataPipe data_pipe;
response_body_stream_ = data_pipe.consumer_handle.Pass();
url_loader_->Start(request.Pass(), data_pipe.producer_handle.Pass());
}
void Launch::OnReceivedResponse(URLResponsePtr response) {
std::string content_type = GetContentType(response->headers);
std::string handler_url = app_->GetHandlerForContentType(content_type);
if (!handler_url.empty()) {
app_->ConnectTo(handler_url, &launchable_);
launchable_->OnLaunch(response.Pass(), response_body_stream_.Pass());
}
ScheduleDestroy();
}
} // namespace launcher
// static
Application* Application::Create() {
return new launcher::LauncherApp;
}
} // namespace mojo
// 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.
import "../network/url_loader.mojom"
module mojo.launcher {
interface Launcher {
Launch(string url);
};
interface Launchable {
OnLaunch(mojo.URLResponse response,
handle<data_pipe_consumer> response_body_stream);
};
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment