OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "extensions/browser/api/lock_screen_data/lock_screen_data_api.h" | 5 #include "extensions/browser/api/lock_screen_data/lock_screen_data_api.h" |
6 | 6 |
7 #include <memory> | 7 #include <string> |
8 #include <utility> | |
8 #include <vector> | 9 #include <vector> |
9 | 10 |
11 #include "extensions/browser/api/lock_screen_data/data_item.h" | |
12 #include "extensions/browser/api/lock_screen_data/lock_screen_item_storage.h" | |
13 #include "extensions/browser/api/lock_screen_data/operation_result.h" | |
10 #include "extensions/common/api/lock_screen_data.h" | 14 #include "extensions/common/api/lock_screen_data.h" |
11 | 15 |
12 namespace extensions { | 16 namespace extensions { |
13 | 17 |
18 namespace { | |
19 | |
20 std::string GetErrorString(lock_screen_data::OperationResult result) { | |
21 switch (result) { | |
22 case lock_screen_data::OperationResult::kSuccess: | |
23 NOTREACHED() << "Expected a failure code."; | |
24 return "Unknown"; | |
25 case lock_screen_data::OperationResult::kFailed: | |
26 return "Unknown"; | |
27 case lock_screen_data::OperationResult::kInvalidKey: | |
28 case lock_screen_data::OperationResult::kWrongKey: | |
29 return "Internal - encryption"; | |
30 case lock_screen_data::OperationResult::kAlreadyRegistered: | |
31 return "Duplicate item"; | |
32 case lock_screen_data::OperationResult::kNotFound: | |
33 case lock_screen_data::OperationResult::kUnknownExtension: | |
34 return "Not found"; | |
35 } | |
36 NOTREACHED() << "Unknown operation status"; | |
37 return "Unknown"; | |
38 } | |
39 | |
40 } // namespace | |
41 | |
14 LockScreenDataCreateFunction::LockScreenDataCreateFunction() {} | 42 LockScreenDataCreateFunction::LockScreenDataCreateFunction() {} |
15 | 43 |
16 LockScreenDataCreateFunction::~LockScreenDataCreateFunction() {} | 44 LockScreenDataCreateFunction::~LockScreenDataCreateFunction() {} |
17 | 45 |
18 ExtensionFunction::ResponseAction LockScreenDataCreateFunction::Run() { | 46 ExtensionFunction::ResponseAction LockScreenDataCreateFunction::Run() { |
47 if (!lock_screen_data::LockScreenItemStorage::GetIfAllowed( | |
Devlin
2017/07/12 02:13:40
Cache the result of GetIfAllowed()?
tbarzic
2017/07/12 04:02:11
Done.
| |
48 browser_context())) { | |
49 LOG(ERROR) << "Attempt to create data item from context which cannot use " | |
50 << "lock screen data item storage: " << source_context_type(); | |
51 return RespondNow(Error("Not available")); | |
52 } | |
53 | |
54 lock_screen_data::LockScreenItemStorage::GetIfAllowed(browser_context()) | |
55 ->CreateItem(extension_id(), | |
56 base::Bind(&LockScreenDataCreateFunction::OnDone, this)); | |
57 return RespondLater(); | |
58 } | |
59 | |
60 void LockScreenDataCreateFunction::OnDone( | |
61 lock_screen_data::OperationResult result, | |
62 const lock_screen_data::DataItem* item) { | |
63 if (result != lock_screen_data::OperationResult::kSuccess) { | |
64 Respond(Error(GetErrorString(result))); | |
65 return; | |
66 } | |
67 | |
19 api::lock_screen_data::DataItemInfo item_info; | 68 api::lock_screen_data::DataItemInfo item_info; |
20 item_info.id = "fake"; | 69 item_info.id = item->id(); |
21 return RespondNow( | 70 Respond( |
22 ArgumentList(api::lock_screen_data::Create::Results::Create(item_info))); | 71 ArgumentList(api::lock_screen_data::Create::Results::Create(item_info))); |
23 } | 72 } |
24 | 73 |
25 LockScreenDataGetAllFunction::LockScreenDataGetAllFunction() {} | 74 LockScreenDataGetAllFunction::LockScreenDataGetAllFunction() {} |
26 | 75 |
27 LockScreenDataGetAllFunction::~LockScreenDataGetAllFunction() {} | 76 LockScreenDataGetAllFunction::~LockScreenDataGetAllFunction() {} |
28 | 77 |
29 ExtensionFunction::ResponseAction LockScreenDataGetAllFunction::Run() { | 78 ExtensionFunction::ResponseAction LockScreenDataGetAllFunction::Run() { |
79 if (!lock_screen_data::LockScreenItemStorage::GetIfAllowed(browser_context())) | |
80 return RespondNow(Error("Not available")); | |
81 | |
82 lock_screen_data::LockScreenItemStorage::GetIfAllowed(browser_context()) | |
83 ->GetAllForExtension( | |
84 extension_id(), | |
85 base::Bind(&LockScreenDataGetAllFunction::OnDone, this)); | |
86 return RespondLater(); | |
87 } | |
88 | |
89 void LockScreenDataGetAllFunction::OnDone( | |
90 const std::vector<const lock_screen_data::DataItem*>& items) { | |
30 std::vector<api::lock_screen_data::DataItemInfo> items_info; | 91 std::vector<api::lock_screen_data::DataItemInfo> items_info; |
31 return RespondNow( | 92 for (auto* const item : items) { |
93 if (!item) | |
94 continue; | |
95 api::lock_screen_data::DataItemInfo item_info; | |
96 item_info.id = item->id(); | |
97 items_info.emplace_back(std::move(item_info)); | |
98 } | |
99 | |
100 Respond( | |
32 ArgumentList(api::lock_screen_data::GetAll::Results::Create(items_info))); | 101 ArgumentList(api::lock_screen_data::GetAll::Results::Create(items_info))); |
33 } | 102 } |
34 | 103 |
35 LockScreenDataGetContentFunction::LockScreenDataGetContentFunction() {} | 104 LockScreenDataGetContentFunction::LockScreenDataGetContentFunction() {} |
36 | 105 |
37 LockScreenDataGetContentFunction::~LockScreenDataGetContentFunction() {} | 106 LockScreenDataGetContentFunction::~LockScreenDataGetContentFunction() {} |
38 | 107 |
39 ExtensionFunction::ResponseAction LockScreenDataGetContentFunction::Run() { | 108 ExtensionFunction::ResponseAction LockScreenDataGetContentFunction::Run() { |
109 if (!lock_screen_data::LockScreenItemStorage::GetIfAllowed(browser_context())) | |
110 return RespondNow(Error("Not available")); | |
111 | |
40 std::unique_ptr<api::lock_screen_data::GetContent::Params> params( | 112 std::unique_ptr<api::lock_screen_data::GetContent::Params> params( |
41 api::lock_screen_data::GetContent::Params::Create(*args_)); | 113 api::lock_screen_data::GetContent::Params::Create(*args_)); |
42 EXTENSION_FUNCTION_VALIDATE(params.get()); | 114 EXTENSION_FUNCTION_VALIDATE(params.get()); |
43 | 115 |
44 return RespondNow(Error("Not found")); | 116 lock_screen_data::LockScreenItemStorage::GetIfAllowed(browser_context()) |
117 ->GetItemContent( | |
118 extension_id(), params->id, | |
119 base::Bind(&LockScreenDataGetContentFunction::OnDone, this)); | |
120 return RespondLater(); | |
121 } | |
122 | |
123 void LockScreenDataGetContentFunction::OnDone( | |
124 lock_screen_data::OperationResult result, | |
125 std::unique_ptr<std::vector<char>> data) { | |
126 if (result == lock_screen_data::OperationResult::kSuccess) { | |
127 Respond(ArgumentList( | |
128 api::lock_screen_data::GetContent::Results::Create(*data))); | |
129 return; | |
130 } | |
131 Respond(Error(GetErrorString(result))); | |
45 } | 132 } |
46 | 133 |
47 LockScreenDataSetContentFunction::LockScreenDataSetContentFunction() {} | 134 LockScreenDataSetContentFunction::LockScreenDataSetContentFunction() {} |
48 | 135 |
49 LockScreenDataSetContentFunction::~LockScreenDataSetContentFunction() {} | 136 LockScreenDataSetContentFunction::~LockScreenDataSetContentFunction() {} |
50 | 137 |
51 ExtensionFunction::ResponseAction LockScreenDataSetContentFunction::Run() { | 138 ExtensionFunction::ResponseAction LockScreenDataSetContentFunction::Run() { |
139 if (!lock_screen_data::LockScreenItemStorage::GetIfAllowed(browser_context())) | |
140 return RespondNow(Error("Not available")); | |
141 | |
52 std::unique_ptr<api::lock_screen_data::SetContent::Params> params( | 142 std::unique_ptr<api::lock_screen_data::SetContent::Params> params( |
53 api::lock_screen_data::SetContent::Params::Create(*args_)); | 143 api::lock_screen_data::SetContent::Params::Create(*args_)); |
54 EXTENSION_FUNCTION_VALIDATE(params.get()); | 144 EXTENSION_FUNCTION_VALIDATE(params.get()); |
55 | 145 |
56 return RespondNow(Error("Not found")); | 146 lock_screen_data::LockScreenItemStorage::GetIfAllowed(browser_context()) |
147 ->SetItemContent( | |
148 extension_id(), params->id, params->data, | |
149 base::Bind(&LockScreenDataSetContentFunction::OnDone, this)); | |
150 return RespondLater(); | |
151 } | |
152 | |
153 void LockScreenDataSetContentFunction::OnDone( | |
154 lock_screen_data::OperationResult result) { | |
155 if (result == lock_screen_data::OperationResult::kSuccess) { | |
156 Respond(NoArguments()); | |
157 return; | |
158 } | |
159 Respond(Error(GetErrorString(result))); | |
57 } | 160 } |
58 | 161 |
59 LockScreenDataDeleteFunction::LockScreenDataDeleteFunction() {} | 162 LockScreenDataDeleteFunction::LockScreenDataDeleteFunction() {} |
60 | 163 |
61 LockScreenDataDeleteFunction::~LockScreenDataDeleteFunction() {} | 164 LockScreenDataDeleteFunction::~LockScreenDataDeleteFunction() {} |
62 | 165 |
63 ExtensionFunction::ResponseAction LockScreenDataDeleteFunction::Run() { | 166 ExtensionFunction::ResponseAction LockScreenDataDeleteFunction::Run() { |
64 std::unique_ptr<api::lock_screen_data::Delete::Params> params( | 167 std::unique_ptr<api::lock_screen_data::Delete::Params> params( |
65 api::lock_screen_data::Delete::Params::Create(*args_)); | 168 api::lock_screen_data::Delete::Params::Create(*args_)); |
66 EXTENSION_FUNCTION_VALIDATE(params.get()); | 169 EXTENSION_FUNCTION_VALIDATE(params.get()); |
67 return RespondNow(Error("Not found.")); | 170 |
171 lock_screen_data::LockScreenItemStorage::GetIfAllowed(browser_context()) | |
172 ->DeleteItem(extension_id(), params->id, | |
173 base::Bind(&LockScreenDataDeleteFunction::OnDone, this)); | |
174 return RespondLater(); | |
175 } | |
176 | |
177 void LockScreenDataDeleteFunction::OnDone( | |
178 lock_screen_data::OperationResult result) { | |
179 if (result == lock_screen_data::OperationResult::kSuccess) { | |
180 Respond(NoArguments()); | |
181 return; | |
182 } | |
183 Respond(Error(GetErrorString(result))); | |
68 } | 184 } |
69 | 185 |
70 } // namespace extensions | 186 } // namespace extensions |
OLD | NEW |