| OLD | NEW | 
|    1 // Copyright 2013 The Chromium Authors. All rights reserved. |    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 |    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 "apps/shell_window.h" |    5 #include "apps/shell_window.h" | 
|    6  |    6  | 
|    7 #include "apps/native_app_window.h" |    7 #include "apps/native_app_window.h" | 
|    8 #include "apps/shell_window_geometry_cache.h" |    8 #include "apps/shell_window_geometry_cache.h" | 
|    9 #include "apps/shell_window_registry.h" |    9 #include "apps/shell_window_registry.h" | 
|   10 #include "base/strings/string_util.h" |   10 #include "base/strings/string_util.h" | 
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   49 using web_modal::WebContentsModalDialogManager; |   49 using web_modal::WebContentsModalDialogManager; | 
|   50  |   50  | 
|   51 namespace { |   51 namespace { | 
|   52 const int kDefaultWidth = 512; |   52 const int kDefaultWidth = 512; | 
|   53 const int kDefaultHeight = 384; |   53 const int kDefaultHeight = 384; | 
|   54  |   54  | 
|   55 }  // namespace |   55 }  // namespace | 
|   56  |   56  | 
|   57 namespace apps { |   57 namespace apps { | 
|   58  |   58  | 
 |   59 ShellWindow::SizeConstraints::SizeConstraints() | 
 |   60     : maximum_size_(kUnboundedSize, kUnboundedSize) { | 
 |   61 } | 
 |   62  | 
 |   63 ShellWindow::SizeConstraints::SizeConstraints(const gfx::Size& min_size, | 
 |   64                                               const gfx::Size& max_size) | 
 |   65     : minimum_size_(min_size), | 
 |   66       maximum_size_(max_size) { | 
 |   67 } | 
 |   68  | 
 |   69 ShellWindow::SizeConstraints::~SizeConstraints() {} | 
 |   70  | 
 |   71 gfx::Size ShellWindow::SizeConstraints::ClampSize(gfx::Size size) const { | 
 |   72   const gfx::Size max_size = GetMaximumSize(); | 
 |   73   if (max_size.width() != kUnboundedSize) | 
 |   74     size.set_width(std::min(size.width(), GetMaximumSize().width())); | 
 |   75   if (max_size.height() != kUnboundedSize) | 
 |   76     size.set_height(std::min(size.height(), GetMaximumSize().height())); | 
 |   77   size.SetToMax(GetMinimumSize()); | 
 |   78   return size; | 
 |   79 } | 
 |   80  | 
 |   81 bool ShellWindow::SizeConstraints::HasMinimumSize() const { | 
 |   82   return GetMinimumSize().width() != kUnboundedSize || | 
 |   83       GetMinimumSize().height() != kUnboundedSize; | 
 |   84 } | 
 |   85  | 
 |   86 bool ShellWindow::SizeConstraints::HasMaximumSize() const { | 
 |   87   const gfx::Size max_size = GetMaximumSize(); | 
 |   88   return max_size.width() != kUnboundedSize || | 
 |   89       max_size.height() != kUnboundedSize; | 
 |   90 } | 
 |   91  | 
 |   92 bool ShellWindow::SizeConstraints::HasFixedSize() const { | 
 |   93   return !GetMinimumSize().IsEmpty() && GetMinimumSize() == GetMaximumSize(); | 
 |   94 } | 
 |   95  | 
 |   96 gfx::Size ShellWindow::SizeConstraints::GetMinimumSize() const { | 
 |   97   return minimum_size_; | 
 |   98 } | 
 |   99  | 
 |  100 gfx::Size ShellWindow::SizeConstraints::GetMaximumSize() const { | 
 |  101   return gfx::Size( | 
 |  102       maximum_size_.width() == kUnboundedSize ? | 
 |  103           kUnboundedSize : | 
 |  104           std::max(maximum_size_.width(), minimum_size_.width()), | 
 |  105       maximum_size_.height() == kUnboundedSize ? | 
 |  106           kUnboundedSize : | 
 |  107           std::max(maximum_size_.height(), minimum_size_.height())); | 
 |  108 } | 
 |  109  | 
|   59 ShellWindow::CreateParams::CreateParams() |  110 ShellWindow::CreateParams::CreateParams() | 
|   60   : window_type(ShellWindow::WINDOW_TYPE_DEFAULT), |  111   : window_type(ShellWindow::WINDOW_TYPE_DEFAULT), | 
|   61     frame(ShellWindow::FRAME_CHROME), |  112     frame(ShellWindow::FRAME_CHROME), | 
|   62     transparent_background(false), |  113     transparent_background(false), | 
|   63     bounds(INT_MIN, INT_MIN, 0, 0), |  114     bounds(INT_MIN, INT_MIN, 0, 0), | 
|   64     creator_process_id(0), |  115     creator_process_id(0), | 
|   65     state(ui::SHOW_STATE_DEFAULT), |  116     state(ui::SHOW_STATE_DEFAULT), | 
|   66     hidden(false), |  117     hidden(false), | 
|   67     resizable(true), |  118     resizable(true), | 
|   68     focused(true) {} |  119     focused(true) {} | 
| (...skipping 25 matching lines...) Expand all  Loading... | 
|   94   WebContents* web_contents = shell_window_contents_->GetWebContents(); |  145   WebContents* web_contents = shell_window_contents_->GetWebContents(); | 
|   95   delegate_->InitWebContents(web_contents); |  146   delegate_->InitWebContents(web_contents); | 
|   96   WebContentsModalDialogManager::CreateForWebContents(web_contents); |  147   WebContentsModalDialogManager::CreateForWebContents(web_contents); | 
|   97  |  148  | 
|   98   web_contents->SetDelegate(this); |  149   web_contents->SetDelegate(this); | 
|   99   WebContentsModalDialogManager::FromWebContents(web_contents)-> |  150   WebContentsModalDialogManager::FromWebContents(web_contents)-> | 
|  100       SetDelegate(this); |  151       SetDelegate(this); | 
|  101   extensions::SetViewType(web_contents, extensions::VIEW_TYPE_APP_SHELL); |  152   extensions::SetViewType(web_contents, extensions::VIEW_TYPE_APP_SHELL); | 
|  102  |  153  | 
|  103   // Initialize the window |  154   // Initialize the window | 
|  104   window_type_ = params.window_type; |  155   CreateParams new_params = LoadDefaultsAndConstrain(params); | 
|  105  |  156   window_type_ = new_params.window_type; | 
|  106   gfx::Rect bounds = params.bounds; |  157   window_key_ = new_params.window_key; | 
|  107  |  | 
|  108   if (bounds.width() == 0) |  | 
|  109     bounds.set_width(kDefaultWidth); |  | 
|  110   if (bounds.height() == 0) |  | 
|  111     bounds.set_height(kDefaultHeight); |  | 
|  112  |  | 
|  113   // If left and top are left undefined, the native shell window will center |  | 
|  114   // the window on the main screen in a platform-defined manner. |  | 
|  115  |  | 
|  116   CreateParams new_params = params; |  | 
|  117  |  | 
|  118   // Load cached state if it exists. |  | 
|  119   if (!params.window_key.empty()) { |  | 
|  120     window_key_ = params.window_key; |  | 
|  121  |  | 
|  122     ShellWindowGeometryCache* cache = ShellWindowGeometryCache::Get(profile()); |  | 
|  123  |  | 
|  124     gfx::Rect cached_bounds; |  | 
|  125     gfx::Rect cached_screen_bounds; |  | 
|  126     ui::WindowShowState cached_state = ui::SHOW_STATE_DEFAULT; |  | 
|  127     if (cache->GetGeometry(extension()->id(), params.window_key, &cached_bounds, |  | 
|  128                            &cached_screen_bounds, &cached_state)) { |  | 
|  129       // App window has cached screen bounds, make sure it fits on screen in |  | 
|  130       // case the screen resolution changed. |  | 
|  131       gfx::Screen* screen = gfx::Screen::GetNativeScreen(); |  | 
|  132       gfx::Display display = screen->GetDisplayMatching(cached_bounds); |  | 
|  133       gfx::Rect current_screen_bounds = display.work_area(); |  | 
|  134       AdjustBoundsToBeVisibleOnScreen(cached_bounds, |  | 
|  135                                       cached_screen_bounds, |  | 
|  136                                       current_screen_bounds, |  | 
|  137                                       params.minimum_size, |  | 
|  138                                       &bounds); |  | 
|  139       new_params.state = cached_state; |  | 
|  140     } |  | 
|  141   } |  | 
|  142  |  | 
|  143   gfx::Size& minimum_size = new_params.minimum_size; |  | 
|  144   gfx::Size& maximum_size = new_params.maximum_size; |  | 
|  145  |  | 
|  146   // In the case that minimum size > maximum size, we consider the minimum |  | 
|  147   // size to be more important. |  | 
|  148   if (maximum_size.width() && maximum_size.width() < minimum_size.width()) |  | 
|  149     maximum_size.set_width(minimum_size.width()); |  | 
|  150   if (maximum_size.height() && maximum_size.height() < minimum_size.height()) |  | 
|  151     maximum_size.set_height(minimum_size.height()); |  | 
|  152  |  | 
|  153   if (maximum_size.width() && bounds.width() > maximum_size.width()) |  | 
|  154     bounds.set_width(maximum_size.width()); |  | 
|  155   if (bounds.width() != INT_MIN && bounds.width() < minimum_size.width()) |  | 
|  156     bounds.set_width(minimum_size.width()); |  | 
|  157  |  | 
|  158   if (maximum_size.height() && bounds.height() > maximum_size.height()) |  | 
|  159     bounds.set_height(maximum_size.height()); |  | 
|  160   if (bounds.height() != INT_MIN && bounds.height() < minimum_size.height()) |  | 
|  161     bounds.set_height(minimum_size.height()); |  | 
|  162  |  | 
|  163   new_params.bounds = bounds; |  | 
|  164  |  | 
|  165   native_app_window_.reset(delegate_->CreateNativeAppWindow(this, new_params)); |  158   native_app_window_.reset(delegate_->CreateNativeAppWindow(this, new_params)); | 
|  166  |  159  | 
|  167   if (!new_params.hidden) { |  160   if (!new_params.hidden) { | 
|  168     if (window_type_is_panel()) |  161     if (window_type_is_panel()) | 
|  169       GetBaseWindow()->ShowInactive();  // Panels are not activated by default. |  162       GetBaseWindow()->ShowInactive();  // Panels are not activated by default. | 
|  170     else |  163     else | 
|  171       GetBaseWindow()->Show(); |  164       GetBaseWindow()->Show(); | 
|  172   } |  165   } | 
|  173  |  166  | 
|  174   if (new_params.state == ui::SHOW_STATE_FULLSCREEN) |  167   if (new_params.state == ui::SHOW_STATE_FULLSCREEN) | 
|  175     Fullscreen(); |  168     Fullscreen(); | 
|  176   else if (new_params.state == ui::SHOW_STATE_MAXIMIZED) |  169   else if (new_params.state == ui::SHOW_STATE_MAXIMIZED) | 
|  177     Maximize(); |  170     Maximize(); | 
|  178   else if (new_params.state == ui::SHOW_STATE_MINIMIZED) |  171   else if (new_params.state == ui::SHOW_STATE_MINIMIZED) | 
|  179     Minimize(); |  172     Minimize(); | 
|  180  |  173  | 
|  181   OnNativeWindowChanged(); |  174   OnNativeWindowChanged(); | 
|  182  |  175  | 
|  183   // When the render view host is changed, the native window needs to know |  176   // When the render view host is changed, the native window needs to know | 
|  184   // about it in case it has any setup to do to make the renderer appear |  177   // about it in case it has any setup to do to make the renderer appear | 
|  185   // properly. In particular, on Windows, the view's clickthrough region needs |  178   // properly. In particular, on Windows, the view's clickthrough region needs | 
|  186   // to be set. |  179   // to be set. | 
|  187   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |  180   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 
|  188                  content::Source<Profile>(profile_)); |  181                  content::Source<Profile>(profile_)); | 
|  189   // Close when the browser process is exiting. |  182   // Close when the browser process is exiting. | 
|  190   registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, |  183   registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, | 
|  191                  content::NotificationService::AllSources()); |  184                  content::NotificationService::AllSources()); | 
|  192  |  185  | 
|  193   shell_window_contents_->LoadContents(params.creator_process_id); |  186   shell_window_contents_->LoadContents(new_params.creator_process_id); | 
|  194  |  187  | 
|  195   // Prevent the browser process from shutting down while this window is open. |  188   // Prevent the browser process from shutting down while this window is open. | 
|  196   chrome::StartKeepAlive(); |  189   chrome::StartKeepAlive(); | 
|  197  |  190  | 
|  198   UpdateExtensionAppIcon(); |  191   UpdateExtensionAppIcon(); | 
|  199  |  192  | 
|  200   ShellWindowRegistry::Get(profile_)->AddShellWindow(this); |  193   ShellWindowRegistry::Get(profile_)->AddShellWindow(this); | 
|  201 } |  194 } | 
|  202  |  195  | 
|  203 ShellWindow::~ShellWindow() { |  196 ShellWindow::~ShellWindow() { | 
| (...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  636         std::max(current_screen_bounds.x(), |  629         std::max(current_screen_bounds.x(), | 
|  637                  std::min(bounds->x(), |  630                  std::min(bounds->x(), | 
|  638                           current_screen_bounds.right() - bounds->width()))); |  631                           current_screen_bounds.right() - bounds->width()))); | 
|  639     bounds->set_y( |  632     bounds->set_y( | 
|  640         std::max(current_screen_bounds.y(), |  633         std::max(current_screen_bounds.y(), | 
|  641                  std::min(bounds->y(), |  634                  std::min(bounds->y(), | 
|  642                           current_screen_bounds.bottom() - bounds->height()))); |  635                           current_screen_bounds.bottom() - bounds->height()))); | 
|  643   } |  636   } | 
|  644 } |  637 } | 
|  645  |  638  | 
 |  639 ShellWindow::CreateParams ShellWindow::LoadDefaultsAndConstrain( | 
 |  640     CreateParams params) const { | 
 |  641   gfx::Rect bounds = params.bounds; | 
 |  642  | 
 |  643   if (bounds.width() == 0) | 
 |  644     bounds.set_width(kDefaultWidth); | 
 |  645   if (bounds.height() == 0) | 
 |  646     bounds.set_height(kDefaultHeight); | 
 |  647  | 
 |  648   // If left and top are left undefined, the native shell window will center | 
 |  649   // the window on the main screen in a platform-defined manner. | 
 |  650  | 
 |  651   // Load cached state if it exists. | 
 |  652   if (!params.window_key.empty()) { | 
 |  653     ShellWindowGeometryCache* cache = ShellWindowGeometryCache::Get(profile()); | 
 |  654  | 
 |  655     gfx::Rect cached_bounds; | 
 |  656     gfx::Rect cached_screen_bounds; | 
 |  657     ui::WindowShowState cached_state = ui::SHOW_STATE_DEFAULT; | 
 |  658     if (cache->GetGeometry(extension()->id(), params.window_key, | 
 |  659                            &cached_bounds, &cached_screen_bounds, | 
 |  660                            &cached_state)) { | 
 |  661       // App window has cached screen bounds, make sure it fits on screen in | 
 |  662       // case the screen resolution changed. | 
 |  663       gfx::Screen* screen = gfx::Screen::GetNativeScreen(); | 
 |  664       gfx::Display display = screen->GetDisplayMatching(cached_bounds); | 
 |  665       gfx::Rect current_screen_bounds = display.work_area(); | 
 |  666       AdjustBoundsToBeVisibleOnScreen(cached_bounds, | 
 |  667                                       cached_screen_bounds, | 
 |  668                                       current_screen_bounds, | 
 |  669                                       params.minimum_size, | 
 |  670                                       &bounds); | 
 |  671       params.state = cached_state; | 
 |  672     } | 
 |  673   } | 
 |  674  | 
 |  675   SizeConstraints size_constraints(params.minimum_size, params.maximum_size); | 
 |  676   params.bounds.set_size(size_constraints.ClampSize(bounds.size())); | 
 |  677   params.minimum_size = size_constraints.GetMinimumSize(); | 
 |  678   params.maximum_size = size_constraints.GetMaximumSize(); | 
 |  679  | 
 |  680   return params; | 
 |  681 } | 
 |  682  | 
