Chromium Code Reviews| 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/copresence/chrome_whispernet_client.h" | |
| 10 #include "chrome/common/chrome_version_info.h" | |
| 11 #include "chrome/common/extensions/api/copresence.h" | |
| 12 #include "components/copresence/proto/data.pb.h" | |
| 13 #include "components/copresence/proto/enums.pb.h" | |
| 14 #include "components/copresence/proto/rpcs.pb.h" | |
| 15 #include "components/copresence/public/copresence_client.h" | |
| 16 #include "components/copresence/public/whispernet_client.h" | |
| 17 #include "content/public/browser/browser_context.h" | |
| 18 #include "extensions/browser/event_router.h" | |
| 19 | |
| 20 namespace extensions { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 base::LazyInstance<BrowserContextKeyedAPIFactory<CopresenceService> > | |
| 25 g_factory = LAZY_INSTANCE_INITIALIZER; | |
| 26 | |
| 27 const char kInvalidOperationsMessage[] = | |
| 28 "Invalid operation in operations array."; | |
| 29 const char kShuttingDownMessage[] = "Shutting down."; | |
| 30 | |
| 31 } // namespace | |
| 32 | |
| 33 // CopresenceService implementation: | |
| 34 | |
| 35 CopresenceService::CopresenceService(content::BrowserContext* context) | |
| 36 : is_shutting_down_(false), browser_context_(context) { | |
| 37 } | |
| 38 | |
| 39 CopresenceService::~CopresenceService() { | |
| 40 } | |
| 41 | |
| 42 copresence::CopresenceClient* CopresenceService::client() { | |
| 43 if (!client_ && !is_shutting_down_) | |
| 44 client_.reset(new copresence::CopresenceClient(this)); | |
| 45 return client_.get(); | |
| 46 } | |
| 47 | |
| 48 copresence::WhispernetClient* CopresenceService::whispernet_client() { | |
| 49 if (!whispernet_client_ && !is_shutting_down_) | |
| 50 whispernet_client_.reset(new ChromeWhispernetClient(browser_context_)); | |
| 51 return whispernet_client_.get(); | |
| 52 } | |
| 53 | |
| 54 void CopresenceService::Shutdown() { | |
| 55 is_shutting_down_ = true; | |
| 56 client_.reset(); | |
| 57 whispernet_client_.reset(); | |
| 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 = apps_by_subscription_id_[subscription_id]; | |
| 73 | |
| 74 if (app_id.empty()) { | |
| 75 LOG(ERROR) << "Skipping message from unrecognized subscription " | |
| 76 << subscription_id; | |
| 77 return; | |
| 78 } | |
| 79 | |
| 80 int message_count = messages.size(); | |
| 81 std::vector<linked_ptr<api::copresence::Message> > api_messages( | |
| 82 message_count); | |
| 83 | |
| 84 for (int m = 0; m < message_count; ++m) { | |
| 85 api_messages[m].reset(new api::copresence::Message); | |
| 86 api_messages[m]->type = messages[m].type().type(); | |
| 87 api_messages[m]->payload = messages[m].payload(); | |
| 88 DVLOG(2) << "Dispatching message of type " << api_messages[m]->type << ":\n" | |
| 89 << api_messages[m]->payload; | |
| 90 } | |
| 91 | |
| 92 // Send the messages to the client app. | |
| 93 scoped_ptr<Event> event( | |
| 94 new Event(api::copresence::OnMessagesReceived::kEventName, | |
| 95 api::copresence::OnMessagesReceived::Create(subscription_id, | |
| 96 api_messages), | |
| 97 browser_context_)); | |
| 98 EventRouter::Get(browser_context_) | |
| 99 ->DispatchEventToExtension(app_id, event.Pass()); | |
| 100 DVLOG(2) << "Passed " << api_messages.size() << " messages to app \"" | |
| 101 << app_id << "\" for subscription \"" << subscription_id << "\""; | |
| 102 } | |
| 103 | |
| 104 net::URLRequestContextGetter* CopresenceService::GetRequestContext() const { | |
| 105 return browser_context_->GetRequestContext(); | |
| 106 } | |
| 107 | |
| 108 const std::string CopresenceService::GetPlatformVersionString() const { | |
| 109 return chrome::VersionInfo().CreateVersionString(); | |
| 110 } | |
| 111 | |
| 112 copresence::WhispernetClient* CopresenceService::GetWhispernetClient() { | |
| 113 return whispernet_client(); | |
| 114 } | |
| 115 | |
| 116 template <> | |
| 117 void | |
| 118 BrowserContextKeyedAPIFactory<CopresenceService>::DeclareFactoryDependencies() { | |
| 119 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | |
| 120 } | |
| 121 | |
| 122 // CopresenceExecuteFunction implementation: | |
| 123 ExtensionFunction::ResponseAction CopresenceExecuteFunction::Run() { | |
| 124 scoped_ptr<api::copresence::Execute::Params> params( | |
| 125 api::copresence::Execute::Params::Create(*args_)); | |
| 126 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 127 | |
| 128 CopresenceService* service = | |
| 129 CopresenceService::GetFactoryInstance()->Get(browser_context()); | |
| 130 | |
| 131 // This can only happen if we're shutting down. In all other cases, if we | |
| 132 // don't have a client, we'll create one. | |
| 133 if (!service->client()) | |
| 134 return RespondNow(Error(kShuttingDownMessage)); | |
| 135 | |
| 136 SubscriptionToAppMap& apps_by_subscription_id = | |
|
not at google - send to devlin
2014/08/07 22:41:27
inline this?
rkc
2014/08/07 22:44:32
Done.
| |
| 137 service->apps_by_subscription_id(); | |
| 138 | |
| 139 // Each execute will correspond to one ReportRequest protocol buffer. | |
| 140 copresence::ReportRequest request; | |
| 141 if (!PrepareReportRequestProto(params->operations, | |
| 142 extension_id(), | |
| 143 &apps_by_subscription_id, | |
| 144 &request)) { | |
| 145 return RespondNow(Error(kInvalidOperationsMessage)); | |
| 146 } | |
| 147 | |
| 148 service->client()->ExecuteReportRequest( | |
| 149 request, | |
| 150 extension_id(), | |
| 151 base::Bind(&CopresenceExecuteFunction::SendResult, this)); | |
| 152 return RespondLater(); | |
| 153 } | |
| 154 | |
| 155 void CopresenceExecuteFunction::SendResult( | |
| 156 copresence::CopresenceStatus status) { | |
| 157 api::copresence::ExecuteStatus api_status = | |
| 158 (status == copresence::SUCCESS) ? api::copresence::EXECUTE_STATUS_SUCCESS | |
| 159 : api::copresence::EXECUTE_STATUS_FAILED; | |
| 160 Respond(ArgumentList(api::copresence::Execute::Results::Create(api_status))); | |
| 161 } | |
| 162 | |
| 163 // CopresenceSetApiKeyFunction implementation: | |
| 164 ExtensionFunction::ResponseAction CopresenceSetApiKeyFunction::Run() { | |
| 165 scoped_ptr<api::copresence::SetApiKey::Params> params( | |
| 166 api::copresence::SetApiKey::Params::Create(*args_)); | |
| 167 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 168 | |
| 169 // TODO(rkc): Use the API key set by this function for this app. | |
| 170 // http://crbug.com/400617. | |
| 171 return RespondNow(NoArguments()); | |
| 172 } | |
| 173 | |
| 174 } // namespace extensions | |
| OLD | NEW |