From ad3c6870f41f0b057697ea8dec59cc8cf8108745 Mon Sep 17 00:00:00 2001 From: hashimoto Date: Fri, 29 Aug 2014 02:46:57 -0700 Subject: Move AppWindow to extensions Move AppWindow, AppWindowRegistry and AppsClient from apps/ to extensions/browser/app_window Change the namespace from apps to extensions. Fix GYP and DEPS. BUG=403726 TBR=sky@chromium.org for include and namespace changes Review URL: https://codereview.chromium.org/494033002 Cr-Commit-Position: refs/heads/master@{#292599} --- apps/BUILD.gn | 6 - apps/app_lifetime_monitor.cc | 4 +- apps/app_lifetime_monitor.h | 14 +- apps/app_lifetime_monitor_factory.cc | 4 +- apps/app_load_service.cc | 2 +- apps/app_load_service_factory.cc | 4 +- apps/app_restore_service.cc | 2 +- apps/app_restore_service.h | 2 +- apps/app_shim/app_shim_handler_mac.cc | 4 +- apps/app_shim/extension_app_shim_handler_mac.cc | 13 +- apps/app_shim/extension_app_shim_handler_mac.h | 18 +- .../extension_app_shim_handler_mac_unittest.cc | 4 +- apps/app_window.cc | 1099 -------------------- apps/app_window.h | 560 ---------- apps/app_window_browsertest.cc | 82 -- apps/app_window_contents.cc | 1 + apps/app_window_contents.h | 8 +- apps/app_window_interactive_uitest.cc | 190 ---- apps/app_window_registry.cc | 353 ------- apps/app_window_registry.h | 160 --- apps/apps.gypi | 6 - apps/ui/apps_client.cc | 29 - apps/ui/apps_client.h | 64 -- apps/ui/views/native_app_window_views.cc | 4 +- apps/ui/views/native_app_window_views.h | 17 +- 25 files changed, 54 insertions(+), 2596 deletions(-) delete mode 100644 apps/app_window.cc delete mode 100644 apps/app_window.h delete mode 100644 apps/app_window_browsertest.cc delete mode 100644 apps/app_window_interactive_uitest.cc delete mode 100644 apps/app_window_registry.cc delete mode 100644 apps/app_window_registry.h delete mode 100644 apps/ui/apps_client.cc delete mode 100644 apps/ui/apps_client.h (limited to 'apps') diff --git a/apps/BUILD.gn b/apps/BUILD.gn index 5cc0849..acbfe889 100644 --- a/apps/BUILD.gn +++ b/apps/BUILD.gn @@ -19,12 +19,8 @@ static_library("apps") { "app_restore_service.h", "app_restore_service_factory.cc", "app_restore_service_factory.h", - "app_window.cc", - "app_window.h", "app_window_contents.cc", "app_window_contents.h", - "app_window_registry.cc", - "app_window_registry.h", "browser_context_keyed_service_factories.cc", "browser_context_keyed_service_factories.h", "custom_launcher_page_contents.cc", @@ -38,8 +34,6 @@ static_library("apps") { "saved_files_service_factory.h", "switches.cc", "switches.h", - "ui/apps_client.cc", - "ui/apps_client.h", ] configs += [ "//build/config/compiler:wexit_time_destructors" ] diff --git a/apps/app_lifetime_monitor.cc b/apps/app_lifetime_monitor.cc index 84b6c82..16323e9 100644 --- a/apps/app_lifetime_monitor.cc +++ b/apps/app_lifetime_monitor.cc @@ -4,17 +4,19 @@ #include "apps/app_lifetime_monitor.h" -#include "apps/app_window.h" #include "chrome/browser/chrome_notification_types.h" #include "chrome/browser/profiles/profile.h" #include "content/public/browser/notification_details.h" #include "content/public/browser/notification_service.h" +#include "extensions/browser/app_window/app_window.h" #include "extensions/browser/extension_host.h" #include "extensions/browser/notification_types.h" #include "extensions/common/extension.h" namespace apps { +using extensions::AppWindow; +using extensions::AppWindowRegistry; using extensions::Extension; using extensions::ExtensionHost; diff --git a/apps/app_lifetime_monitor.h b/apps/app_lifetime_monitor.h index bc46e0e..9dfee75 100644 --- a/apps/app_lifetime_monitor.h +++ b/apps/app_lifetime_monitor.h @@ -8,11 +8,11 @@ #include #include -#include "apps/app_window_registry.h" #include "base/observer_list.h" #include "components/keyed_service/core/keyed_service.h" #include "content/public/browser/notification_observer.h" #include "content/public/browser/notification_registrar.h" +#include "extensions/browser/app_window/app_window_registry.h" namespace extensions { class Extension; @@ -26,7 +26,7 @@ namespace apps { // events. class AppLifetimeMonitor : public KeyedService, public content::NotificationObserver, - public AppWindowRegistry::Observer { + public extensions::AppWindowRegistry::Observer { public: class Observer { public: @@ -60,15 +60,15 @@ class AppLifetimeMonitor : public KeyedService, const content::NotificationSource& source, const content::NotificationDetails& details) OVERRIDE; - // AppWindowRegistry::Observer overrides: - virtual void OnAppWindowRemoved(AppWindow* app_window) OVERRIDE; - virtual void OnAppWindowHidden(apps::AppWindow* app_window) OVERRIDE; - virtual void OnAppWindowShown(apps::AppWindow* app_window) OVERRIDE; + // extensions::AppWindowRegistry::Observer overrides: + virtual void OnAppWindowRemoved(extensions::AppWindow* app_window) OVERRIDE; + virtual void OnAppWindowHidden(extensions::AppWindow* app_window) OVERRIDE; + virtual void OnAppWindowShown(extensions::AppWindow* app_window) OVERRIDE; // KeyedService overrides: virtual void Shutdown() OVERRIDE; - bool HasVisibleAppWindows(apps::AppWindow* app_window) const; + bool HasVisibleAppWindows(extensions::AppWindow* app_window) const; void NotifyAppStart(const std::string& app_id); void NotifyAppActivated(const std::string& app_id); diff --git a/apps/app_lifetime_monitor_factory.cc b/apps/app_lifetime_monitor_factory.cc index e02297d..d561fdd 100644 --- a/apps/app_lifetime_monitor_factory.cc +++ b/apps/app_lifetime_monitor_factory.cc @@ -5,9 +5,9 @@ #include "apps/app_lifetime_monitor_factory.h" #include "apps/app_lifetime_monitor.h" -#include "apps/app_window_registry.h" #include "chrome/browser/profiles/profile.h" #include "components/keyed_service/content/browser_context_dependency_manager.h" +#include "extensions/browser/app_window/app_window_registry.h" #include "extensions/browser/extensions_browser_client.h" namespace apps { @@ -26,7 +26,7 @@ AppLifetimeMonitorFactory::AppLifetimeMonitorFactory() : BrowserContextKeyedServiceFactory( "AppLifetimeMonitor", BrowserContextDependencyManager::GetInstance()) { - DependsOn(AppWindowRegistry::Factory::GetInstance()); + DependsOn(extensions::AppWindowRegistry::Factory::GetInstance()); } AppLifetimeMonitorFactory::~AppLifetimeMonitorFactory() {} diff --git a/apps/app_load_service.cc b/apps/app_load_service.cc index 9560851..efafb1b 100644 --- a/apps/app_load_service.cc +++ b/apps/app_load_service.cc @@ -6,7 +6,6 @@ #include "apps/app_load_service_factory.h" #include "apps/app_restore_service.h" -#include "apps/app_window_registry.h" #include "apps/launcher.h" #include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/unpacked_installer.h" @@ -14,6 +13,7 @@ #include "content/public/browser/notification_details.h" #include "content/public/browser/notification_service.h" #include "content/public/browser/notification_types.h" +#include "extensions/browser/app_window/app_window_registry.h" #include "extensions/browser/extension_host.h" #include "extensions/browser/extension_prefs.h" #include "extensions/browser/extension_system.h" diff --git a/apps/app_load_service_factory.cc b/apps/app_load_service_factory.cc index 23ffd1e..0733667 100644 --- a/apps/app_load_service_factory.cc +++ b/apps/app_load_service_factory.cc @@ -5,9 +5,9 @@ #include "apps/app_load_service_factory.h" #include "apps/app_load_service.h" -#include "apps/app_window_registry.h" #include "chrome/browser/profiles/profile.h" #include "components/keyed_service/content/browser_context_dependency_manager.h" +#include "extensions/browser/app_window/app_window_registry.h" #include "extensions/browser/extension_prefs_factory.h" #include "extensions/browser/extension_system_provider.h" #include "extensions/browser/extensions_browser_client.h" @@ -31,7 +31,7 @@ AppLoadServiceFactory::AppLoadServiceFactory() DependsOn( extensions::ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); DependsOn(extensions::ExtensionPrefsFactory::GetInstance()); - DependsOn(AppWindowRegistry::Factory::GetInstance()); + DependsOn(extensions::AppWindowRegistry::Factory::GetInstance()); } AppLoadServiceFactory::~AppLoadServiceFactory() { diff --git a/apps/app_restore_service.cc b/apps/app_restore_service.cc index 7388cdc1..a68c59e 100644 --- a/apps/app_restore_service.cc +++ b/apps/app_restore_service.cc @@ -6,11 +6,11 @@ #include "apps/app_lifetime_monitor_factory.h" #include "apps/app_restore_service_factory.h" -#include "apps/app_window.h" #include "apps/launcher.h" #include "apps/saved_files_service.h" #include "chrome/browser/chrome_notification_types.h" #include "chrome/browser/profiles/profile.h" +#include "extensions/browser/app_window/app_window.h" #include "extensions/browser/extension_host.h" #include "extensions/browser/extension_prefs.h" #include "extensions/browser/extension_registry.h" diff --git a/apps/app_restore_service.h b/apps/app_restore_service.h index 33a407a..125d50c 100644 --- a/apps/app_restore_service.h +++ b/apps/app_restore_service.h @@ -9,8 +9,8 @@ #include #include "apps/app_lifetime_monitor.h" -#include "apps/app_window_registry.h" #include "components/keyed_service/core/keyed_service.h" +#include "extensions/browser/app_window/app_window_registry.h" namespace extensions { class Extension; diff --git a/apps/app_shim/app_shim_handler_mac.cc b/apps/app_shim/app_shim_handler_mac.cc index 7e67e73..3153bd4 100644 --- a/apps/app_shim/app_shim_handler_mac.cc +++ b/apps/app_shim/app_shim_handler_mac.cc @@ -6,7 +6,6 @@ #include -#include "apps/app_window_registry.h" #include "base/bind.h" #include "base/logging.h" #include "base/memory/singleton.h" @@ -17,6 +16,7 @@ #include "content/public/browser/notification_observer.h" #include "content/public/browser/notification_registrar.h" #include "content/public/browser/notification_service.h" +#include "extensions/browser/app_window/app_window_registry.h" namespace apps { @@ -24,7 +24,7 @@ namespace { void TerminateIfNoAppWindows() { bool app_windows_left = - apps::AppWindowRegistry::IsAppWindowRegisteredInAnyProfile(0); + extensions::AppWindowRegistry::IsAppWindowRegisteredInAnyProfile(0); if (!app_windows_left && !AppListService::Get(chrome::HOST_DESKTOP_TYPE_NATIVE) ->IsAppListVisible()) { diff --git a/apps/app_shim/extension_app_shim_handler_mac.cc b/apps/app_shim/extension_app_shim_handler_mac.cc index f15ad4e..8e90324 100644 --- a/apps/app_shim/extension_app_shim_handler_mac.cc +++ b/apps/app_shim/extension_app_shim_handler_mac.cc @@ -7,8 +7,6 @@ #include "apps/app_lifetime_monitor_factory.h" #include "apps/app_shim/app_shim_host_manager_mac.h" #include "apps/app_shim/app_shim_messages.h" -#include "apps/app_window.h" -#include "apps/app_window_registry.h" #include "apps/launcher.h" #include "base/files/file_path.h" #include "base/logging.h" @@ -25,16 +23,20 @@ #include "content/public/browser/notification_details.h" #include "content/public/browser/notification_service.h" #include "content/public/browser/notification_source.h" +#include "extensions/browser/app_window/app_window.h" +#include "extensions/browser/app_window/app_window_registry.h" #include "extensions/browser/app_window/native_app_window.h" #include "extensions/browser/extension_host.h" #include "extensions/browser/extension_registry.h" #include "ui/base/cocoa/focus_window_set.h" +using extensions::AppWindow; +using extensions::AppWindowRegistry; using extensions::ExtensionRegistry; namespace { -typedef apps::AppWindowRegistry::AppWindowList AppWindowList; +typedef AppWindowRegistry::AppWindowList AppWindowList; void ProfileLoadedCallback(base::Callback callback, Profile* profile, @@ -50,7 +52,7 @@ void ProfileLoadedCallback(base::Callback callback, void SetAppHidden(Profile* profile, const std::string& app_id, bool hidden) { AppWindowList windows = - apps::AppWindowRegistry::Get(profile)->GetAppWindowsForApp(app_id); + AppWindowRegistry::Get(profile)->GetAppWindowsForApp(app_id); for (AppWindowList::const_reverse_iterator it = windows.rbegin(); it != windows.rend(); ++it) { @@ -262,8 +264,7 @@ void ExtensionAppShimHandler::FocusAppForWindow(AppWindow* app_window) { APP_SHIM_FOCUS_NORMAL, std::vector()); } else { - FocusWindows( - apps::AppWindowRegistry::Get(profile)->GetAppWindowsForApp(app_id)); + FocusWindows(AppWindowRegistry::Get(profile)->GetAppWindowsForApp(app_id)); } } diff --git a/apps/app_shim/extension_app_shim_handler_mac.h b/apps/app_shim/extension_app_shim_handler_mac.h index bc8efb1..6bebb04 100644 --- a/apps/app_shim/extension_app_shim_handler_mac.h +++ b/apps/app_shim/extension_app_shim_handler_mac.h @@ -11,11 +11,11 @@ #include "apps/app_lifetime_monitor.h" #include "apps/app_shim/app_shim_handler_mac.h" -#include "apps/app_window_registry.h" #include "base/memory/scoped_ptr.h" #include "base/memory/weak_ptr.h" #include "content/public/browser/notification_observer.h" #include "content/public/browser/notification_registrar.h" +#include "extensions/browser/app_window/app_window_registry.h" class Profile; @@ -28,13 +28,12 @@ class WebContents; } namespace extensions { +class AppWindow; class Extension; } namespace apps { -class AppWindow; - // This app shim handler that handles events for app shims that correspond to an // extension. class ExtensionAppShimHandler : public AppShimHandler, @@ -50,7 +49,7 @@ class ExtensionAppShimHandler : public AppShimHandler, virtual void LoadProfileAsync(const base::FilePath& path, base::Callback callback); - virtual AppWindowRegistry::AppWindowList GetWindows( + virtual extensions::AppWindowRegistry::AppWindowList GetWindows( Profile* profile, const std::string& extension_id); @@ -73,20 +72,21 @@ class ExtensionAppShimHandler : public AppShimHandler, AppShimHandler::Host* FindHost(Profile* profile, const std::string& app_id); - static void QuitAppForWindow(AppWindow* app_window); + static void QuitAppForWindow(extensions::AppWindow* app_window); - static void HideAppForWindow(AppWindow* app_window); + static void HideAppForWindow(extensions::AppWindow* app_window); - static void FocusAppForWindow(AppWindow* app_window); + static void FocusAppForWindow(extensions::AppWindow* app_window); // Brings the window to the front without showing it and instructs the shim to // request user attention. If there is no shim, show the app and return false. - static bool ActivateAndRequestUserAttentionForWindow(AppWindow* app_window); + static bool ActivateAndRequestUserAttentionForWindow( + extensions::AppWindow* app_window); // Instructs the shim to request user attention. Returns false if there is no // shim for this window. static void RequestUserAttentionForWindow( - AppWindow* app_window, + extensions::AppWindow* app_window, AppShimAttentionType attention_type); // Called by AppControllerMac when Chrome hides. diff --git a/apps/app_shim/extension_app_shim_handler_mac_unittest.cc b/apps/app_shim/extension_app_shim_handler_mac_unittest.cc index 573a48f..27791d9 100644 --- a/apps/app_shim/extension_app_shim_handler_mac_unittest.cc +++ b/apps/app_shim/extension_app_shim_handler_mac_unittest.cc @@ -18,7 +18,7 @@ namespace apps { using extensions::Extension; -typedef AppWindowRegistry::AppWindowList AppWindowList; +typedef extensions::AppWindowRegistry::AppWindowList AppWindowList; using ::testing::_; using ::testing::Invoke; @@ -176,7 +176,7 @@ class ExtensionAppShimHandlerTest : public testing::Test { // In most tests, we don't care about the result of GetWindows, it just // needs to be non-empty. AppWindowList app_window_list; - app_window_list.push_back(static_cast(NULL)); + app_window_list.push_back(static_cast(NULL)); EXPECT_CALL(*delegate_, GetWindows(_, _)) .WillRepeatedly(Return(app_window_list)); diff --git a/apps/app_window.cc b/apps/app_window.cc deleted file mode 100644 index 7fd02cd..0000000 --- a/apps/app_window.cc +++ /dev/null @@ -1,1099 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "apps/app_window.h" - -#include -#include -#include - -#include "apps/app_window_registry.h" -#include "apps/ui/apps_client.h" -#include "base/command_line.h" -#include "base/strings/string_util.h" -#include "base/strings/utf_string_conversions.h" -#include "base/values.h" -#include "components/web_modal/web_contents_modal_dialog_manager.h" -#include "content/public/browser/browser_context.h" -#include "content/public/browser/invalidate_type.h" -#include "content/public/browser/navigation_entry.h" -#include "content/public/browser/notification_details.h" -#include "content/public/browser/notification_service.h" -#include "content/public/browser/notification_source.h" -#include "content/public/browser/notification_types.h" -#include "content/public/browser/render_view_host.h" -#include "content/public/browser/resource_dispatcher_host.h" -#include "content/public/browser/web_contents.h" -#include "content/public/common/content_switches.h" -#include "content/public/common/media_stream_request.h" -#include "extensions/browser/app_window/app_delegate.h" -#include "extensions/browser/app_window/app_web_contents_helper.h" -#include "extensions/browser/app_window/app_window_geometry_cache.h" -#include "extensions/browser/app_window/native_app_window.h" -#include "extensions/browser/app_window/size_constraints.h" -#include "extensions/browser/extension_registry.h" -#include "extensions/browser/extension_system.h" -#include "extensions/browser/extensions_browser_client.h" -#include "extensions/browser/notification_types.h" -#include "extensions/browser/process_manager.h" -#include "extensions/browser/suggest_permission_util.h" -#include "extensions/browser/view_type_utils.h" -#include "extensions/common/draggable_region.h" -#include "extensions/common/extension.h" -#include "extensions/common/manifest_handlers/icons_handler.h" -#include "extensions/common/permissions/permissions_data.h" -#include "extensions/common/switches.h" -#include "third_party/skia/include/core/SkRegion.h" -#include "ui/gfx/screen.h" - -#if !defined(OS_MACOSX) -#include "base/prefs/pref_service.h" -#include "extensions/browser/pref_names.h" -#endif - -using content::BrowserContext; -using content::ConsoleMessageLevel; -using content::WebContents; -using extensions::APIPermission; -using extensions::NativeAppWindow; -using web_modal::WebContentsModalDialogHost; -using web_modal::WebContentsModalDialogManager; - -namespace apps { - -namespace { - -const int kDefaultWidth = 512; -const int kDefaultHeight = 384; - -void SetConstraintProperty(const std::string& name, - int value, - base::DictionaryValue* bounds_properties) { - if (value != extensions::SizeConstraints::kUnboundedSize) - bounds_properties->SetInteger(name, value); - else - bounds_properties->Set(name, base::Value::CreateNullValue()); -} - -void SetBoundsProperties(const gfx::Rect& bounds, - const gfx::Size& min_size, - const gfx::Size& max_size, - const std::string& bounds_name, - base::DictionaryValue* window_properties) { - scoped_ptr bounds_properties( - new base::DictionaryValue()); - - bounds_properties->SetInteger("left", bounds.x()); - bounds_properties->SetInteger("top", bounds.y()); - bounds_properties->SetInteger("width", bounds.width()); - bounds_properties->SetInteger("height", bounds.height()); - - SetConstraintProperty("minWidth", min_size.width(), bounds_properties.get()); - SetConstraintProperty( - "minHeight", min_size.height(), bounds_properties.get()); - SetConstraintProperty("maxWidth", max_size.width(), bounds_properties.get()); - SetConstraintProperty( - "maxHeight", max_size.height(), bounds_properties.get()); - - window_properties->Set(bounds_name, bounds_properties.release()); -} - -// Combines the constraints of the content and window, and returns constraints -// for the window. -gfx::Size GetCombinedWindowConstraints(const gfx::Size& window_constraints, - const gfx::Size& content_constraints, - const gfx::Insets& frame_insets) { - gfx::Size combined_constraints(window_constraints); - if (content_constraints.width() > 0) { - combined_constraints.set_width( - content_constraints.width() + frame_insets.width()); - } - if (content_constraints.height() > 0) { - combined_constraints.set_height( - content_constraints.height() + frame_insets.height()); - } - return combined_constraints; -} - -// Combines the constraints of the content and window, and returns constraints -// for the content. -gfx::Size GetCombinedContentConstraints(const gfx::Size& window_constraints, - const gfx::Size& content_constraints, - const gfx::Insets& frame_insets) { - gfx::Size combined_constraints(content_constraints); - if (window_constraints.width() > 0) { - combined_constraints.set_width( - std::max(0, window_constraints.width() - frame_insets.width())); - } - if (window_constraints.height() > 0) { - combined_constraints.set_height( - std::max(0, window_constraints.height() - frame_insets.height())); - } - return combined_constraints; -} - -} // namespace - -// AppWindow::BoundsSpecification - -const int AppWindow::BoundsSpecification::kUnspecifiedPosition = INT_MIN; - -AppWindow::BoundsSpecification::BoundsSpecification() - : bounds(kUnspecifiedPosition, kUnspecifiedPosition, 0, 0) {} - -AppWindow::BoundsSpecification::~BoundsSpecification() {} - -void AppWindow::BoundsSpecification::ResetBounds() { - bounds.SetRect(kUnspecifiedPosition, kUnspecifiedPosition, 0, 0); -} - -// AppWindow::CreateParams - -AppWindow::CreateParams::CreateParams() - : window_type(AppWindow::WINDOW_TYPE_DEFAULT), - frame(AppWindow::FRAME_CHROME), - has_frame_color(false), - active_frame_color(SK_ColorBLACK), - inactive_frame_color(SK_ColorBLACK), - alpha_enabled(false), - creator_process_id(0), - state(ui::SHOW_STATE_DEFAULT), - hidden(false), - resizable(true), - focused(true), - always_on_top(false) { -} - -AppWindow::CreateParams::~CreateParams() {} - -gfx::Rect AppWindow::CreateParams::GetInitialWindowBounds( - const gfx::Insets& frame_insets) const { - // Combine into a single window bounds. - gfx::Rect combined_bounds(window_spec.bounds); - if (content_spec.bounds.x() != BoundsSpecification::kUnspecifiedPosition) - combined_bounds.set_x(content_spec.bounds.x() - frame_insets.left()); - if (content_spec.bounds.y() != BoundsSpecification::kUnspecifiedPosition) - combined_bounds.set_y(content_spec.bounds.y() - frame_insets.top()); - if (content_spec.bounds.width() > 0) { - combined_bounds.set_width( - content_spec.bounds.width() + frame_insets.width()); - } - if (content_spec.bounds.height() > 0) { - combined_bounds.set_height( - content_spec.bounds.height() + frame_insets.height()); - } - - // Constrain the bounds. - extensions::SizeConstraints constraints( - GetCombinedWindowConstraints( - window_spec.minimum_size, content_spec.minimum_size, frame_insets), - GetCombinedWindowConstraints( - window_spec.maximum_size, content_spec.maximum_size, frame_insets)); - combined_bounds.set_size(constraints.ClampSize(combined_bounds.size())); - - return combined_bounds; -} - -gfx::Size AppWindow::CreateParams::GetContentMinimumSize( - const gfx::Insets& frame_insets) const { - return GetCombinedContentConstraints(window_spec.minimum_size, - content_spec.minimum_size, - frame_insets); -} - -gfx::Size AppWindow::CreateParams::GetContentMaximumSize( - const gfx::Insets& frame_insets) const { - return GetCombinedContentConstraints(window_spec.maximum_size, - content_spec.maximum_size, - frame_insets); -} - -gfx::Size AppWindow::CreateParams::GetWindowMinimumSize( - const gfx::Insets& frame_insets) const { - return GetCombinedWindowConstraints(window_spec.minimum_size, - content_spec.minimum_size, - frame_insets); -} - -gfx::Size AppWindow::CreateParams::GetWindowMaximumSize( - const gfx::Insets& frame_insets) const { - return GetCombinedWindowConstraints(window_spec.maximum_size, - content_spec.maximum_size, - frame_insets); -} - -// AppWindow - -AppWindow::AppWindow(BrowserContext* context, - extensions::AppDelegate* app_delegate, - const extensions::Extension* extension) - : browser_context_(context), - extension_id_(extension->id()), - window_type_(WINDOW_TYPE_DEFAULT), - app_delegate_(app_delegate), - image_loader_ptr_factory_(this), - fullscreen_types_(FULLSCREEN_TYPE_NONE), - show_on_first_paint_(false), - first_paint_complete_(false), - has_been_shown_(false), - can_send_events_(false), - is_hidden_(false), - cached_always_on_top_(false), - requested_alpha_enabled_(false) { - extensions::ExtensionsBrowserClient* client = - extensions::ExtensionsBrowserClient::Get(); - CHECK(!client->IsGuestSession(context) || context->IsOffTheRecord()) - << "Only off the record window may be opened in the guest mode."; -} - -void AppWindow::Init(const GURL& url, - AppWindowContents* app_window_contents, - const CreateParams& params) { - // Initialize the render interface and web contents - app_window_contents_.reset(app_window_contents); - app_window_contents_->Initialize(browser_context(), url); - WebContents* web_contents = app_window_contents_->GetWebContents(); - if (CommandLine::ForCurrentProcess()->HasSwitch( - extensions::switches::kEnableAppsShowOnFirstPaint)) { - content::WebContentsObserver::Observe(web_contents); - } - app_delegate_->InitWebContents(web_contents); - - WebContentsModalDialogManager::CreateForWebContents(web_contents); - - web_contents->SetDelegate(this); - WebContentsModalDialogManager::FromWebContents(web_contents) - ->SetDelegate(this); - extensions::SetViewType(web_contents, extensions::VIEW_TYPE_APP_WINDOW); - - // Initialize the window - CreateParams new_params = LoadDefaults(params); - window_type_ = new_params.window_type; - window_key_ = new_params.window_key; - - // Windows cannot be always-on-top in fullscreen mode for security reasons. - cached_always_on_top_ = new_params.always_on_top; - if (new_params.state == ui::SHOW_STATE_FULLSCREEN) - new_params.always_on_top = false; - - requested_alpha_enabled_ = new_params.alpha_enabled; - - AppsClient* apps_client = AppsClient::Get(); - native_app_window_.reset( - apps_client->CreateNativeAppWindow(this, new_params)); - - helper_.reset(new extensions::AppWebContentsHelper( - browser_context_, extension_id_, web_contents, app_delegate_.get())); - - popup_manager_.reset( - new web_modal::PopupManager(GetWebContentsModalDialogHost())); - popup_manager_->RegisterWith(web_contents); - - // Prevent the browser process from shutting down while this window exists. - apps_client->IncrementKeepAliveCount(); - UpdateExtensionAppIcon(); - AppWindowRegistry::Get(browser_context_)->AddAppWindow(this); - - if (new_params.hidden) { - // Although the window starts hidden by default, calling Hide() here - // notifies observers of the window being hidden. - Hide(); - } else { - // Panels are not activated by default. - Show(window_type_is_panel() || !new_params.focused ? SHOW_INACTIVE - : SHOW_ACTIVE); - } - - if (new_params.state == ui::SHOW_STATE_FULLSCREEN) - Fullscreen(); - else if (new_params.state == ui::SHOW_STATE_MAXIMIZED) - Maximize(); - else if (new_params.state == ui::SHOW_STATE_MINIMIZED) - Minimize(); - - OnNativeWindowChanged(); - - // When the render view host is changed, the native window needs to know - // about it in case it has any setup to do to make the renderer appear - // properly. In particular, on Windows, the view's clickthrough region needs - // to be set. - extensions::ExtensionsBrowserClient* client = - extensions::ExtensionsBrowserClient::Get(); - registrar_.Add(this, - extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, - content::Source( - client->GetOriginalContext(browser_context_))); - // Update the app menu if an ephemeral app becomes installed. - registrar_.Add( - this, - extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED, - content::Source( - client->GetOriginalContext(browser_context_))); - - // Close when the browser process is exiting. - app_delegate_->SetTerminatingCallback( - base::Bind(&NativeAppWindow::Close, - base::Unretained(native_app_window_.get()))); - - app_window_contents_->LoadContents(new_params.creator_process_id); - - if (CommandLine::ForCurrentProcess()->HasSwitch( - extensions::switches::kEnableAppsShowOnFirstPaint)) { - // We want to show the window only when the content has been painted. For - // that to happen, we need to define a size for the content, otherwise the - // layout will happen in a 0x0 area. - gfx::Insets frame_insets = native_app_window_->GetFrameInsets(); - gfx::Rect initial_bounds = new_params.GetInitialWindowBounds(frame_insets); - initial_bounds.Inset(frame_insets); - app_delegate_->ResizeWebContents(web_contents, initial_bounds.size()); - } -} - -AppWindow::~AppWindow() { - // Unregister now to prevent getting notified if we're the last window open. - app_delegate_->SetTerminatingCallback(base::Closure()); - - // Remove shutdown prevention. - AppsClient::Get()->DecrementKeepAliveCount(); -} - -void AppWindow::RequestMediaAccessPermission( - content::WebContents* web_contents, - const content::MediaStreamRequest& request, - const content::MediaResponseCallback& callback) { - DCHECK_EQ(AppWindow::web_contents(), web_contents); - helper_->RequestMediaAccessPermission(request, callback); -} - -WebContents* AppWindow::OpenURLFromTab(WebContents* source, - const content::OpenURLParams& params) { - DCHECK_EQ(web_contents(), source); - return helper_->OpenURLFromTab(params); -} - -void AppWindow::AddNewContents(WebContents* source, - WebContents* new_contents, - WindowOpenDisposition disposition, - const gfx::Rect& initial_pos, - bool user_gesture, - bool* was_blocked) { - DCHECK(new_contents->GetBrowserContext() == browser_context_); - app_delegate_->AddNewContents(browser_context_, - new_contents, - disposition, - initial_pos, - user_gesture, - was_blocked); -} - -bool AppWindow::PreHandleKeyboardEvent( - content::WebContents* source, - const content::NativeWebKeyboardEvent& event, - bool* is_keyboard_shortcut) { - const extensions::Extension* extension = GetExtension(); - if (!extension) - return false; - - // Here, we can handle a key event before the content gets it. When we are - // fullscreen and it is not forced, we want to allow the user to leave - // when ESC is pressed. - // However, if the application has the "overrideEscFullscreen" permission, we - // should let it override that behavior. - // ::HandleKeyboardEvent() will only be called if the KeyEvent's default - // action is not prevented. - // Thus, we should handle the KeyEvent here only if the permission is not set. - if (event.windowsKeyCode == ui::VKEY_ESCAPE && IsFullscreen() && - !IsForcedFullscreen() && - !extension->permissions_data()->HasAPIPermission( - APIPermission::kOverrideEscFullscreen)) { - Restore(); - return true; - } - - return false; -} - -void AppWindow::HandleKeyboardEvent( - WebContents* source, - const content::NativeWebKeyboardEvent& event) { - // If the window is currently fullscreen and not forced, ESC should leave - // fullscreen. If this code is being called for ESC, that means that the - // KeyEvent's default behavior was not prevented by the content. - if (event.windowsKeyCode == ui::VKEY_ESCAPE && IsFullscreen() && - !IsForcedFullscreen()) { - Restore(); - return; - } - - native_app_window_->HandleKeyboardEvent(event); -} - -void AppWindow::RequestToLockMouse(WebContents* web_contents, - bool user_gesture, - bool last_unlocked_by_target) { - DCHECK_EQ(AppWindow::web_contents(), web_contents); - helper_->RequestToLockMouse(); -} - -bool AppWindow::PreHandleGestureEvent(WebContents* source, - const blink::WebGestureEvent& event) { - return extensions::AppWebContentsHelper::ShouldSuppressGestureEvent(event); -} - -void AppWindow::DidFirstVisuallyNonEmptyPaint() { - first_paint_complete_ = true; - if (show_on_first_paint_) { - DCHECK(delayed_show_type_ == SHOW_ACTIVE || - delayed_show_type_ == SHOW_INACTIVE); - Show(delayed_show_type_); - } -} - -void AppWindow::OnNativeClose() { - AppWindowRegistry::Get(browser_context_)->RemoveAppWindow(this); - if (app_window_contents_) { - WebContents* web_contents = app_window_contents_->GetWebContents(); - WebContentsModalDialogManager::FromWebContents(web_contents) - ->SetDelegate(NULL); - app_window_contents_->NativeWindowClosed(); - } - delete this; -} - -void AppWindow::OnNativeWindowChanged() { - SaveWindowPosition(); - -#if defined(OS_WIN) - if (native_app_window_ && cached_always_on_top_ && !IsFullscreen() && - !native_app_window_->IsMaximized() && - !native_app_window_->IsMinimized()) { - UpdateNativeAlwaysOnTop(); - } -#endif - - if (app_window_contents_ && native_app_window_) - app_window_contents_->NativeWindowChanged(native_app_window_.get()); -} - -void AppWindow::OnNativeWindowActivated() { - AppWindowRegistry::Get(browser_context_)->AppWindowActivated(this); -} - -content::WebContents* AppWindow::web_contents() const { - return app_window_contents_->GetWebContents(); -} - -const extensions::Extension* AppWindow::GetExtension() const { - return extensions::ExtensionRegistry::Get(browser_context_) - ->enabled_extensions() - .GetByID(extension_id_); -} - -NativeAppWindow* AppWindow::GetBaseWindow() { return native_app_window_.get(); } - -gfx::NativeWindow AppWindow::GetNativeWindow() { - return GetBaseWindow()->GetNativeWindow(); -} - -gfx::Rect AppWindow::GetClientBounds() const { - gfx::Rect bounds = native_app_window_->GetBounds(); - bounds.Inset(native_app_window_->GetFrameInsets()); - return bounds; -} - -base::string16 AppWindow::GetTitle() const { - const extensions::Extension* extension = GetExtension(); - if (!extension) - return base::string16(); - - // WebContents::GetTitle() will return the page's URL if there's no - // specified. However, we'd prefer to show the name of the extension in that - // case, so we directly inspect the NavigationEntry's title. - base::string16 title; - if (!web_contents() || !web_contents()->GetController().GetActiveEntry() || - web_contents()->GetController().GetActiveEntry()->GetTitle().empty()) { - title = base::UTF8ToUTF16(extension->name()); - } else { - title = web_contents()->GetTitle(); - } - base::RemoveChars(title, base::ASCIIToUTF16("\n"), &title); - return title; -} - -void AppWindow::SetAppIconUrl(const GURL& url) { - // If the same url is being used for the badge, ignore it. - if (url == badge_icon_url_) - return; - - // Avoid using any previous icons that were being downloaded. - image_loader_ptr_factory_.InvalidateWeakPtrs(); - - // Reset |app_icon_image_| to abort pending image load (if any). - app_icon_image_.reset(); - - app_icon_url_ = url; - web_contents()->DownloadImage( - url, - true, // is a favicon - 0, // no maximum size - base::Bind(&AppWindow::DidDownloadFavicon, - image_loader_ptr_factory_.GetWeakPtr())); -} - -void AppWindow::SetBadgeIconUrl(const GURL& url) { - // Avoid using any previous icons that were being downloaded. - image_loader_ptr_factory_.InvalidateWeakPtrs(); - - // Reset |app_icon_image_| to abort pending image load (if any). - badge_icon_image_.reset(); - - badge_icon_url_ = url; - web_contents()->DownloadImage( - url, - true, // is a favicon - 0, // no maximum size - base::Bind(&AppWindow::DidDownloadFavicon, - image_loader_ptr_factory_.GetWeakPtr())); -} - -void AppWindow::ClearBadge() { - badge_icon_image_.reset(); - badge_icon_url_ = GURL(); - UpdateBadgeIcon(gfx::Image()); -} - -void AppWindow::UpdateShape(scoped_ptr<SkRegion> region) { - native_app_window_->UpdateShape(region.Pass()); -} - -void AppWindow::UpdateDraggableRegions( - const std::vector<extensions::DraggableRegion>& regions) { - native_app_window_->UpdateDraggableRegions(regions); -} - -void AppWindow::UpdateAppIcon(const gfx::Image& image) { - if (image.IsEmpty()) - return; - app_icon_ = image; - native_app_window_->UpdateWindowIcon(); - AppWindowRegistry::Get(browser_context_)->AppWindowIconChanged(this); -} - -void AppWindow::SetFullscreen(FullscreenType type, bool enable) { - DCHECK_NE(FULLSCREEN_TYPE_NONE, type); - - if (enable) { -#if !defined(OS_MACOSX) - // Do not enter fullscreen mode if disallowed by pref. - // TODO(bartfab): Add a test once it becomes possible to simulate a user - // gesture. http://crbug.com/174178 - if (type != FULLSCREEN_TYPE_FORCED) { - PrefService* prefs = - extensions::ExtensionsBrowserClient::Get()->GetPrefServiceForContext( - browser_context()); - if (!prefs->GetBoolean(extensions::pref_names::kAppFullscreenAllowed)) - return; - } -#endif - fullscreen_types_ |= type; - } else { - fullscreen_types_ &= ~type; - } - SetNativeWindowFullscreen(); -} - -bool AppWindow::IsFullscreen() const { - return fullscreen_types_ != FULLSCREEN_TYPE_NONE; -} - -bool AppWindow::IsForcedFullscreen() const { - return (fullscreen_types_ & FULLSCREEN_TYPE_FORCED) != 0; -} - -bool AppWindow::IsHtmlApiFullscreen() const { - return (fullscreen_types_ & FULLSCREEN_TYPE_HTML_API) != 0; -} - -void AppWindow::Fullscreen() { - SetFullscreen(FULLSCREEN_TYPE_WINDOW_API, true); -} - -void AppWindow::Maximize() { GetBaseWindow()->Maximize(); } - -void AppWindow::Minimize() { GetBaseWindow()->Minimize(); } - -void AppWindow::Restore() { - if (IsFullscreen()) { - fullscreen_types_ = FULLSCREEN_TYPE_NONE; - SetNativeWindowFullscreen(); - } else { - GetBaseWindow()->Restore(); - } -} - -void AppWindow::OSFullscreen() { - SetFullscreen(FULLSCREEN_TYPE_OS, true); -} - -void AppWindow::ForcedFullscreen() { - SetFullscreen(FULLSCREEN_TYPE_FORCED, true); -} - -void AppWindow::SetContentSizeConstraints(const gfx::Size& min_size, - const gfx::Size& max_size) { - extensions::SizeConstraints constraints(min_size, max_size); - native_app_window_->SetContentSizeConstraints(constraints.GetMinimumSize(), - constraints.GetMaximumSize()); - - gfx::Rect bounds = GetClientBounds(); - gfx::Size constrained_size = constraints.ClampSize(bounds.size()); - if (bounds.size() != constrained_size) { - bounds.set_size(constrained_size); - bounds.Inset(-native_app_window_->GetFrameInsets()); - native_app_window_->SetBounds(bounds); - } - OnNativeWindowChanged(); -} - -void AppWindow::Show(ShowType show_type) { - is_hidden_ = false; - - if (CommandLine::ForCurrentProcess()->HasSwitch( - extensions::switches::kEnableAppsShowOnFirstPaint)) { - show_on_first_paint_ = true; - - if (!first_paint_complete_) { - delayed_show_type_ = show_type; - return; - } - } - - switch (show_type) { - case SHOW_ACTIVE: - GetBaseWindow()->Show(); - break; - case SHOW_INACTIVE: - GetBaseWindow()->ShowInactive(); - break; - } - AppWindowRegistry::Get(browser_context_)->AppWindowShown(this); - - has_been_shown_ = true; - SendOnWindowShownIfShown(); -} - -void AppWindow::Hide() { - // This is there to prevent race conditions with Hide() being called before - // there was a non-empty paint. It should have no effect in a non-racy - // scenario where the application is hiding then showing a window: the second - // show will not be delayed. - is_hidden_ = true; - show_on_first_paint_ = false; - GetBaseWindow()->Hide(); - AppWindowRegistry::Get(browser_context_)->AppWindowHidden(this); -} - -void AppWindow::SetAlwaysOnTop(bool always_on_top) { - if (cached_always_on_top_ == always_on_top) - return; - - cached_always_on_top_ = always_on_top; - - // As a security measure, do not allow fullscreen windows or windows that - // overlap the taskbar to be on top. The property will be applied when the - // window exits fullscreen and moves away from the taskbar. - if (!IsFullscreen() && !IntersectsWithTaskbar()) - native_app_window_->SetAlwaysOnTop(always_on_top); - - OnNativeWindowChanged(); -} - -bool AppWindow::IsAlwaysOnTop() const { return cached_always_on_top_; } - -void AppWindow::WindowEventsReady() { - can_send_events_ = true; - SendOnWindowShownIfShown(); -} - -void AppWindow::GetSerializedState(base::DictionaryValue* properties) const { - DCHECK(properties); - - properties->SetBoolean("fullscreen", - native_app_window_->IsFullscreenOrPending()); - properties->SetBoolean("minimized", native_app_window_->IsMinimized()); - properties->SetBoolean("maximized", native_app_window_->IsMaximized()); - properties->SetBoolean("alwaysOnTop", IsAlwaysOnTop()); - properties->SetBoolean("hasFrameColor", native_app_window_->HasFrameColor()); - properties->SetBoolean( - "alphaEnabled", - requested_alpha_enabled_ && native_app_window_->CanHaveAlphaEnabled()); - - // These properties are undocumented and are to enable testing. Alpha is - // removed to - // make the values easier to check. - SkColor transparent_white = ~SK_ColorBLACK; - properties->SetInteger( - "activeFrameColor", - native_app_window_->ActiveFrameColor() & transparent_white); - properties->SetInteger( - "inactiveFrameColor", - native_app_window_->InactiveFrameColor() & transparent_white); - - gfx::Rect content_bounds = GetClientBounds(); - gfx::Size content_min_size = native_app_window_->GetContentMinimumSize(); - gfx::Size content_max_size = native_app_window_->GetContentMaximumSize(); - SetBoundsProperties(content_bounds, - content_min_size, - content_max_size, - "innerBounds", - properties); - - gfx::Insets frame_insets = native_app_window_->GetFrameInsets(); - gfx::Rect frame_bounds = native_app_window_->GetBounds(); - gfx::Size frame_min_size = extensions::SizeConstraints::AddFrameToConstraints( - content_min_size, frame_insets); - gfx::Size frame_max_size = extensions::SizeConstraints::AddFrameToConstraints( - content_max_size, frame_insets); - SetBoundsProperties(frame_bounds, - frame_min_size, - frame_max_size, - "outerBounds", - properties); -} - -//------------------------------------------------------------------------------ -// Private methods - -void AppWindow::UpdateBadgeIcon(const gfx::Image& image) { - badge_icon_ = image; - native_app_window_->UpdateBadgeIcon(); -} - -void AppWindow::DidDownloadFavicon( - int id, - int http_status_code, - const GURL& image_url, - const std::vector<SkBitmap>& bitmaps, - const std::vector<gfx::Size>& original_bitmap_sizes) { - if ((image_url != app_icon_url_ && image_url != badge_icon_url_) || - bitmaps.empty()) { - return; - } - - // Bitmaps are ordered largest to smallest. Choose the smallest bitmap - // whose height >= the preferred size. - int largest_index = 0; - for (size_t i = 1; i < bitmaps.size(); ++i) { - if (bitmaps[i].height() < app_delegate_->PreferredIconSize()) - break; - largest_index = i; - } - const SkBitmap& largest = bitmaps[largest_index]; - if (image_url == app_icon_url_) { - UpdateAppIcon(gfx::Image::CreateFrom1xBitmap(largest)); - return; - } - - UpdateBadgeIcon(gfx::Image::CreateFrom1xBitmap(largest)); -} - -void AppWindow::OnExtensionIconImageChanged(extensions::IconImage* image) { - DCHECK_EQ(app_icon_image_.get(), image); - - UpdateAppIcon(gfx::Image(app_icon_image_->image_skia())); -} - -void AppWindow::UpdateExtensionAppIcon() { - // Avoid using any previous app icons were being downloaded. - image_loader_ptr_factory_.InvalidateWeakPtrs(); - - const extensions::Extension* extension = GetExtension(); - if (!extension) - return; - - app_icon_image_.reset( - new extensions::IconImage(browser_context(), - extension, - extensions::IconsInfo::GetIcons(extension), - app_delegate_->PreferredIconSize(), - app_delegate_->GetAppDefaultIcon(), - this)); - - // Triggers actual image loading with 1x resources. The 2x resource will - // be handled by IconImage class when requested. - app_icon_image_->image_skia().GetRepresentation(1.0f); -} - -void AppWindow::SetNativeWindowFullscreen() { - native_app_window_->SetFullscreen(fullscreen_types_); - - if (cached_always_on_top_) - UpdateNativeAlwaysOnTop(); -} - -bool AppWindow::IntersectsWithTaskbar() const { -#if defined(OS_WIN) - gfx::Screen* screen = gfx::Screen::GetNativeScreen(); - gfx::Rect window_bounds = native_app_window_->GetRestoredBounds(); - std::vector<gfx::Display> displays = screen->GetAllDisplays(); - - for (std::vector<gfx::Display>::const_iterator it = displays.begin(); - it != displays.end(); - ++it) { - gfx::Rect taskbar_bounds = it->bounds(); - taskbar_bounds.Subtract(it->work_area()); - if (taskbar_bounds.IsEmpty()) - continue; - - if (window_bounds.Intersects(taskbar_bounds)) - return true; - } -#endif - - return false; -} - -void AppWindow::UpdateNativeAlwaysOnTop() { - DCHECK(cached_always_on_top_); - bool is_on_top = native_app_window_->IsAlwaysOnTop(); - bool fullscreen = IsFullscreen(); - bool intersects_taskbar = IntersectsWithTaskbar(); - - if (is_on_top && (fullscreen || intersects_taskbar)) { - // When entering fullscreen or overlapping the taskbar, ensure windows are - // not always-on-top. - native_app_window_->SetAlwaysOnTop(false); - } else if (!is_on_top && !fullscreen && !intersects_taskbar) { - // When exiting fullscreen and moving away from the taskbar, reinstate - // always-on-top. - native_app_window_->SetAlwaysOnTop(true); - } -} - -void AppWindow::SendOnWindowShownIfShown() { - if (!can_send_events_ || !has_been_shown_) - return; - - if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) { - app_window_contents_->DispatchWindowShownForTests(); - } -} - -void AppWindow::CloseContents(WebContents* contents) { - native_app_window_->Close(); -} - -bool AppWindow::ShouldSuppressDialogs() { return true; } - -content::ColorChooser* AppWindow::OpenColorChooser( - WebContents* web_contents, - SkColor initial_color, - const std::vector<content::ColorSuggestion>& suggestions) { - return app_delegate_->ShowColorChooser(web_contents, initial_color); -} - -void AppWindow::RunFileChooser(WebContents* tab, - const content::FileChooserParams& params) { - if (window_type_is_panel()) { - // Panels can't host a file dialog, abort. TODO(stevenjb): allow file - // dialogs to be unhosted but still close with the owning web contents. - // crbug.com/172502. - LOG(WARNING) << "File dialog opened by panel."; - return; - } - - app_delegate_->RunFileChooser(tab, params); -} - -bool AppWindow::IsPopupOrPanel(const WebContents* source) const { return true; } - -void AppWindow::MoveContents(WebContents* source, const gfx::Rect& pos) { - native_app_window_->SetBounds(pos); -} - -void AppWindow::NavigationStateChanged(const content::WebContents* source, - content::InvalidateTypes changed_flags) { - if (changed_flags & content::INVALIDATE_TYPE_TITLE) - native_app_window_->UpdateWindowTitle(); - else if (changed_flags & content::INVALIDATE_TYPE_TAB) - native_app_window_->UpdateWindowIcon(); -} - -void AppWindow::ToggleFullscreenModeForTab(content::WebContents* source, - bool enter_fullscreen) { - const extensions::Extension* extension = GetExtension(); - if (!extension) - return; - - if (!IsExtensionWithPermissionOrSuggestInConsole( - APIPermission::kFullscreen, extension, source->GetRenderViewHost())) { - return; - } - - SetFullscreen(FULLSCREEN_TYPE_HTML_API, enter_fullscreen); -} - -bool AppWindow::IsFullscreenForTabOrPending(const content::WebContents* source) - const { - return IsHtmlApiFullscreen(); -} - -void AppWindow::Observe(int type, - const content::NotificationSource& source, - const content::NotificationDetails& details) { - switch (type) { - case extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { - const extensions::Extension* unloaded_extension = - content::Details<extensions::UnloadedExtensionInfo>(details) - ->extension; - if (extension_id_ == unloaded_extension->id()) - native_app_window_->Close(); - break; - } - case extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED: { - const extensions::Extension* installed_extension = - content::Details<const extensions::InstalledExtensionInfo>(details) - ->extension; - DCHECK(installed_extension); - if (installed_extension->id() == extension_id()) - native_app_window_->UpdateShelfMenu(); - break; - } - default: - NOTREACHED() << "Received unexpected notification"; - } -} - -void AppWindow::SetWebContentsBlocked(content::WebContents* web_contents, - bool blocked) { - app_delegate_->SetWebContentsBlocked(web_contents, blocked); -} - -bool AppWindow::IsWebContentsVisible(content::WebContents* web_contents) { - return app_delegate_->IsWebContentsVisible(web_contents); -} - -WebContentsModalDialogHost* AppWindow::GetWebContentsModalDialogHost() { - return native_app_window_.get(); -} - -void AppWindow::SaveWindowPosition() { - if (window_key_.empty()) - return; - if (!native_app_window_) - return; - - extensions::AppWindowGeometryCache* cache = - extensions::AppWindowGeometryCache::Get(browser_context()); - - gfx::Rect bounds = native_app_window_->GetRestoredBounds(); - gfx::Rect screen_bounds = - gfx::Screen::GetNativeScreen()->GetDisplayMatching(bounds).work_area(); - ui::WindowShowState window_state = native_app_window_->GetRestoredState(); - cache->SaveGeometry( - extension_id(), window_key_, bounds, screen_bounds, window_state); -} - -void AppWindow::AdjustBoundsToBeVisibleOnScreen( - const gfx::Rect& cached_bounds, - const gfx::Rect& cached_screen_bounds, - const gfx::Rect& current_screen_bounds, - const gfx::Size& minimum_size, - gfx::Rect* bounds) const { - *bounds = cached_bounds; - - // Reposition and resize the bounds if the cached_screen_bounds is different - // from the current screen bounds and the current screen bounds doesn't - // completely contain the bounds. - if (cached_screen_bounds != current_screen_bounds && - !current_screen_bounds.Contains(cached_bounds)) { - bounds->set_width( - std::max(minimum_size.width(), - std::min(bounds->width(), current_screen_bounds.width()))); - bounds->set_height( - std::max(minimum_size.height(), - std::min(bounds->height(), current_screen_bounds.height()))); - bounds->set_x( - std::max(current_screen_bounds.x(), - std::min(bounds->x(), - current_screen_bounds.right() - bounds->width()))); - bounds->set_y( - std::max(current_screen_bounds.y(), - std::min(bounds->y(), - current_screen_bounds.bottom() - bounds->height()))); - } -} - -AppWindow::CreateParams AppWindow::LoadDefaults(CreateParams params) - const { - // Ensure width and height are specified. - if (params.content_spec.bounds.width() == 0 && - params.window_spec.bounds.width() == 0) { - params.content_spec.bounds.set_width(kDefaultWidth); - } - if (params.content_spec.bounds.height() == 0 && - params.window_spec.bounds.height() == 0) { - params.content_spec.bounds.set_height(kDefaultHeight); - } - - // If left and top are left undefined, the native app window will center - // the window on the main screen in a platform-defined manner. - - // Load cached state if it exists. - if (!params.window_key.empty()) { - extensions::AppWindowGeometryCache* cache = - extensions::AppWindowGeometryCache::Get(browser_context()); - - gfx::Rect cached_bounds; - gfx::Rect cached_screen_bounds; - ui::WindowShowState cached_state = ui::SHOW_STATE_DEFAULT; - if (cache->GetGeometry(extension_id(), - params.window_key, - &cached_bounds, - &cached_screen_bounds, - &cached_state)) { - // App window has cached screen bounds, make sure it fits on screen in - // case the screen resolution changed. - gfx::Screen* screen = gfx::Screen::GetNativeScreen(); - gfx::Display display = screen->GetDisplayMatching(cached_bounds); - gfx::Rect current_screen_bounds = display.work_area(); - extensions::SizeConstraints constraints( - params.GetWindowMinimumSize(gfx::Insets()), - params.GetWindowMaximumSize(gfx::Insets())); - AdjustBoundsToBeVisibleOnScreen(cached_bounds, - cached_screen_bounds, - current_screen_bounds, - constraints.GetMinimumSize(), - ¶ms.window_spec.bounds); - params.state = cached_state; - - // Since we are restoring a cached state, reset the content bounds spec to - // ensure it is not used. - params.content_spec.ResetBounds(); - } - } - - return params; -} - -// static -SkRegion* AppWindow::RawDraggableRegionsToSkRegion( - const std::vector<extensions::DraggableRegion>& regions) { - SkRegion* sk_region = new SkRegion; - for (std::vector<extensions::DraggableRegion>::const_iterator iter = - regions.begin(); - iter != regions.end(); - ++iter) { - const extensions::DraggableRegion& region = *iter; - sk_region->op( - region.bounds.x(), - region.bounds.y(), - region.bounds.right(), - region.bounds.bottom(), - region.draggable ? SkRegion::kUnion_Op : SkRegion::kDifference_Op); - } - return sk_region; -} - -} // namespace apps diff --git a/apps/app_window.h b/apps/app_window.h deleted file mode 100644 index 1379922..0000000 --- a/apps/app_window.h +++ /dev/null @@ -1,560 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef APPS_APP_WINDOW_H_ -#define APPS_APP_WINDOW_H_ - -#include <string> -#include <vector> - -#include "base/memory/scoped_ptr.h" -#include "base/memory/weak_ptr.h" -#include "components/sessions/session_id.h" -#include "components/web_modal/popup_manager.h" -#include "components/web_modal/web_contents_modal_dialog_manager_delegate.h" -#include "content/public/browser/notification_observer.h" -#include "content/public/browser/notification_registrar.h" -#include "content/public/browser/web_contents_delegate.h" -#include "content/public/browser/web_contents_observer.h" -#include "extensions/browser/extension_icon_image.h" -#include "ui/base/ui_base_types.h" // WindowShowState -#include "ui/gfx/image/image.h" -#include "ui/gfx/rect.h" - -class GURL; -class SkRegion; - -namespace base { -class DictionaryValue; -} - -namespace content { -class BrowserContext; -class WebContents; -} - -namespace extensions { -class AppDelegate; -class AppWebContentsHelper; -class Extension; -class NativeAppWindow; -class PlatformAppBrowserTest; -class WindowController; - -struct DraggableRegion; -} - -namespace ui { -class BaseWindow; -} - -namespace apps { - -// Manages the web contents for app windows. The implementation for this -// class should create and maintain the WebContents for the window, and handle -// any message passing between the web contents and the extension system or -// native window. -class AppWindowContents { - public: - AppWindowContents() {} - virtual ~AppWindowContents() {} - - // Called to initialize the WebContents, before the app window is created. - virtual void Initialize(content::BrowserContext* context, - const GURL& url) = 0; - - // Called to load the contents, after the app window is created. - virtual void LoadContents(int32 creator_process_id) = 0; - - // Called when the native window changes. - virtual void NativeWindowChanged( - extensions::NativeAppWindow* native_app_window) = 0; - - // Called when the native window closes. - virtual void NativeWindowClosed() = 0; - - // Called in tests when the window is shown - virtual void DispatchWindowShownForTests() const = 0; - - virtual content::WebContents* GetWebContents() const = 0; - - private: - DISALLOW_COPY_AND_ASSIGN(AppWindowContents); -}; - -// AppWindow is the type of window used by platform apps. App windows -// have a WebContents but none of the chrome of normal browser windows. -class AppWindow : public content::NotificationObserver, - public content::WebContentsDelegate, - public content::WebContentsObserver, - public web_modal::WebContentsModalDialogManagerDelegate, - public extensions::IconImage::Observer { - public: - enum WindowType { - WINDOW_TYPE_DEFAULT = 1 << 0, // Default app window. - WINDOW_TYPE_PANEL = 1 << 1, // OS controlled panel window (Ash only). - WINDOW_TYPE_V1_PANEL = 1 << 2, // For apps v1 support in Ash; deprecate - // with v1 apps. - }; - - enum Frame { - FRAME_CHROME, // Chrome-style window frame. - FRAME_NONE, // Frameless window. - }; - - enum FullscreenType { - // Not fullscreen. - FULLSCREEN_TYPE_NONE = 0, - - // Fullscreen entered by the app.window api. - FULLSCREEN_TYPE_WINDOW_API = 1 << 0, - - // Fullscreen entered by HTML requestFullscreen(). - FULLSCREEN_TYPE_HTML_API = 1 << 1, - - // Fullscreen entered by the OS. ChromeOS uses this type of fullscreen to - // enter immersive fullscreen when the user hits the <F4> key. - FULLSCREEN_TYPE_OS = 1 << 2, - - // Fullscreen mode that could not be exited by the user. ChromeOS uses - // this type of fullscreen to run an app in kiosk mode. - FULLSCREEN_TYPE_FORCED = 1 << 3, - }; - - struct BoundsSpecification { - // INT_MIN represents an unspecified position component. - static const int kUnspecifiedPosition; - - BoundsSpecification(); - ~BoundsSpecification(); - - // INT_MIN designates 'unspecified' for the position components, and 0 - // designates 'unspecified' for the size components. When unspecified, - // they should be replaced with a default value. - gfx::Rect bounds; - - gfx::Size minimum_size; - gfx::Size maximum_size; - - // Reset the bounds fields to their 'unspecified' values. The minimum and - // maximum size constraints remain unchanged. - void ResetBounds(); - }; - - struct CreateParams { - CreateParams(); - ~CreateParams(); - - WindowType window_type; - Frame frame; - - bool has_frame_color; - SkColor active_frame_color; - SkColor inactive_frame_color; - bool alpha_enabled; - - // The initial content/inner bounds specification (excluding any window - // decorations). - BoundsSpecification content_spec; - - // The initial window/outer bounds specification (including window - // decorations). - BoundsSpecification window_spec; - - std::string window_key; - - // The process ID of the process that requested the create. - int32 creator_process_id; - - // Initial state of the window. - ui::WindowShowState state; - - // If true, don't show the window after creation. - bool hidden; - - // If true, the window will be resizable by the user. Defaults to true. - bool resizable; - - // If true, the window will be focused on creation. Defaults to true. - bool focused; - - // If true, the window will stay on top of other windows that are not - // configured to be always on top. Defaults to false. - bool always_on_top; - - // The API enables developers to specify content or window bounds. This - // function combines them into a single, constrained window size. - gfx::Rect GetInitialWindowBounds(const gfx::Insets& frame_insets) const; - - // The API enables developers to specify content or window size constraints. - // These functions combine them so that we can work with one set of - // constraints. - gfx::Size GetContentMinimumSize(const gfx::Insets& frame_insets) const; - gfx::Size GetContentMaximumSize(const gfx::Insets& frame_insets) const; - gfx::Size GetWindowMinimumSize(const gfx::Insets& frame_insets) const; - gfx::Size GetWindowMaximumSize(const gfx::Insets& frame_insets) const; - }; - - // Convert draggable regions in raw format to SkRegion format. Caller is - // responsible for deleting the returned SkRegion instance. - static SkRegion* RawDraggableRegionsToSkRegion( - const std::vector<extensions::DraggableRegion>& regions); - - // The constructor and Init methods are public for constructing a AppWindow - // with a non-standard render interface (e.g. v1 apps using Ash Panels). - // Normally AppWindow::Create should be used. - // Takes ownership of |app_delegate| and |delegate|. - AppWindow(content::BrowserContext* context, - extensions::AppDelegate* app_delegate, - const extensions::Extension* extension); - - // Initializes the render interface, web contents, and native window. - // |app_window_contents| will become owned by AppWindow. - void Init(const GURL& url, - AppWindowContents* app_window_contents, - const CreateParams& params); - - const std::string& window_key() const { return window_key_; } - const SessionID& session_id() const { return session_id_; } - const std::string& extension_id() const { return extension_id_; } - content::WebContents* web_contents() const; - WindowType window_type() const { return window_type_; } - bool window_type_is_panel() const { - return (window_type_ == WINDOW_TYPE_PANEL || - window_type_ == WINDOW_TYPE_V1_PANEL); - } - content::BrowserContext* browser_context() const { return browser_context_; } - const gfx::Image& app_icon() const { return app_icon_; } - const GURL& app_icon_url() const { return app_icon_url_; } - const gfx::Image& badge_icon() const { return badge_icon_; } - const GURL& badge_icon_url() const { return badge_icon_url_; } - bool is_hidden() const { return is_hidden_; } - - const extensions::Extension* GetExtension() const; - extensions::NativeAppWindow* GetBaseWindow(); - gfx::NativeWindow GetNativeWindow(); - - // Returns the bounds that should be reported to the renderer. - gfx::Rect GetClientBounds() const; - - // NativeAppWindows should call this to determine what the window's title - // is on startup and from within UpdateWindowTitle(). - base::string16 GetTitle() const; - - // Call to notify ShellRegistry and delete the window. Subclasses should - // invoke this method instead of using "delete this". - void OnNativeClose(); - - // Should be called by native implementations when the window size, position, - // or minimized/maximized state has changed. - void OnNativeWindowChanged(); - - // Should be called by native implementations when the window is activated. - void OnNativeWindowActivated(); - - // Specifies a url for the launcher icon. - void SetAppIconUrl(const GURL& icon_url); - - // Specifies a url for the window badge. - void SetBadgeIconUrl(const GURL& icon_url); - - // Clear the current badge. - void ClearBadge(); - - // Set the window shape. Passing a NULL |region| sets the default shape. - void UpdateShape(scoped_ptr<SkRegion> region); - - // Called from the render interface to modify the draggable regions. - void UpdateDraggableRegions( - const std::vector<extensions::DraggableRegion>& regions); - - // Updates the app image to |image|. Called internally from the image loader - // callback. Also called externally for v1 apps using Ash Panels. - void UpdateAppIcon(const gfx::Image& image); - - // Enable or disable fullscreen mode. |type| specifies which type of - // fullscreen mode to change (note that disabling one type of fullscreen may - // not exit fullscreen mode because a window may have a different type of - // fullscreen enabled). If |type| is not FORCED, checks that the extension has - // the required permission. - void SetFullscreen(FullscreenType type, bool enable); - - // Returns true if the app window is in a fullscreen state. - bool IsFullscreen() const; - - // Returns true if the app window is in a forced fullscreen state (one that - // cannot be exited by the user). - bool IsForcedFullscreen() const; - - // Returns true if the app window is in a fullscreen state entered from an - // HTML API request. - bool IsHtmlApiFullscreen() const; - - // Transitions window into fullscreen, maximized, minimized or restores based - // on chrome.app.window API. - void Fullscreen(); - void Maximize(); - void Minimize(); - void Restore(); - - // Transitions to OS fullscreen. See FULLSCREEN_TYPE_OS for more details. - void OSFullscreen(); - - // Transitions to forced fullscreen. See FULLSCREEN_TYPE_FORCED for more - // details. - void ForcedFullscreen(); - - // Set the minimum and maximum size of the content bounds. - void SetContentSizeConstraints(const gfx::Size& min_size, - const gfx::Size& max_size); - - enum ShowType { SHOW_ACTIVE, SHOW_INACTIVE }; - - // Shows the window if its contents have been painted; otherwise flags the - // window to be shown as soon as its contents are painted for the first time. - void Show(ShowType show_type); - - // Hides the window. If the window was previously flagged to be shown on - // first paint, it will be unflagged. - void Hide(); - - AppWindowContents* app_window_contents_for_test() { - return app_window_contents_.get(); - } - - int fullscreen_types_for_test() { - return fullscreen_types_; - } - - // Set whether the window should stay above other windows which are not - // configured to be always-on-top. - void SetAlwaysOnTop(bool always_on_top); - - // Whether the always-on-top property has been set by the chrome.app.window - // API. Note that the actual value of this property in the native app window - // may be false if the bit is silently switched off for security reasons. - bool IsAlwaysOnTop() const; - - // Retrieve the current state of the app window as a dictionary, to pass to - // the renderer. - void GetSerializedState(base::DictionaryValue* properties) const; - - // Called by the window API when events can be sent to the window for this - // app. - void WindowEventsReady(); - - // Whether the app window wants to be alpha enabled. - bool requested_alpha_enabled() const { return requested_alpha_enabled_; } - - void SetAppWindowContentsForTesting(scoped_ptr<AppWindowContents> contents) { - app_window_contents_ = contents.Pass(); - } - - protected: - virtual ~AppWindow(); - - private: - // PlatformAppBrowserTest needs access to web_contents() - friend class extensions::PlatformAppBrowserTest; - - // content::WebContentsDelegate implementation. - virtual void CloseContents(content::WebContents* contents) OVERRIDE; - virtual bool ShouldSuppressDialogs() OVERRIDE; - virtual content::ColorChooser* OpenColorChooser( - content::WebContents* web_contents, - SkColor color, - const std::vector<content::ColorSuggestion>& suggestions) OVERRIDE; - virtual void RunFileChooser(content::WebContents* tab, - const content::FileChooserParams& params) - OVERRIDE; - virtual bool IsPopupOrPanel(const content::WebContents* source) - const OVERRIDE; - virtual void MoveContents(content::WebContents* source, - const gfx::Rect& pos) OVERRIDE; - virtual void NavigationStateChanged( - const content::WebContents* source, - content::InvalidateTypes changed_flags) OVERRIDE; - virtual void ToggleFullscreenModeForTab(content::WebContents* source, - bool enter_fullscreen) OVERRIDE; - virtual bool IsFullscreenForTabOrPending(const content::WebContents* source) - const OVERRIDE; - virtual void RequestMediaAccessPermission( - content::WebContents* web_contents, - const content::MediaStreamRequest& request, - const content::MediaResponseCallback& callback) OVERRIDE; - virtual content::WebContents* OpenURLFromTab( - content::WebContents* source, - const content::OpenURLParams& params) OVERRIDE; - virtual void AddNewContents(content::WebContents* source, - content::WebContents* new_contents, - WindowOpenDisposition disposition, - const gfx::Rect& initial_pos, - bool user_gesture, - bool* was_blocked) OVERRIDE; - virtual bool PreHandleKeyboardEvent( - content::WebContents* source, - const content::NativeWebKeyboardEvent& event, - bool* is_keyboard_shortcut) OVERRIDE; - virtual void HandleKeyboardEvent(content::WebContents* source, - const content::NativeWebKeyboardEvent& event) - OVERRIDE; - virtual void RequestToLockMouse(content::WebContents* web_contents, - bool user_gesture, - bool last_unlocked_by_target) OVERRIDE; - virtual bool PreHandleGestureEvent(content::WebContents* source, - const blink::WebGestureEvent& event) - OVERRIDE; - - // content::WebContentsObserver implementation. - virtual void DidFirstVisuallyNonEmptyPaint() OVERRIDE; - - // content::NotificationObserver implementation. - virtual void Observe(int type, - const content::NotificationSource& source, - const content::NotificationDetails& details) OVERRIDE; - - // web_modal::WebContentsModalDialogManagerDelegate implementation. - virtual void SetWebContentsBlocked(content::WebContents* web_contents, - bool blocked) OVERRIDE; - virtual bool IsWebContentsVisible(content::WebContents* web_contents) - OVERRIDE; - - // Saves the window geometry/position/screen bounds. - void SaveWindowPosition(); - - // Helper method to adjust the cached bounds so that we can make sure it can - // be visible on the screen. See http://crbug.com/145752. - void AdjustBoundsToBeVisibleOnScreen(const gfx::Rect& cached_bounds, - const gfx::Rect& cached_screen_bounds, - const gfx::Rect& current_screen_bounds, - const gfx::Size& minimum_size, - gfx::Rect* bounds) const; - - // Loads the appropriate default or cached window bounds. Returns a new - // CreateParams that should be used to create the window. - CreateParams LoadDefaults(CreateParams params) const; - - // Load the app's image, firing a load state change when loaded. - void UpdateExtensionAppIcon(); - - // Set the fullscreen state in the native app window. - void SetNativeWindowFullscreen(); - - // Returns true if there is any overlap between the window and the taskbar - // (Windows only). - bool IntersectsWithTaskbar() const; - - // Update the always-on-top bit in the native app window. - void UpdateNativeAlwaysOnTop(); - - // Sends the onWindowShown event to the app if the window has been shown. Only - // has an effect in tests. - void SendOnWindowShownIfShown(); - - // web_modal::WebContentsModalDialogManagerDelegate implementation. - virtual web_modal::WebContentsModalDialogHost* GetWebContentsModalDialogHost() - OVERRIDE; - - // Updates the badge to |image|. Called internally from the image loader - // callback. - void UpdateBadgeIcon(const gfx::Image& image); - - // Callback from web_contents()->DownloadFavicon. - void DidDownloadFavicon(int id, - int http_status_code, - const GURL& image_url, - const std::vector<SkBitmap>& bitmaps, - const std::vector<gfx::Size>& original_bitmap_sizes); - - // extensions::IconImage::Observer implementation. - virtual void OnExtensionIconImageChanged(extensions::IconImage* image) - OVERRIDE; - - // The browser context with which this window is associated. AppWindow does - // not own this object. - content::BrowserContext* browser_context_; - - const std::string extension_id_; - - // Identifier that is used when saving and restoring geometry for this - // window. - std::string window_key_; - - const SessionID session_id_; - WindowType window_type_; - content::NotificationRegistrar registrar_; - - // Icon shown in the task bar. - gfx::Image app_icon_; - - // Icon URL to be used for setting the app icon. If not empty, app_icon_ will - // be fetched and set using this URL. - GURL app_icon_url_; - - // An object to load the app's icon as an extension resource. - scoped_ptr<extensions::IconImage> app_icon_image_; - - // Badge for icon shown in the task bar. - gfx::Image badge_icon_; - - // URL to be used for setting the badge on the app icon. - GURL badge_icon_url_; - - // An object to load the badge as an extension resource. - scoped_ptr<extensions::IconImage> badge_icon_image_; - - scoped_ptr<extensions::NativeAppWindow> native_app_window_; - scoped_ptr<AppWindowContents> app_window_contents_; - scoped_ptr<extensions::AppDelegate> app_delegate_; - scoped_ptr<extensions::AppWebContentsHelper> helper_; - - // Manages popup windows (bubbles, tab-modals) visible overlapping the - // app window. - scoped_ptr<web_modal::PopupManager> popup_manager_; - - base::WeakPtrFactory<AppWindow> image_loader_ptr_factory_; - - // Bit field of FullscreenType. - int fullscreen_types_; - - // Show has been called, so the window should be shown once the first visually - // non-empty paint occurs. - bool show_on_first_paint_; - - // The first visually non-empty paint has completed. - bool first_paint_complete_; - - // Whether the window has been shown or not. - bool has_been_shown_; - - // Whether events can be sent to the window. - bool can_send_events_; - - // Whether the window is hidden or not. Hidden in this context means actively - // by the chrome.app.window API, not in an operating system context. For - // example windows which are minimized are not hidden, and windows which are - // part of a hidden app on OS X are not hidden. Windows which were created - // with the |hidden| flag set to true, or which have been programmatically - // hidden, are considered hidden. - bool is_hidden_; - - // Whether the delayed Show() call was for an active or inactive window. - ShowType delayed_show_type_; - - // Cache the desired value of the always-on-top property. When windows enter - // fullscreen or overlap the Windows taskbar, this property will be - // automatically and silently switched off for security reasons. It is - // reinstated when the window exits fullscreen and moves away from the - // taskbar. - bool cached_always_on_top_; - - // Whether |alpha_enabled| was set in the CreateParams. - bool requested_alpha_enabled_; - - DISALLOW_COPY_AND_ASSIGN(AppWindow); -}; - -} // namespace apps - -#endif // APPS_APP_WINDOW_H_ diff --git a/apps/app_window_browsertest.cc b/apps/app_window_browsertest.cc deleted file mode 100644 index a132a1b..0000000 --- a/apps/app_window_browsertest.cc +++ /dev/null @@ -1,82 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/apps/app_browsertest_util.h" -#include "extensions/browser/app_window/native_app_window.h" - -using extensions::Extension; -using extensions::NativeAppWindow; - -namespace apps { - -namespace { - -typedef extensions::PlatformAppBrowserTest AppWindowBrowserTest; - -// This test is disabled on Linux because of the unpredictable nature of native -// windows. We cannot assume that the window manager will insert any title bar -// at all, so the test may fail on certain window managers. -#if defined(OS_LINUX) && !defined(OS_CHROMEOS) -#define MAYBE_FrameInsetsForDefaultFrame DISABLED_FrameInsetsForDefaultFrame -#else -#define MAYBE_FrameInsetsForDefaultFrame FrameInsetsForDefaultFrame -#endif - -// Verifies that the NativeAppWindows implement GetFrameInsets() correctly. -// See http://crbug.com/346115 -IN_PROC_BROWSER_TEST_F(AppWindowBrowserTest, MAYBE_FrameInsetsForDefaultFrame) { - AppWindow* app_window = CreateTestAppWindow("{}"); - NativeAppWindow* native_window = app_window->GetBaseWindow(); - gfx::Insets insets = native_window->GetFrameInsets(); - - // It is a reasonable assumption that the top padding must be greater than - // the bottom padding due to the title bar. - EXPECT_GT(insets.top(), insets.bottom()); - - CloseAppWindow(app_window); -} - -// This test is also disabled on Linux because frame: color is ignored on stable -// and beta channels (so it can fail the same as the previous test). -// TODO(benwells): Re-enable on Linux after frame: color is on stable. -#if defined(OS_LINUX) && !defined(OS_CHROMEOS) -#define MAYBE_FrameInsetsForColoredFrame DISABLED_FrameInsetsForColoredFrame -#else -#define MAYBE_FrameInsetsForColoredFrame FrameInsetsForColoredFrame -#endif - -// Verifies that the NativeAppWindows implement GetFrameInsets() correctly. -// See http://crbug.com/346115 -IN_PROC_BROWSER_TEST_F(AppWindowBrowserTest, MAYBE_FrameInsetsForColoredFrame) { - AppWindow* app_window = - CreateTestAppWindow("{ \"frame\": { \"color\": \"#ffffff\" } }"); - NativeAppWindow* native_window = app_window->GetBaseWindow(); - gfx::Insets insets = native_window->GetFrameInsets(); - - // It is a reasonable assumption that the top padding must be greater than - // the bottom padding due to the title bar. - EXPECT_GT(insets.top(), insets.bottom()); - - CloseAppWindow(app_window); -} - -// Verifies that the NativeAppWindows implement GetFrameInsets() correctly for -// frameless windows. -IN_PROC_BROWSER_TEST_F(AppWindowBrowserTest, FrameInsetsForNoFrame) { - AppWindow* app_window = CreateTestAppWindow("{ \"frame\": \"none\" }"); - NativeAppWindow* native_window = app_window->GetBaseWindow(); - gfx::Insets insets = native_window->GetFrameInsets(); - - // All insets must be zero. - EXPECT_EQ(0, insets.top()); - EXPECT_EQ(0, insets.bottom()); - EXPECT_EQ(0, insets.left()); - EXPECT_EQ(0, insets.right()); - - CloseAppWindow(app_window); -} - -} // namespace - -} // namespace apps diff --git a/apps/app_window_contents.cc b/apps/app_window_contents.cc index 783564a..8461e24 100644 --- a/apps/app_window_contents.cc +++ b/apps/app_window_contents.cc @@ -21,6 +21,7 @@ #include "extensions/common/extension_messages.h" namespace app_window = extensions::api::app_window; +using extensions::AppWindow; namespace apps { diff --git a/apps/app_window_contents.h b/apps/app_window_contents.h index 8762913..fb3534e 100644 --- a/apps/app_window_contents.h +++ b/apps/app_window_contents.h @@ -5,11 +5,11 @@ #ifndef APPS_APP_WINDOW_CONTENTS_H_ #define APPS_APP_WINDOW_CONTENTS_H_ -#include "apps/app_window.h" #include "base/basictypes.h" #include "base/memory/scoped_ptr.h" #include "content/public/browser/notification_observer.h" #include "content/public/browser/web_contents_observer.h" +#include "extensions/browser/app_window/app_window.h" #include "extensions/browser/extension_function_dispatcher.h" #include "url/gurl.h" @@ -27,11 +27,11 @@ namespace apps { // WebContents instance and observes it for the purpose of passing // messages to the extensions system. class AppWindowContentsImpl - : public AppWindowContents, + : public extensions::AppWindowContents, public content::WebContentsObserver, public extensions::ExtensionFunctionDispatcher::Delegate { public: - explicit AppWindowContentsImpl(AppWindow* host); + explicit AppWindowContentsImpl(extensions::AppWindow* host); virtual ~AppWindowContentsImpl(); // AppWindowContents @@ -58,7 +58,7 @@ class AppWindowContentsImpl const std::vector<extensions::DraggableRegion>& regions); void SuspendRenderViewHost(content::RenderViewHost* rvh); - AppWindow* host_; // This class is owned by |host_| + extensions::AppWindow* host_; // This class is owned by |host_| GURL url_; scoped_ptr<content::WebContents> web_contents_; scoped_ptr<extensions::ExtensionFunctionDispatcher> diff --git a/apps/app_window_interactive_uitest.cc b/apps/app_window_interactive_uitest.cc deleted file mode 100644 index 665d4656..0000000 --- a/apps/app_window_interactive_uitest.cc +++ /dev/null @@ -1,190 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/apps/app_browsertest_util.h" -#include "extensions/browser/app_window/native_app_window.h" - -using extensions::PlatformAppBrowserTest; -using extensions::Extension; - -namespace apps { - -namespace { - -class AppWindowTest : public PlatformAppBrowserTest { - protected: - void CheckAlwaysOnTopToFullscreen(AppWindow* window) { - ASSERT_TRUE(window->GetBaseWindow()->IsAlwaysOnTop()); - - // The always-on-top property should be temporarily disabled when the window - // enters fullscreen. - window->Fullscreen(); - EXPECT_FALSE(window->GetBaseWindow()->IsAlwaysOnTop()); - - // From the API's point of view, the always-on-top property is enabled. - EXPECT_TRUE(window->IsAlwaysOnTop()); - - // The always-on-top property is restored when the window exits fullscreen. - window->Restore(); - EXPECT_TRUE(window->GetBaseWindow()->IsAlwaysOnTop()); - } - - void CheckNormalToFullscreen(AppWindow* window) { - // If the always-on-top property is false, it should remain this way when - // entering and exiting fullscreen mode. - ASSERT_FALSE(window->GetBaseWindow()->IsAlwaysOnTop()); - window->Fullscreen(); - EXPECT_FALSE(window->GetBaseWindow()->IsAlwaysOnTop()); - window->Restore(); - EXPECT_FALSE(window->GetBaseWindow()->IsAlwaysOnTop()); - } - - void CheckFullscreenToAlwaysOnTop(AppWindow* window) { - ASSERT_TRUE(window->GetBaseWindow()->IsFullscreenOrPending()); - - // Now enable always-on-top at runtime and ensure the property does not get - // applied immediately because the window is in fullscreen mode. - window->SetAlwaysOnTop(true); - EXPECT_FALSE(window->GetBaseWindow()->IsAlwaysOnTop()); - - // From the API's point of view, the always-on-top property is enabled. - EXPECT_TRUE(window->IsAlwaysOnTop()); - - // Ensure the always-on-top property is applied when exiting fullscreen. - window->Restore(); - EXPECT_TRUE(window->GetBaseWindow()->IsAlwaysOnTop()); - } -}; - -} // namespace - -// Tests are flaky on Mac as transitioning to fullscreen is not instantaneous -// and throws errors when entering/exiting fullscreen too quickly. -#if defined(OS_MACOSX) -#define MAYBE_InitAlwaysOnTopToFullscreen DISABLED_InitAlwaysOnTopToFullscreen -#else -#define MAYBE_InitAlwaysOnTopToFullscreen InitAlwaysOnTopToFullscreen -#endif - -// Tests a window created with always-on-top enabled and ensures that the -// property is temporarily switched off when entering fullscreen mode. -IN_PROC_BROWSER_TEST_F(AppWindowTest, MAYBE_InitAlwaysOnTopToFullscreen) { - AppWindow* window = CreateTestAppWindow("{ \"alwaysOnTop\": true }"); - ASSERT_TRUE(window); - CheckAlwaysOnTopToFullscreen(window); - - window->SetAlwaysOnTop(false); - CheckNormalToFullscreen(window); - - CloseAppWindow(window); -} - -#if defined(OS_MACOSX) -#define MAYBE_RuntimeAlwaysOnTopToFullscreen \ - DISABLED_RuntimeAlwaysOnTopToFullscreen -#else -#define MAYBE_RuntimeAlwaysOnTopToFullscreen RuntimeAlwaysOnTopToFullscreen -#endif - -// Tests a window with always-on-top enabled at runtime and ensures that the -// property is temporarily switched off when entering fullscreen mode. -IN_PROC_BROWSER_TEST_F(AppWindowTest, MAYBE_RuntimeAlwaysOnTopToFullscreen) { - AppWindow* window = CreateTestAppWindow("{}"); - ASSERT_TRUE(window); - CheckNormalToFullscreen(window); - - window->SetAlwaysOnTop(true); - CheckAlwaysOnTopToFullscreen(window); - - CloseAppWindow(window); -} - -#if defined(OS_MACOSX) -#define MAYBE_InitFullscreenToAlwaysOnTop DISABLED_InitFullscreenToAlwaysOnTop -#else -#define MAYBE_InitFullscreenToAlwaysOnTop InitFullscreenToAlwaysOnTop -#endif - -// Tests a window created initially in fullscreen mode and ensures that the -// always-on-top property does not get applied until it exits fullscreen. -IN_PROC_BROWSER_TEST_F(AppWindowTest, MAYBE_InitFullscreenToAlwaysOnTop) { - AppWindow* window = CreateTestAppWindow("{ \"state\": \"fullscreen\" }"); - ASSERT_TRUE(window); - CheckFullscreenToAlwaysOnTop(window); - - CloseAppWindow(window); -} - -#if defined(OS_MACOSX) -#define MAYBE_RuntimeFullscreenToAlwaysOnTop \ - DISABLED_RuntimeFullscreenToAlwaysOnTop -#else -#define MAYBE_RuntimeFullscreenToAlwaysOnTop RuntimeFullscreenToAlwaysOnTop -#endif - -// Tests a window that enters fullscreen mode at runtime and ensures that the -// always-on-top property does not get applied until it exits fullscreen. -IN_PROC_BROWSER_TEST_F(AppWindowTest, MAYBE_RuntimeFullscreenToAlwaysOnTop) { - AppWindow* window = CreateTestAppWindow("{}"); - ASSERT_TRUE(window); - - window->Fullscreen(); - CheckFullscreenToAlwaysOnTop(window); - - CloseAppWindow(window); -} - -#if defined(OS_MACOSX) -#define MAYBE_InitFullscreenAndAlwaysOnTop DISABLED_InitFullscreenAndAlwaysOnTop -#else -#define MAYBE_InitFullscreenAndAlwaysOnTop InitFullscreenAndAlwaysOnTop -#endif - -// Tests a window created with both fullscreen and always-on-top enabled. Ensure -// that always-on-top is only applied when the window exits fullscreen. -IN_PROC_BROWSER_TEST_F(AppWindowTest, MAYBE_InitFullscreenAndAlwaysOnTop) { - AppWindow* window = CreateTestAppWindow( - "{ \"alwaysOnTop\": true, \"state\": \"fullscreen\" }"); - ASSERT_TRUE(window); - - EXPECT_TRUE(window->GetBaseWindow()->IsFullscreenOrPending()); - EXPECT_FALSE(window->GetBaseWindow()->IsAlwaysOnTop()); - - // From the API's point of view, the always-on-top property is enabled. - EXPECT_TRUE(window->IsAlwaysOnTop()); - - window->Restore(); - EXPECT_TRUE(window->GetBaseWindow()->IsAlwaysOnTop()); - - CloseAppWindow(window); -} - -#if defined(OS_MACOSX) -#define MAYBE_DisableAlwaysOnTopInFullscreen \ - DISABLED_DisableAlwaysOnTopInFullscreen -#else -#define MAYBE_DisableAlwaysOnTopInFullscreen DisableAlwaysOnTopInFullscreen -#endif - -// Tests a window created with always-on-top enabled, but then disabled while -// in fullscreen mode. After exiting fullscreen, always-on-top should remain -// disabled. -IN_PROC_BROWSER_TEST_F(AppWindowTest, MAYBE_DisableAlwaysOnTopInFullscreen) { - AppWindow* window = CreateTestAppWindow("{ \"alwaysOnTop\": true }"); - ASSERT_TRUE(window); - - // Disable always-on-top while in fullscreen mode. - window->Fullscreen(); - EXPECT_FALSE(window->GetBaseWindow()->IsAlwaysOnTop()); - window->SetAlwaysOnTop(false); - EXPECT_FALSE(window->GetBaseWindow()->IsAlwaysOnTop()); - - // Ensure that always-on-top remains disabled. - window->Restore(); - EXPECT_FALSE(window->GetBaseWindow()->IsAlwaysOnTop()); - - CloseAppWindow(window); -} - -} // namespace apps diff --git a/apps/app_window_registry.cc b/apps/app_window_registry.cc deleted file mode 100644 index 0874a5e..0000000 --- a/apps/app_window_registry.cc +++ /dev/null @@ -1,353 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "apps/app_window_registry.h" - -#include <string> -#include <vector> - -#include "apps/app_window.h" -#include "apps/ui/apps_client.h" -#include "components/keyed_service/content/browser_context_dependency_manager.h" -#include "content/public/browser/browser_context.h" -#include "content/public/browser/devtools_agent_host.h" -#include "content/public/browser/render_process_host.h" -#include "content/public/browser/render_view_host.h" -#include "content/public/browser/site_instance.h" -#include "content/public/browser/web_contents.h" -#include "extensions/browser/app_window/native_app_window.h" -#include "extensions/browser/extensions_browser_client.h" -#include "extensions/common/extension.h" - -namespace { - -// Create a key that identifies a AppWindow in a RenderViewHost across App -// reloads. If the window was given an id in CreateParams, the key is the -// extension id, a colon separator, and the AppWindow's |id|. If there is no -// |id|, the chrome-extension://extension-id/page.html URL will be used. If the -// RenderViewHost is not for a AppWindow, return an empty string. -std::string GetWindowKeyForRenderViewHost( - const apps::AppWindowRegistry* registry, - content::RenderViewHost* render_view_host) { - apps::AppWindow* app_window = - registry->GetAppWindowForRenderViewHost(render_view_host); - if (!app_window) - return std::string(); // Not a AppWindow. - - if (app_window->window_key().empty()) - return app_window->web_contents()->GetURL().possibly_invalid_spec(); - - std::string key = app_window->extension_id(); - key += ':'; - key += app_window->window_key(); - return key; -} - -} // namespace - -namespace apps { - -void AppWindowRegistry::Observer::OnAppWindowAdded(AppWindow* app_window) { -} - -void AppWindowRegistry::Observer::OnAppWindowIconChanged( - AppWindow* app_window) { -} - -void AppWindowRegistry::Observer::OnAppWindowRemoved(AppWindow* app_window) { -} - -void AppWindowRegistry::Observer::OnAppWindowHidden(AppWindow* app_window) { -} - -void AppWindowRegistry::Observer::OnAppWindowShown(AppWindow* app_window) { -} - -AppWindowRegistry::Observer::~Observer() { -} - -AppWindowRegistry::AppWindowRegistry(content::BrowserContext* context) - : context_(context), - devtools_callback_(base::Bind(&AppWindowRegistry::OnDevToolsStateChanged, - base::Unretained(this))) { - content::DevToolsAgentHost::AddAgentStateCallback(devtools_callback_); -} - -AppWindowRegistry::~AppWindowRegistry() { - content::DevToolsAgentHost::RemoveAgentStateCallback(devtools_callback_); -} - -// static -AppWindowRegistry* AppWindowRegistry::Get(content::BrowserContext* context) { - return Factory::GetForBrowserContext(context, true /* create */); -} - -void AppWindowRegistry::AddAppWindow(AppWindow* app_window) { - BringToFront(app_window); - FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowAdded(app_window)); -} - -void AppWindowRegistry::AppWindowIconChanged(AppWindow* app_window) { - AddAppWindowToList(app_window); - FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowIconChanged(app_window)); -} - -void AppWindowRegistry::AppWindowActivated(AppWindow* app_window) { - BringToFront(app_window); -} - -void AppWindowRegistry::AppWindowHidden(AppWindow* app_window) { - FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowHidden(app_window)); -} - -void AppWindowRegistry::AppWindowShown(AppWindow* app_window) { - FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowShown(app_window)); -} - -void AppWindowRegistry::RemoveAppWindow(AppWindow* app_window) { - const AppWindowList::iterator it = - std::find(app_windows_.begin(), app_windows_.end(), app_window); - if (it != app_windows_.end()) - app_windows_.erase(it); - FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowRemoved(app_window)); -} - -void AppWindowRegistry::AddObserver(Observer* observer) { - observers_.AddObserver(observer); -} - -void AppWindowRegistry::RemoveObserver(Observer* observer) { - observers_.RemoveObserver(observer); -} - -AppWindowRegistry::AppWindowList AppWindowRegistry::GetAppWindowsForApp( - const std::string& app_id) const { - AppWindowList app_windows; - for (AppWindowList::const_iterator i = app_windows_.begin(); - i != app_windows_.end(); - ++i) { - if ((*i)->extension_id() == app_id) - app_windows.push_back(*i); - } - return app_windows; -} - -void AppWindowRegistry::CloseAllAppWindowsForApp(const std::string& app_id) { - const AppWindowList windows = GetAppWindowsForApp(app_id); - for (AppWindowRegistry::const_iterator it = windows.begin(); - it != windows.end(); - ++it) { - (*it)->GetBaseWindow()->Close(); - } -} - -AppWindow* AppWindowRegistry::GetAppWindowForRenderViewHost( - content::RenderViewHost* render_view_host) const { - for (AppWindowList::const_iterator i = app_windows_.begin(); - i != app_windows_.end(); - ++i) { - if ((*i)->web_contents()->GetRenderViewHost() == render_view_host) - return *i; - } - - return NULL; -} - -AppWindow* AppWindowRegistry::GetAppWindowForNativeWindow( - gfx::NativeWindow window) const { - for (AppWindowList::const_iterator i = app_windows_.begin(); - i != app_windows_.end(); - ++i) { - if ((*i)->GetNativeWindow() == window) - return *i; - } - - return NULL; -} - -AppWindow* AppWindowRegistry::GetCurrentAppWindowForApp( - const std::string& app_id) const { - AppWindow* result = NULL; - for (AppWindowList::const_iterator i = app_windows_.begin(); - i != app_windows_.end(); - ++i) { - if ((*i)->extension_id() == app_id) { - result = *i; - if (result->GetBaseWindow()->IsActive()) - return result; - } - } - - return result; -} - -AppWindow* AppWindowRegistry::GetAppWindowForAppAndKey( - const std::string& app_id, - const std::string& window_key) const { - AppWindow* result = NULL; - for (AppWindowList::const_iterator i = app_windows_.begin(); - i != app_windows_.end(); - ++i) { - if ((*i)->extension_id() == app_id && (*i)->window_key() == window_key) { - result = *i; - if (result->GetBaseWindow()->IsActive()) - return result; - } - } - return result; -} - -bool AppWindowRegistry::HadDevToolsAttached( - content::RenderViewHost* render_view_host) const { - std::string key = GetWindowKeyForRenderViewHost(this, render_view_host); - return key.empty() ? false : inspected_windows_.count(key) != 0; -} - -// static -AppWindow* AppWindowRegistry::GetAppWindowForNativeWindowAnyProfile( - gfx::NativeWindow window) { - std::vector<content::BrowserContext*> contexts = - AppsClient::Get()->GetLoadedBrowserContexts(); - for (std::vector<content::BrowserContext*>::const_iterator i = - contexts.begin(); - i != contexts.end(); - ++i) { - AppWindowRegistry* registry = - Factory::GetForBrowserContext(*i, false /* create */); - if (!registry) - continue; - - AppWindow* app_window = registry->GetAppWindowForNativeWindow(window); - if (app_window) - return app_window; - } - - return NULL; -} - -// static -bool AppWindowRegistry::IsAppWindowRegisteredInAnyProfile( - int window_type_mask) { - std::vector<content::BrowserContext*> contexts = - AppsClient::Get()->GetLoadedBrowserContexts(); - for (std::vector<content::BrowserContext*>::const_iterator i = - contexts.begin(); - i != contexts.end(); - ++i) { - AppWindowRegistry* registry = - Factory::GetForBrowserContext(*i, false /* create */); - if (!registry) - continue; - - const AppWindowList& app_windows = registry->app_windows(); - if (app_windows.empty()) - continue; - - if (window_type_mask == 0) - return true; - - for (const_iterator j = app_windows.begin(); j != app_windows.end(); ++j) { - if ((*j)->window_type() & window_type_mask) - return true; - } - } - - return false; -} - -// static -void AppWindowRegistry::CloseAllAppWindows() { - std::vector<content::BrowserContext*> contexts = - AppsClient::Get()->GetLoadedBrowserContexts(); - for (std::vector<content::BrowserContext*>::const_iterator i = - contexts.begin(); - i != contexts.end(); - ++i) { - AppWindowRegistry* registry = - Factory::GetForBrowserContext(*i, false /* create */); - if (!registry) - continue; - - while (!registry->app_windows().empty()) - registry->app_windows().front()->GetBaseWindow()->Close(); - } -} - -void AppWindowRegistry::OnDevToolsStateChanged( - content::DevToolsAgentHost* agent_host, - bool attached) { - content::WebContents* web_contents = agent_host->GetWebContents(); - // Ignore unrelated notifications. - if (!web_contents || web_contents->GetBrowserContext() != context_) - return; - - std::string key = - GetWindowKeyForRenderViewHost(this, web_contents->GetRenderViewHost()); - if (key.empty()) - return; - - if (attached) - inspected_windows_.insert(key); - else - inspected_windows_.erase(key); -} - -void AppWindowRegistry::AddAppWindowToList(AppWindow* app_window) { - const AppWindowList::iterator it = - std::find(app_windows_.begin(), app_windows_.end(), app_window); - if (it != app_windows_.end()) - return; - app_windows_.push_back(app_window); -} - -void AppWindowRegistry::BringToFront(AppWindow* app_window) { - const AppWindowList::iterator it = - std::find(app_windows_.begin(), app_windows_.end(), app_window); - if (it != app_windows_.end()) - app_windows_.erase(it); - app_windows_.push_front(app_window); -} - -/////////////////////////////////////////////////////////////////////////////// -// Factory boilerplate - -// static -AppWindowRegistry* AppWindowRegistry::Factory::GetForBrowserContext( - content::BrowserContext* context, - bool create) { - return static_cast<AppWindowRegistry*>( - GetInstance()->GetServiceForBrowserContext(context, create)); -} - -AppWindowRegistry::Factory* AppWindowRegistry::Factory::GetInstance() { - return Singleton<AppWindowRegistry::Factory>::get(); -} - -AppWindowRegistry::Factory::Factory() - : BrowserContextKeyedServiceFactory( - "AppWindowRegistry", - BrowserContextDependencyManager::GetInstance()) {} - -AppWindowRegistry::Factory::~Factory() {} - -KeyedService* AppWindowRegistry::Factory::BuildServiceInstanceFor( - content::BrowserContext* context) const { - return new AppWindowRegistry(context); -} - -bool AppWindowRegistry::Factory::ServiceIsCreatedWithBrowserContext() const { - return true; -} - -bool AppWindowRegistry::Factory::ServiceIsNULLWhileTesting() const { - return false; -} - -content::BrowserContext* AppWindowRegistry::Factory::GetBrowserContextToUse( - content::BrowserContext* context) const { - return extensions::ExtensionsBrowserClient::Get()->GetOriginalContext( - context); -} - -} // namespace apps diff --git a/apps/app_window_registry.h b/apps/app_window_registry.h deleted file mode 100644 index 8320b7f..0000000 --- a/apps/app_window_registry.h +++ /dev/null @@ -1,160 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef APPS_APP_WINDOW_REGISTRY_H_ -#define APPS_APP_WINDOW_REGISTRY_H_ - -#include <list> -#include <string> -#include <set> - -#include "base/callback.h" -#include "base/compiler_specific.h" -#include "base/memory/singleton.h" -#include "base/observer_list.h" -#include "components/keyed_service/content/browser_context_keyed_service_factory.h" -#include "components/keyed_service/core/keyed_service.h" -#include "ui/gfx/native_widget_types.h" - -namespace content { -class BrowserContext; -class DevToolsAgentHost; -class RenderViewHost; -} - -namespace apps { - -class AppWindow; - -// The AppWindowRegistry tracks the AppWindows for all platform apps for a -// particular browser context. -class AppWindowRegistry : public KeyedService { - public: - class Observer { - public: - // Called just after a app window was added. - virtual void OnAppWindowAdded(apps::AppWindow* app_window); - // Called when the window icon changes. - virtual void OnAppWindowIconChanged(apps::AppWindow* app_window); - // Called just after a app window was removed. - virtual void OnAppWindowRemoved(apps::AppWindow* app_window); - // Called just after a app window was hidden. This is different from - // window visibility as a minimize does not hide a window, but does make - // it not visible. - virtual void OnAppWindowHidden(apps::AppWindow* app_window); - // Called just after a app window was shown. - virtual void OnAppWindowShown(apps::AppWindow* app_window); - - protected: - virtual ~Observer(); - }; - - typedef std::list<apps::AppWindow*> AppWindowList; - typedef AppWindowList::const_iterator const_iterator; - typedef std::set<std::string> InspectedWindowSet; - - explicit AppWindowRegistry(content::BrowserContext* context); - virtual ~AppWindowRegistry(); - - // Returns the instance for the given browser context, or NULL if none. This - // is a convenience wrapper around - // AppWindowRegistry::Factory::GetForBrowserContext(). - static AppWindowRegistry* Get(content::BrowserContext* context); - - void AddAppWindow(apps::AppWindow* app_window); - void AppWindowIconChanged(apps::AppWindow* app_window); - // Called by |app_window| when it is activated. - void AppWindowActivated(apps::AppWindow* app_window); - void AppWindowHidden(apps::AppWindow* app_window); - void AppWindowShown(apps::AppWindow* app_window); - void RemoveAppWindow(apps::AppWindow* app_window); - - void AddObserver(Observer* observer); - void RemoveObserver(Observer* observer); - - // Returns a set of windows owned by the application identified by app_id. - AppWindowList GetAppWindowsForApp(const std::string& app_id) const; - const AppWindowList& app_windows() const { return app_windows_; } - - // Close all app windows associated with an app. - void CloseAllAppWindowsForApp(const std::string& app_id); - - // Helper functions to find app windows with particular attributes. - apps::AppWindow* GetAppWindowForRenderViewHost( - content::RenderViewHost* render_view_host) const; - apps::AppWindow* GetAppWindowForNativeWindow(gfx::NativeWindow window) const; - // Returns an app window for the given app, or NULL if no app windows are - // open. If there is a window for the given app that is active, that one will - // be returned, otherwise an arbitrary window will be returned. - apps::AppWindow* GetCurrentAppWindowForApp(const std::string& app_id) const; - // Returns an app window for the given app and window key, or NULL if no app - // window with the key are open. If there is a window for the given app and - // key that is active, that one will be returned, otherwise an arbitrary - // window will be returned. - apps::AppWindow* GetAppWindowForAppAndKey(const std::string& app_id, - const std::string& window_key) - const; - - // Returns whether a AppWindow's ID was last known to have a DevToolsAgent - // attached to it, which should be restored during a reload of a corresponding - // newly created |render_view_host|. - bool HadDevToolsAttached(content::RenderViewHost* render_view_host) const; - - // Returns the app window for |window|, looking in all browser contexts. - static apps::AppWindow* GetAppWindowForNativeWindowAnyProfile( - gfx::NativeWindow window); - - // Returns true if the number of app windows registered across all browser - // contexts is non-zero. |window_type_mask| is a bitwise OR filter of - // AppWindow::WindowType, or 0 for any window type. - static bool IsAppWindowRegisteredInAnyProfile(int window_type_mask); - - // Close all app windows in all profiles. - static void CloseAllAppWindows(); - - class Factory : public BrowserContextKeyedServiceFactory { - public: - static AppWindowRegistry* GetForBrowserContext( - content::BrowserContext* context, - bool create); - - static Factory* GetInstance(); - - private: - friend struct DefaultSingletonTraits<Factory>; - - Factory(); - virtual ~Factory(); - - // BrowserContextKeyedServiceFactory - virtual KeyedService* BuildServiceInstanceFor( - content::BrowserContext* context) const OVERRIDE; - virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE; - virtual bool ServiceIsNULLWhileTesting() const OVERRIDE; - virtual content::BrowserContext* GetBrowserContextToUse( - content::BrowserContext* context) const OVERRIDE; - }; - - protected: - void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached); - - private: - // Ensures the specified |app_window| is included in |app_windows_|. - // Otherwise adds |app_window| to the back of |app_windows_|. - void AddAppWindowToList(apps::AppWindow* app_window); - - // Bring |app_window| to the front of |app_windows_|. If it is not in the - // list, add it first. - void BringToFront(apps::AppWindow* app_window); - - content::BrowserContext* context_; - AppWindowList app_windows_; - InspectedWindowSet inspected_windows_; - ObserverList<Observer> observers_; - base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_; -}; - -} // namespace apps - -#endif // APPS_APP_WINDOW_REGISTRY_H_ diff --git a/apps/apps.gypi b/apps/apps.gypi index 724799d..ddf0c99 100644 --- a/apps/apps.gypi +++ b/apps/apps.gypi @@ -38,12 +38,8 @@ 'app_restore_service.h', 'app_restore_service_factory.cc', 'app_restore_service_factory.h', - 'app_window.cc', - 'app_window.h', 'app_window_contents.cc', 'app_window_contents.h', - 'app_window_registry.cc', - 'app_window_registry.h', 'browser_context_keyed_service_factories.cc', 'browser_context_keyed_service_factories.h', 'custom_launcher_page_contents.cc', @@ -57,8 +53,6 @@ 'saved_files_service_factory.h', 'switches.cc', 'switches.h', - 'ui/apps_client.cc', - 'ui/apps_client.h', 'ui/views/app_window_frame_view.cc', 'ui/views/app_window_frame_view.h', 'ui/views/native_app_window_views.cc', diff --git a/apps/ui/apps_client.cc b/apps/ui/apps_client.cc deleted file mode 100644 index d62e55d..0000000 --- a/apps/ui/apps_client.cc +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "apps/ui/apps_client.h" - -#include "base/basictypes.h" - -namespace apps { - -namespace { - -AppsClient* g_client = NULL; - -} // namespace - -AppsClient* AppsClient::Get() { - return g_client; -} - -void AppsClient::Set(AppsClient* client) { - // This can happen in unit tests, where the utility thread runs in-process. - if (g_client) - return; - - g_client = client; -} - -} // namespace apps diff --git a/apps/ui/apps_client.h b/apps/ui/apps_client.h deleted file mode 100644 index f601e67..0000000 --- a/apps/ui/apps_client.h +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef APPS_UI_APPS_CLIENT_H_ -#define APPS_UI_APPS_CLIENT_H_ - -#include <vector> - -#include "apps/app_window.h" -#include "base/callback_forward.h" - -namespace content { -class BrowserContext; -class WebContents; -} - -namespace extensions { -class Extension; -class NativeAppWindow; -} - -namespace apps { - -// Sets up global state for the apps system. Should be Set() once in each -// process. This should be implemented by the client of the apps system. -class AppsClient { - public: - // Get all loaded browser contexts. - virtual std::vector<content::BrowserContext*> GetLoadedBrowserContexts() = 0; - - // Creates a new apps::AppWindow for the app in |extension| for |context|. - // Caller takes ownership. - virtual AppWindow* CreateAppWindow( - content::BrowserContext* context, - const extensions::Extension* extension) = 0; - - // Creates a new extensions::NativeAppWindow for |window|. - virtual extensions::NativeAppWindow* CreateNativeAppWindow( - AppWindow* window, - const AppWindow::CreateParams& params) = 0; - - // A positive keep-alive count is a request for the embedding application to - // keep running after all windows are closed. The count starts at zero. - virtual void IncrementKeepAliveCount() = 0; - virtual void DecrementKeepAliveCount() = 0; - - // Opens DevTools window and runs the callback. - virtual void OpenDevToolsWindow(content::WebContents* web_contents, - const base::Closure& callback) = 0; - - // Returns true if the current channel is older than dev. - virtual bool IsCurrentChannelOlderThanDev() = 0; - - // Return the apps client. - static AppsClient* Get(); - - // Initialize the apps system with this apps client. - static void Set(AppsClient* client); -}; - -} // namespace apps - -#endif // APPS_UI_APPS_CLIENT_H_ diff --git a/apps/ui/views/native_app_window_views.cc b/apps/ui/views/native_app_window_views.cc index d3c478b..4579c73 100644 --- a/apps/ui/views/native_app_window_views.cc +++ b/apps/ui/views/native_app_window_views.cc @@ -4,11 +4,11 @@ #include "apps/ui/views/native_app_window_views.h" -#include "apps/app_window.h" #include "base/threading/sequenced_worker_pool.h" #include "content/public/browser/render_view_host.h" #include "content/public/browser/render_widget_host_view.h" #include "content/public/browser/web_contents.h" +#include "extensions/browser/app_window/app_window.h" #include "extensions/common/draggable_region.h" #include "third_party/skia/include/core/SkRegion.h" #include "ui/gfx/path.h" @@ -20,6 +20,8 @@ #include "ui/aura/window.h" #endif +using extensions::AppWindow; + namespace apps { NativeAppWindowViews::NativeAppWindowViews() diff --git a/apps/ui/views/native_app_window_views.h b/apps/ui/views/native_app_window_views.h index 94bc960..9988b17 100644 --- a/apps/ui/views/native_app_window_views.h +++ b/apps/ui/views/native_app_window_views.h @@ -5,9 +5,9 @@ #ifndef APPS_UI_VIEWS_NATIVE_APP_WINDOW_VIEWS_H_ #define APPS_UI_VIEWS_NATIVE_APP_WINDOW_VIEWS_H_ -#include "apps/app_window.h" #include "base/observer_list.h" #include "content/public/browser/web_contents_observer.h" +#include "extensions/browser/app_window/app_window.h" #include "extensions/browser/app_window/native_app_window.h" #include "extensions/browser/app_window/size_constraints.h" #include "ui/gfx/rect.h" @@ -50,8 +50,8 @@ class NativeAppWindowViews : public extensions::NativeAppWindow, public: NativeAppWindowViews(); virtual ~NativeAppWindowViews(); - void Init(AppWindow* app_window, - const AppWindow::CreateParams& create_params); + void Init(extensions::AppWindow* app_window, + const extensions::AppWindow::CreateParams& create_params); // Signal that CanHaveTransparentBackground has changed. void OnCanHaveAlphaEnabledChanged(); @@ -62,16 +62,17 @@ class NativeAppWindowViews : public extensions::NativeAppWindow, void set_web_view_for_testing(views::WebView* view) { web_view_ = view; } protected: - AppWindow* app_window() { return app_window_; } - const AppWindow* app_window() const { return app_window_; } + extensions::AppWindow* app_window() { return app_window_; } + const extensions::AppWindow* app_window() const { return app_window_; } const views::Widget* widget() const { return widget_; } views::WebView* web_view() { return web_view_; } // Initializes |widget_| for |app_window|. - virtual void InitializeWindow(AppWindow* app_window, - const AppWindow::CreateParams& create_params); + virtual void InitializeWindow( + extensions::AppWindow* app_window, + const extensions::AppWindow::CreateParams& create_params); // ui::BaseWindow implementation. virtual bool IsActive() const OVERRIDE; @@ -174,7 +175,7 @@ class NativeAppWindowViews : public extensions::NativeAppWindow, // Informs modal dialogs that they need to update their positions. void OnViewWasResized(); - AppWindow* app_window_; // Not owned. + extensions::AppWindow* app_window_; // Not owned. views::WebView* web_view_; views::Widget* widget_; -- cgit v1.1