|  646 // static |  683 // static | 
|  647 SkRegion* ShellWindow::RawDraggableRegionsToSkRegion( |  684 SkRegion* ShellWindow::RawDraggableRegionsToSkRegion( | 
|  648       const std::vector<extensions::DraggableRegion>& regions) { |  685       const std::vector<extensions::DraggableRegion>& regions) { | 
|  649   SkRegion* sk_region = new SkRegion; |  686   SkRegion* sk_region = new SkRegion; | 
|  650   for (std::vector<extensions::DraggableRegion>::const_iterator iter = |  687   for (std::vector<extensions::DraggableRegion>::const_iterator iter = | 
|  651            regions.begin(); |  688            regions.begin(); | 
|  652        iter != regions.end(); ++iter) { |  689        iter != regions.end(); ++iter) { | 
|  653     const extensions::DraggableRegion& region = *iter; |  690     const extensions::DraggableRegion& region = *iter; | 
|  654     sk_region->op( |  691     sk_region->op( | 
|  655         region.bounds.x(), |  692         region.bounds.x(), | 
|  656         region.bounds.y(), |  693         region.bounds.y(), | 
|  657         region.bounds.right(), |  694         region.bounds.right(), | 
|  658         region.bounds.bottom(), |  695         region.bounds.bottom(), | 
|  659         region.draggable ? SkRegion::kUnion_Op : SkRegion::kDifference_Op); |  696         region.draggable ? SkRegion::kUnion_Op : SkRegion::kDifference_Op); | 
|  660   } |  697   } | 
|  661   return sk_region; |  698   return sk_region; | 
|  662 } |  699 } | 
|  663  |  700  | 
|  664 }  // namespace apps |  701 }  // namespace apps | 
| OLD | NEW |