OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mandoline/ui/browser/browser.h" | 5 #include "mandoline/ui/browser/browser.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "components/view_manager/public/cpp/view.h" |
| 10 #include "components/view_manager/public/cpp/view_manager_init.h" |
9 #include "mandoline/ui/browser/browser_ui.h" | 11 #include "mandoline/ui/browser/browser_ui.h" |
10 #include "mandoline/ui/browser/merged_service_provider.h" | 12 #include "mandoline/ui/browser/merged_service_provider.h" |
11 #include "mojo/application/application_runner_chromium.h" | 13 #include "mojo/application/application_runner_chromium.h" |
12 #include "mojo/common/common_type_converters.h" | 14 #include "mojo/common/common_type_converters.h" |
| 15 #include "mojo/converters/geometry/geometry_type_converters.h" |
13 #include "third_party/mojo/src/mojo/public/c/system/main.h" | 16 #include "third_party/mojo/src/mojo/public/c/system/main.h" |
14 #include "ui/gfx/geometry/size.h" | 17 #include "ui/gfx/geometry/size.h" |
15 | 18 |
16 namespace mandoline { | 19 namespace mandoline { |
| 20 namespace { |
| 21 |
| 22 gfx::Size GetInitialViewportSize() { |
| 23 #if defined(OS_ANDROID) |
| 24 // Resize to match the Nexus 5 aspect ratio: |
| 25 return gfx::Size(320, 640); |
| 26 #else |
| 27 return gfx::Size(1280, 800); |
| 28 #endif |
| 29 } |
| 30 |
| 31 } // namespace |
17 | 32 |
18 Browser::Browser() | 33 Browser::Browser() |
19 : window_manager_app_(new window_manager::WindowManagerApp(this, this)), | 34 : root_(nullptr), |
20 root_(nullptr), | |
21 content_(nullptr), | 35 content_(nullptr), |
22 omnibox_(nullptr), | 36 omnibox_(nullptr), |
23 navigator_host_(this), | 37 navigator_host_(this), |
24 ui_(nullptr) { | 38 ui_(nullptr) { |
25 exposed_services_impl_.AddService(this); | 39 exposed_services_impl_.AddService(this); |
26 } | 40 } |
27 | 41 |
28 Browser::~Browser() { | 42 Browser::~Browser() { |
29 } | 43 } |
30 | 44 |
31 void Browser::ReplaceContentWithURL(const mojo::String& url) { | 45 void Browser::ReplaceContentWithURL(const mojo::String& url) { |
32 Embed(url, nullptr, nullptr); | 46 Embed(url, nullptr, nullptr); |
33 } | 47 } |
34 | 48 |
35 void Browser::Initialize(mojo::ApplicationImpl* app) { | 49 void Browser::Initialize(mojo::ApplicationImpl* app) { |
36 window_manager_app_->Initialize(app); | 50 view_manager_init_.reset(new mojo::ViewManagerInit(app, this, this)); |
| 51 |
37 ui_.reset(BrowserUI::Create(this, app)); | 52 ui_.reset(BrowserUI::Create(this, app)); |
38 | 53 |
39 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 54 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
40 base::CommandLine::StringVector args = command_line->GetArgs(); | 55 base::CommandLine::StringVector args = command_line->GetArgs(); |
41 if (args.empty()) { | 56 if (args.empty()) { |
42 default_url_ = "http://www.google.com/"; | 57 default_url_ = "http://www.google.com/"; |
43 } else { | 58 } else { |
44 #if defined(OS_WIN) | 59 #if defined(OS_WIN) |
45 default_url_ = base::WideToUTF8(args[0]); | 60 default_url_ = base::WideToUTF8(args[0]); |
46 #else | 61 #else |
47 default_url_ = args[0]; | 62 default_url_ = args[0]; |
48 #endif | 63 #endif |
49 } | 64 } |
50 } | 65 } |
51 | 66 |
52 bool Browser::ConfigureIncomingConnection( | 67 bool Browser::ConfigureIncomingConnection( |
53 mojo::ApplicationConnection* connection) { | 68 mojo::ApplicationConnection* connection) { |
54 window_manager_app_->ConfigureIncomingConnection(connection); | 69 // TODO: register embed interface here. |
55 return true; | 70 return true; |
56 } | 71 } |
57 | 72 |
58 bool Browser::ConfigureOutgoingConnection( | 73 bool Browser::ConfigureOutgoingConnection( |
59 mojo::ApplicationConnection* connection) { | 74 mojo::ApplicationConnection* connection) { |
60 window_manager_app_->ConfigureOutgoingConnection(connection); | |
61 return true; | 75 return true; |
62 } | 76 } |
63 | 77 |
64 void Browser::OnEmbed( | 78 void Browser::OnEmbed( |
65 mojo::View* root, | 79 mojo::View* root, |
66 mojo::InterfaceRequest<mojo::ServiceProvider> services, | 80 mojo::InterfaceRequest<mojo::ServiceProvider> services, |
67 mojo::ServiceProviderPtr exposed_services) { | 81 mojo::ServiceProviderPtr exposed_services) { |
68 // Browser does not support being embedded more than once. | 82 // Browser does not support being embedded more than once. |
69 CHECK(!root_); | 83 CHECK(!root_); |
70 | 84 |
71 // TODO(beng): still unhappy with the fact that both this class & the UI class | 85 // TODO(beng): still unhappy with the fact that both this class & the UI class |
72 // know so much about these views. Figure out how to shift more to | 86 // know so much about these views. Figure out how to shift more to |
73 // the UI class. | 87 // the UI class. |
74 root_ = root; | 88 root_ = root; |
75 content_ = root->view_manager()->CreateView(); | 89 content_ = root->view_manager()->CreateView(); |
76 ui_->Init(root_); | 90 ui_->Init(root_); |
77 | 91 |
78 #if defined(OS_ANDROID) | 92 view_manager_init_->view_manager_root()->SetViewportSize( |
79 // Resize to match the Nexus 5 aspect ratio: | 93 mojo::Size::From(GetInitialViewportSize())); |
80 window_manager_app_->SetViewportSize(gfx::Size(320, 640)); | |
81 #else | |
82 window_manager_app_->SetViewportSize(gfx::Size(1280, 800)); | |
83 #endif | |
84 | 94 |
85 root_->AddChild(content_); | 95 root_->AddChild(content_); |
86 content_->SetVisible(true); | 96 content_->SetVisible(true); |
87 | 97 |
88 window_manager_app_->AddAccelerator(mojo::KEYBOARD_CODE_BROWSER_BACK, | 98 view_manager_init_->view_manager_root()->AddAccelerator( |
89 mojo::EVENT_FLAGS_NONE); | 99 mojo::KEYBOARD_CODE_BROWSER_BACK, mojo::EVENT_FLAGS_NONE); |
90 | 100 |
91 // Now that we're ready, either load a pending url or the default url. | 101 // Now that we're ready, either load a pending url or the default url. |
92 if (!pending_url_.empty()) | 102 if (!pending_url_.empty()) |
93 Embed(pending_url_, services.Pass(), exposed_services.Pass()); | 103 Embed(pending_url_, services.Pass(), exposed_services.Pass()); |
94 else if (!default_url_.empty()) | 104 else if (!default_url_.empty()) |
95 Embed(default_url_, services.Pass(), exposed_services.Pass()); | 105 Embed(default_url_, services.Pass(), exposed_services.Pass()); |
96 } | 106 } |
97 | 107 |
98 void Browser::OnViewManagerDisconnected( | 108 void Browser::OnViewManagerDisconnected( |
99 mojo::ViewManager* view_manager) { | 109 mojo::ViewManager* view_manager) { |
100 ui_.reset(); | 110 ui_.reset(); |
101 root_ = nullptr; | 111 root_ = nullptr; |
102 } | 112 } |
103 | 113 |
| 114 void Browser::OnAccelerator(mojo::EventPtr event) { |
| 115 DCHECK_EQ(mojo::KEYBOARD_CODE_BROWSER_BACK, |
| 116 event->key_data->windows_key_code); |
| 117 navigator_host_.RequestNavigateHistory(-1); |
| 118 } |
| 119 |
| 120 void Browser::OpenURL(const mojo::String& url) { |
| 121 omnibox_->SetVisible(false); |
| 122 ReplaceContentWithURL(url); |
| 123 } |
| 124 |
| 125 void Browser::Create(mojo::ApplicationConnection* connection, |
| 126 mojo::InterfaceRequest<mojo::NavigatorHost> request) { |
| 127 navigator_host_.Bind(request.Pass()); |
| 128 } |
| 129 |
| 130 void Browser::ShowOmnibox( |
| 131 const mojo::String& url, |
| 132 mojo::InterfaceRequest<mojo::ServiceProvider> services, |
| 133 mojo::ServiceProviderPtr exposed_services) { |
| 134 if (!omnibox_) { |
| 135 omnibox_ = root_->view_manager()->CreateView(); |
| 136 root_->AddChild(omnibox_); |
| 137 omnibox_->SetVisible(true); |
| 138 omnibox_->SetBounds(root_->bounds()); |
| 139 } |
| 140 omnibox_->Embed(url, services.Pass(), exposed_services.Pass()); |
| 141 } |
| 142 |
104 void Browser::Embed(const mojo::String& url, | 143 void Browser::Embed(const mojo::String& url, |
105 mojo::InterfaceRequest<mojo::ServiceProvider> services, | 144 mojo::InterfaceRequest<mojo::ServiceProvider> services, |
106 mojo::ServiceProviderPtr exposed_services) { | 145 mojo::ServiceProviderPtr exposed_services) { |
107 if (url == "mojo:omnibox") { | 146 if (url == "mojo:omnibox") { |
108 ShowOmnibox(url, services.Pass(), exposed_services.Pass()); | 147 ShowOmnibox(url, services.Pass(), exposed_services.Pass()); |
109 return; | 148 return; |
110 } | 149 } |
111 | 150 |
112 // We can get Embed calls before we've actually been | 151 // We can get Embed calls before we've actually been |
113 // embedded into the root view and content_ is created. | 152 // embedded into the root view and content_ is created. |
(...skipping 10 matching lines...) Expand all Loading... |
124 ui_->OnURLChanged(); | 163 ui_->OnURLChanged(); |
125 | 164 |
126 merged_service_provider_.reset( | 165 merged_service_provider_.reset( |
127 new MergedServiceProvider(exposed_services.Pass(), this)); | 166 new MergedServiceProvider(exposed_services.Pass(), this)); |
128 content_->Embed(url, services.Pass(), | 167 content_->Embed(url, services.Pass(), |
129 merged_service_provider_->GetServiceProviderPtr().Pass()); | 168 merged_service_provider_->GetServiceProviderPtr().Pass()); |
130 | 169 |
131 navigator_host_.RecordNavigation(url); | 170 navigator_host_.RecordNavigation(url); |
132 } | 171 } |
133 | 172 |
134 void Browser::OnAcceleratorPressed(mojo::View* view, | |
135 mojo::KeyboardCode keyboard_code, | |
136 mojo::EventFlags flags) { | |
137 DCHECK_EQ(mojo::KEYBOARD_CODE_BROWSER_BACK, keyboard_code); | |
138 navigator_host_.RequestNavigateHistory(-1); | |
139 } | |
140 | |
141 void Browser::OpenURL(const mojo::String& url) { | |
142 omnibox_->SetVisible(false); | |
143 ReplaceContentWithURL(url); | |
144 } | |
145 | |
146 void Browser::Create(mojo::ApplicationConnection* connection, | |
147 mojo::InterfaceRequest<mojo::NavigatorHost> request) { | |
148 navigator_host_.Bind(request.Pass()); | |
149 } | |
150 | |
151 void Browser::ShowOmnibox( | |
152 const mojo::String& url, | |
153 mojo::InterfaceRequest<mojo::ServiceProvider> services, | |
154 mojo::ServiceProviderPtr exposed_services) { | |
155 if (!omnibox_) { | |
156 omnibox_ = root_->view_manager()->CreateView(); | |
157 root_->AddChild(omnibox_); | |
158 omnibox_->SetVisible(true); | |
159 omnibox_->SetBounds(root_->bounds()); | |
160 } | |
161 omnibox_->Embed(url, services.Pass(), exposed_services.Pass()); | |
162 } | |
163 | |
164 } // namespace mandoline | 173 } // namespace mandoline |
OLD | NEW |