Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / content / browser / device_orientation / provider.h
blob4bc0635fda6c6453b8de4a9c79e42f9fdd2821f5
1 // Copyright (c) 2012 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.
5 #ifndef CONTENT_BROWSER_DEVICE_ORIENTATION_PROVIDER_H_
6 #define CONTENT_BROWSER_DEVICE_ORIENTATION_PROVIDER_H_
8 #include "base/memory/ref_counted.h"
9 #include "content/browser/device_orientation/device_data.h"
10 #include "content/common/content_export.h"
12 namespace content {
14 class CONTENT_EXPORT Provider : public base::RefCountedThreadSafe<Provider> {
15 public:
16 class Observer {
17 public:
18 // Called when device data changes.
19 // An Observer must not synchronously call Provider::RemoveObserver
20 // or Provider::AddObserver when this is called.
21 virtual void OnDeviceDataUpdate(const DeviceData* device_data,
22 DeviceData::Type device_data_type) = 0;
23 DeviceData::Type device_data_type() { return device_data_type_; }
25 protected:
26 Observer(DeviceData::Type device_data_type)
27 : device_data_type_(device_data_type) {
29 virtual ~Observer() {}
31 private:
32 // Each Observer observes exactly one type of DeviceData.
33 DeviceData::Type device_data_type_;
36 // Returns a pointer to the singleton instance of this class.
37 // The caller should store the returned pointer in a scoped_refptr.
38 // The Provider instance is lazily constructed when GetInstance() is called,
39 // and destructed when the last scoped_refptr referring to it is destructed.
40 static Provider* GetInstance();
42 // Inject a mock Provider for testing. Only a weak pointer to the injected
43 // object will be held by Provider, i.e. it does not itself contribute to the
44 // injected object's reference count.
45 static void SetInstanceForTests(Provider* provider);
47 // Get the current instance. Used for testing.
48 static Provider* GetInstanceForTests();
50 // Note: AddObserver may call back synchronously to the observer with data.
51 virtual void AddObserver(Observer* observer) = 0;
52 virtual void RemoveObserver(Observer* observer) = 0;
54 protected:
55 Provider();
56 virtual ~Provider();
58 private:
59 friend class base::RefCountedThreadSafe<Provider>;
60 static Provider* instance_;
62 DISALLOW_COPY_AND_ASSIGN(Provider);
65 } // namespace content
67 #endif // CONTENT_BROWSER_DEVICE_ORIENTATION_PROVIDER_H_