Home | History | Annotate | Download | only in chromeos
      1 // Copyright (c) 2006-2008 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/google/google_update.h"
      6 
      7 #include "base/message_loop.h"
      8 #include "base/path_service.h"
      9 #include "base/string_util.h"
     10 #include "base/task.h"
     11 #include "base/threading/thread.h"
     12 #include "base/utf_string_conversions.h"
     13 #include "chrome/browser/chromeos/cros/cros_library.h"
     14 #include "content/browser/browser_thread.h"
     15 #include "third_party/cros/chromeos_update.h"
     16 #include "views/window/window.h"
     17 
     18 using views::Window;
     19 
     20 ////////////////////////////////////////////////////////////////////////////////
     21 // GoogleUpdate, public:
     22 
     23 GoogleUpdate::GoogleUpdate()
     24     : listener_(NULL) {
     25   chromeos::CrosLibrary::Get()->EnsureLoaded();
     26 }
     27 
     28 GoogleUpdate::~GoogleUpdate() {
     29 }
     30 
     31 ////////////////////////////////////////////////////////////////////////////////
     32 // GoogleUpdate, views::DialogDelegate implementation:
     33 
     34 void GoogleUpdate::CheckForUpdate(bool install_if_newer, Window* window) {
     35   // We need to shunt this request over to InitiateGoogleUpdateCheck and have
     36   // it run in the file thread.
     37   BrowserThread::PostTask(
     38       BrowserThread::FILE, FROM_HERE,
     39       NewRunnableMethod(
     40           this, &GoogleUpdate::InitiateGoogleUpdateCheck, install_if_newer,
     41           window, MessageLoop::current()));
     42 }
     43 
     44 ////////////////////////////////////////////////////////////////////////////////
     45 // GoogleUpdate, private:
     46 
     47 bool GoogleUpdate::InitiateGoogleUpdateCheck(bool install_if_newer,
     48                                              Window* window,
     49                                              MessageLoop* main_loop) {
     50   chromeos::UpdateInformation result;
     51   bool success = false;
     52 
     53   if (install_if_newer) {
     54     // Possible Results:
     55     //  UPGRADE_SUCCESSFUL
     56     //  UPGRADE_ALREADY_UP_TO_DATE
     57     //  UPGRADE_ERROR
     58     if (chromeos::Update) {
     59       success = chromeos::Update(&result);
     60     }
     61   } else {
     62     // Possible Results:
     63     //  UPGRADE_ALREADY_UP_TO_DATE
     64     //  UPGRADE_IS_AVAILABLE
     65     //  UPGRADE_ERROR
     66     if (chromeos::CheckForUpdate) {
     67       success = chromeos::CheckForUpdate(&result);
     68     }
     69     if (result.version_) {
     70       UTF8ToWide(result.version_, std::strlen(result.version_),
     71                  &version_available_);
     72     }
     73   }
     74 
     75   // Map chromeos::UpdateStatus to GoogleUpdateUpgradeResult
     76 
     77   GoogleUpdateUpgradeResult final = UPGRADE_ERROR;
     78 
     79   switch (result.status_) {
     80     case chromeos::UPDATE_ERROR:
     81       final = UPGRADE_ERROR;
     82       break;
     83     case chromeos::UPDATE_IS_AVAILABLE:
     84       final = UPGRADE_IS_AVAILABLE;
     85       break;
     86     case chromeos::UPDATE_SUCCESSFUL:
     87       final = UPGRADE_SUCCESSFUL;
     88       break;
     89     case chromeos::UPDATE_ALREADY_UP_TO_DATE:
     90       final = UPGRADE_ALREADY_UP_TO_DATE;
     91       break;
     92     default:
     93       // UPGRADE_ERROR
     94       break;
     95   }
     96 
     97   // Post the results as a task since this is run on a thread.
     98 
     99   main_loop->PostTask(FROM_HERE, NewRunnableMethod(this,
    100       &GoogleUpdate::ReportResults, final, success
    101       ?  GOOGLE_UPDATE_NO_ERROR : GOOGLE_UPDATE_ERROR_UPDATING));
    102 
    103   return true;
    104 }
    105 
    106 void GoogleUpdate::ReportResults(GoogleUpdateUpgradeResult results,
    107                                  GoogleUpdateErrorCode error_code) {
    108   // If we get an error, then error code must not be blank, and vice versa.
    109   DCHECK(results == UPGRADE_ERROR ? error_code != GOOGLE_UPDATE_NO_ERROR :
    110                                     error_code == GOOGLE_UPDATE_NO_ERROR);
    111   if (listener_)
    112     listener_->OnReportResults(results, error_code, version_available_);
    113 }
    114 
    115