Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / tabs / app_window_controller.cc
blob2e3fb68db79e1c1b5bcef72d8f81d5e4cb50918f
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/values.h"
6 #include "chrome/browser/extensions/api/tabs/app_base_window.h"
7 #include "chrome/browser/extensions/api/tabs/app_window_controller.h"
8 #include "chrome/browser/extensions/api/tabs/tabs_constants.h"
9 #include "chrome/browser/extensions/extension_tab_util.h"
10 #include "chrome/browser/extensions/window_controller.h"
11 #include "chrome/browser/extensions/window_controller_list.h"
12 #include "chrome/browser/sessions/session_tab_helper.h"
13 #include "chrome/common/url_constants.h"
14 #include "extensions/browser/app_window/app_window.h"
15 #include "extensions/browser/app_window/native_app_window.h"
16 #include "extensions/common/extension.h"
18 namespace extensions {
20 AppWindowController::AppWindowController(AppWindow* app_window,
21 scoped_ptr<AppBaseWindow> base_window,
22 Profile* profile)
23 : WindowController(base_window.get(), profile),
24 app_window_(app_window),
25 base_window_(base_window.Pass()) {
26 WindowControllerList::GetInstance()->AddExtensionWindow(this);
29 AppWindowController::~AppWindowController() {
30 WindowControllerList::GetInstance()->RemoveExtensionWindow(this);
33 int AppWindowController::GetWindowId() const {
34 return static_cast<int>(app_window_->session_id().id());
37 std::string AppWindowController::GetWindowTypeText() const {
38 if (app_window_->window_type_is_panel())
39 return tabs_constants::kWindowTypeValuePanel;
40 return tabs_constants::kWindowTypeValueApp;
43 base::DictionaryValue* AppWindowController::CreateWindowValueWithTabs(
44 const Extension* extension) const {
45 base::DictionaryValue* result = CreateWindowValue();
47 base::DictionaryValue* tab_value = CreateTabValue(extension, 0);
48 if (!tab_value)
49 return result;
51 base::ListValue* tab_list = new base::ListValue();
52 tab_list->Append(tab_value);
53 result->Set(tabs_constants::kTabsKey, tab_list);
55 return result;
58 base::DictionaryValue* AppWindowController::CreateTabValue(
59 const Extension* extension,
60 int tab_index) const {
61 if (tab_index > 0)
62 return nullptr;
64 content::WebContents* web_contents = app_window_->web_contents();
65 if (!web_contents)
66 return nullptr;
68 base::DictionaryValue* tab_value = new base::DictionaryValue();
69 tab_value->SetInteger(tabs_constants::kIdKey,
70 SessionTabHelper::IdForTab(web_contents));
71 tab_value->SetInteger(tabs_constants::kIndexKey, 0);
72 tab_value->SetInteger(
73 tabs_constants::kWindowIdKey,
74 SessionTabHelper::IdForWindowContainingTab(web_contents));
75 tab_value->SetString(tabs_constants::kUrlKey, web_contents->GetURL().spec());
76 tab_value->SetString(
77 tabs_constants::kStatusKey,
78 ExtensionTabUtil::GetTabStatusText(web_contents->IsLoading()));
79 tab_value->SetBoolean(tabs_constants::kActiveKey,
80 app_window_->GetBaseWindow()->IsActive());
81 tab_value->SetBoolean(tabs_constants::kSelectedKey, true);
82 tab_value->SetBoolean(tabs_constants::kHighlightedKey, true);
83 tab_value->SetBoolean(tabs_constants::kPinnedKey, false);
84 tab_value->SetString(tabs_constants::kTitleKey, web_contents->GetTitle());
85 tab_value->SetBoolean(tabs_constants::kIncognitoKey,
86 app_window_->GetBaseWindow()->IsActive());
88 gfx::Rect bounds = app_window_->GetBaseWindow()->GetBounds();
89 tab_value->SetInteger(tabs_constants::kWidthKey, bounds.width());
90 tab_value->SetInteger(tabs_constants::kHeightKey, bounds.height());
92 const Extension* ext = app_window_->GetExtension();
93 if (ext) {
94 std::string icon_str(chrome::kChromeUIFaviconURL);
95 icon_str.append(app_window_->GetExtension()->url().spec());
96 tab_value->SetString(tabs_constants::kFaviconUrlKey, icon_str);
99 return tab_value;
102 bool AppWindowController::CanClose(Reason* reason) const {
103 return true;
106 void AppWindowController::SetFullscreenMode(bool is_fullscreen,
107 const GURL& extension_url) const {
108 // Do nothing. Panels cannot be fullscreen.
109 if (app_window_->window_type_is_panel())
110 return;
111 // TODO(llandwerlin): should we prevent changes in fullscreen mode
112 // when the fullscreen state is FULLSCREEN_TYPE_FORCED?
113 app_window_->SetFullscreen(AppWindow::FULLSCREEN_TYPE_WINDOW_API,
114 is_fullscreen);
117 Browser* AppWindowController::GetBrowser() const {
118 return nullptr;
121 bool AppWindowController::IsVisibleToExtension(
122 const Extension* extension) const {
123 DCHECK(extension);
124 return extension->id() == app_window_->extension_id();
127 } // namespace extensions