OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/tabs/ash_panel_contents.h" | 5 #include "chrome/browser/extensions/api/tabs/ash_panel_contents.h" |
6 | 6 |
7 #include "apps/ui/native_app_window.h" | 7 #include "apps/ui/native_app_window.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chrome/browser/chrome_notification_types.h" | 9 #include "chrome/browser/chrome_notification_types.h" |
10 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 10 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
11 #include "chrome/browser/extensions/api/tabs/tabs_windows_api.h" | 11 #include "chrome/browser/extensions/api/tabs/tabs_windows_api.h" |
12 #include "chrome/browser/extensions/api/tabs/windows_event_router.h" | 12 #include "chrome/browser/extensions/api/tabs/windows_event_router.h" |
13 #include "chrome/browser/extensions/extension_tab_util.h" | 13 #include "chrome/browser/extensions/extension_tab_util.h" |
14 #include "chrome/browser/extensions/window_controller_list.h" | 14 #include "chrome/browser/extensions/window_controller_list.h" |
15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/sessions/session_tab_helper.h" | 16 #include "chrome/browser/sessions/session_tab_helper.h" |
17 #include "chrome/common/extensions/extension_messages.h" | 17 #include "chrome/common/extensions/extension_messages.h" |
18 #include "content/public/browser/browser_context.h" | 18 #include "content/public/browser/browser_context.h" |
19 #include "content/public/browser/site_instance.h" | 19 #include "content/public/browser/site_instance.h" |
20 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
21 #include "extensions/common/extension.h" | 21 #include "extensions/common/extension.h" |
22 #include "ui/gfx/image/image.h" | 22 #include "ui/gfx/image/image.h" |
23 | 23 |
24 using apps::ShellWindow; | 24 using apps::AppWindow; |
25 using apps::NativeAppWindow; | 25 using apps::NativeAppWindow; |
26 | 26 |
27 // AshPanelWindowController ---------------------------------------------------- | 27 // AshPanelWindowController ---------------------------------------------------- |
28 | 28 |
29 // This class enables a ShellWindow instance to be accessed (to a limited | 29 // This class enables an AppWindow instance to be accessed (to a limited |
30 // extent) via the chrome.windows and chrome.tabs API. This is a temporary | 30 // extent) via the chrome.windows and chrome.tabs API. This is a temporary |
31 // bridge to support instantiating ShellWindows from v1 apps, specifically | 31 // bridge to support instantiating AppWindows from v1 apps, specifically |
32 // for creating Panels in Ash. See crbug.com/160645. | 32 // for creating Panels in Ash. See crbug.com/160645. |
33 class AshPanelWindowController : public extensions::WindowController { | 33 class AshPanelWindowController : public extensions::WindowController { |
34 public: | 34 public: |
35 AshPanelWindowController(ShellWindow* window, Profile* profile); | 35 AshPanelWindowController(AppWindow* window, Profile* profile); |
36 virtual ~AshPanelWindowController(); | 36 virtual ~AshPanelWindowController(); |
37 | 37 |
38 void NativeWindowChanged(); | 38 void NativeWindowChanged(); |
39 | 39 |
40 // Overridden from extensions::WindowController. | 40 // Overridden from extensions::WindowController. |
41 virtual int GetWindowId() const OVERRIDE; | 41 virtual int GetWindowId() const OVERRIDE; |
42 virtual std::string GetWindowTypeText() const OVERRIDE; | 42 virtual std::string GetWindowTypeText() const OVERRIDE; |
43 virtual base::DictionaryValue* CreateWindowValueWithTabs( | 43 virtual base::DictionaryValue* CreateWindowValueWithTabs( |
44 const extensions::Extension* extension) const OVERRIDE; | 44 const extensions::Extension* extension) const OVERRIDE; |
45 virtual base::DictionaryValue* CreateTabValue( | 45 virtual base::DictionaryValue* CreateTabValue( |
46 const extensions::Extension* extension, int tab_index) const OVERRIDE; | 46 const extensions::Extension* extension, int tab_index) const OVERRIDE; |
47 virtual bool CanClose(Reason* reason) const OVERRIDE; | 47 virtual bool CanClose(Reason* reason) const OVERRIDE; |
48 virtual void SetFullscreenMode(bool is_fullscreen, | 48 virtual void SetFullscreenMode(bool is_fullscreen, |
49 const GURL& extension_url) const OVERRIDE; | 49 const GURL& extension_url) const OVERRIDE; |
50 virtual bool IsVisibleToExtension( | 50 virtual bool IsVisibleToExtension( |
51 const extensions::Extension* extension) const OVERRIDE; | 51 const extensions::Extension* extension) const OVERRIDE; |
52 | 52 |
53 private: | 53 private: |
54 ShellWindow* shell_window_; // Weak pointer; this is owned by shell_window_ | 54 AppWindow* app_window_; // Weak pointer; this is owned by app_window_ |
55 bool is_active_; | 55 bool is_active_; |
56 | 56 |
57 DISALLOW_COPY_AND_ASSIGN(AshPanelWindowController); | 57 DISALLOW_COPY_AND_ASSIGN(AshPanelWindowController); |
58 }; | 58 }; |
59 | 59 |
60 AshPanelWindowController::AshPanelWindowController( | 60 AshPanelWindowController::AshPanelWindowController(AppWindow* app_window, |
61 ShellWindow* shell_window, Profile* profile) | 61 Profile* profile) |
62 : extensions::WindowController(shell_window->GetBaseWindow(), profile), | 62 : extensions::WindowController(app_window->GetBaseWindow(), profile), |
63 shell_window_(shell_window), | 63 app_window_(app_window), |
64 is_active_(shell_window->GetBaseWindow()->IsActive()) { | 64 is_active_(app_window->GetBaseWindow()->IsActive()) { |
65 extensions::WindowControllerList::GetInstance()->AddExtensionWindow(this); | 65 extensions::WindowControllerList::GetInstance()->AddExtensionWindow(this); |
66 } | 66 } |
67 | 67 |
68 AshPanelWindowController::~AshPanelWindowController() { | 68 AshPanelWindowController::~AshPanelWindowController() { |
69 extensions::WindowControllerList::GetInstance()->RemoveExtensionWindow(this); | 69 extensions::WindowControllerList::GetInstance()->RemoveExtensionWindow(this); |
70 } | 70 } |
71 | 71 |
72 int AshPanelWindowController::GetWindowId() const { | 72 int AshPanelWindowController::GetWindowId() const { |
73 return static_cast<int>(shell_window_->session_id().id()); | 73 return static_cast<int>(app_window_->session_id().id()); |
74 } | 74 } |
75 | 75 |
76 std::string AshPanelWindowController::GetWindowTypeText() const { | 76 std::string AshPanelWindowController::GetWindowTypeText() const { |
77 return extensions::tabs_constants::kWindowTypeValuePanel; | 77 return extensions::tabs_constants::kWindowTypeValuePanel; |
78 } | 78 } |
79 | 79 |
80 base::DictionaryValue* AshPanelWindowController::CreateWindowValueWithTabs( | 80 base::DictionaryValue* AshPanelWindowController::CreateWindowValueWithTabs( |
81 const extensions::Extension* extension) const { | 81 const extensions::Extension* extension) const { |
82 DCHECK(IsVisibleToExtension(extension)); | 82 DCHECK(IsVisibleToExtension(extension)); |
83 base::DictionaryValue* result = CreateWindowValue(); | 83 base::DictionaryValue* result = CreateWindowValue(); |
84 base::DictionaryValue* tab_value = CreateTabValue(extension, 0); | 84 base::DictionaryValue* tab_value = CreateTabValue(extension, 0); |
85 if (tab_value) { | 85 if (tab_value) { |
86 base::ListValue* tab_list = new base::ListValue(); | 86 base::ListValue* tab_list = new base::ListValue(); |
87 tab_list->Append(tab_value); | 87 tab_list->Append(tab_value); |
88 result->Set(extensions::tabs_constants::kTabsKey, tab_list); | 88 result->Set(extensions::tabs_constants::kTabsKey, tab_list); |
89 } | 89 } |
90 return result; | 90 return result; |
91 } | 91 } |
92 | 92 |
93 base::DictionaryValue* AshPanelWindowController::CreateTabValue( | 93 base::DictionaryValue* AshPanelWindowController::CreateTabValue( |
94 const extensions::Extension* extension, int tab_index) const { | 94 const extensions::Extension* extension, int tab_index) const { |
95 if ((extension && !IsVisibleToExtension(extension)) || | 95 if ((extension && !IsVisibleToExtension(extension)) || |
96 (tab_index > 0)) { | 96 (tab_index > 0)) { |
97 return NULL; | 97 return NULL; |
98 } | 98 } |
99 content::WebContents* web_contents = shell_window_->web_contents(); | 99 content::WebContents* web_contents = app_window_->web_contents(); |
100 if (!web_contents) | 100 if (!web_contents) |
101 return NULL; | 101 return NULL; |
102 | 102 |
103 base::DictionaryValue* tab_value = new base::DictionaryValue(); | 103 base::DictionaryValue* tab_value = new base::DictionaryValue(); |
104 tab_value->SetInteger(extensions::tabs_constants::kIdKey, | 104 tab_value->SetInteger(extensions::tabs_constants::kIdKey, |
105 SessionID::IdForTab(web_contents)); | 105 SessionID::IdForTab(web_contents)); |
106 tab_value->SetInteger(extensions::tabs_constants::kIndexKey, 0); | 106 tab_value->SetInteger(extensions::tabs_constants::kIndexKey, 0); |
107 const int window_id = GetWindowId(); | 107 const int window_id = GetWindowId(); |
108 tab_value->SetInteger(extensions::tabs_constants::kWindowIdKey, window_id); | 108 tab_value->SetInteger(extensions::tabs_constants::kWindowIdKey, window_id); |
109 tab_value->SetString( | 109 tab_value->SetString( |
110 extensions::tabs_constants::kUrlKey, web_contents->GetURL().spec()); | 110 extensions::tabs_constants::kUrlKey, web_contents->GetURL().spec()); |
111 tab_value->SetString( | 111 tab_value->SetString( |
112 extensions::tabs_constants::kStatusKey, | 112 extensions::tabs_constants::kStatusKey, |
113 extensions::ExtensionTabUtil::GetTabStatusText( | 113 extensions::ExtensionTabUtil::GetTabStatusText( |
114 web_contents->IsLoading())); | 114 web_contents->IsLoading())); |
115 tab_value->SetBoolean( | 115 tab_value->SetBoolean(extensions::tabs_constants::kActiveKey, |
116 extensions::tabs_constants::kActiveKey, | 116 app_window_->GetBaseWindow()->IsActive()); |
117 shell_window_->GetBaseWindow()->IsActive()); | 117 // AppWindow only ever contains one tab, so that tab is always effectively |
118 // ShellWindow only ever contains one tab, so that tab is always effectively | |
119 // selcted and highlighted (for purposes of the chrome.tabs API). | 118 // selcted and highlighted (for purposes of the chrome.tabs API). |
120 tab_value->SetInteger(extensions::tabs_constants::kWindowIdKey, window_id); | 119 tab_value->SetInteger(extensions::tabs_constants::kWindowIdKey, window_id); |
121 tab_value->SetInteger(extensions::tabs_constants::kIdKey, window_id); | 120 tab_value->SetInteger(extensions::tabs_constants::kIdKey, window_id); |
122 tab_value->SetBoolean(extensions::tabs_constants::kSelectedKey, true); | 121 tab_value->SetBoolean(extensions::tabs_constants::kSelectedKey, true); |
123 tab_value->SetBoolean(extensions::tabs_constants::kHighlightedKey, true); | 122 tab_value->SetBoolean(extensions::tabs_constants::kHighlightedKey, true); |
124 tab_value->SetBoolean(extensions::tabs_constants::kPinnedKey, false); | 123 tab_value->SetBoolean(extensions::tabs_constants::kPinnedKey, false); |
125 tab_value->SetString( | 124 tab_value->SetString( |
126 extensions::tabs_constants::kTitleKey, web_contents->GetTitle()); | 125 extensions::tabs_constants::kTitleKey, web_contents->GetTitle()); |
127 tab_value->SetBoolean( | 126 tab_value->SetBoolean( |
128 extensions::tabs_constants::kIncognitoKey, | 127 extensions::tabs_constants::kIncognitoKey, |
129 web_contents->GetBrowserContext()->IsOffTheRecord()); | 128 web_contents->GetBrowserContext()->IsOffTheRecord()); |
130 return tab_value; | 129 return tab_value; |
131 } | 130 } |
132 | 131 |
133 bool AshPanelWindowController::CanClose(Reason* reason) const { | 132 bool AshPanelWindowController::CanClose(Reason* reason) const { |
134 return true; | 133 return true; |
135 } | 134 } |
136 | 135 |
137 void AshPanelWindowController::SetFullscreenMode( | 136 void AshPanelWindowController::SetFullscreenMode( |
138 bool is_fullscreen, const GURL& extension_url) const { | 137 bool is_fullscreen, const GURL& extension_url) const { |
139 // Do nothing. Panels cannot be fullscreen. | 138 // Do nothing. Panels cannot be fullscreen. |
140 } | 139 } |
141 | 140 |
142 bool AshPanelWindowController::IsVisibleToExtension( | 141 bool AshPanelWindowController::IsVisibleToExtension( |
143 const extensions::Extension* extension) const { | 142 const extensions::Extension* extension) const { |
144 return shell_window_->extension() && | 143 return app_window_->extension() && |
145 extension->id() == shell_window_->extension()->id(); | 144 extension->id() == app_window_->extension()->id(); |
146 } | 145 } |
147 | 146 |
148 void AshPanelWindowController::NativeWindowChanged() { | 147 void AshPanelWindowController::NativeWindowChanged() { |
149 bool active = shell_window_->GetBaseWindow()->IsActive(); | 148 bool active = app_window_->GetBaseWindow()->IsActive(); |
150 if (active == is_active_) | 149 if (active == is_active_) |
151 return; | 150 return; |
152 is_active_ = active; | 151 is_active_ = active; |
153 // Let the extension API know that the active window changed. | 152 // Let the extension API know that the active window changed. |
154 extensions::TabsWindowsAPI* tabs_windows_api = | 153 extensions::TabsWindowsAPI* tabs_windows_api = |
155 extensions::TabsWindowsAPI::Get(profile()); | 154 extensions::TabsWindowsAPI::Get(profile()); |
156 if (!tabs_windows_api) | 155 if (!tabs_windows_api) |
157 return; | 156 return; |
158 tabs_windows_api->windows_event_router()->OnActiveWindowChanged( | 157 tabs_windows_api->windows_event_router()->OnActiveWindowChanged( |
159 active ? this : NULL); | 158 active ? this : NULL); |
160 } | 159 } |
161 | 160 |
162 // AshPanelContents ----------------------------------------------------- | 161 // AshPanelContents ----------------------------------------------------- |
163 | 162 |
164 AshPanelContents::AshPanelContents(ShellWindow* host) | 163 AshPanelContents::AshPanelContents(AppWindow* host) : host_(host) {} |
165 : host_(host) { | |
166 } | |
167 | 164 |
168 AshPanelContents::~AshPanelContents() { | 165 AshPanelContents::~AshPanelContents() { |
169 } | 166 } |
170 | 167 |
171 void AshPanelContents::Initialize(content::BrowserContext* context, | 168 void AshPanelContents::Initialize(content::BrowserContext* context, |
172 const GURL& url) { | 169 const GURL& url) { |
173 url_ = url; | 170 url_ = url; |
174 | 171 |
175 extension_function_dispatcher_.reset( | 172 extension_function_dispatcher_.reset( |
176 new ExtensionFunctionDispatcher(context, this)); | 173 new ExtensionFunctionDispatcher(context, this)); |
177 | 174 |
178 web_contents_.reset( | 175 web_contents_.reset( |
179 content::WebContents::Create(content::WebContents::CreateParams( | 176 content::WebContents::Create(content::WebContents::CreateParams( |
180 context, content::SiteInstance::CreateForURL(context, url_)))); | 177 context, content::SiteInstance::CreateForURL(context, url_)))); |
181 | 178 |
182 // Needed to give the web contents a Window ID. Extension APIs expect web | 179 // Needed to give the web contents a Window ID. Extension APIs expect web |
183 // contents to have a Window ID. Also required for FaviconTabHelper to | 180 // contents to have a Window ID. Also required for FaviconTabHelper to |
184 // correctly set the window icon and title. | 181 // correctly set the window icon and title. |
185 SessionTabHelper::CreateForWebContents(web_contents_.get()); | 182 SessionTabHelper::CreateForWebContents(web_contents_.get()); |
186 SessionTabHelper::FromWebContents(web_contents_.get())->SetWindowID( | 183 SessionTabHelper::FromWebContents(web_contents_.get())->SetWindowID( |
187 host_->session_id()); | 184 host_->session_id()); |
188 | 185 |
189 // Responsible for loading favicons for the Launcher, which uses different | 186 // Responsible for loading favicons for the Launcher, which uses different |
190 // logic than the FaviconTabHelper associated with web_contents_ | 187 // logic than the FaviconTabHelper associated with web_contents_ |
191 // (instantiated in ShellWindow::Init()) | 188 // (instantiated in AppWindow::Init()) |
192 launcher_favicon_loader_.reset( | 189 launcher_favicon_loader_.reset( |
193 new LauncherFaviconLoader(this, web_contents_.get())); | 190 new LauncherFaviconLoader(this, web_contents_.get())); |
194 | 191 |
195 content::WebContentsObserver::Observe(web_contents_.get()); | 192 content::WebContentsObserver::Observe(web_contents_.get()); |
196 } | 193 } |
197 | 194 |
198 void AshPanelContents::LoadContents(int32 creator_process_id) { | 195 void AshPanelContents::LoadContents(int32 creator_process_id) { |
199 // This must be created after the native window has been created. | 196 // This must be created after the native window has been created. |
200 window_controller_.reset(new AshPanelWindowController( | 197 window_controller_.reset(new AshPanelWindowController( |
201 host_, Profile::FromBrowserContext(host_->browser_context()))); | 198 host_, Profile::FromBrowserContext(host_->browser_context()))); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 | 236 |
240 content::WebContents* AshPanelContents::GetAssociatedWebContents() const { | 237 content::WebContents* AshPanelContents::GetAssociatedWebContents() const { |
241 return web_contents_.get(); | 238 return web_contents_.get(); |
242 } | 239 } |
243 | 240 |
244 void AshPanelContents::OnRequest( | 241 void AshPanelContents::OnRequest( |
245 const ExtensionHostMsg_Request_Params& params) { | 242 const ExtensionHostMsg_Request_Params& params) { |
246 extension_function_dispatcher_->Dispatch( | 243 extension_function_dispatcher_->Dispatch( |
247 params, web_contents_->GetRenderViewHost()); | 244 params, web_contents_->GetRenderViewHost()); |
248 } | 245 } |
OLD | NEW |