Chromium Code Reviews| Index: chrome/browser/extensions/api/tabs/app_window_controller.cc |
| diff --git a/chrome/browser/extensions/api/tabs/app_window_controller.cc b/chrome/browser/extensions/api/tabs/app_window_controller.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..760f40aa0da3cbe73750262d188ed873fc568ca8 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/tabs/app_window_controller.cc |
| @@ -0,0 +1,232 @@ |
| +// Copyright 2015 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 "base/values.h" |
| +#include "chrome/browser/extensions/api/tabs/app_window_controller.h" |
| +#include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
| +#include "chrome/browser/extensions/extension_tab_util.h" |
| +#include "chrome/browser/extensions/window_controller.h" |
| +#include "chrome/browser/extensions/window_controller_list.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/sessions/session_tab_helper.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 "extensions/common/extension.h" |
| +#include "extensions/common/permissions/permissions_data.h" |
| + |
| +namespace extensions { |
| + |
| +AppBaseWindow::AppBaseWindow(AppWindow* app_window) : app_window_(app_window) { |
| +} |
| + |
| +AppBaseWindow::~AppBaseWindow() { |
| +} |
| + |
| +bool AppBaseWindow::IsActive() const { |
| + return GetBaseWindow()->IsActive(); |
| +} |
| + |
| +bool AppBaseWindow::IsMaximized() const { |
| + return GetBaseWindow()->IsMaximized(); |
| +} |
| + |
| +bool AppBaseWindow::IsMinimized() const { |
| + return GetBaseWindow()->IsMinimized(); |
| +} |
| + |
| +bool AppBaseWindow::IsFullscreen() const { |
| + return GetBaseWindow()->IsFullscreen(); |
| +} |
| + |
| +gfx::NativeWindow AppBaseWindow::GetNativeWindow() const { |
| + return GetBaseWindow()->GetNativeWindow(); |
| +} |
| + |
| +gfx::Rect AppBaseWindow::GetRestoredBounds() const { |
| + return GetBaseWindow()->GetRestoredBounds(); |
| +} |
| + |
| +ui::WindowShowState AppBaseWindow::GetRestoredState() const { |
| + return GetBaseWindow()->GetRestoredState(); |
| +} |
| + |
| +gfx::Rect AppBaseWindow::GetBounds() const { |
| + return GetBaseWindow()->GetBounds(); |
| +} |
| + |
| +void AppBaseWindow::Show() { |
| + GetBaseWindow()->Show(); |
| +} |
| + |
| +void AppBaseWindow::Hide() { |
| + GetBaseWindow()->Hide(); |
| +} |
| + |
| +void AppBaseWindow::ShowInactive() { |
| + GetBaseWindow()->ShowInactive(); |
| +} |
| + |
| +void AppBaseWindow::Close() { |
| + GetBaseWindow()->Close(); |
| +} |
| + |
| +void AppBaseWindow::Activate() { |
| + GetBaseWindow()->Activate(); |
| +} |
| + |
| +void AppBaseWindow::Deactivate() { |
| + GetBaseWindow()->Deactivate(); |
| +} |
| + |
| +void AppBaseWindow::Maximize() { |
| + GetBaseWindow()->Maximize(); |
| +} |
| + |
| +void AppBaseWindow::Minimize() { |
| + GetBaseWindow()->Minimize(); |
| +} |
| + |
| +void AppBaseWindow::Restore() { |
| + GetBaseWindow()->Restore(); |
| +} |
| + |
| +void AppBaseWindow::SetBounds(const gfx::Rect& bounds) { |
| + // We constraint the given size to the min/max sizes of the |
| + // application window. |
| + gfx::Rect original_window_bounds = GetBaseWindow()->GetBounds(); |
| + gfx::Insets frame_insets = GetBaseWindow()->GetFrameInsets(); |
| + SizeConstraints constraints( |
| + SizeConstraints::AddFrameToConstraints( |
| + GetBaseWindow()->GetContentMinimumSize(), frame_insets), |
| + SizeConstraints::AddFrameToConstraints( |
| + GetBaseWindow()->GetContentMaximumSize(), frame_insets)); |
| + |
| + gfx::Rect new_bounds = bounds; |
| + new_bounds.set_size(constraints.ClampSize(bounds.size())); |
| + |
| + GetBaseWindow()->SetBounds(new_bounds); |
| +} |
| + |
| +void AppBaseWindow::FlashFrame(bool flash) { |
| + GetBaseWindow()->FlashFrame(flash); |
| +} |
| + |
| +bool AppBaseWindow::IsAlwaysOnTop() const { |
| + return GetBaseWindow()->IsAlwaysOnTop(); |
| +} |
| + |
| +void AppBaseWindow::SetAlwaysOnTop(bool always_on_top) { |
| + GetBaseWindow()->SetAlwaysOnTop(always_on_top); |
| +} |
| + |
| +NativeAppWindow* AppBaseWindow::GetBaseWindow() const { |
| + return app_window_->GetBaseWindow(); |
| +} |
| + |
| +AppWindowController::AppWindowController(AppWindow* app_window, |
| + scoped_ptr<AppBaseWindow> base_window, |
| + Profile* profile) |
| + : WindowController(base_window.get(), profile), |
| + app_window_(app_window), |
| + base_window_(base_window.Pass()) { |
| + WindowControllerList::GetInstance()->AddExtensionWindow(this); |
| +} |
| + |
| +AppWindowController::~AppWindowController() { |
| + WindowControllerList::GetInstance()->RemoveExtensionWindow(this); |
| +} |
| + |
| +int AppWindowController::GetWindowId() const { |
| + return static_cast<int>(app_window_->session_id().id()); |
| +} |
| + |
| +std::string AppWindowController::GetWindowTypeText() const { |
| + if (app_window_->window_type_is_panel()) |
| + return tabs_constants::kWindowTypeValuePanel; |
| + return tabs_constants::kWindowTypeValueApp; |
| +} |
| + |
| +base::DictionaryValue* AppWindowController::CreateWindowValueWithTabs( |
| + const Extension* extension) const { |
| + base::DictionaryValue* result = CreateWindowValue(); |
| + |
| + DCHECK(IsVisibleToExtension(extension)); |
| + base::DictionaryValue* tab_value = CreateTabValue(extension, 0); |
| + if (!tab_value) |
| + return result; |
| + |
| + base::ListValue* tab_list = new base::ListValue(); |
| + tab_list->Append(tab_value); |
| + result->Set(tabs_constants::kTabsKey, tab_list); |
| + |
| + return result; |
| +} |
| + |
| +base::DictionaryValue* AppWindowController::CreateTabValue( |
|
dcheng
2015/06/29 18:15:49
Please use scoped_ptr here and elsewhere.
llandwerlin-old
2015/06/30 10:20:47
These methods are overridden from extension::Windo
|
| + const Extension* extension, |
| + int tab_index) const { |
| + if (tab_index > 0) |
| + return nullptr; |
| + |
| + content::WebContents* web_contents = app_window_->web_contents(); |
| + if (!web_contents) |
| + return nullptr; |
| + |
| + base::DictionaryValue* tab_value = new base::DictionaryValue(); |
| + tab_value->SetInteger(tabs_constants::kIdKey, |
| + SessionTabHelper::IdForTab(web_contents)); |
| + tab_value->SetInteger(tabs_constants::kIndexKey, 0); |
| + tab_value->SetInteger( |
| + tabs_constants::kWindowIdKey, |
| + SessionTabHelper::IdForWindowContainingTab(web_contents)); |
| + tab_value->SetString(tabs_constants::kUrlKey, web_contents->GetURL().spec()); |
| + tab_value->SetString( |
| + tabs_constants::kStatusKey, |
| + ExtensionTabUtil::GetTabStatusText(web_contents->IsLoading())); |
| + tab_value->SetBoolean(tabs_constants::kActiveKey, |
| + app_window_->GetBaseWindow()->IsActive()); |
| + tab_value->SetBoolean(tabs_constants::kSelectedKey, true); |
| + tab_value->SetBoolean(tabs_constants::kHighlightedKey, true); |
| + tab_value->SetBoolean(tabs_constants::kPinnedKey, false); |
| + tab_value->SetString(tabs_constants::kTitleKey, web_contents->GetTitle()); |
| + tab_value->SetBoolean(tabs_constants::kIncognitoKey, |
| + app_window_->GetBaseWindow()->IsActive()); |
| + |
| + gfx::Rect bounds = app_window_->GetBaseWindow()->GetBounds(); |
| + tab_value->SetInteger(tabs_constants::kWidthKey, bounds.width()); |
| + tab_value->SetInteger(tabs_constants::kHeightKey, bounds.height()); |
| + |
| + std::string icon_str("chrome://favicon/"); |
| + icon_str.append(app_window_->GetExtension()->url().spec()); |
| + tab_value->SetString(tabs_constants::kFaviconUrlKey, icon_str); |
| + |
| + return tab_value; |
| +} |
| + |
| +bool AppWindowController::CanClose(Reason* reason) const { |
| + return true; |
| +} |
| + |
| +void AppWindowController::SetFullscreenMode(bool is_fullscreen, |
| + const GURL& extension_url) const { |
| + // TODO(llandwerlin): should we prevent changes in fullscreen mode |
| + // when the fullscreen state is FULLSCREEN_TYPE_FORCED? |
| + app_window_->SetFullscreen(AppWindow::FULLSCREEN_TYPE_WINDOW_API, |
| + is_fullscreen); |
| +} |
| + |
| +Browser* AppWindowController::GetBrowser() const { |
| + return nullptr; |
| +} |
| + |
| +bool AppWindowController::IsVisibleToExtension( |
| + const Extension* extension) const { |
| + if (extension->permissions_data()->HasAPIPermission( |
| + APIPermission::kWindowsGlobal)) |
| + return true; |
| + return extension->id() == app_window_->extension_id(); |
| +} |
| + |
| +} // namespace extensions |