OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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/api/copresence/copresence_api.h" |
| 6 |
| 7 #include "base/lazy_instance.h" |
| 8 #include "base/memory/linked_ptr.h" |
| 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/copresence/chrome_whispernet_client.h" |
| 11 #include "chrome/browser/extensions/api/copresence/copresence_util.h" |
| 12 #include "chrome/common/chrome_version_info.h" |
| 13 #include "chrome/common/extensions/api/copresence.h" |
| 14 #include "components/copresence/proto/data.pb.h" |
| 15 #include "components/copresence/proto/enums.pb.h" |
| 16 #include "components/copresence/proto/rpcs.pb.h" |
| 17 #include "components/copresence/public/copresence_client.h" |
| 18 #include "components/copresence/public/whispernet_client.h" |
| 19 #include "content/public/browser/browser_context.h" |
| 20 #include "extensions/browser/event_router.h" |
| 21 |
| 22 namespace extensions { |
| 23 |
| 24 namespace { |
| 25 |
| 26 base::LazyInstance<BrowserContextKeyedAPIFactory<CopresenceService> > |
| 27 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 28 |
| 29 const char kInvalidOperationsMessage[] = |
| 30 "Invalid operation in operations array."; |
| 31 |
| 32 } // namespace |
| 33 |
| 34 // CopresenceService implementation: |
| 35 |
| 36 CopresenceService::CopresenceService(content::BrowserContext* context) |
| 37 : browser_context_(context) { |
| 38 } |
| 39 |
| 40 CopresenceService::~CopresenceService() { |
| 41 } |
| 42 |
| 43 copresence::CopresenceClient* CopresenceService::client() { |
| 44 if (!client_) |
| 45 client_.reset(new copresence::CopresenceClient(this)); |
| 46 return client_.get(); |
| 47 } |
| 48 |
| 49 copresence::WhispernetClient* CopresenceService::whispernet_client() { |
| 50 if (!whispernet_client_) |
| 51 whispernet_client_.reset(new ChromeWhispernetClient(browser_context_)); |
| 52 return whispernet_client_.get(); |
| 53 } |
| 54 |
| 55 void CopresenceService::Shutdown() { |
| 56 if (client_.get()) |
| 57 client_->Shutdown(); |
| 58 } |
| 59 |
| 60 // static |
| 61 BrowserContextKeyedAPIFactory<CopresenceService>* |
| 62 CopresenceService::GetFactoryInstance() { |
| 63 return g_factory.Pointer(); |
| 64 } |
| 65 |
| 66 void CopresenceService::HandleMessages( |
| 67 const std::string& /* app_id */, |
| 68 const std::string& subscription_id, |
| 69 const std::vector<copresence::Message>& messages) { |
| 70 // TODO(ckehoe): Once the server starts sending back the app ids associated |
| 71 // with subscriptions, use that instead of the apps_by_subs registry. |
| 72 std::string app_id = GetCopresenceService(browser_context_) |
| 73 ->apps_by_subscription_id()[subscription_id]; |
| 74 |
| 75 if (app_id.empty()) { |
| 76 LOG(ERROR) << "Skipping message from unrecognized subscription " |
| 77 << subscription_id; |
| 78 return; |
| 79 } |
| 80 |
| 81 int message_count = messages.size(); |
| 82 std::vector<linked_ptr<api::copresence::Message> > api_messages( |
| 83 message_count); |
| 84 |
| 85 for (int m = 0; m < message_count; ++m) { |
| 86 api_messages[m].reset(new api::copresence::Message); |
| 87 api_messages[m]->type = messages[m].type().type(); |
| 88 api_messages[m]->payload = messages[m].payload(); |
| 89 DVLOG(2) << "Dispatching message of type " << api_messages[m]->type << ":\n" |
| 90 << api_messages[m]->payload; |
| 91 } |
| 92 |
| 93 // Send the messages to the client app. |
| 94 scoped_ptr<Event> event( |
| 95 new Event(api::copresence::OnMessagesReceived::kEventName, |
| 96 api::copresence::OnMessagesReceived::Create(subscription_id, |
| 97 api_messages))); |
| 98 event->restrict_to_browser_context = browser_context_; |
| 99 EventRouter::Get(browser_context_) |
| 100 ->DispatchEventToExtension(app_id, event.Pass()); |
| 101 DVLOG(2) << "Passed " << api_messages.size() << " messages to app \"" |
| 102 << app_id << "\" for subscription \"" << subscription_id << "\""; |
| 103 } |
| 104 |
| 105 net::URLRequestContextGetter* CopresenceService::GetRequestContext() const { |
| 106 return browser_context_->GetRequestContext(); |
| 107 } |
| 108 |
| 109 const std::string CopresenceService::GetPlatformVersionString() const { |
| 110 return chrome::VersionInfo().CreateVersionString(); |
| 111 } |
| 112 |
| 113 copresence::WhispernetClient* CopresenceService::GetWhispernetClient() { |
| 114 return whispernet_client_.get(); |
| 115 } |
| 116 |
| 117 template <> |
| 118 void |
| 119 BrowserContextKeyedAPIFactory<CopresenceService>::DeclareFactoryDependencies() { |
| 120 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); |
| 121 } |
| 122 |
| 123 // CopresenceExecuteFunction implementation: |
| 124 ExtensionFunction::ResponseAction CopresenceExecuteFunction::Run() { |
| 125 scoped_ptr<api::copresence::Execute::Params> params( |
| 126 api::copresence::Execute::Params::Create(*args_)); |
| 127 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 128 |
| 129 SubscriptionToAppMap& apps_by_subscription_id = |
| 130 GetCopresenceService(browser_context())->apps_by_subscription_id(); |
| 131 |
| 132 // Each execute will correspond to one ReportRequest protocol buffer. |
| 133 copresence::ReportRequest request; |
| 134 if (!PrepareReportRequestProto(params->operations, |
| 135 extension_id(), |
| 136 &apps_by_subscription_id, |
| 137 &request)) { |
| 138 return RespondNow(Error(kInvalidOperationsMessage)); |
| 139 } |
| 140 |
| 141 GetCopresenceClient(browser_context())->ExecuteReportRequest( |
| 142 request, |
| 143 extension_id(), |
| 144 base::Bind(&CopresenceExecuteFunction::SendResult, this)); |
| 145 return RespondLater(); |
| 146 } |
| 147 |
| 148 void CopresenceExecuteFunction::SendResult( |
| 149 copresence::CopresenceStatus status) { |
| 150 api::copresence::ExecuteStatus api_status = |
| 151 (status == copresence::SUCCESS) ? api::copresence::EXECUTE_STATUS_SUCCESS |
| 152 : api::copresence::EXECUTE_STATUS_FAILED; |
| 153 Respond(ArgumentList(api::copresence::Execute::Results::Create(api_status))); |
| 154 } |
| 155 |
| 156 // CopresenceSetApiKeyFunction implementation: |
| 157 ExtensionFunction::ResponseAction CopresenceSetApiKeyFunction::Run() { |
| 158 scoped_ptr<api::copresence::SetApiKey::Params> params( |
| 159 api::copresence::SetApiKey::Params::Create(*args_)); |
| 160 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 161 |
| 162 // TODO(rkc): Use the API key set by this function for this app. |
| 163 // http://crbug.com/400617. |
| 164 return RespondNow(NoArguments()); |
| 165 } |
| 166 |
| 167 } // namespace extensions |
OLD | NEW |