| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/platform_util.h" | 5 #include "chrome/browser/platform_util.h" |
| 6 | 6 |
| 7 #include "base/bind.h" |
| 7 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 8 #include "base/process_util.h" | 9 #include "base/process_util.h" |
| 9 #include "base/task.h" | 10 #include "base/task.h" |
| 10 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/browser/extensions/file_manager_util.h" | 12 #include "chrome/browser/extensions/file_manager_util.h" |
| 12 #include "chrome/browser/tabs/tab_strip_model.h" | 13 #include "chrome/browser/tabs/tab_strip_model.h" |
| 13 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/browser_list.h" | 15 #include "chrome/browser/ui/browser_list.h" |
| 15 #include "content/common/process_watcher.h" | 16 #include "content/common/process_watcher.h" |
| 16 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 17 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
| 18 | 19 |
| 19 using content::BrowserThread; | 20 using content::BrowserThread; |
| 20 | 21 |
| 21 class Profile; | 22 class Profile; |
| 22 | 23 |
| 23 namespace platform_util { | 24 namespace { |
| 24 | 25 |
| 25 static const std::string kGmailComposeUrl = | 26 const char kGmailComposeUrl[] = |
| 26 "https://mail.google.com/mail/?extsrc=mailto&url="; | 27 "https://mail.google.com/mail/?extsrc=mailto&url="; |
| 27 | 28 |
| 28 // Opens file browser on UI thread. | |
| 29 static | |
| 30 void OpenFileBrowserOnUIThread(const FilePath& dir) { | 29 void OpenFileBrowserOnUIThread(const FilePath& dir) { |
| 31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 32 | 31 |
| 33 Browser* browser = BrowserList::GetLastActive(); | 32 Browser* browser = BrowserList::GetLastActive(); |
| 34 if (!browser) | 33 if (!browser) |
| 35 return; | 34 return; |
| 36 | 35 |
| 37 FilePath virtual_path; | 36 FilePath virtual_path; |
| 38 if (!FileManagerUtil::ConvertFileToRelativeFileSystemPath(browser->profile(), | 37 if (!FileManagerUtil::ConvertFileToRelativeFileSystemPath(browser->profile(), |
| 39 dir, | 38 dir, |
| 40 &virtual_path)) { | 39 &virtual_path)) { |
| 41 return; | 40 return; |
| 42 } | 41 } |
| 43 | 42 |
| 44 GURL url = FileManagerUtil::GetFileBrowserUrlWithParams( | 43 GURL url = FileManagerUtil::GetFileBrowserUrlWithParams( |
| 45 SelectFileDialog::SELECT_NONE, string16(), virtual_path, NULL, 0, | 44 SelectFileDialog::SELECT_NONE, string16(), virtual_path, NULL, 0, |
| 46 FilePath::StringType()); | 45 FilePath::StringType()); |
| 47 browser->ShowSingletonTab(url); | 46 browser->ShowSingletonTab(url); |
| 48 } | 47 } |
| 49 | 48 |
| 50 void ShowItemInFolder(const FilePath& full_path) { | 49 // file_util::DirectoryExists must be called on the FILE thread. |
| 50 void ShowItemInFolderOnFileThread(const FilePath& full_path) { |
| 51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 51 FilePath dir = full_path.DirName(); | 52 FilePath dir = full_path.DirName(); |
| 52 if (!file_util::DirectoryExists(dir)) | 53 if (file_util::DirectoryExists(dir)) { |
| 53 return; | 54 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 54 | 55 base::Bind(&OpenFileBrowserOnUIThread, dir)); |
| 55 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 56 OpenFileBrowserOnUIThread(dir); | |
| 57 } else { | |
| 58 BrowserThread::PostTask( | |
| 59 BrowserThread::UI, FROM_HERE, | |
| 60 NewRunnableFunction(&OpenFileBrowserOnUIThread, dir)); | |
| 61 } | 56 } |
| 62 } | 57 } |
| 63 | 58 |
| 64 void OpenItem(const FilePath& full_path) { | 59 void OpenItemOnFileThread(const FilePath& full_path) { |
| 65 FileManagerUtil::ViewItem(full_path, false); | 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 61 base::Closure callback; |
| 62 if (file_util::DirectoryExists(full_path)) |
| 63 callback = base::Bind(&FileManagerUtil::ViewFolder, full_path); |
| 64 else |
| 65 callback = base::Bind(&FileManagerUtil::ViewItem, full_path, false); |
| 66 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback); |
| 66 } | 67 } |
| 67 | 68 |
| 68 static void OpenURL(const std::string& url) { | 69 void OpenURL(const std::string& url) { |
| 69 Browser* browser = BrowserList::GetLastActive(); | 70 Browser* browser = BrowserList::GetLastActive(); |
| 70 browser->AddSelectedTabWithURL(GURL(url), content::PAGE_TRANSITION_LINK); | 71 browser->AddSelectedTabWithURL(GURL(url), content::PAGE_TRANSITION_LINK); |
| 71 } | 72 } |
| 72 | 73 |
| 74 } // namespace |
| 75 |
| 76 namespace platform_util { |
| 77 |
| 78 void ShowItemInFolder(const FilePath& full_path) { |
| 79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 80 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 81 base::Bind(&ShowItemInFolderOnFileThread, full_path)); |
| 82 } |
| 83 |
| 84 void OpenItem(const FilePath& full_path) { |
| 85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 86 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 87 base::Bind(&OpenItemOnFileThread, full_path)); |
| 88 } |
| 89 |
| 73 void OpenExternal(const GURL& url) { | 90 void OpenExternal(const GURL& url) { |
| 74 if (url.SchemeIs("mailto")) { | 91 if (url.SchemeIs("mailto")) { |
| 75 std::string string_url = kGmailComposeUrl; | 92 std::string string_url = kGmailComposeUrl; |
| 76 string_url.append(url.spec()); | 93 string_url.append(url.spec()); |
| 77 BrowserThread::PostTask( | 94 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 78 BrowserThread::UI, FROM_HERE, NewRunnableFunction(OpenURL, string_url)); | 95 base::Bind(OpenURL, string_url)); |
| 79 } | 96 } |
| 80 } | 97 } |
| 81 | 98 |
| 82 } // namespace platform_util | 99 } // namespace platform_util |
| OLD | NEW |