Commit dc571218 authored by aa@chromium.org's avatar aa@chromium.org

Turn 'browser' app into a 'launcher' app that window_manager

embeds. This also adds the 'RequestNavigate' part of the
navigation protocol.

I will rename the mojo_browser target and related names to
mojo_launcher_ui in a separate CL.

This depends on https://codereview.chromium.org/331323007/.

BUG=386275
R=ben@chromium.org

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@278312 0039d316-1c4b-4281-b951-d872f2087c98
parent 4812ba0f
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
// found in the LICENSE file. // found in the LICENSE file.
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "mojo/public/cpp/application/application.h" #include "mojo/public/cpp/application/application.h"
#include "mojo/services/navigation/navigation.mojom.h" #include "mojo/services/navigation/navigation.mojom.h"
#include "mojo/services/public/cpp/view_manager/node.h" #include "mojo/services/public/cpp/view_manager/node.h"
...@@ -10,9 +12,9 @@ ...@@ -10,9 +12,9 @@
#include "mojo/services/public/cpp/view_manager/view_manager.h" #include "mojo/services/public/cpp/view_manager/view_manager.h"
#include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" #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_observer.h"
#include "mojo/services/public/interfaces/launcher/launcher.mojom.h"
#include "mojo/views/native_widget_view_manager.h" #include "mojo/views/native_widget_view_manager.h"
#include "mojo/views/views_init.h" #include "mojo/views/views_init.h"
#include "ui/events/event.h"
#include "ui/views/controls/textfield/textfield.h" #include "ui/views/controls/textfield/textfield.h"
#include "ui/views/controls/textfield/textfield_controller.h" #include "ui/views/controls/textfield/textfield_controller.h"
#include "ui/views/layout/layout_manager.h" #include "ui/views/layout/layout_manager.h"
...@@ -23,28 +25,6 @@ ...@@ -23,28 +25,6 @@
namespace mojo { namespace mojo {
namespace examples { namespace examples {
class NodeView : public views::View {
public:
explicit NodeView(view_manager::Node* node) : node_(node) {
// This class is provisional and assumes that the node has already been
// added to a parent. I suspect we'll want to make an improved version of
// this that lives in ui/views akin to NativeViewHost that properly
// attaches/detaches when the view is.
DCHECK(node->parent());
}
virtual ~NodeView() {}
// Overridden from views::View:
virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE {
node_->SetBounds(ConvertRectToWidget(GetLocalBounds()));
}
private:
view_manager::Node* node_;
DISALLOW_COPY_AND_ASSIGN(NodeView);
};
class BrowserLayoutManager : public views::LayoutManager { class BrowserLayoutManager : public views::LayoutManager {
public: public:
BrowserLayoutManager() {} BrowserLayoutManager() {}
...@@ -53,16 +33,11 @@ class BrowserLayoutManager : public views::LayoutManager { ...@@ -53,16 +33,11 @@ class BrowserLayoutManager : public views::LayoutManager {
private: private:
// Overridden from views::LayoutManager: // Overridden from views::LayoutManager:
virtual void Layout(views::View* host) OVERRIDE { virtual void Layout(views::View* host) OVERRIDE {
// Browser view has two children: // Browser view has one child, a text input field.
// 1. text input field. DCHECK_EQ(1, host->child_count());
// 2. content view.
DCHECK_EQ(2, host->child_count());
views::View* text_field = host->child_at(0); views::View* text_field = host->child_at(0);
gfx::Size ps = text_field->GetPreferredSize(); gfx::Size ps = text_field->GetPreferredSize();
text_field->SetBoundsRect(gfx::Rect(host->width(), ps.height())); text_field->SetBoundsRect(gfx::Rect(host->width(), ps.height()));
views::View* content_area = host->child_at(1);
content_area->SetBounds(0, text_field->bounds().bottom(), host->width(),
host->height() - text_field->bounds().bottom());
} }
virtual gfx::Size GetPreferredSize(const views::View* host) const OVERRIDE { virtual gfx::Size GetPreferredSize(const views::View* host) const OVERRIDE {
return gfx::Size(); return gfx::Size();
...@@ -75,10 +50,9 @@ class BrowserLayoutManager : public views::LayoutManager { ...@@ -75,10 +50,9 @@ class BrowserLayoutManager : public views::LayoutManager {
// TODO: cleanup! // TODO: cleanup!
class Browser : public Application, class Browser : public Application,
public view_manager::ViewManagerDelegate, public view_manager::ViewManagerDelegate,
public views::TextfieldController, public views::TextfieldController {
public InterfaceImpl<launcher::LauncherClient> {
public: public:
Browser() : view_manager_(NULL), view_(NULL), content_node_(NULL) {} Browser() : view_manager_(NULL), view_(NULL) {}
virtual ~Browser() { virtual ~Browser() {
} }
...@@ -88,8 +62,7 @@ class Browser : public Application, ...@@ -88,8 +62,7 @@ class Browser : public Application,
virtual void Initialize() MOJO_OVERRIDE { virtual void Initialize() MOJO_OVERRIDE {
views_init_.reset(new ViewsInit); views_init_.reset(new ViewsInit);
view_manager::ViewManager::Create(this, this); view_manager::ViewManager::Create(this, this);
ConnectTo("mojo:mojo_launcher", &launcher_); ConnectTo("mojo:mojo_window_manager", &navigator_host_);
launcher_.set_client(this);
} }
void CreateWidget(const gfx::Size& size) { void CreateWidget(const gfx::Size& size) {
...@@ -98,13 +71,12 @@ class Browser : public Application, ...@@ -98,13 +71,12 @@ class Browser : public Application,
views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView; views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView;
widget_delegate->GetContentsView()->AddChildView(textfield); widget_delegate->GetContentsView()->AddChildView(textfield);
widget_delegate->GetContentsView()->AddChildView(
new NodeView(content_node_));
widget_delegate->GetContentsView()->SetLayoutManager( widget_delegate->GetContentsView()->SetLayoutManager(
new BrowserLayoutManager); new BrowserLayoutManager);
views::Widget* widget = new views::Widget; views::Widget* widget = new views::Widget;
views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW); views::Widget::InitParams params(
views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
params.native_widget = new NativeWidgetViewManager(widget, view_); params.native_widget = new NativeWidgetViewManager(widget, view_);
params.delegate = widget_delegate; params.delegate = widget_delegate;
params.bounds = gfx::Rect(size.width(), size.height()); params.bounds = gfx::Rect(size.width(), size.height());
...@@ -120,12 +92,7 @@ class Browser : public Application, ...@@ -120,12 +92,7 @@ class Browser : public Application,
view_manager_ = view_manager; view_manager_ = view_manager;
view_ = view_manager::View::Create(view_manager_); view_ = view_manager::View::Create(view_manager_);
view_manager_->GetRoots().front()->SetActiveView(view_); view_manager_->GetRoots().front()->SetActiveView(view_);
content_node_ = view_manager::Node::Create(view_manager_);
root->AddChild(content_node_);
root->SetFocus(); root->SetFocus();
CreateWidget(root->bounds().size()); CreateWidget(root->bounds().size());
} }
...@@ -135,34 +102,30 @@ class Browser : public Application, ...@@ -135,34 +102,30 @@ class Browser : public Application,
if (key_event.key_code() == ui::VKEY_RETURN) { if (key_event.key_code() == ui::VKEY_RETURN) {
GURL url(sender->text()); GURL url(sender->text());
printf("User entered this URL: %s\n", url.spec().c_str()); printf("User entered this URL: %s\n", url.spec().c_str());
launcher_->Launch(url.spec()); navigation::NavigationDetailsPtr nav_details(
navigation::NavigationDetails::New());
nav_details->url = url.spec();
navigator_host_->RequestNavigate(view_manager_->GetRoots().front()->id(),
nav_details.Pass());
} }
return false; return false;
} }
// launcher::LauncherClient: virtual void ContentsChanged(views::Textfield* sender,
virtual void OnLaunch( const base::string16& new_contents) OVERRIDE {
const String& handler_url, // Ghetto workaround for crbug.com/386256.
navigation::ResponseDetailsPtr response_details) OVERRIDE { if (EndsWith(new_contents, base::ASCIIToUTF16("]"), false)) {
content_node_->Embed(handler_url); sender->SetText(new_contents.substr(0, new_contents.size() - 1));
HandleKeyEvent(sender, ui::KeyEvent(ui::ET_KEY_RELEASED, ui::VKEY_RETURN,
navigation::NavigationDetailsPtr navigation_details( 0, false));
navigation::NavigationDetails::New()); }
navigation_details->url = response_details->response->url;
navigation::NavigatorPtr navigator;
ConnectTo(handler_url, &navigator);
navigator->Navigate(content_node_->id(),
navigation_details.Pass(),
response_details.Pass());
} }
scoped_ptr<ViewsInit> views_init_; scoped_ptr<ViewsInit> views_init_;
view_manager::ViewManager* view_manager_; view_manager::ViewManager* view_manager_;
view_manager::View* view_; view_manager::View* view_;
view_manager::Node* content_node_; navigation::NavigatorHostPtr navigator_host_;
launcher::LauncherPtr launcher_;
DISALLOW_COPY_AND_ASSIGN(Browser); DISALLOW_COPY_AND_ASSIGN(Browser);
}; };
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "mojo/services/public/cpp/view_manager/view_manager.h" #include "mojo/services/public/cpp/view_manager/view_manager.h"
#include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" #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_observer.h"
#include "mojo/services/public/interfaces/launcher/launcher.mojom.h"
#include "ui/events/event_constants.h" #include "ui/events/event_constants.h"
#if defined CreateWindow #if defined CreateWindow
...@@ -39,10 +40,6 @@ const SkColor kColors[] = { SK_ColorYELLOW, ...@@ -39,10 +40,6 @@ const SkColor kColors[] = { SK_ColorYELLOW,
SK_ColorGREEN, SK_ColorGREEN,
SK_ColorMAGENTA }; SK_ColorMAGENTA };
const char kEmbeddedAppURL[] = "mojo:mojo_embedded_app";
const char kNestingAppURL[] = "mojo:mojo_nesting_app";
const char kMojoBrowserURL[] = "mojo:mojo_browser";
} // namespace } // namespace
class WindowManagerConnection : public InterfaceImpl<IWindowManager> { class WindowManagerConnection : public InterfaceImpl<IWindowManager> {
...@@ -60,33 +57,75 @@ class WindowManagerConnection : public InterfaceImpl<IWindowManager> { ...@@ -60,33 +57,75 @@ class WindowManagerConnection : public InterfaceImpl<IWindowManager> {
DISALLOW_COPY_AND_ASSIGN(WindowManagerConnection); DISALLOW_COPY_AND_ASSIGN(WindowManagerConnection);
}; };
class NavigatorHost : public InterfaceImpl<navigation::NavigatorHost> {
public:
explicit NavigatorHost(WindowManager* window_manager)
: window_manager_(window_manager) {
}
virtual ~NavigatorHost() {
}
private:
virtual void RequestNavigate(
uint32 source_node_id,
navigation::NavigationDetailsPtr nav_details) OVERRIDE;
WindowManager* window_manager_;
DISALLOW_COPY_AND_ASSIGN(NavigatorHost);
};
class WindowManager : public Application, class WindowManager : public Application,
public ViewObserver, public ViewObserver,
public ViewManagerDelegate { public ViewManagerDelegate,
public InterfaceImpl<launcher::LauncherClient> {
public: public:
WindowManager() : view_manager_(NULL) {} WindowManager() : launcher_ui_(NULL), view_manager_(NULL) {}
virtual ~WindowManager() {} virtual ~WindowManager() {}
void CloseWindow(Id node_id) { void CloseWindow(Id node_id) {
Node* node = view_manager_->GetNodeById(node_id); Node* node = view_manager_->GetNodeById(node_id);
DCHECK(node); DCHECK(node);
std::vector<Node*>::iterator iter =
std::find(windows_.begin(), windows_.end(), node);
DCHECK(iter != windows_.end());
windows_.erase(iter);
node->Destroy(); node->Destroy();
} }
void RequestNavigate(
uint32 source_node_id,
navigation::NavigationDetailsPtr nav_details) {
if (!launcher_.get()) {
ConnectTo("mojo:mojo_launcher", &launcher_);
launcher_.set_client(this);
}
launcher_->Launch(nav_details->url);
}
private: private:
// Overridden from Application: // Overridden from Application:
virtual void Initialize() MOJO_OVERRIDE { virtual void Initialize() MOJO_OVERRIDE {
AddService<WindowManagerConnection>(this); AddService<WindowManagerConnection>(this);
AddService<NavigatorHost>(this);
ViewManager::Create(this, this); ViewManager::Create(this, this);
} }
// Overridden from ViewObserver: // Overridden from ViewObserver:
virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE { virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE {
if (event->action == ui::ET_MOUSE_RELEASED) { if (event->action == ui::ET_MOUSE_RELEASED) {
std::string app_url;
if (event->flags & ui::EF_LEFT_MOUSE_BUTTON) if (event->flags & ui::EF_LEFT_MOUSE_BUTTON)
CreateWindow(kEmbeddedAppURL); app_url = "mojo:mojo_embedded_app";
else if (event->flags & ui::EF_RIGHT_MOUSE_BUTTON) else if (event->flags & ui::EF_RIGHT_MOUSE_BUTTON)
CreateWindow(kNestingAppURL); app_url = "mojo:mojo_nesting_app";
DCHECK(!app_url.empty());
Node* node = view_manager_->GetNodeById(parent_node_id_);
navigation::NavigationDetailsPtr nav_details(
navigation::NavigationDetails::New());
size_t index = node->children().size() - 1;
nav_details->url = base::StringPrintf(
"%s/%x", app_url.c_str(), kColors[index % arraysize(kColors)]);
navigation::ResponseDetailsPtr response;
CreateWindow(app_url, nav_details.Pass(), response.Pass());
} }
} }
...@@ -105,49 +144,65 @@ class WindowManager : public Application, ...@@ -105,49 +144,65 @@ class WindowManager : public Application,
view->SetColor(SK_ColorBLUE); view->SetColor(SK_ColorBLUE);
view->AddObserver(this); view->AddObserver(this);
CreateWindow(kMojoBrowserURL); CreateLauncherUI();
} }
void CreateWindow(const std::string& url) { // Overridden from LauncherClient:
Node* node = view_manager_->GetNodeById(parent_node_id_); virtual void OnLaunch(
const mojo::String& handler_url, mojo::URLResponsePtr response,
mojo::ScopedDataPipeConsumerHandle response_body_stream) OVERRIDE {
navigation::NavigationDetailsPtr nav_details(
navigation::NavigationDetails::New());
nav_details->url = response->url;
navigation::ResponseDetailsPtr response_details(
navigation::ResponseDetails::New());
response_details->response = response.Pass();
response_details->response_body_stream = response_body_stream.Pass();
CreateWindow(handler_url, nav_details.Pass(), response_details.Pass());
}
gfx::Rect bounds(node->bounds().size()); void CreateLauncherUI() {
bounds.Inset(25, 25); navigation::NavigationDetailsPtr nav_details;
if (!node->children().empty()) { navigation::ResponseDetailsPtr response;
gfx::Point position = node->children().back()->bounds().origin(); launcher_ui_ = CreateChild("mojo:mojo_browser", gfx::Rect(25, 25, 400, 25),
position.Offset(25, 25); nav_details.Pass(), response.Pass());
}
void CreateWindow(const std::string& handler_url,
navigation::NavigationDetailsPtr nav_details,
navigation::ResponseDetailsPtr response) {
gfx::Rect bounds(25, 75, 400, 400);
if (!windows_.empty()) {
gfx::Point position = windows_.back()->bounds().origin();
position.Offset(35, 35);
bounds.set_origin(position); bounds.set_origin(position);
} }
windows_.push_back(CreateChild(handler_url, bounds, nav_details.Pass(),
response.Pass()));
}
Node* CreateChild(const std::string& url,
const gfx::Rect& bounds,
navigation::NavigationDetailsPtr nav_details,
navigation::ResponseDetailsPtr response) {
Node* node = view_manager_->GetNodeById(parent_node_id_);
Node* embedded = Node::Create(view_manager_); Node* embedded = Node::Create(view_manager_);
node->AddChild(embedded); node->AddChild(embedded);
embedded->SetBounds(bounds); embedded->SetBounds(bounds);
embedded->Embed(url); embedded->Embed(url);
// TODO(aa): Is there a way to ask for an interface and test whether it if (nav_details.get()) {
// succeeded? That would be nicer than hard-coding the URLs that are known
// to support navigation.
if (url == kEmbeddedAppURL || url == kNestingAppURL) {
// TODO(aa): This means that there can only ever be one instance of every
// app, which seems wrong. Instead, perhaps embedder should get back a
// service provider that allows it to talk to embeddee.
navigation::NavigatorPtr navigator; navigation::NavigatorPtr navigator;
ConnectTo(url, &navigator); ConnectTo(url, &navigator);
navigation::NavigationDetailsPtr details( navigator->Navigate(embedded->id(), nav_details.Pass(), response.Pass());
navigation::NavigationDetails::New());
size_t index = node->children().size() - 1;
details->url = base::StringPrintf(
"%s/%x", kEmbeddedAppURL, kColors[index % arraysize(kColors)]);
// TODO(beng): remove once nullable parameters land.
navigation::ResponseDetailsPtr response_details(
navigation::ResponseDetails::New());
navigator->Navigate(embedded->id(),
details.Pass(),
response_details.Pass());
} }
return embedded;
} }
launcher::LauncherPtr launcher_;
Node* launcher_ui_;
std::vector<Node*> windows_;
ViewManager* view_manager_; ViewManager* view_manager_;
Id parent_node_id_; Id parent_node_id_;
...@@ -158,6 +213,12 @@ void WindowManagerConnection::CloseWindow(Id node_id) { ...@@ -158,6 +213,12 @@ void WindowManagerConnection::CloseWindow(Id node_id) {
window_manager_->CloseWindow(node_id); window_manager_->CloseWindow(node_id);
} }
void NavigatorHost::RequestNavigate(
uint32 source_node_id,
navigation::NavigationDetailsPtr nav_details) {
window_manager_->RequestNavigate(source_node_id, nav_details.Pass());
}
} // namespace examples } // namespace examples
// static // static
......
...@@ -296,7 +296,6 @@ ...@@ -296,7 +296,6 @@
'mojo_geometry_bindings', 'mojo_geometry_bindings',
'mojo_geometry_lib', 'mojo_geometry_lib',
'mojo_input_events_lib', 'mojo_input_events_lib',
'mojo_launcher_bindings',
'mojo_navigation_bindings', 'mojo_navigation_bindings',
'mojo_system_impl', 'mojo_system_impl',
'mojo_views_support', 'mojo_views_support',
...@@ -364,6 +363,7 @@ ...@@ -364,6 +363,7 @@
'mojo_environment_chromium', 'mojo_environment_chromium',
'mojo_geometry_bindings', 'mojo_geometry_bindings',
'mojo_gles2', 'mojo_gles2',
'mojo_launcher_bindings',
'mojo_navigation_bindings', 'mojo_navigation_bindings',
'mojo_view_manager_lib', 'mojo_view_manager_lib',
'mojo_window_manager_bindings', 'mojo_window_manager_bindings',
......
...@@ -259,7 +259,6 @@ ...@@ -259,7 +259,6 @@
'dependencies': [ 'dependencies': [
'mojo_cpp_bindings', 'mojo_cpp_bindings',
'mojo_network_bindings', 'mojo_network_bindings',
'mojo_navigation_bindings',
], ],
}, },
{ {
......
...@@ -145,12 +145,8 @@ void LaunchInstance::OnReceivedResponse(URLResponsePtr response) { ...@@ -145,12 +145,8 @@ void LaunchInstance::OnReceivedResponse(URLResponsePtr response) {
std::string content_type = GetContentType(response->headers); std::string content_type = GetContentType(response->headers);
std::string handler_url = app_->GetHandlerForContentType(content_type); std::string handler_url = app_->GetHandlerForContentType(content_type);
if (!handler_url.empty()) { if (!handler_url.empty()) {
navigation::ResponseDetailsPtr response_details = client_->OnLaunch(handler_url, response.Pass(),
navigation::ResponseDetails::New(); response_body_stream_.Pass());
response_details->response = response.Pass();
response_details->response_body_stream = response_body_stream_.Pass();
client_->OnLaunch(handler_url, response_details.Pass());
} }
} }
......
...@@ -18,6 +18,11 @@ struct ResponseDetails { ...@@ -18,6 +18,11 @@ struct ResponseDetails {
handle<data_pipe_consumer> response_body_stream; handle<data_pipe_consumer> response_body_stream;
}; };
// Embedders that support navigation of implement this interface.
interface NavigatorHost {
RequestNavigate(uint32 source_node_id, NavigationDetails details);
};
// Applications implement this interface to support navigation of their views // Applications implement this interface to support navigation of their views
// by embedders. // by embedders.
// |response_details| can be NULL when a navigation was not the result of a // |response_details| can be NULL when a navigation was not the result of a
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
// found in the LICENSE file. // found in the LICENSE file.
import "../network/url_loader.mojom" import "../network/url_loader.mojom"
import "../../../navigation/navigation.mojom"
module mojo.launcher { module mojo.launcher {
...@@ -14,7 +13,8 @@ interface Launcher { ...@@ -14,7 +13,8 @@ interface Launcher {
[Client=Launcher] [Client=Launcher]
interface LauncherClient { interface LauncherClient {
OnLaunch(string handler_url, mojo.navigation.ResponseDetails response_details); OnLaunch(string handler_url, 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