OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/ui/extensions/shell_window.h" | 5 #include "chrome/browser/ui/extensions/shell_window.h" |
6 | 6 |
7 #include "chrome/browser/ui/browser_list.h" | 7 #include "chrome/browser/ui/browser_list.h" |
8 #include "chrome/browser/extensions/extension_process_manager.h" | 8 #include "chrome/browser/extensions/extension_process_manager.h" |
9 #include "chrome/browser/extensions/extension_tabs_module_constants.h" | 9 #include "chrome/browser/extensions/extension_tabs_module_constants.h" |
10 #include "chrome/browser/extensions/extension_window_controller.h" | 10 #include "chrome/browser/extensions/extension_window_controller.h" |
11 #include "chrome/browser/extensions/platform_app_registry.h" | |
11 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/sessions/session_id.h" | 13 #include "chrome/browser/sessions/session_id.h" |
13 #include "chrome/common/chrome_notification_types.h" | 14 #include "chrome/common/chrome_notification_types.h" |
15 #include "chrome/common/chrome_view_type.h" | |
14 #include "chrome/common/extensions/extension.h" | 16 #include "chrome/common/extensions/extension.h" |
17 #include "chrome/common/extensions/extension_messages.h" | |
15 #include "content/public/browser/notification_details.h" | 18 #include "content/public/browser/notification_details.h" |
16 #include "content/public/browser/notification_service.h" | 19 #include "content/public/browser/notification_service.h" |
17 #include "content/public/browser/notification_source.h" | 20 #include "content/public/browser/notification_source.h" |
18 #include "content/public/browser/notification_types.h" | 21 #include "content/public/browser/notification_types.h" |
22 #include "content/public/browser/render_view_host.h" | |
23 #include "content/public/browser/site_instance.h" | |
24 #include "content/public/browser/web_contents.h" | |
25 #include "content/public/common/renderer_preferences.h" | |
26 | |
27 using content::SiteInstance; | |
28 using content::WebContents; | |
19 | 29 |
20 namespace internal { | 30 namespace internal { |
21 | 31 |
22 class ShellWindowController : public ExtensionWindowController { | 32 class ShellWindowController : public ExtensionWindowController { |
23 public: | 33 public: |
24 ShellWindowController(ShellWindow* shell_window, Profile* profile); | 34 ShellWindowController(ShellWindow* shell_window, Profile* profile); |
25 | 35 |
26 // Overriden from ExtensionWindowController | 36 // Overriden from ExtensionWindowController |
27 virtual int GetWindowId() const OVERRIDE; | 37 virtual int GetWindowId() const OVERRIDE; |
28 virtual std::string GetWindowTypeText() const OVERRIDE; | 38 virtual std::string GetWindowTypeText() const OVERRIDE; |
(...skipping 13 matching lines...) Expand all Loading... | |
42 ShellWindow* shell_window, | 52 ShellWindow* shell_window, |
43 Profile* profile) | 53 Profile* profile) |
44 : ExtensionWindowController(shell_window, profile), | 54 : ExtensionWindowController(shell_window, profile), |
45 shell_window_(shell_window) { | 55 shell_window_(shell_window) { |
46 } | 56 } |
47 | 57 |
48 int ShellWindowController::GetWindowId() const { | 58 int ShellWindowController::GetWindowId() const { |
49 return static_cast<int>(shell_window_->session_id().id()); | 59 return static_cast<int>(shell_window_->session_id().id()); |
50 } | 60 } |
51 | 61 |
52 namespace keys = extension_tabs_module_constants; | 62 namespace keys = extension_tabs_module_constants; |
Aaron Boodman
2012/04/27 16:20:31
This is only used in one place, just put it inline
benwells
2012/05/03 09:22:40
Done.
| |
53 | 63 |
54 std::string ShellWindowController::GetWindowTypeText() const { | 64 std::string ShellWindowController::GetWindowTypeText() const { |
55 return keys::kWindowTypeValueShell; | 65 return keys::kWindowTypeValueShell; |
56 } | 66 } |
57 | 67 |
58 base::DictionaryValue* ShellWindowController::CreateWindowValue() const { | 68 base::DictionaryValue* ShellWindowController::CreateWindowValue() const { |
Aaron Boodman
2012/04/27 16:20:31
Why override this if you're not adding behavior?
benwells
2012/05/03 09:22:40
Done.
| |
59 DictionaryValue* result = ExtensionWindowController::CreateWindowValue(); | 69 DictionaryValue* result = ExtensionWindowController::CreateWindowValue(); |
60 return result; | 70 return result; |
61 } | 71 } |
62 | 72 |
63 base::DictionaryValue* ShellWindowController::CreateWindowValueWithTabs() | 73 base::DictionaryValue* ShellWindowController::CreateWindowValueWithTabs() |
64 const { | 74 const { |
65 return CreateWindowValue(); | 75 return CreateWindowValue(); |
66 } | 76 } |
67 | 77 |
68 bool ShellWindowController::CanClose(Reason* reason) const { | 78 bool ShellWindowController::CanClose(Reason* reason) const { |
69 return true; | 79 return true; |
70 } | 80 } |
71 | 81 |
72 void ShellWindowController::SetFullscreenMode(bool is_fullscreen, | 82 void ShellWindowController::SetFullscreenMode(bool is_fullscreen, |
73 const GURL& extension_url) const { | 83 const GURL& extension_url) const { |
74 // TODO(mihaip): implement | 84 // TODO(mihaip): implement |
75 } | 85 } |
76 | 86 |
77 } // namespace internal | 87 } // namespace internal |
78 | 88 |
79 ShellWindow* ShellWindow::Create(Profile* profile, | 89 ShellWindow* ShellWindow::Create(Profile* profile, |
80 const Extension* extension, | 90 const Extension* extension, |
81 const GURL& url) { | 91 const GURL& url) { |
82 ExtensionProcessManager* manager = profile->GetExtensionProcessManager(); | 92 ExtensionProcessManager* manager = profile->GetExtensionProcessManager(); |
Aaron Boodman
2012/04/27 16:20:31
Unused code?
benwells
2012/05/03 09:22:40
Done.
| |
83 DCHECK(manager); | 93 DCHECK(manager); |
84 | 94 |
85 // This object will delete itself when the window is closed. | 95 // This object will delete itself when the window is closed. |
86 return ShellWindow::CreateShellWindow( | 96 return ShellWindow::CreateShellWindow(profile, extension, url); |
87 manager->CreateShellHost(extension, url)); | 97 } |
98 | |
99 WebContents* ShellWindow::GetAssociatedWebContents() const { | |
100 return NULL; | |
Aaron Boodman
2012/04/27 16:20:31
EFD::Delegate could do this by default.
benwells
2012/05/03 09:22:40
Done.
| |
101 } | |
102 | |
103 bool ShellWindow::OnMessageReceived(const IPC::Message& message) { | |
104 bool handled = true; | |
105 IPC_BEGIN_MESSAGE_MAP(ShellWindow, message) | |
106 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) | |
107 IPC_MESSAGE_UNHANDLED(handled = false) | |
108 IPC_END_MESSAGE_MAP() | |
109 return handled; | |
110 } | |
111 | |
112 void ShellWindow::CloseContents(WebContents* contents) { | |
113 Close(); | |
114 } | |
115 | |
116 bool ShellWindow::ShouldSuppressDialogs() { | |
Aaron Boodman
2012/04/27 16:20:31
This seems like it is unnecessarily inversion of c
benwells
2012/05/03 09:22:40
Added todo.
| |
117 return true; | |
88 } | 118 } |
89 | 119 |
90 void ShellWindow::Observe(int type, | 120 void ShellWindow::Observe(int type, |
91 const content::NotificationSource& source, | 121 const content::NotificationSource& source, |
92 const content::NotificationDetails& details) { | 122 const content::NotificationDetails& details) { |
93 switch (type) { | 123 switch (type) { |
94 case chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE: | |
95 if (content::Details<ExtensionHost>(host_.get()) == details) | |
96 Close(); | |
97 break; | |
98 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { | 124 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { |
99 const Extension* unloaded_extension = | 125 const Extension* unloaded_extension = |
100 content::Details<UnloadedExtensionInfo>(details)->extension; | 126 content::Details<UnloadedExtensionInfo>(details)->extension; |
101 // We compare extension IDs and not Extension pointers since ExtensionHost | 127 if (extension_ == unloaded_extension) |
102 // nulls out its Extension pointer when it gets this notification. | |
103 if (host_->extension_id() == unloaded_extension->id()) | |
104 Close(); | 128 Close(); |
105 break; | 129 break; |
106 } | 130 } |
107 case content::NOTIFICATION_APP_TERMINATING: | 131 case content::NOTIFICATION_APP_TERMINATING: |
108 Close(); | 132 Close(); |
109 break; | 133 break; |
110 default: | 134 default: |
111 NOTREACHED() << "Received unexpected notification"; | 135 NOTREACHED() << "Received unexpected notification"; |
112 } | 136 } |
113 } | 137 } |
114 | 138 |
115 ShellWindow::ShellWindow(ExtensionHost* host) | 139 ShellWindow::ShellWindow(Profile* profile, |
116 : host_(host) { | 140 const Extension* extension, |
117 // Close the window in response to window.close() and the like. | 141 const GURL& url) |
118 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE, | 142 : profile_(profile), |
119 content::Source<Profile>(host->profile())); | 143 extension_(extension), |
120 // Also close if the window if the extension has been unloaded (parallels | 144 ALLOW_THIS_IN_INITIALIZER_LIST( |
121 // NOTIFICATION_EXTENSION_UNLOADED closing the app's tabs in TabStripModel). | 145 extension_function_dispatcher_(profile, this)) { |
146 web_contents_.reset(WebContents::Create( | |
147 profile, SiteInstance::CreateForURL(profile, url), MSG_ROUTING_NONE, NULL, | |
148 NULL)); | |
149 content::WebContentsObserver::Observe(web_contents_.get()); | |
150 web_contents_->SetDelegate(this); | |
151 web_contents_->SetViewType(chrome::VIEW_TYPE_APP_SHELL); | |
152 web_contents_->GetMutableRendererPrefs()->browser_handles_all_requests = | |
153 true; | |
154 web_contents_->GetRenderViewHost()->SyncRendererPrefs(); | |
155 | |
156 web_contents_->GetController().LoadURL( | |
157 url, content::Referrer(), content::PAGE_TRANSITION_LINK, | |
158 std::string()); | |
122 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 159 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
123 content::Source<Profile>(host->profile())); | 160 content::Source<Profile>(profile_)); |
124 // Close when the browser is exiting. | 161 // Close when the browser is exiting. |
125 // TODO(mihaip): we probably don't want this in the long run (when platform | 162 // TODO(mihaip): we probably don't want this in the long run (when platform |
126 // apps are no longer tied to the browser process). | 163 // apps are no longer tied to the browser process). |
127 registrar_.Add(this, content::NOTIFICATION_APP_TERMINATING, | 164 registrar_.Add(this, content::NOTIFICATION_APP_TERMINATING, |
128 content::NotificationService::AllSources()); | 165 content::NotificationService::AllSources()); |
129 | 166 |
130 // Prevent the browser process from shutting down while this window is open. | 167 // Prevent the browser process from shutting down while this window is open. |
131 BrowserList::StartKeepAlive(); | 168 BrowserList::StartKeepAlive(); |
132 | 169 |
133 // Make this window available to the extension API. | 170 // Make this window available to the extension API. |
134 extension_window_controller_.reset( | 171 extension_window_controller_.reset( |
135 new internal::ShellWindowController(this, host->profile())); | 172 new internal::ShellWindowController(this, profile_)); |
173 | |
174 PlatformAppRegistry::Get(profile_)->AddShellWindow(this); | |
136 } | 175 } |
137 | 176 |
138 ShellWindow::~ShellWindow() { | 177 ShellWindow::~ShellWindow() { |
139 // Unregister now to prevent getting NOTIFICATION_APP_TERMINATING if we're the | 178 // Unregister now to prevent getting NOTIFICATION_APP_TERMINATING if we're the |
140 // last window open. | 179 // last window open. |
141 registrar_.RemoveAll(); | 180 registrar_.RemoveAll(); |
142 | 181 |
182 PlatformAppRegistry::Get(profile_)->RemoveShellWindow(this); | |
183 | |
143 // Remove shutdown prevention. | 184 // Remove shutdown prevention. |
144 BrowserList::EndKeepAlive(); | 185 BrowserList::EndKeepAlive(); |
145 } | 186 } |
187 | |
188 Browser* ShellWindow::GetBrowser() { | |
Aaron Boodman
2012/04/27 16:20:31
EFD::Delegate could do this by default.
benwells
2012/05/03 09:22:40
Done.
| |
189 return NULL; | |
190 } | |
191 | |
192 void ShellWindow::OnRequest(const ExtensionHostMsg_Request_Params& params) { | |
193 extension_function_dispatcher_.Dispatch(params, | |
194 web_contents_->GetRenderViewHost()); | |
195 } | |
OLD | NEW |