| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #include "base/stringprintf.h" |
| 6 #include "chrome/browser/extensions/web_intent_callbacks.h" |
| 7 #include "chrome/browser/profiles/profile_dependency_manager.h" |
| 8 #include "chrome/common/extensions/extension.h" |
| 9 #include "content/public/browser/web_contents.h" |
| 10 #include "content/public/browser/web_contents_observer.h" |
| 11 #include "content/public/browser/web_intents_dispatcher.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 // Get the key for use inside the internal pending WebIntentCallbacks map. |
| 16 std::string GetKey(const extensions::Extension* extension, int id) { |
| 17 return StringPrintf("%s/%d", extension->id().c_str(), id); |
| 18 } |
| 19 |
| 20 } // namespace |
| 21 |
| 22 namespace extensions { |
| 23 |
| 24 // SourceObserver is a subclass of WebContentsObserver that is instantiated |
| 25 // on RegisterCallback to wait for the source WebContents to be destroyed. |
| 26 // If it is destroyed, this automatically clears the callback from pending_. |
| 27 class WebIntentCallbacks::SourceObserver : content::WebContentsObserver { |
| 28 public: |
| 29 SourceObserver(content::WebContents* web_contents, |
| 30 WebIntentCallbacks* callbacks, |
| 31 const std::string key) |
| 32 : content::WebContentsObserver(web_contents), |
| 33 callbacks_(callbacks), |
| 34 key_(key) {} |
| 35 virtual ~SourceObserver() {} |
| 36 |
| 37 // Implement WebContentsObserver |
| 38 virtual void WebContentsDestroyed(content::WebContents* web_contents) |
| 39 OVERRIDE { |
| 40 content::WebIntentsDispatcher* dispatcher = |
| 41 callbacks_->GetAndClear(key_); |
| 42 if (dispatcher) |
| 43 delete dispatcher; |
| 44 |
| 45 delete this; |
| 46 } |
| 47 |
| 48 private: |
| 49 WebIntentCallbacks* callbacks_; |
| 50 const std::string key_; |
| 51 }; |
| 52 |
| 53 WebIntentCallbacks::WebIntentCallbacks() : last_id_(0) {} |
| 54 |
| 55 WebIntentCallbacks::~WebIntentCallbacks() {} |
| 56 |
| 57 // static |
| 58 WebIntentCallbacks* WebIntentCallbacks::Get(Profile* profile) { |
| 59 return Factory::GetForProfile(profile); |
| 60 } |
| 61 |
| 62 int WebIntentCallbacks::RegisterCallback( |
| 63 const Extension* extension, |
| 64 content::WebIntentsDispatcher* dispatcher, |
| 65 content::WebContents* source) { |
| 66 int id = ++last_id_; |
| 67 std::string key = GetKey(extension, id); |
| 68 pending_[key] = dispatcher; |
| 69 |
| 70 // TODO(thorogood): We should also listen for extension suspend events, and |
| 71 // clear the relevant callback (i.e., the handler of this intent) |
| 72 |
| 73 // TODO(thorogood): The source window may actually be a ShellWindow of a |
| 74 // platform app. In this case, we should not wait for its destruction: |
| 75 // rather, we should be listening to platform app suspend events. |
| 76 |
| 77 // Listen to when the source WebContent is destroyed. This object is self- |
| 78 // deleting on this event. |
| 79 DCHECK(source); |
| 80 new SourceObserver(source, this, key); |
| 81 |
| 82 return id; |
| 83 } |
| 84 |
| 85 content::WebIntentsDispatcher* WebIntentCallbacks::RetrieveCallback( |
| 86 const Extension* extension, int id) { |
| 87 std::string key = GetKey(extension, id); |
| 88 return GetAndClear(key); |
| 89 } |
| 90 |
| 91 content::WebIntentsDispatcher* WebIntentCallbacks::GetAndClear( |
| 92 std::string key) { |
| 93 if (!pending_.count(key)) |
| 94 return NULL; |
| 95 |
| 96 content::WebIntentsDispatcher* dispatcher = pending_[key]; |
| 97 pending_.erase(key); |
| 98 return dispatcher; |
| 99 } |
| 100 |
| 101 /////////////////////////////////////////////////////////////////////////////// |
| 102 // Factory boilerplate |
| 103 |
| 104 // static |
| 105 WebIntentCallbacks* WebIntentCallbacks::Factory::GetForProfile( |
| 106 Profile* profile) { |
| 107 return static_cast<WebIntentCallbacks*>( |
| 108 GetInstance()->GetServiceForProfile(profile, true)); |
| 109 } |
| 110 |
| 111 WebIntentCallbacks::Factory* WebIntentCallbacks::Factory::GetInstance() { |
| 112 return Singleton<WebIntentCallbacks::Factory>::get(); |
| 113 } |
| 114 |
| 115 WebIntentCallbacks::Factory::Factory() |
| 116 : ProfileKeyedServiceFactory("WebIntentCallbacks", |
| 117 ProfileDependencyManager::GetInstance()) { |
| 118 } |
| 119 |
| 120 WebIntentCallbacks::Factory::~Factory() { |
| 121 } |
| 122 |
| 123 ProfileKeyedService* WebIntentCallbacks::Factory::BuildServiceInstanceFor( |
| 124 Profile* profile) const { |
| 125 return new WebIntentCallbacks(); |
| 126 } |
| 127 |
| 128 } // namespace extensions |
| OLD | NEW |