platform_util_chromeos.cc revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2009 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.
4
5#include "chrome/browser/platform_util.h"
6
7#include <gtk/gtk.h>
8
9#include "app/l10n_util.h"
10#include "app/gtk_util.h"
11#include "base/file_util.h"
12#include "base/process_util.h"
13#include "base/task.h"
14#include "base/utf_string_conversions.h"
15#include "chrome/common/process_watcher.h"
16#include "googleurl/src/gurl.h"
17#include "grit/generated_resources.h"
18
19#include "chrome/browser/chrome_thread.h"
20#include "chrome/browser/browser_list.h"
21#include "chrome/browser/dom_ui/filebrowse_ui.h"
22#include "chrome/browser/dom_ui/mediaplayer_ui.h"
23
24class Profile;
25
26namespace platform_util {
27
28static const std::string kGmailComposeUrl =
29    "https://mail.google.com/mail/?extsrc=mailto&url=";
30
31// Opens file browser on UI thread.
32void OpenFileBrowserOnUIThread(const FilePath& dir) {
33  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
34
35  Profile* profile;
36  profile = BrowserList::GetLastActive()->profile();
37
38  FileBrowseUI::OpenPopup(profile,
39                          dir.value(),
40                          FileBrowseUI::kPopupWidth,
41                          FileBrowseUI::kPopupHeight);
42}
43
44// TODO(estade): It would be nice to be able to select the file in the file
45// manager, but that probably requires extending xdg-open. For now just
46// show the folder.
47void ShowItemInFolder(const FilePath& full_path) {
48  FilePath dir = full_path.DirName();
49  if (!file_util::DirectoryExists(dir))
50    return;
51
52  if (ChromeThread::CurrentlyOn(ChromeThread::UI)) {
53    OpenFileBrowserOnUIThread(dir);
54  } else {
55    ChromeThread::PostTask(
56        ChromeThread::UI, FROM_HERE,
57        NewRunnableFunction(&OpenFileBrowserOnUIThread, dir));
58  }
59}
60
61void OpenItem(const FilePath& full_path) {
62  std::string ext = full_path.Extension();
63  // For things supported natively by the browser, we should open it
64  // in a tab.
65  if (ext == ".jpg" ||
66      ext == ".jpeg" ||
67      ext == ".png" ||
68      ext == ".gif" ||
69      ext == ".txt" ||
70      ext == ".html" ||
71      ext == ".htm") {
72    std::string path;
73    path = "file://";
74    path.append(full_path.value());
75    if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) {
76      bool result = ChromeThread::PostTask(
77          ChromeThread::UI, FROM_HERE,
78          NewRunnableFunction(&OpenItem, full_path));
79      DCHECK(result);
80      return;
81    }
82    Browser* browser = BrowserList::GetLastActive();
83    browser->AddTabWithURL(
84        GURL(path), GURL(), PageTransition::LINK, -1,
85        TabStripModel::ADD_SELECTED, NULL, std::string());
86    return;
87  }
88  if (ext == ".avi" ||
89      ext == ".wav" ||
90      ext == ".mp4" ||
91      ext == ".mp3" ||
92      ext == ".mkv" ||
93      ext == ".ogg") {
94    MediaPlayer* mediaplayer = MediaPlayer::Get();
95    std::string url = "file://";
96    url += full_path.value();
97    GURL gurl(url);
98    mediaplayer->EnqueueMediaURL(gurl, NULL);
99    return;
100  }
101
102  // Unknwon file type. Show an error message to user.
103  ChromeThread::PostTask(
104      ChromeThread::UI, FROM_HERE,
105      NewRunnableFunction(
106          &SimpleErrorBox,
107          static_cast<gfx::NativeWindow>(NULL),
108          l10n_util::GetStringUTF16(IDS_FILEBROWSER_ERROR_TITLE),
109          l10n_util::GetStringFUTF16(IDS_FILEBROWSER_ERROR_UNKNOWN_FILE_TYPE,
110                                     UTF8ToUTF16(full_path.BaseName().value()))
111          ));
112}
113
114static void OpenURL(const std::string& url) {
115  Browser* browser = BrowserList::GetLastActive();
116  browser->AddTabWithURL(
117      GURL(url), GURL(), PageTransition::LINK, -1,
118      TabStripModel::ADD_SELECTED, NULL, std::string());
119}
120
121void OpenExternal(const GURL& url) {
122  if (url.SchemeIs("mailto")) {
123    std::string string_url = kGmailComposeUrl;
124    string_url.append(url.spec());
125    ChromeThread::PostTask(
126        ChromeThread::UI, FROM_HERE, NewRunnableFunction(OpenURL, string_url));
127  }
128}
129
130}  // namespace platform_util
131