Close crashed incognito-mode app's shell window.
[chromium-blink-merge.git] / apps / shell / web_view_window.cc
blob91e804968a52ff594812cec0c27a4869ca36485a
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "apps/shell/web_view_window.h"
7 #include "content/public/browser/web_contents.h"
8 #include "content/public/browser/web_contents_view.h"
9 #include "ui/aura/window.h"
10 #include "ui/views/controls/webview/webview.h"
11 #include "ui/views/layout/fill_layout.h"
12 #include "ui/views/widget/widget.h"
13 #include "ui/views/widget/widget_delegate.h"
15 namespace {
17 // A simple window containing a single web view.
18 class WebViewWindowContents : public views::WidgetDelegateView {
19 public:
20 explicit WebViewWindowContents(content::BrowserContext* browser_context)
21 : browser_context_(browser_context) {}
22 virtual ~WebViewWindowContents() {}
24 // views::WidgetDelegateView overrides:
25 virtual views::View* GetContentsView() OVERRIDE;
26 virtual void WindowClosing() OVERRIDE;
28 // views::View overrides:
29 virtual void ViewHierarchyChanged(
30 const ViewHierarchyChangedDetails& details) OVERRIDE;
32 private:
33 // Initialize this view's children.
34 void InitChildViews();
36 content::BrowserContext* browser_context_;
38 DISALLOW_COPY_AND_ASSIGN(WebViewWindowContents);
41 views::View* WebViewWindowContents::GetContentsView() {
42 return this;
45 void WebViewWindowContents::WindowClosing() {
46 // Close the app when the window is closed.
47 base::MessageLoopForUI::current()->Quit();
50 void WebViewWindowContents::ViewHierarchyChanged(
51 const ViewHierarchyChangedDetails& details) {
52 // Initialize child views when this view is attached.
53 if (details.is_add && details.child == this)
54 InitChildViews();
57 void WebViewWindowContents::InitChildViews() {
58 // Create a WebView that fills the window.
59 SetLayoutManager(new views::FillLayout);
60 views::WebView* web_view = new views::WebView(browser_context_);
61 AddChildView(web_view);
63 web_view->LoadInitialURL(GURL("http://www.google.com/"));
64 web_view->web_contents()->GetView()->Focus();
67 } // namespace
69 namespace apps {
71 void ShowWebViewWindow(content::BrowserContext* browser_context,
72 aura::Window* window_context) {
73 views::Widget* widget = new views::Widget;
74 views::Widget::InitParams params;
75 params.delegate = new WebViewWindowContents(browser_context);
76 params.context = window_context;
77 params.bounds = window_context->bounds();
78 params.top_level = true;
79 widget->Init(params);
80 widget->Show();
83 } // namespace apps