| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2009 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 "chrome/browser/extensions/extension_function_dispatcher.h" |
| 6 |
| 7 #include "base/json_reader.h" |
| 8 #include "base/json_writer.h" |
| 9 #include "base/singleton.h" |
| 10 #include "base/values.h" |
| 11 #include "chrome/browser/extensions/extension_function.h" |
| 12 #include "chrome/browser/extensions/extension_tabs_module.h" |
| 13 #include "chrome/browser/renderer_host/render_view_host.h" |
| 14 |
| 15 // FactoryRegistry ------------------------------------------------------------- |
| 16 |
| 17 namespace { |
| 18 |
| 19 // A pointer to a function that create an instance of an ExtensionFunction. |
| 20 typedef ExtensionFunction* (*ExtensionFunctionFactory)(); |
| 21 |
| 22 // Contains a list of all known extension functions and allows clients to create |
| 23 // instances of them. |
| 24 class FactoryRegistry { |
| 25 public: |
| 26 static FactoryRegistry* instance(); |
| 27 FactoryRegistry(); |
| 28 void GetAllNames(std::vector<std::string>* names); |
| 29 ExtensionFunction* NewFunction(const std::string& name); |
| 30 |
| 31 private: |
| 32 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap; |
| 33 FactoryMap factories_; |
| 34 }; |
| 35 |
| 36 // Template for defining ExtensionFunctionFactory. |
| 37 template<class T> |
| 38 ExtensionFunction* NewExtensionFunction() { |
| 39 return new T(); |
| 40 } |
| 41 |
| 42 FactoryRegistry* FactoryRegistry::instance() { |
| 43 return Singleton<FactoryRegistry>::get(); |
| 44 } |
| 45 |
| 46 FactoryRegistry::FactoryRegistry() { |
| 47 // Register all functions here. |
| 48 factories_["GetTabsForWindow"] = |
| 49 &NewExtensionFunction<GetTabsForWindowFunction>; |
| 50 factories_["CreateTab"] = &NewExtensionFunction<CreateTabFunction>; |
| 51 } |
| 52 |
| 53 void FactoryRegistry::GetAllNames( |
| 54 std::vector<std::string>* names) { |
| 55 for (FactoryMap::iterator iter = factories_.begin(); iter != factories_.end(); |
| 56 ++iter) { |
| 57 names->push_back(iter->first); |
| 58 } |
| 59 } |
| 60 |
| 61 ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) { |
| 62 FactoryMap::iterator iter = factories_.find(name); |
| 63 DCHECK(iter != factories_.end()); |
| 64 return iter->second(); |
| 65 } |
| 66 |
| 67 }; |
| 68 |
| 69 |
| 70 // ExtensionFunctionDispatcher ------------------------------------------------- |
| 71 |
| 72 void ExtensionFunctionDispatcher::GetAllFunctionNames( |
| 73 std::vector<std::string>* names) { |
| 74 FactoryRegistry::instance()->GetAllNames(names); |
| 75 } |
| 76 |
| 77 ExtensionFunctionDispatcher::ExtensionFunctionDispatcher( |
| 78 RenderViewHost* render_view_host) |
| 79 : render_view_host_(render_view_host) {} |
| 80 |
| 81 void ExtensionFunctionDispatcher::HandleRequest(const std::string& name, |
| 82 const std::string& args, |
| 83 int callback_id) { |
| 84 scoped_ptr<Value> value; |
| 85 if (!args.empty()) { |
| 86 JSONReader reader; |
| 87 value.reset(reader.JsonToValue(args, false, false)); |
| 88 |
| 89 // Since we do the serialization in the v8 extension, we should always get |
| 90 // valid JSON. |
| 91 if (!value.get()) { |
| 92 DCHECK(false); |
| 93 return; |
| 94 } |
| 95 } |
| 96 |
| 97 // TODO(aa): This will get a bit more complicated when we support functions |
| 98 // that live longer than the stack frame. |
| 99 scoped_ptr<ExtensionFunction> function( |
| 100 FactoryRegistry::instance()->NewFunction(name)); |
| 101 function->set_dispatcher(this); |
| 102 function->set_args(value.get()); |
| 103 function->set_callback_id(callback_id); |
| 104 function->Run(); |
| 105 } |
| 106 |
| 107 void ExtensionFunctionDispatcher::SendResponse(ExtensionFunction* function) { |
| 108 std::string json; |
| 109 |
| 110 // Some functions might not need to return any results. |
| 111 if (function->result()) |
| 112 JSONWriter::Write(function->result(), false, &json); |
| 113 |
| 114 render_view_host_->SendExtensionResponse(function->callback_id(), json); |
| 115 } |
| OLD | NEW |