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 "base/memory/singleton.h" |
7 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
8 #include "chrome/browser/extensions/extension_process_manager.h" | 9 #include "chrome/browser/extensions/extension_process_manager.h" |
9 #include "chrome/browser/extensions/extension_system.h" | 10 #include "chrome/browser/extensions/extension_system.h" |
10 #include "chrome/browser/extensions/shell_window_geometry_cache.h" | 11 #include "chrome/browser/extensions/shell_window_geometry_cache.h" |
11 #include "chrome/browser/extensions/shell_window_registry.h" | 12 #include "chrome/browser/extensions/shell_window_registry.h" |
12 #include "chrome/browser/extensions/tab_helper.h" | 13 #include "chrome/browser/extensions/tab_helper.h" |
13 #include "chrome/browser/file_select_helper.h" | 14 #include "chrome/browser/file_select_helper.h" |
14 #include "chrome/browser/infobars/infobar_tab_helper.h" | 15 #include "chrome/browser/infobars/infobar_tab_helper.h" |
15 #include "chrome/browser/intents/web_intents_util.h" | 16 #include "chrome/browser/intents/web_intents_util.h" |
16 #include "chrome/browser/lifetime/application_lifetime.h" | 17 #include "chrome/browser/lifetime/application_lifetime.h" |
| 18 #include "chrome/browser/platform_util.h" |
17 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/browser/sessions/session_id.h" | 20 #include "chrome/browser/sessions/session_id.h" |
19 #include "chrome/browser/ui/browser.h" | 21 #include "chrome/browser/ui/browser.h" |
20 #include "chrome/browser/ui/browser_finder.h" | 22 #include "chrome/browser/ui/browser_finder.h" |
21 #include "chrome/browser/ui/browser_tabstrip.h" | 23 #include "chrome/browser/ui/browser_tabstrip.h" |
22 #include "chrome/browser/ui/browser_window.h" | 24 #include "chrome/browser/ui/browser_window.h" |
23 #include "chrome/browser/ui/extensions/native_shell_window.h" | 25 #include "chrome/browser/ui/extensions/native_shell_window.h" |
24 #include "chrome/browser/ui/intents/web_intent_picker_controller.h" | 26 #include "chrome/browser/ui/intents/web_intent_picker_controller.h" |
25 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 27 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
26 #include "chrome/browser/view_type_utils.h" | 28 #include "chrome/browser/view_type_utils.h" |
(...skipping 20 matching lines...) Expand all Loading... |
47 using content::ConsoleMessageLevel; | 49 using content::ConsoleMessageLevel; |
48 using content::RenderViewHost; | 50 using content::RenderViewHost; |
49 using content::ResourceDispatcherHost; | 51 using content::ResourceDispatcherHost; |
50 using content::SiteInstance; | 52 using content::SiteInstance; |
51 using content::WebContents; | 53 using content::WebContents; |
52 using extensions::APIPermission; | 54 using extensions::APIPermission; |
53 | 55 |
54 namespace { | 56 namespace { |
55 const int kDefaultWidth = 512; | 57 const int kDefaultWidth = 512; |
56 const int kDefaultHeight = 384; | 58 const int kDefaultHeight = 384; |
| 59 static content::WebContentsDelegate* url_controller_for_test_ = NULL; |
57 | 60 |
58 void SuspendRenderViewHost(RenderViewHost* rvh) { | 61 void SuspendRenderViewHost(RenderViewHost* rvh) { |
59 DCHECK(rvh); | 62 DCHECK(rvh); |
60 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 63 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
61 base::Bind(&ResourceDispatcherHost::BlockRequestsForRoute, | 64 base::Bind(&ResourceDispatcherHost::BlockRequestsForRoute, |
62 base::Unretained(ResourceDispatcherHost::Get()), | 65 base::Unretained(ResourceDispatcherHost::Get()), |
63 rvh->GetProcess()->GetID(), rvh->GetRoutingID())); | 66 rvh->GetProcess()->GetID(), rvh->GetRoutingID())); |
64 } | 67 } |
65 | 68 |
66 } // namespace | 69 } // namespace |
67 | 70 |
| 71 // ExternalUrlController is a singleton class for link navigation. |
| 72 // It helps to open URL in system default browser. |
| 73 class ExternalUrlController : public content::WebContentsDelegate { |
| 74 public: |
| 75 static ExternalUrlController* GetInstance(); |
| 76 |
| 77 private: |
| 78 ExternalUrlController() {} |
| 79 friend struct DefaultSingletonTraits<ExternalUrlController>; |
| 80 |
| 81 // content::WebContentsDelegate implementation. |
| 82 virtual content::WebContents* OpenURLFromTab( |
| 83 content::WebContents* source, |
| 84 const content::OpenURLParams& params) OVERRIDE; |
| 85 |
| 86 DISALLOW_COPY_AND_ASSIGN(ExternalUrlController); |
| 87 }; |
| 88 |
| 89 // static |
| 90 ExternalUrlController* ExternalUrlController::GetInstance() { |
| 91 return Singleton<ExternalUrlController>::get(); |
| 92 } |
| 93 |
| 94 content::WebContents* ExternalUrlController::OpenURLFromTab( |
| 95 content::WebContents* source, |
| 96 const content::OpenURLParams& params) { |
| 97 // Delete useless web content first to |
| 98 // avoid a potential leak in a render process host. |
| 99 delete source; |
| 100 |
| 101 #if defined(OS_MACOSX) |
| 102 // This must run on the UI thread on OS X. |
| 103 platform_util::OpenExternal(params.url); |
| 104 #else |
| 105 // Otherwise put this work on the file thread. On Windows ShellExecute may |
| 106 // block for a significant amount of time, and it shouldn't hurt on Linux. |
| 107 BrowserThread::PostTask( |
| 108 BrowserThread::FILE, |
| 109 FROM_HERE, |
| 110 base::Bind(&platform_util::OpenExternal, params.url)); |
| 111 #endif |
| 112 |
| 113 return NULL; |
| 114 } |
| 115 |
68 ShellWindow::CreateParams::CreateParams() | 116 ShellWindow::CreateParams::CreateParams() |
69 : frame(ShellWindow::CreateParams::FRAME_CHROME), | 117 : frame(ShellWindow::CreateParams::FRAME_CHROME), |
70 bounds(-1, -1, kDefaultWidth, kDefaultHeight), | 118 bounds(-1, -1, kDefaultWidth, kDefaultHeight), |
71 restore_position(true), restore_size(true) { | 119 restore_position(true), restore_size(true) { |
72 } | 120 } |
73 | 121 |
74 ShellWindow::CreateParams::~CreateParams() { | 122 ShellWindow::CreateParams::~CreateParams() { |
75 } | 123 } |
76 | 124 |
77 ShellWindow* ShellWindow::Create(Profile* profile, | 125 ShellWindow* ShellWindow::Create(Profile* profile, |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 | 305 |
258 void ShellWindow::AddNewContents(WebContents* source, | 306 void ShellWindow::AddNewContents(WebContents* source, |
259 WebContents* new_contents, | 307 WebContents* new_contents, |
260 WindowOpenDisposition disposition, | 308 WindowOpenDisposition disposition, |
261 const gfx::Rect& initial_pos, | 309 const gfx::Rect& initial_pos, |
262 bool user_gesture, | 310 bool user_gesture, |
263 bool* was_blocked) { | 311 bool* was_blocked) { |
264 DCHECK(source == web_contents_); | 312 DCHECK(source == web_contents_); |
265 DCHECK(Profile::FromBrowserContext(new_contents->GetBrowserContext()) == | 313 DCHECK(Profile::FromBrowserContext(new_contents->GetBrowserContext()) == |
266 profile_); | 314 profile_); |
| 315 |
| 316 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) |
| 317 if (url_controller_for_test_) |
| 318 new_contents->SetDelegate(url_controller_for_test_); |
| 319 else |
| 320 new_contents->SetDelegate(ExternalUrlController::GetInstance()); |
| 321 #else |
267 Browser* browser = browser::FindOrCreateTabbedBrowser(profile_); | 322 Browser* browser = browser::FindOrCreateTabbedBrowser(profile_); |
268 // Force all links to open in a new tab, even if they were trying to open a | 323 // Force all links to open in a new tab, even if they were trying to open a |
269 // new window. | 324 // new window. |
270 disposition = | 325 disposition = |
271 disposition == NEW_BACKGROUND_TAB ? disposition : NEW_FOREGROUND_TAB; | 326 disposition == NEW_BACKGROUND_TAB ? disposition : NEW_FOREGROUND_TAB; |
272 chrome::AddWebContents(browser, NULL, new_contents, disposition, initial_pos, | 327 chrome::AddWebContents(browser, NULL, new_contents, disposition, initial_pos, |
273 user_gesture, was_blocked); | 328 user_gesture, was_blocked); |
| 329 #endif |
274 } | 330 } |
275 | 331 |
276 void ShellWindow::HandleKeyboardEvent( | 332 void ShellWindow::HandleKeyboardEvent( |
277 WebContents* source, | 333 WebContents* source, |
278 const content::NativeWebKeyboardEvent& event) { | 334 const content::NativeWebKeyboardEvent& event) { |
279 DCHECK_EQ(source, web_contents_); | 335 DCHECK_EQ(source, web_contents_); |
280 native_window_->HandleKeyboardEvent(event); | 336 native_window_->HandleKeyboardEvent(event); |
281 } | 337 } |
282 | 338 |
283 void ShellWindow::OnNativeClose() { | 339 void ShellWindow::OnNativeClose() { |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 if (window_key_.empty()) | 528 if (window_key_.empty()) |
473 return; | 529 return; |
474 | 530 |
475 extensions::ShellWindowGeometryCache* cache = | 531 extensions::ShellWindowGeometryCache* cache = |
476 extensions::ExtensionSystem::Get(profile())-> | 532 extensions::ExtensionSystem::Get(profile())-> |
477 shell_window_geometry_cache(); | 533 shell_window_geometry_cache(); |
478 | 534 |
479 gfx::Rect bounds = native_window_->GetBounds(); | 535 gfx::Rect bounds = native_window_->GetBounds(); |
480 cache->SaveGeometry(extension()->id(), window_key_, bounds); | 536 cache->SaveGeometry(extension()->id(), window_key_, bounds); |
481 } | 537 } |
| 538 |
| 539 void ShellWindow::SetExternalUrlControllerForTesting( |
| 540 content::WebContentsDelegate* controller) { |
| 541 url_controller_for_test_ = controller; |
| 542 } |
OLD | NEW |