OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "mojo/examples/keyboard/keyboard.mojom.h" | 7 #include "mojo/examples/keyboard/keyboard.mojom.h" |
8 #include "mojo/examples/window_manager/debug_panel.h" | 8 #include "mojo/examples/window_manager/debug_panel.h" |
9 #include "mojo/examples/window_manager/window_manager.mojom.h" | 9 #include "mojo/examples/window_manager/window_manager.mojom.h" |
10 #include "mojo/public/cpp/application/application_connection.h" | 10 #include "mojo/public/cpp/application/application_connection.h" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 // Overridden from IWindowManager: | 55 // Overridden from IWindowManager: |
56 virtual void CloseWindow(Id node_id) OVERRIDE; | 56 virtual void CloseWindow(Id node_id) OVERRIDE; |
57 virtual void ShowKeyboard(Id view_id, RectPtr bounds) OVERRIDE; | 57 virtual void ShowKeyboard(Id view_id, RectPtr bounds) OVERRIDE; |
58 virtual void HideKeyboard(Id view_id) OVERRIDE; | 58 virtual void HideKeyboard(Id view_id) OVERRIDE; |
59 | 59 |
60 WindowManager* window_manager_; | 60 WindowManager* window_manager_; |
61 | 61 |
62 DISALLOW_COPY_AND_ASSIGN(WindowManagerConnection); | 62 DISALLOW_COPY_AND_ASSIGN(WindowManagerConnection); |
63 }; | 63 }; |
64 | 64 |
65 class NavigatorHost : public InterfaceImpl<navigation::NavigatorHost> { | 65 class NavigatorHostImpl : public InterfaceImpl<NavigatorHost> { |
66 public: | 66 public: |
67 explicit NavigatorHost(WindowManager* window_manager) | 67 explicit NavigatorHostImpl(WindowManager* window_manager) |
68 : window_manager_(window_manager) {} | 68 : window_manager_(window_manager) {} |
69 virtual ~NavigatorHost() { | 69 virtual ~NavigatorHostImpl() { |
70 } | 70 } |
71 | 71 |
72 private: | 72 private: |
73 virtual void DidNavigateLocally(uint32 source_node_id, | 73 virtual void DidNavigateLocally(uint32 source_node_id, |
74 const mojo::String& url) OVERRIDE; | 74 const mojo::String& url) OVERRIDE; |
75 virtual void RequestNavigate( | 75 virtual void RequestNavigate( |
76 uint32 source_node_id, | 76 uint32 source_node_id, |
77 navigation::Target target, | 77 Target target, |
78 navigation::NavigationDetailsPtr nav_details) OVERRIDE; | 78 NavigationDetailsPtr nav_details) OVERRIDE; |
79 WindowManager* window_manager_; | 79 WindowManager* window_manager_; |
80 | 80 |
81 DISALLOW_COPY_AND_ASSIGN(NavigatorHost); | 81 DISALLOW_COPY_AND_ASSIGN(NavigatorHostImpl); |
82 }; | 82 }; |
83 | 83 |
84 class KeyboardManager : public KeyboardClient, | 84 class KeyboardManager : public KeyboardClient, |
85 public NodeObserver { | 85 public NodeObserver { |
86 public: | 86 public: |
87 KeyboardManager() : view_manager_(NULL), node_(NULL) { | 87 KeyboardManager() : view_manager_(NULL), node_(NULL) { |
88 } | 88 } |
89 virtual ~KeyboardManager() { | 89 virtual ~KeyboardManager() { |
90 if (node_) | 90 if (node_) |
91 node_->parent()->RemoveObserver(this); | 91 node_->parent()->RemoveObserver(this); |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 DISALLOW_COPY_AND_ASSIGN(RootLayoutManager); | 247 DISALLOW_COPY_AND_ASSIGN(RootLayoutManager); |
248 }; | 248 }; |
249 | 249 |
250 class WindowManager | 250 class WindowManager |
251 : public ApplicationDelegate, | 251 : public ApplicationDelegate, |
252 public DebugPanel::Delegate, | 252 public DebugPanel::Delegate, |
253 public ViewManagerDelegate, | 253 public ViewManagerDelegate, |
254 public WindowManagerDelegate, | 254 public WindowManagerDelegate, |
255 public InterfaceFactoryWithContext<WindowManagerConnection, | 255 public InterfaceFactoryWithContext<WindowManagerConnection, |
256 WindowManager>, | 256 WindowManager>, |
257 public InterfaceFactoryWithContext<NavigatorHost, WindowManager> { | 257 public InterfaceFactoryWithContext<NavigatorHostImpl, WindowManager> { |
258 public: | 258 public: |
259 WindowManager() | 259 WindowManager() |
260 : InterfaceFactoryWithContext<WindowManagerConnection, WindowManager>( | 260 : InterfaceFactoryWithContext<WindowManagerConnection, WindowManager>( |
261 this), | 261 this), |
262 InterfaceFactoryWithContext<NavigatorHost, WindowManager>(this), | 262 InterfaceFactoryWithContext<NavigatorHostImpl, WindowManager>(this), |
263 launcher_ui_(NULL), | 263 launcher_ui_(NULL), |
264 view_manager_(NULL), | 264 view_manager_(NULL), |
265 view_manager_client_factory_(this), | 265 view_manager_client_factory_(this), |
266 app_(NULL) {} | 266 app_(NULL) {} |
267 | 267 |
268 virtual ~WindowManager() {} | 268 virtual ~WindowManager() {} |
269 | 269 |
270 void CloseWindow(Id node_id) { | 270 void CloseWindow(Id node_id) { |
271 Node* node = view_manager_->GetNodeById(node_id); | 271 Node* node = view_manager_->GetNodeById(node_id); |
272 DCHECK(node); | 272 DCHECK(node); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 } | 308 } |
309 | 309 |
310 // Overridden from DebugPanel::Delegate: | 310 // Overridden from DebugPanel::Delegate: |
311 virtual void CloseTopWindow() OVERRIDE { | 311 virtual void CloseTopWindow() OVERRIDE { |
312 if (!windows_.empty()) | 312 if (!windows_.empty()) |
313 CloseWindow(windows_.back()->id()); | 313 CloseWindow(windows_.back()->id()); |
314 } | 314 } |
315 | 315 |
316 virtual void RequestNavigate( | 316 virtual void RequestNavigate( |
317 uint32 source_node_id, | 317 uint32 source_node_id, |
318 navigation::Target target, | 318 Target target, |
319 navigation::NavigationDetailsPtr nav_details) OVERRIDE { | 319 NavigationDetailsPtr nav_details) OVERRIDE { |
320 launcher_->Launch(nav_details->url, | 320 launcher_->Launch(nav_details->url, |
321 base::Bind(&WindowManager::OnLaunch, | 321 base::Bind(&WindowManager::OnLaunch, |
322 base::Unretained(this), | 322 base::Unretained(this), |
323 source_node_id, | 323 source_node_id, |
324 target)); | 324 target)); |
325 } | 325 } |
326 | 326 |
327 private: | 327 private: |
328 // Overridden from ApplicationDelegate: | 328 // Overridden from ApplicationDelegate: |
329 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE { | 329 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE { |
330 app_ = app; | 330 app_ = app; |
331 app->ConnectToService("mojo:mojo_launcher", &launcher_); | 331 app->ConnectToService("mojo:mojo_launcher", &launcher_); |
332 views_init_.reset(new ViewsInit); | 332 views_init_.reset(new ViewsInit); |
333 } | 333 } |
334 | 334 |
335 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) | 335 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) |
336 MOJO_OVERRIDE { | 336 MOJO_OVERRIDE { |
337 connection->AddService<IWindowManager>(this); | 337 connection->AddService<IWindowManager>(this); |
338 connection->AddService<navigation::NavigatorHost>(this); | 338 connection->AddService<mojo::NavigatorHost>(this); |
339 connection->AddService(&view_manager_client_factory_); | 339 connection->AddService(&view_manager_client_factory_); |
340 return true; | 340 return true; |
341 } | 341 } |
342 | 342 |
343 // Overridden from ViewManagerDelegate: | 343 // Overridden from ViewManagerDelegate: |
344 virtual void OnRootAdded(ViewManager* view_manager, Node* root) OVERRIDE { | 344 virtual void OnRootAdded(ViewManager* view_manager, Node* root) OVERRIDE { |
345 DCHECK(!view_manager_); | 345 DCHECK(!view_manager_); |
346 view_manager_ = view_manager; | 346 view_manager_ = view_manager; |
347 view_manager_->SetWindowManagerDelegate(this); | 347 view_manager_->SetWindowManagerDelegate(this); |
348 | 348 |
(...skipping 18 matching lines...) Expand all Loading... |
367 } | 367 } |
368 virtual void OnViewManagerDisconnected(ViewManager* view_manager) OVERRIDE { | 368 virtual void OnViewManagerDisconnected(ViewManager* view_manager) OVERRIDE { |
369 DCHECK_EQ(view_manager_, view_manager); | 369 DCHECK_EQ(view_manager_, view_manager); |
370 view_manager_ = NULL; | 370 view_manager_ = NULL; |
371 base::MessageLoop::current()->Quit(); | 371 base::MessageLoop::current()->Quit(); |
372 } | 372 } |
373 | 373 |
374 // Overridden from WindowManagerDelegate: | 374 // Overridden from WindowManagerDelegate: |
375 virtual void Embed(const String& url) OVERRIDE { | 375 virtual void Embed(const String& url) OVERRIDE { |
376 CreateWindow(url, | 376 CreateWindow(url, |
377 navigation::NavigationDetailsPtr().Pass(), | 377 NavigationDetailsPtr().Pass(), |
378 navigation::ResponseDetailsPtr().Pass()); | 378 ResponseDetailsPtr().Pass()); |
379 } | 379 } |
380 virtual void DispatchEvent(View* target, EventPtr event) OVERRIDE { | 380 virtual void DispatchEvent(View* target, EventPtr event) OVERRIDE { |
381 // TODO(beng): More sophisticated focus handling than this is required! | 381 // TODO(beng): More sophisticated focus handling than this is required! |
382 if (event->action == EVENT_TYPE_MOUSE_PRESSED && | 382 if (event->action == EVENT_TYPE_MOUSE_PRESSED && |
383 !IsDescendantOfKeyboard(target)) { | 383 !IsDescendantOfKeyboard(target)) { |
384 target->node()->SetFocus(); | 384 target->node()->SetFocus(); |
385 } | 385 } |
386 view_manager_->DispatchEvent(target, event.Pass()); | 386 view_manager_->DispatchEvent(target, event.Pass()); |
387 } | 387 } |
388 | 388 |
389 void OnLaunch( | 389 void OnLaunch( |
390 uint32 source_node_id, | 390 uint32 source_node_id, |
391 navigation::Target requested_target, | 391 Target requested_target, |
392 const mojo::String& handler_url, | 392 const mojo::String& handler_url, |
393 const mojo::String& view_url, | 393 const mojo::String& view_url, |
394 navigation::ResponseDetailsPtr response) { | 394 ResponseDetailsPtr response) { |
395 navigation::NavigationDetailsPtr nav_details( | 395 NavigationDetailsPtr nav_details(NavigationDetails::New()); |
396 navigation::NavigationDetails::New()); | |
397 nav_details->url = view_url; | 396 nav_details->url = view_url; |
398 | 397 |
399 navigation::Target target = debug_panel_->navigation_target(); | 398 Target target = debug_panel_->navigation_target(); |
400 if (target == navigation::TARGET_DEFAULT) { | 399 if (target == TARGET_DEFAULT) { |
401 if (requested_target != navigation::TARGET_DEFAULT) { | 400 if (requested_target != TARGET_DEFAULT) { |
402 target = requested_target; | 401 target = requested_target; |
403 } else { | 402 } else { |
404 // TODO(aa): Should be TARGET_NEW_NODE if source origin and dest origin | 403 // TODO(aa): Should be TARGET_NEW_NODE if source origin and dest origin |
405 // are different? | 404 // are different? |
406 target = navigation::TARGET_SOURCE_NODE; | 405 target = TARGET_SOURCE_NODE; |
407 } | 406 } |
408 } | 407 } |
409 | 408 |
410 Node* dest_node = NULL; | 409 Node* dest_node = NULL; |
411 if (target == navigation::TARGET_SOURCE_NODE) { | 410 if (target == TARGET_SOURCE_NODE) { |
412 Node* source_node = view_manager_->GetNodeById(source_node_id); | 411 Node* source_node = view_manager_->GetNodeById(source_node_id); |
413 bool app_initiated = std::find(windows_.begin(), windows_.end(), | 412 bool app_initiated = std::find(windows_.begin(), windows_.end(), |
414 source_node) != windows_.end(); | 413 source_node) != windows_.end(); |
415 if (app_initiated) | 414 if (app_initiated) |
416 dest_node = source_node; | 415 dest_node = source_node; |
417 else if (!windows_.empty()) | 416 else if (!windows_.empty()) |
418 dest_node = windows_.back(); | 417 dest_node = windows_.back(); |
419 } | 418 } |
420 | 419 |
421 if (dest_node) | 420 if (dest_node) |
422 Embed(dest_node, handler_url, nav_details.Pass(), response.Pass()); | 421 Embed(dest_node, handler_url, nav_details.Pass(), response.Pass()); |
423 else | 422 else |
424 CreateWindow(handler_url, nav_details.Pass(), response.Pass()); | 423 CreateWindow(handler_url, nav_details.Pass(), response.Pass()); |
425 } | 424 } |
426 | 425 |
427 // TODO(beng): proper layout manager!! | 426 // TODO(beng): proper layout manager!! |
428 Id CreateLauncherUI() { | 427 Id CreateLauncherUI() { |
429 navigation::NavigationDetailsPtr nav_details; | 428 NavigationDetailsPtr nav_details; |
430 navigation::ResponseDetailsPtr response; | 429 ResponseDetailsPtr response; |
431 Node* node = view_manager_->GetNodeById(content_node_id_); | 430 Node* node = view_manager_->GetNodeById(content_node_id_); |
432 gfx::Rect bounds = node->bounds(); | 431 gfx::Rect bounds = node->bounds(); |
433 bounds.Inset(kBorderInset, kBorderInset); | 432 bounds.Inset(kBorderInset, kBorderInset); |
434 bounds.set_height(kTextfieldHeight); | 433 bounds.set_height(kTextfieldHeight); |
435 launcher_ui_ = CreateChild(content_node_id_, "mojo:mojo_browser", bounds, | 434 launcher_ui_ = CreateChild(content_node_id_, "mojo:mojo_browser", bounds, |
436 nav_details.Pass(), response.Pass()); | 435 nav_details.Pass(), response.Pass()); |
437 return launcher_ui_->id(); | 436 return launcher_ui_->id(); |
438 } | 437 } |
439 | 438 |
440 void CreateWindow(const std::string& handler_url, | 439 void CreateWindow(const std::string& handler_url, |
441 navigation::NavigationDetailsPtr nav_details, | 440 NavigationDetailsPtr nav_details, |
442 navigation::ResponseDetailsPtr response) { | 441 ResponseDetailsPtr response) { |
443 Node* node = view_manager_->GetNodeById(content_node_id_); | 442 Node* node = view_manager_->GetNodeById(content_node_id_); |
444 gfx::Rect bounds(kBorderInset, | 443 gfx::Rect bounds(kBorderInset, |
445 2 * kBorderInset + kTextfieldHeight, | 444 2 * kBorderInset + kTextfieldHeight, |
446 node->bounds().width() - 3 * kBorderInset - | 445 node->bounds().width() - 3 * kBorderInset - |
447 kControlPanelWidth, | 446 kControlPanelWidth, |
448 node->bounds().height() - | 447 node->bounds().height() - |
449 (3 * kBorderInset + kTextfieldHeight)); | 448 (3 * kBorderInset + kTextfieldHeight)); |
450 if (!windows_.empty()) { | 449 if (!windows_.empty()) { |
451 gfx::Point position = windows_.back()->bounds().origin(); | 450 gfx::Point position = windows_.back()->bounds().origin(); |
452 position.Offset(35, 35); | 451 position.Offset(35, 35); |
453 bounds.set_origin(position); | 452 bounds.set_origin(position); |
454 } | 453 } |
455 windows_.push_back(CreateChild(content_node_id_, handler_url, bounds, | 454 windows_.push_back(CreateChild(content_node_id_, handler_url, bounds, |
456 nav_details.Pass(), response.Pass())); | 455 nav_details.Pass(), response.Pass())); |
457 } | 456 } |
458 | 457 |
459 Node* CreateChild(Id parent_id, | 458 Node* CreateChild(Id parent_id, |
460 const std::string& url, | 459 const std::string& url, |
461 const gfx::Rect& bounds, | 460 const gfx::Rect& bounds, |
462 navigation::NavigationDetailsPtr nav_details, | 461 NavigationDetailsPtr nav_details, |
463 navigation::ResponseDetailsPtr response) { | 462 ResponseDetailsPtr response) { |
464 Node* node = view_manager_->GetNodeById(parent_id); | 463 Node* node = view_manager_->GetNodeById(parent_id); |
465 Node* embedded = Node::Create(view_manager_); | 464 Node* embedded = Node::Create(view_manager_); |
466 node->AddChild(embedded); | 465 node->AddChild(embedded); |
467 embedded->SetBounds(bounds); | 466 embedded->SetBounds(bounds); |
468 Embed(embedded, url, nav_details.Pass(), response.Pass()); | 467 Embed(embedded, url, nav_details.Pass(), response.Pass()); |
469 embedded->SetFocus(); | 468 embedded->SetFocus(); |
470 return embedded; | 469 return embedded; |
471 } | 470 } |
472 | 471 |
473 void Embed(Node* node, const std::string& app_url, | 472 void Embed(Node* node, const std::string& app_url, |
474 navigation::NavigationDetailsPtr nav_details, | 473 NavigationDetailsPtr nav_details, |
475 navigation::ResponseDetailsPtr response) { | 474 ResponseDetailsPtr response) { |
476 node->Embed(app_url); | 475 node->Embed(app_url); |
477 if (nav_details) { | 476 if (nav_details) { |
478 navigation::NavigatorPtr navigator; | 477 NavigatorPtr navigator; |
479 app_->ConnectToService(app_url, &navigator); | 478 app_->ConnectToService(app_url, &navigator); |
480 navigator->Navigate(node->id(), nav_details.Pass(), response.Pass()); | 479 navigator->Navigate(node->id(), nav_details.Pass(), response.Pass()); |
481 } | 480 } |
482 } | 481 } |
483 | 482 |
484 bool IsDescendantOfKeyboard(View* target) { | 483 bool IsDescendantOfKeyboard(View* target) { |
485 return keyboard_manager_.get() && | 484 return keyboard_manager_.get() && |
486 keyboard_manager_->node()->Contains(target->node()); | 485 keyboard_manager_->node()->Contains(target->node()); |
487 } | 486 } |
488 | 487 |
(...skipping 10 matching lines...) Expand all Loading... |
499 root->bounds().height() - kBorderInset * 3 - | 498 root->bounds().height() - kBorderInset * 3 - |
500 kTextfieldHeight); | 499 kTextfieldHeight); |
501 node->SetBounds(bounds); | 500 node->SetBounds(bounds); |
502 | 501 |
503 debug_panel_ = new DebugPanel(this, node); | 502 debug_panel_ = new DebugPanel(this, node); |
504 return node->id(); | 503 return node->id(); |
505 } | 504 } |
506 | 505 |
507 scoped_ptr<ViewsInit> views_init_; | 506 scoped_ptr<ViewsInit> views_init_; |
508 DebugPanel* debug_panel_; | 507 DebugPanel* debug_panel_; |
509 launcher::LauncherPtr launcher_; | 508 LauncherPtr launcher_; |
510 Node* launcher_ui_; | 509 Node* launcher_ui_; |
511 std::vector<Node*> windows_; | 510 std::vector<Node*> windows_; |
512 ViewManager* view_manager_; | 511 ViewManager* view_manager_; |
513 ViewManagerClientFactory view_manager_client_factory_; | 512 ViewManagerClientFactory view_manager_client_factory_; |
514 scoped_ptr<RootLayoutManager> root_layout_manager_; | 513 scoped_ptr<RootLayoutManager> root_layout_manager_; |
515 | 514 |
516 // Id of the node most content is added to. The keyboard is NOT added here. | 515 // Id of the node most content is added to. The keyboard is NOT added here. |
517 Id content_node_id_; | 516 Id content_node_id_; |
518 | 517 |
519 scoped_ptr<KeyboardManager> keyboard_manager_; | 518 scoped_ptr<KeyboardManager> keyboard_manager_; |
520 ApplicationImpl* app_; | 519 ApplicationImpl* app_; |
521 | 520 |
522 DISALLOW_COPY_AND_ASSIGN(WindowManager); | 521 DISALLOW_COPY_AND_ASSIGN(WindowManager); |
523 }; | 522 }; |
524 | 523 |
525 void WindowManagerConnection::CloseWindow(Id node_id) { | 524 void WindowManagerConnection::CloseWindow(Id node_id) { |
526 window_manager_->CloseWindow(node_id); | 525 window_manager_->CloseWindow(node_id); |
527 } | 526 } |
528 | 527 |
529 void WindowManagerConnection::ShowKeyboard(Id view_id, RectPtr bounds) { | 528 void WindowManagerConnection::ShowKeyboard(Id view_id, RectPtr bounds) { |
530 window_manager_->ShowKeyboard(view_id, bounds.To<gfx::Rect>()); | 529 window_manager_->ShowKeyboard(view_id, bounds.To<gfx::Rect>()); |
531 } | 530 } |
532 | 531 |
533 void WindowManagerConnection::HideKeyboard(Id node_id) { | 532 void WindowManagerConnection::HideKeyboard(Id node_id) { |
534 window_manager_->HideKeyboard(node_id); | 533 window_manager_->HideKeyboard(node_id); |
535 } | 534 } |
536 | 535 |
537 void NavigatorHost::DidNavigateLocally(uint32 source_node_id, | 536 void NavigatorHostImpl::DidNavigateLocally(uint32 source_node_id, |
538 const mojo::String& url) { | 537 const mojo::String& url) { |
539 window_manager_->DidNavigateLocally(source_node_id, url); | 538 window_manager_->DidNavigateLocally(source_node_id, url); |
540 } | 539 } |
541 | 540 |
542 void NavigatorHost::RequestNavigate( | 541 void NavigatorHostImpl::RequestNavigate( |
543 uint32 source_node_id, | 542 uint32 source_node_id, |
544 navigation::Target target, | 543 Target target, |
545 navigation::NavigationDetailsPtr nav_details) { | 544 NavigationDetailsPtr nav_details) { |
546 window_manager_->RequestNavigate(source_node_id, target, nav_details.Pass()); | 545 window_manager_->RequestNavigate(source_node_id, target, nav_details.Pass()); |
547 } | 546 } |
548 | 547 |
549 } // namespace examples | 548 } // namespace examples |
550 | 549 |
551 // static | 550 // static |
552 ApplicationDelegate* ApplicationDelegate::Create() { | 551 ApplicationDelegate* ApplicationDelegate::Create() { |
553 return new examples::WindowManager; | 552 return new examples::WindowManager; |
554 } | 553 } |
555 | 554 |
556 } // namespace mojo | 555 } // namespace mojo |
OLD | NEW |