Home | History | Annotate | Download | only in diagnostics
      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 #ifndef CHROME_BROWSER_DIAGNOSTICS_DIAGNOSTICS_MODEL_H_
      6 #define CHROME_BROWSER_DIAGNOSTICS_DIAGNOSTICS_MODEL_H_
      7 
      8 #include <string>
      9 #include "base/time/time.h"
     10 
     11 class CommandLine;
     12 
     13 namespace diagnostics {
     14 
     15 // The chrome diagnostics system is a model-view-controller system. The Model
     16 // responsible for holding and running the individual tests and providing a
     17 // uniform interface for querying the outcome.
     18 class DiagnosticsModel {
     19  public:
     20   // A particular test can be in one of the following states.
     21   enum TestResult {
     22     TEST_NOT_RUN,
     23     TEST_RUNNING,
     24     TEST_OK,
     25     TEST_FAIL_CONTINUE,
     26     TEST_FAIL_STOP,
     27     RECOVERY_RUNNING,
     28     RECOVERY_OK,
     29     RECOVERY_FAIL_STOP,
     30   };
     31 
     32   // Number of diagnostic tests available on the current platform. To be used
     33   // only by tests to verify that the right number of tests were run.
     34   static const int kDiagnosticsTestCount;
     35 
     36   // Observer derived form this class which provides a way to be notified of
     37   // changes to the model as the tests are run. For all the callbacks |id|
     38   // is the index of the test in question and information can be obtained by
     39   // calling model->GetTest(id).
     40   class Observer {
     41    public:
     42     virtual ~Observer() {}
     43     // Called when a test has finished, regardless of outcome.
     44     virtual void OnTestFinished(int index, DiagnosticsModel* model) = 0;
     45     // Called once all the test are run.
     46     virtual void OnAllTestsDone(DiagnosticsModel* model) = 0;
     47     // Called when a recovery has finished regardless of outcome.
     48     virtual void OnRecoveryFinished(int index, DiagnosticsModel* model) = 0;
     49     // Called once all the recoveries are run.
     50     virtual void OnAllRecoveryDone(DiagnosticsModel* model) = 0;
     51   };
     52 
     53   // Encapsulates what you can know about a given test.
     54   class TestInfo {
     55    public:
     56     virtual ~TestInfo() {}
     57     // A parse-able ASCII string that indicates what is being tested.
     58     virtual std::string GetId() const = 0;
     59     // A human readable string that tells you what is being tested.
     60     // This is not localized: it is only meant for developer consumption.
     61     virtual std::string GetTitle() const = 0;
     62     // The result of running the test. If called before the test is ran the
     63     // answer is TEST_NOT_RUN.
     64     virtual TestResult GetResult() const = 0;
     65     // A human readable string that tells you more about what happened. If
     66     // called before the test is run it returns the empty string.
     67     // This is not localized: it is only meant for developer consumption.
     68     virtual std::string GetAdditionalInfo() const = 0;
     69     // A test-specific code representing what happened. If called before the
     70     // test is run, it should return -1.
     71     virtual int GetOutcomeCode() const = 0;
     72     // Returns the system time when the test was performed.
     73     virtual base::Time GetStartTime() const = 0;
     74     // Returns the system time when the test was finished.
     75     virtual base::Time GetEndTime() const = 0;
     76   };
     77 
     78   virtual ~DiagnosticsModel() {}
     79   // Returns how many tests have been run.
     80   virtual int GetTestRunCount() const = 0;
     81   // Returns how many tests are available. This value never changes.
     82   virtual int GetTestAvailableCount() const = 0;
     83   // Runs all the available tests, the |observer| callbacks will be called as
     84   // the diagnostics progress. |observer| maybe NULL if no observation is
     85   // needed.
     86   virtual void RunAll(DiagnosticsModel::Observer* observer) = 0;
     87   // Attempt to recover from any failures discovered by testing.
     88   virtual void RecoverAll(DiagnosticsModel::Observer* observer) = 0;
     89   // Get the information for a particular test. Lifetime of returned object is
     90   // limited to the lifetime of this model.
     91   virtual const TestInfo& GetTest(size_t index) const = 0;
     92   // Get the information for a test with given |id|. Lifetime of returned object
     93   // is limited to the lifetime of this model. Returns false if there is no such
     94   // id. |result| may not be NULL.
     95   virtual bool GetTestInfo(const std::string& id,
     96                            const TestInfo** result) const = 0;
     97 };
     98 
     99 // The factory for the model. The main purpose is to hide the creation of
    100 // different models for different platforms.
    101 DiagnosticsModel* MakeDiagnosticsModel(const CommandLine& cmdline);
    102 
    103 }  // namespace diagnostics
    104 
    105 #endif  // CHROME_BROWSER_DIAGNOSTICS_DIAGNOSTICS_MODEL_H_
    106