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/prefs/pref_registry_simple.h" | |
9 #include "base/prefs/pref_service.h" | |
10 #include "chrome/browser/browser_process.h" | |
11 #include "chrome/browser/copresence/chrome_whispernet_client.h" | |
12 #include "chrome/browser/extensions/api/copresence/copresence_util.h" | |
13 #include "chrome/browser/profiles/profile.h" | |
xiyuan
2014/08/05 21:02:35
nit: seems not used
rkc
2014/08/05 22:25:21
Done.
| |
14 #include "chrome/common/chrome_version_info.h" | |
15 #include "chrome/common/extensions/api/copresence.h" | |
16 #include "chrome/common/pref_names.h" | |
17 #include "components/copresence/proto/data.pb.h" | |
18 #include "components/copresence/proto/enums.pb.h" | |
19 #include "components/copresence/proto/rpcs.pb.h" | |
20 #include "components/copresence/public/copresence_client.h" | |
21 #include "components/copresence/public/whispernet_client.h" | |
22 #include "content/public/browser/browser_context.h" | |
23 #include "extensions/browser/event_router.h" | |
24 | |
25 namespace extensions { | |
26 | |
27 namespace { | |
28 | |
29 base::LazyInstance<BrowserContextKeyedAPIFactory<CopresenceService> > | |
30 g_factory = LAZY_INSTANCE_INITIALIZER; | |
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 void CopresenceService::RegisterPrefs(PrefRegistrySimple* registry) { | |
62 registry->RegisterStringPref(prefs::kCopresenceDeviceId, std::string()); | |
63 } | |
64 | |
65 // static | |
66 BrowserContextKeyedAPIFactory<CopresenceService>* | |
67 CopresenceService::GetFactoryInstance() { | |
68 return g_factory.Pointer(); | |
69 } | |
70 | |
71 void CopresenceService::HandleMessages( | |
72 const std::string& /* app_id */, | |
73 const std::string& subscription_id, | |
74 const std::vector<copresence::Message>& messages) { | |
75 std::string app_id = GetCopresenceService(browser_context_) | |
xiyuan
2014/08/05 21:02:35
What is difference between this |app_id| from subs
rkc
2014/08/05 22:25:22
Eventually the server API will return the app_id t
| |
76 ->SubscriptionToAppsRegistry()[subscription_id]; | |
77 | |
78 if (app_id.empty()) { | |
79 LOG(ERROR) << "Skipping message from unrecognized subscription " | |
80 << subscription_id; | |
81 return; | |
82 } | |
83 | |
84 int message_count = messages.size(); | |
85 std::vector<linked_ptr<api::copresence::Message> > api_messages( | |
xiyuan
2014/08/05 21:02:35
nit: #include "base/memory/linked_ptr.h"
rkc
2014/08/05 22:25:22
Done.
| |
86 message_count); | |
87 | |
88 for (int m = 0; m < message_count; ++m) { | |
89 api_messages[m].reset(new api::copresence::Message); | |
90 api_messages[m]->type = messages[m].type().type(); | |
91 api_messages[m]->payload = messages[m].payload(); | |
92 DVLOG(2) << "Dispatching message of type " << api_messages[m]->type << ":\n" | |
93 << api_messages[m]->payload; | |
94 } | |
95 | |
96 // Send the messages to the client app. | |
97 scoped_ptr<Event> event( | |
98 new Event(api::copresence::OnMessagesReceived::kEventName, | |
99 api::copresence::OnMessagesReceived::Create(subscription_id, | |
100 api_messages))); | |
101 event->restrict_to_browser_context = browser_context_; | |
102 EventRouter::Get(browser_context_) | |
103 ->DispatchEventToExtension(app_id, event.Pass()); | |
104 DVLOG(2) << "Passed " << api_messages.size() << " messages to app \"" | |
105 << app_id << "\" for subscription \"" << subscription_id << "\""; | |
106 } | |
107 | |
108 net::URLRequestContextGetter* CopresenceService::GetRequestContext() const { | |
109 return browser_context_->GetRequestContext(); | |
110 } | |
111 | |
112 const std::string CopresenceService::GetPlatformVersionString() const { | |
113 return chrome::VersionInfo().CreateVersionString(); | |
114 } | |
115 | |
116 const std::string CopresenceService::GetDeviceId() const { | |
not at google - send to devlin
2014/08/05 17:09:55
I don't see Get/SaveDeviceId used anywhere, which
rkc
2014/08/05 18:06:24
Get/SaveDeviceId are called from https://coderevie
not at google - send to devlin
2014/08/05 18:17:21
I see.
That is never exposed to the extension, ri
rkc
2014/08/05 22:25:21
Spoke with jyasskin@
Removing all of this code.
| |
117 PrefService* prefs = g_browser_process->local_state(); | |
118 return prefs->GetString(prefs::kCopresenceDeviceId); | |
119 } | |
120 | |
121 void CopresenceService::SaveDeviceId(const std::string& device_id) { | |
122 PrefService* prefs = g_browser_process->local_state(); | |
123 prefs->SetString(prefs::kCopresenceDeviceId, device_id); | |
124 } | |
125 | |
126 copresence::WhispernetClient* CopresenceService::GetWhispernetClient() { | |
127 return whispernet_client_.get(); | |
128 } | |
129 | |
130 template <> | |
131 void | |
132 BrowserContextKeyedAPIFactory<CopresenceService>::DeclareFactoryDependencies() { | |
133 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | |
134 } | |
135 | |
136 // CopresenceBatchExecuteFunction implementation: | |
137 ExtensionFunction::ResponseAction CopresenceBatchExecuteFunction::Run() { | |
138 scoped_ptr<api::copresence::BatchExecute::Params> params( | |
139 api::copresence::BatchExecute::Params::Create(*args_)); | |
140 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
141 | |
142 SubscriptionToAppMap& apps_by_subscription_id = | |
143 GetCopresenceService(browser_context())->SubscriptionToAppsRegistry(); | |
144 | |
145 // Each batch execute will correspond to one ReportRequest protocol buffer. | |
146 copresence::ReportRequest request; | |
147 if (!PrepareReportRequestProto(params->operations, | |
148 extension_id(), | |
149 &apps_by_subscription_id, | |
150 &request)) | |
151 return RespondNow(BadMessage()); | |
not at google - send to devlin
2014/08/05 21:05:38
nit: multi-line condition should have {} around th
rkc
2014/08/05 22:25:22
Done.
| |
152 | |
153 GetCopresenceClient(browser_context())->ExecuteReportRequest( | |
154 request, | |
155 extension_id(), | |
156 base::Bind(&CopresenceBatchExecuteFunction::SendResult, this)); | |
157 return RespondLater(); | |
158 } | |
159 | |
160 void CopresenceBatchExecuteFunction::SendResult( | |
161 copresence::CopresenceStatus status) { | |
162 api::copresence::BatchExecuteStatus api_status = | |
163 (status == copresence::SUCCESS) | |
164 ? api::copresence::BATCH_EXECUTE_STATUS_SUCCESS | |
165 : api::copresence::BATCH_EXECUTE_STATUS_FAILED; | |
166 Respond( | |
167 ArgumentList(api::copresence::BatchExecute::Results::Create(api_status))); | |
168 } | |
169 | |
170 // CopresenceSetApiKeyFunction implementation: | |
171 ExtensionFunction::ResponseAction CopresenceSetApiKeyFunction::Run() { | |
172 scoped_ptr<api::copresence::SetApiKey::Params> params( | |
173 api::copresence::SetApiKey::Params::Create(*args_)); | |
174 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
175 | |
176 // TODO(rkc): Use the API key set by this function for this app. | |
177 // http://crbug.com/400617. | |
178 return RespondNow(NoArguments()); | |
179 } | |
180 | |
181 } // namespace extensions | |
OLD | NEW |