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/browser/extensions/api/copresence/copresence_util.h" | |
| 11 #include "chrome/common/chrome_version_info.h" | |
| 12 #include "chrome/common/extensions/api/copresence.h" | |
| 13 #include "components/copresence/proto/data.pb.h" | |
| 14 #include "components/copresence/proto/enums.pb.h" | |
| 15 #include "components/copresence/proto/rpcs.pb.h" | |
| 16 #include "components/copresence/public/copresence_client.h" | |
| 17 #include "components/copresence/public/whispernet_client.h" | |
| 18 #include "content/public/browser/browser_context.h" | |
| 19 #include "extensions/browser/event_router.h" | |
| 20 | |
| 21 namespace extensions { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 base::LazyInstance<BrowserContextKeyedAPIFactory<CopresenceService> > | |
| 26 g_factory = LAZY_INSTANCE_INITIALIZER; | |
| 27 | |
| 28 const char kInvalidOperationsMessage[] = | |
| 29 "Invalid operation in operations array."; | |
| 30 | |
| 31 } // namespace | |
| 32 | |
| 33 // CopresenceService implementation: | |
| 34 | |
| 35 CopresenceService::CopresenceService(content::BrowserContext* context) | |
| 36 : browser_context_(context) { | |
| 37 } | |
| 38 | |
| 39 CopresenceService::~CopresenceService() { | |
| 40 } | |
| 41 | |
| 42 copresence::CopresenceClient* CopresenceService::client() { | |
| 43 if (!client_) | |
| 44 client_.reset(new copresence::CopresenceClient(this)); | |
| 45 return client_.get(); | |
| 46 } | |
| 47 | |
| 48 copresence::WhispernetClient* CopresenceService::whispernet_client() { | |
| 49 if (!whispernet_client_) | |
| 50 whispernet_client_.reset(new ChromeWhispernetClient(browser_context_)); | |
| 51 return whispernet_client_.get(); | |
| 52 } | |
| 53 | |
| 54 void CopresenceService::Shutdown() { | |
| 55 if (client_.get()) | |
| 56 client_->Shutdown(); | |
| 57 } | |
| 58 | |
| 59 // static | |
| 60 BrowserContextKeyedAPIFactory<CopresenceService>* | |
| 61 CopresenceService::GetFactoryInstance() { | |
| 62 return g_factory.Pointer(); | |
| 63 } | |
| 64 | |
| 65 void CopresenceService::HandleMessages( | |
| 66 const std::string& /* app_id */, | |
| 67 const std::string& subscription_id, | |
| 68 const std::vector<copresence::Message>& messages) { | |
| 69 // TODO(ckehoe): Once the server starts sending back the app ids associated | |
| 70 // with subscriptions, use that instead of the apps_by_subs registry. | |
| 71 std::string app_id = GetCopresenceService(browser_context_) | |
|
not at google - send to devlin
2014/08/07 20:41:18
I don't understand this call. Why is a CopresenceS
rkc
2014/08/07 21:43:02
Whoops, that's kinda silly :) Fixed.
Done.
| |
| 72 ->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 event->restrict_to_browser_context = browser_context_; | |
|
not at google - send to devlin
2014/08/07 20:41:18
you can construct this directly: https://code.goog
rkc
2014/08/07 21:43:02
Done.
| |
| 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_.get(); | |
| 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 SubscriptionToAppMap& apps_by_subscription_id = | |
| 129 GetCopresenceService(browser_context())->apps_by_subscription_id(); | |
|
not at google - send to devlin
2014/08/07 20:41:18
you seem to be checking for a NULL copresence serv
rkc
2014/08/07 21:43:02
Copresence service should always be available (or
| |
| 130 | |
| 131 // Each execute will correspond to one ReportRequest protocol buffer. | |
| 132 copresence::ReportRequest request; | |
| 133 if (!PrepareReportRequestProto(params->operations, | |
| 134 extension_id(), | |
| 135 &apps_by_subscription_id, | |
| 136 &request)) { | |
| 137 return RespondNow(Error(kInvalidOperationsMessage)); | |
| 138 } | |
| 139 | |
| 140 GetCopresenceClient(browser_context())->ExecuteReportRequest( | |
| 141 request, | |
| 142 extension_id(), | |
| 143 base::Bind(&CopresenceExecuteFunction::SendResult, this)); | |
| 144 return RespondLater(); | |
| 145 } | |
| 146 | |
| 147 void CopresenceExecuteFunction::SendResult( | |
| 148 copresence::CopresenceStatus status) { | |
| 149 api::copresence::ExecuteStatus api_status = | |
| 150 (status == copresence::SUCCESS) ? api::copresence::EXECUTE_STATUS_SUCCESS | |
| 151 : api::copresence::EXECUTE_STATUS_FAILED; | |
| 152 Respond(ArgumentList(api::copresence::Execute::Results::Create(api_status))); | |
| 153 } | |
| 154 | |
| 155 // CopresenceSetApiKeyFunction implementation: | |
| 156 ExtensionFunction::ResponseAction CopresenceSetApiKeyFunction::Run() { | |
| 157 scoped_ptr<api::copresence::SetApiKey::Params> params( | |
| 158 api::copresence::SetApiKey::Params::Create(*args_)); | |
| 159 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 160 | |
| 161 // TODO(rkc): Use the API key set by this function for this app. | |
| 162 // http://crbug.com/400617. | |
| 163 return RespondNow(NoArguments()); | |
| 164 } | |
| 165 | |
| 166 } // namespace extensions | |
| OLD | NEW |