• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2010 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 "net/base/android_network_library.h"
6 
7 #include "base/lazy_instance.h"
8 #include "base/logging.h"
9 #include "base/synchronization/lock.h"
10 
11 using base::Lock;
12 using base::AutoLock;
13 
14 namespace net {
15 
16 class LibHolder {
17  public:
LibHolder()18   LibHolder() : lib_(NULL) {}
~LibHolder()19   ~LibHolder() {
20     Reset();
21   }
Register(AndroidNetworkLibrary * lib)22   void Register(AndroidNetworkLibrary* lib) {
23     AutoLock lock(lock_);
24     if (lib_) {
25       LOG(WARNING) << "Ignoring duplicate call " << lib;
26       delete lib;
27       return;
28     }
29     lib_ = lib;
30   }
Reset()31   void Reset() {
32     AutoLock lock(lock_);
33     delete lib_;
34     lib_ = NULL;
35   }
GetLibrary()36   AndroidNetworkLibrary* GetLibrary() {
37     AutoLock lock(lock_);
38     return lib_;
39   }
40 
41  private:
42   AndroidNetworkLibrary* lib_;
43   Lock lock_;
44 };
45 
46 base::LazyInstance<LibHolder> g_holder(base::LINKER_INITIALIZED);
47 
48 // static
RegisterSharedInstance(AndroidNetworkLibrary * lib)49 void AndroidNetworkLibrary::RegisterSharedInstance(AndroidNetworkLibrary* lib) {
50   g_holder.Get().Register(lib);
51 }
52 
53 // static
UnregisterSharedInstance()54 void AndroidNetworkLibrary::UnregisterSharedInstance() {
55   g_holder.Get().Reset();
56 }
57 
58 // static
GetSharedInstance()59 AndroidNetworkLibrary* AndroidNetworkLibrary::GetSharedInstance() {
60   return g_holder.Get().GetLibrary();
61 }
62 
AndroidNetworkLibrary()63 AndroidNetworkLibrary::AndroidNetworkLibrary() {
64 }
65 
~AndroidNetworkLibrary()66 AndroidNetworkLibrary::~AndroidNetworkLibrary() {
67 }
68 
69 }  // namespace net
70