15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_BROWSER_DIAGNOSTICS_DIAGNOSTICS_MODEL_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_DIAGNOSTICS_DIAGNOSTICS_MODEL_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
87dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include <string>
97dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "base/time/time.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace base {
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CommandLine;
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochnamespace diagnostics {
167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The chrome diagnostics system is a model-view-controller system. The Model
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// responsible for holding and running the individual tests and providing a
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// uniform interface for querying the outcome.
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DiagnosticsModel {
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A particular test can be in one of the following states.
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum TestResult {
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TEST_NOT_RUN,
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TEST_RUNNING,
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TEST_OK,
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TEST_FAIL_CONTINUE,
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TEST_FAIL_STOP,
293240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    RECOVERY_RUNNING,
303240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    RECOVERY_OK,
313240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    RECOVERY_FAIL_STOP,
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
343240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  // Number of diagnostic tests available on the current platform. To be used
353240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  // only by tests to verify that the right number of tests were run.
363240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  static const int kDiagnosticsTestCount;
373240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Observer derived form this class which provides a way to be notified of
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // changes to the model as the tests are run. For all the callbacks |id|
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is the index of the test in question and information can be obtained by
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // calling model->GetTest(id).
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class Observer {
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   public:
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual ~Observer() {}
453240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    // Called when a test has finished, regardless of outcome.
463240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    virtual void OnTestFinished(int index, DiagnosticsModel* model) = 0;
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Called once all the test are run.
483240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    virtual void OnAllTestsDone(DiagnosticsModel* model) = 0;
493240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    // Called when a recovery has finished regardless of outcome.
503240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    virtual void OnRecoveryFinished(int index, DiagnosticsModel* model) = 0;
513240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    // Called once all the recoveries are run.
523240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    virtual void OnAllRecoveryDone(DiagnosticsModel* model) = 0;
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Encapsulates what you can know about a given test.
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class TestInfo {
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   public:
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual ~TestInfo() {}
59424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // A numerical id for this test. Must be a unique number among all the
60424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // tests.
61424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    virtual int GetId() const = 0;
627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // A parse-able ASCII string that indicates what is being tested.
63424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    virtual std::string GetName() const = 0;
647dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // A human readable string that tells you what is being tested.
657dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // This is not localized: it is only meant for developer consumption.
667dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    virtual std::string GetTitle() const = 0;
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The result of running the test. If called before the test is ran the
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // answer is TEST_NOT_RUN.
697dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    virtual TestResult GetResult() const = 0;
707dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // A human readable string that tells you more about what happened. If
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // called before the test is run it returns the empty string.
727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // This is not localized: it is only meant for developer consumption.
737dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    virtual std::string GetAdditionalInfo() const = 0;
747dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // A test-specific code representing what happened. If called before the
757dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // test is run, it should return -1.
767dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    virtual int GetOutcomeCode() const = 0;
777dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // Returns the system time when the test was performed.
787dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    virtual base::Time GetStartTime() const = 0;
797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // Returns the system time when the test was finished.
807dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    virtual base::Time GetEndTime() const = 0;
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~DiagnosticsModel() {}
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns how many tests have been run.
857dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  virtual int GetTestRunCount() const = 0;
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns how many tests are available. This value never changes.
877dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  virtual int GetTestAvailableCount() const = 0;
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Runs all the available tests, the |observer| callbacks will be called as
897dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // the diagnostics progress. |observer| maybe NULL if no observation is
907dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // needed.
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void RunAll(DiagnosticsModel::Observer* observer) = 0;
923240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  // Attempt to recover from any failures discovered by testing.
933240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  virtual void RecoverAll(DiagnosticsModel::Observer* observer) = 0;
947dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Get the information for a particular test. Lifetime of returned object is
957dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // limited to the lifetime of this model.
963240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  virtual const TestInfo& GetTest(size_t index) const = 0;
97424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Get the information for a test with given numerical |id|. Lifetime of
98424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // returned object is limited to the lifetime of this model. Returns false if
99424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // there is no such id. |result| may not be NULL.
100424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  virtual bool GetTestInfo(int id, const TestInfo** result) const = 0;
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The factory for the model. The main purpose is to hide the creation of
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// different models for different platforms.
105a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)DiagnosticsModel* MakeDiagnosticsModel(const base::CommandLine& cmdline);
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1077dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}  // namespace diagnostics
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_BROWSER_DIAGNOSTICS_DIAGNOSTICS_MODEL_H_
110