1// Copyright (c) 2013 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 REMOTING_HOST_WIN_CHROMOTING_MODULE_H_
6#define REMOTING_HOST_WIN_CHROMOTING_MODULE_H_
7
8#include <atlbase.h>
9#include <atlcom.h>
10#include <atlctl.h>
11
12#include "base/basictypes.h"
13#include "base/memory/ref_counted.h"
14#include "base/win/scoped_com_initializer.h"
15
16// chromoting_lib.h contains MIDL-generated declarations.
17#include "remoting/host/chromoting_lib.h"
18
19namespace base {
20namespace win {
21class ScopedCOMInitializer;
22}  // namespace win
23}  // namespace base
24
25namespace remoting {
26
27class AutoThreadTaskRunner;
28
29// A custom version of |CAtlModuleT<>| that registers only those classes which
30// registration entries are passed to the constructor. |ChromotingModule| runs
31// |MessageLoop| allowing Chromium code to post tasks to it. Unlike
32// |CAtlExeModuleT<>|, |ChromotingModule| shuts itself down immediately once
33// the last COM object is released.
34class ChromotingModule : public ATL::CAtlModuleT<ChromotingModule> {
35 public:
36  // Initializes the module. |classes| and |classes_end| must outlive |this|.
37  ChromotingModule(ATL::_ATL_OBJMAP_ENTRY* classes,
38                   ATL::_ATL_OBJMAP_ENTRY* classes_end);
39  virtual ~ChromotingModule();
40
41  // Returns the task runner used by the module. Returns NULL if the task runner
42  // hasn't been registered yet or if the server is shutting down.
43  static scoped_refptr<AutoThreadTaskRunner> task_runner();
44
45  // Registers COM classes and runs the main message loop until there are
46  // components using it.
47  bool Run();
48
49  // ATL::CAtlModuleT<> overrides
50  virtual LONG Unlock() OVERRIDE;
51
52  DECLARE_LIBID(LIBID_ChromotingLib)
53
54 private:
55  // Registers/unregisters class objects from |classes_| - |classes_end_|.
56  HRESULT RegisterClassObjects(DWORD class_context, DWORD flags);
57  HRESULT RevokeClassObjects();
58
59  // Used to initialize COM library.
60  base::win::ScopedCOMInitializer com_initializer_;
61
62  // Point to the vector of classes registered by this module.
63  ATL::_ATL_OBJMAP_ENTRY* classes_;
64  ATL::_ATL_OBJMAP_ENTRY* classes_end_;
65
66  DISALLOW_COPY_AND_ASSIGN(ChromotingModule);
67};
68
69} // namespace remoting
70
71#endif  // REMOTING_HOST_WIN_CHROMOTING_MODULE_H_
72