mash: Use ChromeNativeAppWindowViewsAuraAsh; workaround crashes.

Revert the ChromeNativeAppWindowViewsAura workaround.
Initialize the views::Widget::InitParams::mus_properties for mash.
(set the correct container, display id, and shelf item type)

Use ChromeNativeAppWindowViews's NonClientFrameView for panels too.
(ash::CustomFrameViewAsh and ash::PanelFrameView are still broken in mash)

BUG=679028,672599
TEST=chrome --mash panel windows are right-aligned.
R=sky@chromium.org

Review-Url: https://codereview.chromium.org/2665523002
Cr-Commit-Position: refs/heads/master@{#447014}
diff --git a/chrome/browser/ui/views/apps/chrome_app_window_client_views_chromeos.cc b/chrome/browser/ui/views/apps/chrome_app_window_client_views_chromeos.cc
index 1832d08..289f916e 100644
--- a/chrome/browser/ui/views/apps/chrome_app_window_client_views_chromeos.cc
+++ b/chrome/browser/ui/views/apps/chrome_app_window_client_views_chromeos.cc
@@ -5,18 +5,13 @@
 #include "chrome/browser/ui/apps/chrome_app_window_client.h"
 
 #include "chrome/browser/ui/views/apps/chrome_native_app_window_views_aura_ash.h"
-#include "services/service_manager/runner/common/client_util.h"
 
 // static
 extensions::NativeAppWindow* ChromeAppWindowClient::CreateNativeAppWindowImpl(
     extensions::AppWindow* app_window,
     const extensions::AppWindow::CreateParams& params) {
-  // TODO: Mash should use ChromeNativeAppWindowViewsAuraAsh, but
-  // it can't because of dependencies on ash. http://crbug.com/679028.
-  ChromeNativeAppWindowViewsAura* window =
-      service_manager::ServiceManagerIsRemote()
-          ? new ChromeNativeAppWindowViewsAura
-          : new ChromeNativeAppWindowViewsAuraAsh;
+  ChromeNativeAppWindowViewsAuraAsh* window =
+      new ChromeNativeAppWindowViewsAuraAsh;
   window->Init(app_window, params);
   return window;
 }
diff --git a/chrome/browser/ui/views/apps/chrome_native_app_window_views_aura_ash.cc b/chrome/browser/ui/views/apps/chrome_native_app_window_views_aura_ash.cc
index 2f719c0..0f74b5f 100644
--- a/chrome/browser/ui/views/apps/chrome_native_app_window_views_aura_ash.cc
+++ b/chrome/browser/ui/views/apps/chrome_native_app_window_views_aura_ash.cc
@@ -29,6 +29,7 @@
 #include "services/ui/public/cpp/property_type_converters.h"
 #include "services/ui/public/interfaces/window_manager.mojom.h"
 #include "ui/aura/client/aura_constants.h"
+#include "ui/aura/mus/property_converter.h"
 #include "ui/aura/mus/window_tree_host_mus.h"
 #include "ui/aura/window.h"
 #include "ui/aura/window_observer.h"
@@ -142,7 +143,9 @@
 
   if (app_window->window_type_is_panel()) {
     // Ash's ShelfWindowWatcher handles app panel windows once this type is set.
-    window->SetProperty<int>(ash::kShelfItemTypeKey, ash::TYPE_APP_PANEL);
+    // The type should have been initialized for mash below, via mus_properties.
+    if (!chrome::IsRunningInMash())
+      window->SetProperty<int>(ash::kShelfItemTypeKey, ash::TYPE_APP_PANEL);
   } else {
     window->SetProperty(aura::client::kAppType,
                         static_cast<int>(ash::AppType::CHROME_APP));
@@ -156,11 +159,23 @@
   ChromeNativeAppWindowViewsAura::OnBeforeWidgetInit(create_params, init_params,
                                                      widget);
   if (create_params.is_ime_window) {
-    // Puts ime windows into the ime window container.
-    init_params->parent =
-        ash::Shell::GetContainer(ash::Shell::GetPrimaryRootWindow(),
-                                 ash::kShellWindowId_ImeWindowParentContainer);
+    // Put ime windows into the ime window container on the primary display.
+    int container_id = ash::kShellWindowId_ImeWindowParentContainer;
+    if (chrome::IsRunningInMash()) {
+      init_params->mus_properties
+          [ui::mojom::WindowManager::kContainerId_InitProperty] =
+          mojo::ConvertTo<std::vector<uint8_t>>(container_id);
+      int display_id = display::Screen::GetScreen()->GetPrimaryDisplay().id();
+      init_params
+          ->mus_properties[ui::mojom::WindowManager::kDisplayId_InitProperty] =
+          mojo::ConvertTo<std::vector<uint8_t>>(display_id);
+    } else {
+      init_params->parent = ash::Shell::GetContainer(
+          ash::Shell::GetPrimaryRootWindow(), container_id);
+    }
   }
+  DCHECK_NE(AppWindow::WINDOW_TYPE_PANEL, create_params.window_type);
+  DCHECK_NE(AppWindow::WINDOW_TYPE_V1_PANEL, create_params.window_type);
   init_params->mus_properties
       [ui::mojom::WindowManager::kRemoveStandardFrame_InitProperty] =
       mojo::ConvertTo<std::vector<uint8_t>>(init_params->remove_standard_frame);
@@ -174,7 +189,14 @@
                                                           init_params,
                                                           widget);
 
-  if (ash::Shell::HasInstance() && use_default_bounds) {
+  if (chrome::IsRunningInMash()) {
+    // Ash's ShelfWindowWatcher handles app panel windows once this type is set.
+    init_params
+        ->mus_properties[ui::mojom::WindowManager::kShelfItemType_Property] =
+        mojo::ConvertTo<std::vector<uint8_t>>(
+            static_cast<aura::PropertyConverter::PrimitiveType>(
+                ash::TYPE_APP_PANEL));
+  } else if (ash::Shell::HasInstance() && use_default_bounds) {
     // Open a new panel on the target root.
     init_params->context = ash::Shell::GetTargetRootWindow();
     init_params->bounds = ash::ScreenUtil::ConvertRectToScreen(
@@ -294,6 +316,9 @@
   if (IsFrameless())
     return CreateNonStandardAppFrame();
 
+  if (chrome::IsRunningInMash())
+    return ChromeNativeAppWindowViews::CreateNonClientFrameView(widget);
+
   if (app_window()->window_type_is_panel()) {
     ash::PanelFrameView* frame_view =
         new ash::PanelFrameView(widget, ash::PanelFrameView::FRAME_ASH);
@@ -303,9 +328,6 @@
     return frame_view;
   }
 
-  if (chrome::IsRunningInMash())
-    return ChromeNativeAppWindowViews::CreateNonClientFrameView(widget);
-
   ash::CustomFrameViewAsh* custom_frame_view =
       new ash::CustomFrameViewAsh(widget);
   // Non-frameless app windows can be put into immersive fullscreen.
@@ -338,8 +360,10 @@
         ash::wm::GetWindowState(widget()->GetNativeWindow());
     window_state->set_hide_shelf_when_fullscreen(fullscreen_types !=
                                                  AppWindow::FULLSCREEN_TYPE_OS);
-    DCHECK(ash::Shell::HasInstance());
-    ash::Shell::GetInstance()->UpdateShelfVisibility();
+    if (!chrome::IsRunningInMash()) {
+      DCHECK(ash::Shell::HasInstance());
+      ash::Shell::GetInstance()->UpdateShelfVisibility();
+    }
   }
 }