Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(99)

Side by Side Diff: chrome/browser/extensions/api/copresence/copresence_api.cc

Issue 444513005: Add the Copresence API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 // Each execute will correspond to one ReportRequest protocol buffer.
137 copresence::ReportRequest request;
138 if (!PrepareReportRequestProto(params->operations,
139 extension_id(),
140 &service->apps_by_subscription_id(),
141 &request)) {
142 return RespondNow(Error(kInvalidOperationsMessage));
143 }
144
145 service->client()->ExecuteReportRequest(
146 request,
147 extension_id(),
148 base::Bind(&CopresenceExecuteFunction::SendResult, this));
149 return RespondLater();
150 }
151
152 void CopresenceExecuteFunction::SendResult(
153 copresence::CopresenceStatus status) {
154 api::copresence::ExecuteStatus api_status =
155 (status == copresence::SUCCESS) ? api::copresence::EXECUTE_STATUS_SUCCESS
156 : api::copresence::EXECUTE_STATUS_FAILED;
157 Respond(ArgumentList(api::copresence::Execute::Results::Create(api_status)));
158 }
159
160 // CopresenceSetApiKeyFunction implementation:
161 ExtensionFunction::ResponseAction CopresenceSetApiKeyFunction::Run() {
162 scoped_ptr<api::copresence::SetApiKey::Params> params(
163 api::copresence::SetApiKey::Params::Create(*args_));
164 EXTENSION_FUNCTION_VALIDATE(params.get());
165
166 // TODO(rkc): Use the API key set by this function for this app.
167 // http://crbug.com/400617.
168 return RespondNow(NoArguments());
169 }
170
171 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698