OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/copresence/copresence_api.h" | 5 #include "chrome/browser/extensions/api/copresence/copresence_api.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/memory/linked_ptr.h" | 8 #include "base/memory/linked_ptr.h" |
9 #include "chrome/browser/copresence/chrome_whispernet_client.h" | 9 #include "chrome/browser/copresence/chrome_whispernet_client.h" |
10 #include "chrome/common/chrome_version_info.h" | 10 #include "chrome/common/chrome_version_info.h" |
11 #include "chrome/common/extensions/api/copresence.h" | 11 #include "chrome/common/extensions/api/copresence.h" |
12 #include "components/copresence/proto/data.pb.h" | 12 #include "components/copresence/proto/data.pb.h" |
13 #include "components/copresence/proto/enums.pb.h" | 13 #include "components/copresence/proto/enums.pb.h" |
14 #include "components/copresence/proto/rpcs.pb.h" | 14 #include "components/copresence/proto/rpcs.pb.h" |
15 #include "components/copresence/public/copresence_client.h" | 15 #include "components/copresence/public/copresence_manager.h" |
16 #include "components/copresence/public/whispernet_client.h" | 16 #include "components/copresence/public/whispernet_client.h" |
17 #include "content/public/browser/browser_context.h" | 17 #include "content/public/browser/browser_context.h" |
18 #include "extensions/browser/event_router.h" | 18 #include "extensions/browser/event_router.h" |
19 | 19 |
20 namespace extensions { | 20 namespace extensions { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 base::LazyInstance<BrowserContextKeyedAPIFactory<CopresenceService> > | 24 base::LazyInstance<BrowserContextKeyedAPIFactory<CopresenceService> > |
25 g_factory = LAZY_INSTANCE_INITIALIZER; | 25 g_factory = LAZY_INSTANCE_INITIALIZER; |
26 | 26 |
27 const char kInvalidOperationsMessage[] = | 27 const char kInvalidOperationsMessage[] = |
28 "Invalid operation in operations array."; | 28 "Invalid operation in operations array."; |
29 const char kShuttingDownMessage[] = "Shutting down."; | 29 const char kShuttingDownMessage[] = "Shutting down."; |
30 | 30 |
31 } // namespace | 31 } // namespace |
32 | 32 |
33 // CopresenceService implementation: | 33 // CopresenceService implementation: |
34 | 34 |
35 CopresenceService::CopresenceService(content::BrowserContext* context) | 35 CopresenceService::CopresenceService(content::BrowserContext* context) |
36 : is_shutting_down_(false), browser_context_(context) {} | 36 : is_shutting_down_(false), browser_context_(context) {} |
37 | 37 |
38 CopresenceService::~CopresenceService() {} | 38 CopresenceService::~CopresenceService() {} |
39 | 39 |
40 copresence::CopresenceClient* CopresenceService::client() { | 40 copresence::CopresenceManager* CopresenceService::manager() { |
41 if (!client_ && !is_shutting_down_) | 41 if (!manager_ && !is_shutting_down_) |
42 client_.reset(new copresence::CopresenceClient(this)); | 42 manager_ = copresence::CopresenceManager::Create(this); |
43 return client_.get(); | 43 return manager_.get(); |
44 } | 44 } |
45 | 45 |
46 copresence::WhispernetClient* CopresenceService::whispernet_client() { | 46 copresence::WhispernetClient* CopresenceService::whispernet_client() { |
47 if (!whispernet_client_ && !is_shutting_down_) | 47 if (!whispernet_client_ && !is_shutting_down_) |
48 whispernet_client_.reset(new ChromeWhispernetClient(browser_context_)); | 48 whispernet_client_.reset(new ChromeWhispernetClient(browser_context_)); |
49 return whispernet_client_.get(); | 49 return whispernet_client_.get(); |
50 } | 50 } |
51 | 51 |
52 void CopresenceService::Shutdown() { | 52 void CopresenceService::Shutdown() { |
53 is_shutting_down_ = true; | 53 is_shutting_down_ = true; |
54 client_.reset(); | 54 manager_.reset(); |
55 whispernet_client_.reset(); | 55 whispernet_client_.reset(); |
56 } | 56 } |
57 | 57 |
| 58 void CopresenceService::set_manager_for_testing( |
| 59 scoped_ptr<copresence::CopresenceManager> manager) { |
| 60 manager_ = manager.Pass(); |
| 61 } |
| 62 |
58 // static | 63 // static |
59 BrowserContextKeyedAPIFactory<CopresenceService>* | 64 BrowserContextKeyedAPIFactory<CopresenceService>* |
60 CopresenceService::GetFactoryInstance() { | 65 CopresenceService::GetFactoryInstance() { |
61 return g_factory.Pointer(); | 66 return g_factory.Pointer(); |
62 } | 67 } |
63 | 68 |
64 void CopresenceService::HandleMessages( | 69 void CopresenceService::HandleMessages( |
65 const std::string& /* app_id */, | 70 const std::string& /* app_id */, |
66 const std::string& subscription_id, | 71 const std::string& subscription_id, |
67 const std::vector<copresence::Message>& messages) { | 72 const std::vector<copresence::Message>& messages) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 return api_key_; | 116 return api_key_; |
112 } | 117 } |
113 | 118 |
114 copresence::WhispernetClient* CopresenceService::GetWhispernetClient() { | 119 copresence::WhispernetClient* CopresenceService::GetWhispernetClient() { |
115 return whispernet_client(); | 120 return whispernet_client(); |
116 } | 121 } |
117 | 122 |
118 template <> | 123 template <> |
119 void | 124 void |
120 BrowserContextKeyedAPIFactory<CopresenceService>::DeclareFactoryDependencies() { | 125 BrowserContextKeyedAPIFactory<CopresenceService>::DeclareFactoryDependencies() { |
| 126 DCHECK(ExtensionsBrowserClient::Get()); |
121 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | 127 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); |
122 } | 128 } |
123 | 129 |
124 // CopresenceExecuteFunction implementation: | 130 // CopresenceExecuteFunction implementation: |
125 ExtensionFunction::ResponseAction CopresenceExecuteFunction::Run() { | 131 ExtensionFunction::ResponseAction CopresenceExecuteFunction::Run() { |
126 scoped_ptr<api::copresence::Execute::Params> params( | 132 scoped_ptr<api::copresence::Execute::Params> params( |
127 api::copresence::Execute::Params::Create(*args_)); | 133 api::copresence::Execute::Params::Create(*args_)); |
128 EXTENSION_FUNCTION_VALIDATE(params.get()); | 134 EXTENSION_FUNCTION_VALIDATE(params.get()); |
129 | 135 |
130 CopresenceService* service = | 136 CopresenceService* service = |
131 CopresenceService::GetFactoryInstance()->Get(browser_context()); | 137 CopresenceService::GetFactoryInstance()->Get(browser_context()); |
132 | 138 |
133 // This can only happen if we're shutting down. In all other cases, if we | 139 // This can only happen if we're shutting down. In all other cases, if we |
134 // don't have a client, we'll create one. | 140 // don't have a client, we'll create one. |
135 if (!service->client()) | 141 if (!service->manager()) |
136 return RespondNow(Error(kShuttingDownMessage)); | 142 return RespondNow(Error(kShuttingDownMessage)); |
137 | 143 |
138 // Each execute will correspond to one ReportRequest protocol buffer. | 144 // Each execute will correspond to one ReportRequest protocol buffer. |
139 copresence::ReportRequest request; | 145 copresence::ReportRequest request; |
140 if (!PrepareReportRequestProto(params->operations, | 146 if (!PrepareReportRequestProto(params->operations, |
141 extension_id(), | 147 extension_id(), |
142 &service->apps_by_subscription_id(), | 148 &service->apps_by_subscription_id(), |
143 &request)) { | 149 &request)) { |
144 return RespondNow(Error(kInvalidOperationsMessage)); | 150 return RespondNow(Error(kInvalidOperationsMessage)); |
145 } | 151 } |
146 | 152 |
147 service->client()->ExecuteReportRequest( | 153 service->manager()->ExecuteReportRequest( |
148 request, | 154 request, |
149 extension_id(), | 155 extension_id(), |
150 base::Bind(&CopresenceExecuteFunction::SendResult, this)); | 156 base::Bind(&CopresenceExecuteFunction::SendResult, this)); |
151 return RespondLater(); | 157 return RespondLater(); |
152 } | 158 } |
153 | 159 |
154 void CopresenceExecuteFunction::SendResult( | 160 void CopresenceExecuteFunction::SendResult( |
155 copresence::CopresenceStatus status) { | 161 copresence::CopresenceStatus status) { |
156 api::copresence::ExecuteStatus api_status = | 162 api::copresence::ExecuteStatus api_status = |
157 (status == copresence::SUCCESS) ? api::copresence::EXECUTE_STATUS_SUCCESS | 163 (status == copresence::SUCCESS) ? api::copresence::EXECUTE_STATUS_SUCCESS |
158 : api::copresence::EXECUTE_STATUS_FAILED; | 164 : api::copresence::EXECUTE_STATUS_FAILED; |
159 Respond(ArgumentList(api::copresence::Execute::Results::Create(api_status))); | 165 Respond(ArgumentList(api::copresence::Execute::Results::Create(api_status))); |
160 } | 166 } |
161 | 167 |
162 // CopresenceSetApiKeyFunction implementation: | 168 // CopresenceSetApiKeyFunction implementation: |
163 ExtensionFunction::ResponseAction CopresenceSetApiKeyFunction::Run() { | 169 ExtensionFunction::ResponseAction CopresenceSetApiKeyFunction::Run() { |
164 scoped_ptr<api::copresence::SetApiKey::Params> params( | 170 scoped_ptr<api::copresence::SetApiKey::Params> params( |
165 api::copresence::SetApiKey::Params::Create(*args_)); | 171 api::copresence::SetApiKey::Params::Create(*args_)); |
166 EXTENSION_FUNCTION_VALIDATE(params.get()); | 172 EXTENSION_FUNCTION_VALIDATE(params.get()); |
167 | 173 |
168 // The api key may be set to empty, to clear it. | 174 // The api key may be set to empty, to clear it. |
169 CopresenceService::GetFactoryInstance()->Get(browser_context()) | 175 CopresenceService::GetFactoryInstance()->Get(browser_context()) |
170 ->set_api_key(params->api_key); | 176 ->set_api_key(params->api_key); |
171 return RespondNow(NoArguments()); | 177 return RespondNow(NoArguments()); |
172 } | 178 } |
173 | 179 |
174 } // namespace extensions | 180 } // namespace extensions |
OLD | NEW |