summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/web_intent_callbacks.h
blob: 067d825d5a27cf442905e4a5f2aeda83bdd66575 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) 2012 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 CHROME_BROWSER_EXTENSIONS_WEB_INTENT_CALLBACKS_H_
#define CHROME_BROWSER_EXTENSIONS_WEB_INTENT_CALLBACKS_H_

#include "base/memory/singleton.h"
#include "chrome/browser/profiles/profile_keyed_service.h"
#include "chrome/browser/profiles/profile_keyed_service_factory.h"

class Profile;

namespace content {
class WebContents;
class WebIntentsDispatcher;
}

namespace extensions {
class Extension;

// The WebIntentCallbacks class tracks the pending callbacks for web intents
// that have been delivered to packaged apps (i.e., new-style apps containing
// shell windows), for a particular profile.
class WebIntentCallbacks : public ProfileKeyedService {
 public:
  WebIntentCallbacks();
  virtual ~WebIntentCallbacks();

  // Returns the instance for the given profile. This is a convenience wrapper
  // around WebIntentCallbacks::Factory::GetForProfile.
  static WebIntentCallbacks* Get(Profile* profile);

  // Registers the callback for the Web Intent we're about to dispatch to the
  // given extension. Returns an identifier that should later be dispatched to
  // RetrieveCallback in order to invoke the callback registered here.
  // This transfers ownership of WebIntentsDispatcher to this class.
  int RegisterCallback(const Extension* extension,
                       content::WebIntentsDispatcher* dispatcher,
                       content::WebContents* source);

  // Retrieves the callback for the given identifier, for a response from the
  // specified extension. This will clear the callback. If there is no callback
  // registered under this identifier, then this will return NULL. This
  // transfers ownership of WebIntentsDispatcher to the caller.
  content::WebIntentsDispatcher* RetrieveCallback(const Extension* extension,
                                                  int intent_id);

 private:
  typedef std::map<std::string, content::WebIntentsDispatcher*> CallbackMap;

  class Factory : public ProfileKeyedServiceFactory {
   public:
    static WebIntentCallbacks* GetForProfile(Profile* profile);

    static Factory* GetInstance();
   private:
    friend struct DefaultSingletonTraits<Factory>;

    Factory();
    virtual ~Factory();

    // ProfileKeyedServiceFactory
    virtual ProfileKeyedService* BuildServiceInstanceFor(
        Profile* profile) const OVERRIDE;
  };

  // Private method to get and clear the dispatcher for the given string key.
  // If there is no dispatcher available, this will return NULL. Otherwise, this
  // transfers ownership of the WebIntentsDispatcher to the caller.
  content::WebIntentsDispatcher* GetAndClear(const std::string& key);

  class SourceObserver;

  // Used as an incrementing ID for callback keys.
  int last_id_;

  // Stores all pending callbacks sent to platform apps.
  CallbackMap pending_;
};

}  // namespace extensions

#endif  // CHROME_BROWSER_EXTENSIONS_WEB_INTENT_CALLBACKS_H_