summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/web_intent_callbacks.cc
blob: 38708b4523adb7ecbb1a9de01ac6b54e0980816c (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// 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.

#include "base/stringprintf.h"
#include "chrome/browser/extensions/web_intent_callbacks.h"
#include "chrome/browser/profiles/profile_dependency_manager.h"
#include "chrome/common/extensions/extension.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_intents_dispatcher.h"

namespace {

// Get the key for use inside the internal pending WebIntentCallbacks map.
std::string GetKey(const extensions::Extension* extension, int id) {
  return StringPrintf("%s/%d", extension->id().c_str(), id);
}

}  // namespace

namespace extensions {

// SourceObserver is a subclass of WebContentsObserver that is instantiated
// on RegisterCallback to wait for the source WebContents to be destroyed.
// If it is destroyed, this automatically clears the callback from pending_.
class WebIntentCallbacks::SourceObserver : content::WebContentsObserver {
 public:
  SourceObserver(content::WebContents* web_contents,
                 WebIntentCallbacks* callbacks,
                 const std::string& key)
      : content::WebContentsObserver(web_contents),
        callbacks_(callbacks),
        key_(key) {}
  virtual ~SourceObserver() {}

  // Implement WebContentsObserver
  virtual void WebContentsDestroyed(content::WebContents* web_contents)
      OVERRIDE {
    content::WebIntentsDispatcher* dispatcher =
        callbacks_->GetAndClear(key_);
    if (dispatcher)
      delete dispatcher;

    delete this;
  }

 private:
  WebIntentCallbacks* callbacks_;
  const std::string key_;
};

WebIntentCallbacks::WebIntentCallbacks() : last_id_(0) {}

WebIntentCallbacks::~WebIntentCallbacks() {}

// static
WebIntentCallbacks* WebIntentCallbacks::Get(Profile* profile) {
  return Factory::GetForProfile(profile);
}

int WebIntentCallbacks::RegisterCallback(
    const Extension* extension,
    content::WebIntentsDispatcher* dispatcher,
    content::WebContents* source) {
  int id = ++last_id_;
  std::string key = GetKey(extension, id);
  pending_[key] = dispatcher;

  // TODO(thorogood): We should also listen for extension suspend events, and
  // clear the relevant callback (i.e., the handler of this intent)

  // TODO(thorogood): The source window may actually be a ShellWindow of a
  // platform app. In this case, we should not wait for its destruction:
  // rather, we should be listening to platform app suspend events.

  // Listen to when the source WebContent is destroyed. This object is self-
  // deleting on this event.
  DCHECK(source);
  new SourceObserver(source, this, key);

  return id;
}

content::WebIntentsDispatcher* WebIntentCallbacks::RetrieveCallback(
    const Extension* extension, int id) {
  std::string key = GetKey(extension, id);
  return GetAndClear(key);
}

content::WebIntentsDispatcher* WebIntentCallbacks::GetAndClear(
    const std::string& key) {
  if (!pending_.count(key))
    return NULL;

  content::WebIntentsDispatcher* dispatcher = pending_[key];
  pending_.erase(key);
  return dispatcher;
}

///////////////////////////////////////////////////////////////////////////////
// Factory boilerplate

// static
WebIntentCallbacks* WebIntentCallbacks::Factory::GetForProfile(
    Profile* profile) {
  return static_cast<WebIntentCallbacks*>(
      GetInstance()->GetServiceForProfile(profile, true));
}

WebIntentCallbacks::Factory* WebIntentCallbacks::Factory::GetInstance() {
  return Singleton<WebIntentCallbacks::Factory>::get();
}

WebIntentCallbacks::Factory::Factory()
    : ProfileKeyedServiceFactory("WebIntentCallbacks",
                                 ProfileDependencyManager::GetInstance()) {
}

WebIntentCallbacks::Factory::~Factory() {
}

ProfileKeyedService* WebIntentCallbacks::Factory::BuildServiceInstanceFor(
    Profile* profile) const {
  return new WebIntentCallbacks();
}

}  // namespace extensions