1 // Copyright 2017 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.
4 
5 #include "chrome/browser/ui/views/native_widget_factory.h"
6 
7 #include "chrome/browser/profiles/profile.h"
8 #include "chrome/browser/ui/views/theme_profile_key.h"
9 #include "ui/aura/window.h"
10 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
11 #include "ui/views/widget/native_widget_aura.h"
12 
CreateNativeWidget(NativeWidgetType type,views::Widget::InitParams * params,views::internal::NativeWidgetDelegate * delegate)13 views::NativeWidget* CreateNativeWidget(
14     NativeWidgetType type,
15     views::Widget::InitParams* params,
16     views::internal::NativeWidgetDelegate* delegate) {
17   // While the majority of the time, context wasn't plumbed through due to the
18   // existence of a global WindowParentingClient, if this window is toplevel,
19   // it's possible that there is no contextual state that we can use.
20   gfx::NativeWindow parent_or_context =
21       params->parent ? params->parent : params->context;
22   Profile* profile = nullptr;
23   if (parent_or_context)
24     profile = GetThemeProfileForWindow(parent_or_context);
25   views::NativeWidget* native_widget = nullptr;
26   aura::Window* window = nullptr;
27   if (type == NativeWidgetType::DESKTOP_NATIVE_WIDGET_AURA ||
28       (!params->parent && !params->context && !params->child)) {
29     // In the desktop case, do not always set the profile window
30     // property from the parent since there are windows (like the task
31     // manager) that are not associated with a specific profile.
32     views::DesktopNativeWidgetAura* desktop_native_widget =
33         new views::DesktopNativeWidgetAura(delegate);
34     window = desktop_native_widget->GetNativeWindow();
35     native_widget = desktop_native_widget;
36   } else {
37     views::NativeWidgetAura* native_widget_aura =
38         new views::NativeWidgetAura(delegate);
39     if (params->parent) {
40       Profile* parent_profile = reinterpret_cast<Profile*>(
41           params->parent->GetNativeWindowProperty(Profile::kProfileKey));
42       native_widget_aura->SetNativeWindowProperty(Profile::kProfileKey,
43                                                   parent_profile);
44     }
45     window = native_widget_aura->GetNativeWindow();
46     native_widget = native_widget_aura;
47   }
48   // Use the original profile because |window| may outlive the profile
49   // of the context window.  This can happen with incognito profiles.
50   // However, the original profile will stick around until shutdown.
51   SetThemeProfileForWindow(window,
52                            profile ? profile->GetOriginalProfile() : nullptr);
53   return native_widget;
54 }
55