summaryrefslogtreecommitdiffstats
path: root/mojo/service_manager/background_service_loader.h
blob: b5059417ccd19ab151b17c744651fd262f196ea4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MOJO_SERVICE_MANAGER_BACKGROUND_SERVICE_LOADER_H_
#define MOJO_SERVICE_MANAGER_BACKGROUND_SERVICE_LOADER_H_

#include "base/memory/scoped_ptr.h"
#include "base/threading/thread.h"
#include "mojo/service_manager/service_loader.h"

namespace mojo {

class ServiceManager;

// ServiceLoader implementation that creates a background thread and issues load
// requests there.
class MOJO_SERVICE_MANAGER_EXPORT BackgroundServiceLoader
    : public ServiceLoader {
 public:
  BackgroundServiceLoader(scoped_ptr<ServiceLoader> real_loader,
                          const char* thread_name);
  virtual ~BackgroundServiceLoader();

  // ServiceLoader overrides:
  virtual void LoadService(ServiceManager* manager,
                           const GURL& url,
                           ScopedShellHandle service_handle) OVERRIDE;
  virtual void OnServiceError(ServiceManager* manager,
                              const GURL& url) OVERRIDE;

 private:
  class BackgroundLoader;

  // These functions are exected on the background thread. They call through
  // to |background_loader_| to do the actual loading.
  // TODO: having this code take a |manager| is fragile (as ServiceManager isn't
  // thread safe).
  void LoadServiceOnBackgroundThread(ServiceManager* manager,
                                     const GURL& url,
                                     ScopedShellHandle* shell_handle);
  void OnServiceErrorOnBackgroundThread(ServiceManager* manager,
                                        const GURL& url);
  void ShutdownOnBackgroundThread();

  scoped_ptr<ServiceLoader> loader_;
  base::Thread thread_;

  // Lives on |thread_|. Trivial interface that calls through to |loader_|.
  BackgroundLoader* background_loader_;

  DISALLOW_COPY_AND_ASSIGN(BackgroundServiceLoader);
};

}  // namespace mojo

#endif  // MOJO_SERVICE_MANAGER_BACKGROUND_SERVICE_LOADER_H_