Home | History | Annotate | Download | only in browser
      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
      3 // found in the LICENSE file.
      4 
      5 #include <unistd.h>
      6 
      7 #include "base/command_line.h"
      8 #include "base/environment.h"
      9 #include "base/file_path.h"
     10 #include "base/file_util.h"
     11 #include "base/logging.h"
     12 #include "base/nix/xdg_util.h"
     13 #include "base/task.h"
     14 #include "chrome/browser/background_mode_manager.h"
     15 #include "chrome/browser/shell_integration.h"
     16 #include "chrome/browser/ui/gtk/gtk_util.h"
     17 #include "chrome/common/auto_start_linux.h"
     18 #include "chrome/common/chrome_switches.h"
     19 #include "chrome/common/chrome_version_info.h"
     20 #include "content/browser/browser_thread.h"
     21 #include "grit/generated_resources.h"
     22 #include "ui/base/l10n/l10n_util.h"
     23 
     24 namespace {
     25 
     26 class DisableLaunchOnStartupTask : public Task {
     27  public:
     28   virtual void Run();
     29 };
     30 
     31 class EnableLaunchOnStartupTask : public Task {
     32  public:
     33   virtual void Run();
     34 };
     35 
     36 }  // namespace
     37 
     38 void BackgroundModeManager::EnableLaunchOnStartup(bool should_launch) {
     39   // This functionality is only defined for default profile, currently.
     40   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUserDataDir))
     41     return;
     42   if (should_launch) {
     43     BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
     44                             new EnableLaunchOnStartupTask());
     45   } else {
     46     BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
     47                             new DisableLaunchOnStartupTask());
     48   }
     49 }
     50 
     51 void DisableLaunchOnStartupTask::Run() {
     52   scoped_ptr<base::Environment> environment(base::Environment::Create());
     53   if (!AutoStart::Remove(ShellIntegration::GetDesktopName(environment.get()))) {
     54     NOTREACHED() << "Failed to deregister launch on login.";
     55   }
     56 }
     57 
     58 // TODO(rickcam): Bug 56280: Share implementation with ShellIntegration
     59 void EnableLaunchOnStartupTask::Run() {
     60   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
     61   scoped_ptr<base::Environment> environment(base::Environment::Create());
     62   scoped_ptr<chrome::VersionInfo> version_info(new chrome::VersionInfo());
     63 
     64   std::string wrapper_script;
     65   if (!environment->GetVar("CHROME_WRAPPER", &wrapper_script)) {
     66     LOG(WARNING)
     67         << "Failed to register launch on login.  CHROME_WRAPPER not set.";
     68     return;
     69   }
     70   std::string command_line = wrapper_script +
     71       " --" + switches::kNoStartupWindow;
     72   if (!AutoStart::AddApplication(
     73           ShellIntegration::GetDesktopName(environment.get()),
     74           version_info->Name(),
     75           command_line,
     76           false)) {
     77     NOTREACHED() << "Failed to register launch on login.";
     78   }
     79 }
     80 
     81 void BackgroundModeManager::DisplayAppInstalledNotification(
     82     const Extension* extension) {
     83   // TODO(atwilson): Display a platform-appropriate notification here.
     84   // http://crbug.com/74970
     85 }
     86 
     87 string16 BackgroundModeManager::GetPreferencesMenuLabel() {
     88   string16 result = gtk_util::GetStockPreferencesMenuLabel();
     89   if (!result.empty())
     90     return result;
     91   return l10n_util::GetStringUTF16(IDS_PREFERENCES);
     92 }
     93