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 CopresenceService* GetCopresenceService( | |
32 content::BrowserContext* browser_context) { | |
33 return CopresenceService::GetFactoryInstance()->Get(browser_context); | |
34 } | |
35 | |
36 copresence::CopresenceClient* GetCopresenceClient( | |
37 content::BrowserContext* browser_context) { | |
not at google - send to devlin
2014/08/07 22:23:38
make both of these instance methods, then you don'
rkc
2014/08/07 22:32:36
Actually, removing them completely doesn't really
| |
38 CopresenceService* service_ptr = GetCopresenceService(browser_context); | |
39 return service_ptr ? service_ptr->client() : NULL; | |
40 } | |
41 | |
42 } // namespace | |
43 | |
44 // CopresenceService implementation: | |
45 | |
46 CopresenceService::CopresenceService(content::BrowserContext* context) | |
47 : is_shutting_down_(false), browser_context_(context) { | |
48 } | |
49 | |
50 CopresenceService::~CopresenceService() { | |
51 } | |
52 | |
53 copresence::CopresenceClient* CopresenceService::client() { | |
not at google - send to devlin
2014/08/07 22:23:38
what is the difference between this method and Get
rkc
2014/08/07 22:32:36
Removed all the GetCopresenceXXX() functions.
Done
| |
54 if (!client_ && !is_shutting_down_) | |
55 client_.reset(new copresence::CopresenceClient(this)); | |
56 return client_.get(); | |
not at google - send to devlin
2014/08/07 22:23:38
this returns the current client if it's shutting d
rkc
2014/08/07 22:32:36
Yes, and the current client is set to NULL if we'r
| |
57 } | |
58 | |
59 copresence::WhispernetClient* CopresenceService::whispernet_client() { | |
60 if (!whispernet_client_ && !is_shutting_down_) | |
61 whispernet_client_.reset(new ChromeWhispernetClient(browser_context_)); | |
62 return whispernet_client_.get(); | |
63 } | |
64 | |
65 void CopresenceService::Shutdown() { | |
66 is_shutting_down_ = true; | |
67 client_.reset(); | |
68 whispernet_client_.reset(); | |
69 } | |
70 | |
71 // static | |
72 BrowserContextKeyedAPIFactory<CopresenceService>* | |
73 CopresenceService::GetFactoryInstance() { | |
74 return g_factory.Pointer(); | |
75 } | |
76 | |
77 void CopresenceService::HandleMessages( | |
78 const std::string& /* app_id */, | |
79 const std::string& subscription_id, | |
80 const std::vector<copresence::Message>& messages) { | |
81 // TODO(ckehoe): Once the server starts sending back the app ids associated | |
82 // with subscriptions, use that instead of the apps_by_subs registry. | |
83 std::string app_id = apps_by_subscription_id_[subscription_id]; | |
84 | |
85 if (app_id.empty()) { | |
86 LOG(ERROR) << "Skipping message from unrecognized subscription " | |
87 << subscription_id; | |
88 return; | |
89 } | |
90 | |
91 int message_count = messages.size(); | |
92 std::vector<linked_ptr<api::copresence::Message> > api_messages( | |
93 message_count); | |
94 | |
95 for (int m = 0; m < message_count; ++m) { | |
96 api_messages[m].reset(new api::copresence::Message); | |
97 api_messages[m]->type = messages[m].type().type(); | |
98 api_messages[m]->payload = messages[m].payload(); | |
99 DVLOG(2) << "Dispatching message of type " << api_messages[m]->type << ":\n" | |
100 << api_messages[m]->payload; | |
101 } | |
102 | |
103 // Send the messages to the client app. | |
104 scoped_ptr<Event> event( | |
105 new Event(api::copresence::OnMessagesReceived::kEventName, | |
106 api::copresence::OnMessagesReceived::Create(subscription_id, | |
107 api_messages), | |
108 browser_context_)); | |
109 EventRouter::Get(browser_context_) | |
110 ->DispatchEventToExtension(app_id, event.Pass()); | |
111 DVLOG(2) << "Passed " << api_messages.size() << " messages to app \"" | |
112 << app_id << "\" for subscription \"" << subscription_id << "\""; | |
113 } | |
114 | |
115 net::URLRequestContextGetter* CopresenceService::GetRequestContext() const { | |
116 return browser_context_->GetRequestContext(); | |
117 } | |
118 | |
119 const std::string CopresenceService::GetPlatformVersionString() const { | |
120 return chrome::VersionInfo().CreateVersionString(); | |
121 } | |
122 | |
123 copresence::WhispernetClient* CopresenceService::GetWhispernetClient() { | |
124 return whispernet_client(); | |
125 } | |
126 | |
127 template <> | |
128 void | |
129 BrowserContextKeyedAPIFactory<CopresenceService>::DeclareFactoryDependencies() { | |
130 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | |
131 } | |
132 | |
133 // CopresenceExecuteFunction implementation: | |
134 ExtensionFunction::ResponseAction CopresenceExecuteFunction::Run() { | |
135 scoped_ptr<api::copresence::Execute::Params> params( | |
136 api::copresence::Execute::Params::Create(*args_)); | |
137 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
138 | |
139 // This can only happen if we're shutting down. In all other cases, if we | |
140 // don't have a client, we'll create one. | |
141 if (!GetCopresenceService(browser_context())->client()) | |
142 return RespondNow(Error(kShuttingDownMessage)); | |
143 | |
144 SubscriptionToAppMap& apps_by_subscription_id = | |
145 GetCopresenceService(browser_context())->apps_by_subscription_id(); | |
146 | |
147 // Each execute will correspond to one ReportRequest protocol buffer. | |
148 copresence::ReportRequest request; | |
149 if (!PrepareReportRequestProto(params->operations, | |
150 extension_id(), | |
151 &apps_by_subscription_id, | |
152 &request)) { | |
153 return RespondNow(Error(kInvalidOperationsMessage)); | |
154 } | |
155 | |
156 GetCopresenceClient(browser_context())->ExecuteReportRequest( | |
157 request, | |
158 extension_id(), | |
159 base::Bind(&CopresenceExecuteFunction::SendResult, this)); | |
160 return RespondLater(); | |
161 } | |
162 | |
163 void CopresenceExecuteFunction::SendResult( | |
164 copresence::CopresenceStatus status) { | |
165 api::copresence::ExecuteStatus api_status = | |
166 (status == copresence::SUCCESS) ? api::copresence::EXECUTE_STATUS_SUCCESS | |
167 : api::copresence::EXECUTE_STATUS_FAILED; | |
168 Respond(ArgumentList(api::copresence::Execute::Results::Create(api_status))); | |
169 } | |
170 | |
171 // CopresenceSetApiKeyFunction implementation: | |
172 ExtensionFunction::ResponseAction CopresenceSetApiKeyFunction::Run() { | |
173 scoped_ptr<api::copresence::SetApiKey::Params> params( | |
174 api::copresence::SetApiKey::Params::Create(*args_)); | |
175 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
176 | |
177 // TODO(rkc): Use the API key set by this function for this app. | |
178 // http://crbug.com/400617. | |
179 return RespondNow(NoArguments()); | |
180 } | |
181 | |
182 } // namespace extensions | |
OLD | NEW |