Home | History | Annotate | Download | only in cros
      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_CHROMEOS_CROS_CROS_LIBRARY_H_
      6 #define CHROME_BROWSER_CHROMEOS_CROS_CROS_LIBRARY_H_
      7 #pragma once
      8 
      9 #include <string>
     10 #include "base/basictypes.h"
     11 #include "base/command_line.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "chrome/common/chrome_switches.h"
     14 
     15 namespace base {
     16 template <typename T> struct DefaultLazyInstanceTraits;
     17 }
     18 
     19 namespace chromeos {
     20 
     21 class BrightnessLibrary;
     22 class BurnLibrary;
     23 class CryptohomeLibrary;
     24 class InputMethodLibrary;
     25 class LibCrosServiceLibrary;
     26 class LibraryLoader;
     27 class LoginLibrary;
     28 class MountLibrary;
     29 class NetworkLibrary;
     30 class PowerLibrary;
     31 class ScreenLockLibrary;
     32 class SpeechSynthesisLibrary;
     33 class SyslogsLibrary;
     34 class TouchpadLibrary;
     35 class UpdateLibrary;
     36 
     37 // This class handles access to sub-parts of ChromeOS library. it provides
     38 // a level of indirection so individual libraries that it exposes can
     39 // be mocked for testing.
     40 class CrosLibrary {
     41  public:
     42   // This class provides access to internal members of CrosLibrary class for
     43   // purpose of testing (i.e. replacement of members' implementation with
     44   // mock objects).
     45   class TestApi {
     46    public:
     47     // Use the stub implementations of the library. This is mainly for
     48     // running the chromeos build of chrome on the desktop.
     49     void SetUseStubImpl();
     50 
     51     // Reset the stub implementations of the library, called after
     52     // SetUseStubImp is called.
     53     void ResetUseStubImpl();
     54 
     55     // Passing true for own for these setters will cause them to be deleted
     56     // when the CrosLibrary is deleted (or other mocks are set).
     57     // Setter for LibraryLoader.
     58     void SetLibraryLoader(LibraryLoader* loader, bool own);
     59     void SetBrightnessLibrary(BrightnessLibrary* library, bool own);
     60     void SetBurnLibrary(BurnLibrary* library, bool own);
     61     void SetCryptohomeLibrary(CryptohomeLibrary* library, bool own);
     62     void SetInputMethodLibrary(InputMethodLibrary* library, bool own);
     63     void SetLibCrosServiceLibrary(LibCrosServiceLibrary* library, bool own);
     64     void SetLoginLibrary(LoginLibrary* library, bool own);
     65     void SetMountLibrary(MountLibrary* library, bool own);
     66     void SetNetworkLibrary(NetworkLibrary* library, bool own);
     67     void SetPowerLibrary(PowerLibrary* library, bool own);
     68     void SetScreenLockLibrary(ScreenLockLibrary* library, bool own);
     69     void SetSpeechSynthesisLibrary(SpeechSynthesisLibrary* library, bool own);
     70     void SetSyslogsLibrary(SyslogsLibrary* library, bool own);
     71     void SetTouchpadLibrary(TouchpadLibrary* library, bool own);
     72     void SetUpdateLibrary(UpdateLibrary* library, bool own);
     73 
     74    private:
     75     friend class CrosLibrary;
     76     explicit TestApi(CrosLibrary* library) : library_(library) {}
     77     CrosLibrary* library_;
     78   };
     79 
     80   // This gets the CrosLibrary.
     81   static CrosLibrary* Get();
     82 
     83   BrightnessLibrary* GetBrightnessLibrary();
     84   BurnLibrary* GetBurnLibrary();
     85   CryptohomeLibrary* GetCryptohomeLibrary();
     86   InputMethodLibrary* GetInputMethodLibrary();
     87   LibCrosServiceLibrary* GetLibCrosServiceLibrary();
     88   LoginLibrary* GetLoginLibrary();
     89   MountLibrary* GetMountLibrary();
     90   NetworkLibrary* GetNetworkLibrary();
     91   PowerLibrary* GetPowerLibrary();
     92   ScreenLockLibrary* GetScreenLockLibrary();
     93   SpeechSynthesisLibrary* GetSpeechSynthesisLibrary();
     94   SyslogsLibrary* GetSyslogsLibrary();
     95   TouchpadLibrary* GetTouchpadLibrary();
     96   UpdateLibrary* GetUpdateLibrary();
     97 
     98   // Getter for Test API that gives access to internal members of this class.
     99   TestApi* GetTestApi();
    100 
    101   // Ensures that the library is loaded, loading it if needed. If the library
    102   // could not be loaded, returns false.
    103   bool EnsureLoaded();
    104 
    105   // Returns an unlocalized string describing the last load error (if any).
    106   const std::string& load_error_string() {
    107     return load_error_string_;
    108   }
    109 
    110  private:
    111   friend struct base::DefaultLazyInstanceTraits<chromeos::CrosLibrary>;
    112   friend class CrosLibrary::TestApi;
    113 
    114   CrosLibrary();
    115   virtual ~CrosLibrary();
    116 
    117   LibraryLoader* library_loader_;
    118 
    119   bool own_library_loader_;
    120 
    121   // This template supports the creation, setting and optional deletion of
    122   // the cros libraries.
    123   template <class L>
    124   class Library {
    125    public:
    126     Library() : library_(NULL), own_(true) {}
    127 
    128     ~Library() {
    129       if (own_)
    130         delete library_;
    131     }
    132 
    133     L* GetDefaultImpl(bool use_stub_impl) {
    134       if (!library_) {
    135         own_ = true;
    136         if (CommandLine::ForCurrentProcess()->HasSwitch(
    137             switches::kForceStubLibcros))
    138           use_stub_impl = true;
    139         library_ = L::GetImpl(use_stub_impl);
    140       }
    141       return library_;
    142     }
    143 
    144     void SetImpl(L* library, bool own) {
    145       if (library != library_) {
    146         if (own_)
    147           delete library_;
    148         library_ = library;
    149         own_ = own;
    150       }
    151     }
    152 
    153    private:
    154     L* library_;
    155     bool own_;
    156   };
    157 
    158   Library<BrightnessLibrary> brightness_lib_;
    159   Library<BurnLibrary> burn_lib_;
    160   Library<CryptohomeLibrary> crypto_lib_;
    161   Library<InputMethodLibrary> input_method_lib_;
    162   Library<LibCrosServiceLibrary> libcros_service_lib_;
    163   Library<LoginLibrary> login_lib_;
    164   Library<MountLibrary> mount_lib_;
    165   Library<NetworkLibrary> network_lib_;
    166   Library<PowerLibrary> power_lib_;
    167   Library<ScreenLockLibrary> screen_lock_lib_;
    168   Library<SpeechSynthesisLibrary> speech_synthesis_lib_;
    169   Library<SyslogsLibrary> syslogs_lib_;
    170   Library<TouchpadLibrary> touchpad_lib_;
    171   Library<UpdateLibrary> update_lib_;
    172 
    173   // Stub implementations of the libraries should be used.
    174   bool use_stub_impl_;
    175   // True if libcros was successfully loaded.
    176   bool loaded_;
    177   // True if the last load attempt had an error.
    178   bool load_error_;
    179   // Contains the error string from the last load attempt.
    180   std::string load_error_string_;
    181   scoped_ptr<TestApi> test_api_;
    182 
    183   DISALLOW_COPY_AND_ASSIGN(CrosLibrary);
    184 };
    185 
    186 // The class is used for enabling the stub libcros, and cleaning it up at
    187 // the end of the object lifetime. Useful for testing.
    188 class ScopedStubCrosEnabler {
    189  public:
    190   ScopedStubCrosEnabler() {
    191     chromeos::CrosLibrary::Get()->GetTestApi()->SetUseStubImpl();
    192   }
    193 
    194   ~ScopedStubCrosEnabler() {
    195     chromeos::CrosLibrary::Get()->GetTestApi()->ResetUseStubImpl();
    196   }
    197 
    198  private:
    199   DISALLOW_COPY_AND_ASSIGN(ScopedStubCrosEnabler);
    200 };
    201 
    202 }  // namespace chromeos
    203 
    204 #endif  // CHROME_BROWSER_CHROMEOS_CROS_CROS_LIBRARY_H_
    205