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/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::ItemStorage::Get(browser_context())) | |
48 return RespondNow(Error("Not available")); | |
rkc
2017/07/10 20:04:00
This should never happen right? If so, then put in
tbarzic
2017/07/10 22:29:48
For create, it should not happen (as this one is o
| |
49 | |
50 lock_screen_data::ItemStorage::Get(browser_context()) | |
51 ->CreateItem(extension_id(), | |
52 base::Bind(&LockScreenDataCreateFunction::OnDone, this)); | |
53 return RespondLater(); | |
54 } | |
55 | |
56 void LockScreenDataCreateFunction::OnDone( | |
57 lock_screen_data::OperationResult result, | |
58 const lock_screen_data::DataItem* item) { | |
59 if (result != lock_screen_data::OperationResult::kSuccess) { | |
60 Respond(Error(GetErrorString(result))); | |
61 return; | |
62 } | |
63 | |
19 api::lock_screen_data::DataItemInfo item_info; | 64 api::lock_screen_data::DataItemInfo item_info; |
20 item_info.id = "fake"; | 65 item_info.id = item->id(); |
21 return RespondNow( | 66 Respond( |
22 ArgumentList(api::lock_screen_data::Create::Results::Create(item_info))); | 67 ArgumentList(api::lock_screen_data::Create::Results::Create(item_info))); |
23 } | 68 } |
24 | 69 |
25 LockScreenDataGetAllFunction::LockScreenDataGetAllFunction() {} | 70 LockScreenDataGetAllFunction::LockScreenDataGetAllFunction() {} |
26 | 71 |
27 LockScreenDataGetAllFunction::~LockScreenDataGetAllFunction() {} | 72 LockScreenDataGetAllFunction::~LockScreenDataGetAllFunction() {} |
28 | 73 |
29 ExtensionFunction::ResponseAction LockScreenDataGetAllFunction::Run() { | 74 ExtensionFunction::ResponseAction LockScreenDataGetAllFunction::Run() { |
75 if (!lock_screen_data::ItemStorage::Get(browser_context())) | |
76 return RespondNow(Error("Not available")); | |
77 | |
78 lock_screen_data::ItemStorage::Get(browser_context()) | |
79 ->GetAllForExtension( | |
80 extension_id(), | |
81 base::Bind(&LockScreenDataGetAllFunction::OnDone, this)); | |
82 return RespondLater(); | |
83 } | |
84 | |
85 void LockScreenDataGetAllFunction::OnDone( | |
86 const std::vector<const lock_screen_data::DataItem*>& items) { | |
30 std::vector<api::lock_screen_data::DataItemInfo> items_info; | 87 std::vector<api::lock_screen_data::DataItemInfo> items_info; |
31 return RespondNow( | 88 for (auto* const item : items) { |
89 if (!item) | |
90 continue; | |
91 api::lock_screen_data::DataItemInfo item_info; | |
92 item_info.id = item->id(); | |
93 items_info.emplace_back(std::move(item_info)); | |
94 } | |
95 | |
96 Respond( | |
32 ArgumentList(api::lock_screen_data::GetAll::Results::Create(items_info))); | 97 ArgumentList(api::lock_screen_data::GetAll::Results::Create(items_info))); |
33 } | 98 } |
34 | 99 |
35 LockScreenDataGetContentFunction::LockScreenDataGetContentFunction() {} | 100 LockScreenDataGetContentFunction::LockScreenDataGetContentFunction() {} |
36 | 101 |
37 LockScreenDataGetContentFunction::~LockScreenDataGetContentFunction() {} | 102 LockScreenDataGetContentFunction::~LockScreenDataGetContentFunction() {} |
38 | 103 |
39 ExtensionFunction::ResponseAction LockScreenDataGetContentFunction::Run() { | 104 ExtensionFunction::ResponseAction LockScreenDataGetContentFunction::Run() { |
105 if (!lock_screen_data::ItemStorage::Get(browser_context())) | |
106 return RespondNow(Error("Not available")); | |
107 | |
40 std::unique_ptr<api::lock_screen_data::GetContent::Params> params( | 108 std::unique_ptr<api::lock_screen_data::GetContent::Params> params( |
41 api::lock_screen_data::GetContent::Params::Create(*args_)); | 109 api::lock_screen_data::GetContent::Params::Create(*args_)); |
42 EXTENSION_FUNCTION_VALIDATE(params.get()); | 110 EXTENSION_FUNCTION_VALIDATE(params.get()); |
43 | 111 |
44 return RespondNow(Error("Not found")); | 112 lock_screen_data::ItemStorage::Get(browser_context()) |
113 ->GetItemContent( | |
114 extension_id(), params->id, | |
115 base::Bind(&LockScreenDataGetContentFunction::OnDone, this)); | |
116 return RespondLater(); | |
117 } | |
118 | |
119 void LockScreenDataGetContentFunction::OnDone( | |
120 lock_screen_data::OperationResult result, | |
121 std::unique_ptr<std::vector<char>> data) { | |
122 if (result == lock_screen_data::OperationResult::kSuccess) { | |
123 Respond(ArgumentList( | |
124 api::lock_screen_data::GetContent::Results::Create(*data))); | |
125 return; | |
126 } | |
127 Respond(Error(GetErrorString(result))); | |
45 } | 128 } |
46 | 129 |
47 LockScreenDataSetContentFunction::LockScreenDataSetContentFunction() {} | 130 LockScreenDataSetContentFunction::LockScreenDataSetContentFunction() {} |
48 | 131 |
49 LockScreenDataSetContentFunction::~LockScreenDataSetContentFunction() {} | 132 LockScreenDataSetContentFunction::~LockScreenDataSetContentFunction() {} |
50 | 133 |
51 ExtensionFunction::ResponseAction LockScreenDataSetContentFunction::Run() { | 134 ExtensionFunction::ResponseAction LockScreenDataSetContentFunction::Run() { |
135 if (!lock_screen_data::ItemStorage::Get(browser_context())) | |
136 return RespondNow(Error("Not available")); | |
137 | |
52 std::unique_ptr<api::lock_screen_data::SetContent::Params> params( | 138 std::unique_ptr<api::lock_screen_data::SetContent::Params> params( |
53 api::lock_screen_data::SetContent::Params::Create(*args_)); | 139 api::lock_screen_data::SetContent::Params::Create(*args_)); |
54 EXTENSION_FUNCTION_VALIDATE(params.get()); | 140 EXTENSION_FUNCTION_VALIDATE(params.get()); |
55 | 141 |
56 return RespondNow(Error("Not found")); | 142 lock_screen_data::ItemStorage::Get(browser_context()) |
143 ->SetItemContent( | |
144 extension_id(), params->id, params->data, | |
145 base::Bind(&LockScreenDataSetContentFunction::OnDone, this)); | |
146 return RespondLater(); | |
147 } | |
148 | |
149 void LockScreenDataSetContentFunction::OnDone( | |
150 lock_screen_data::OperationResult result) { | |
151 if (result == lock_screen_data::OperationResult::kSuccess) { | |
152 Respond(NoArguments()); | |
153 return; | |
154 } | |
155 Respond(Error(GetErrorString(result))); | |
57 } | 156 } |
58 | 157 |
59 LockScreenDataDeleteFunction::LockScreenDataDeleteFunction() {} | 158 LockScreenDataDeleteFunction::LockScreenDataDeleteFunction() {} |
60 | 159 |
61 LockScreenDataDeleteFunction::~LockScreenDataDeleteFunction() {} | 160 LockScreenDataDeleteFunction::~LockScreenDataDeleteFunction() {} |
62 | 161 |
63 ExtensionFunction::ResponseAction LockScreenDataDeleteFunction::Run() { | 162 ExtensionFunction::ResponseAction LockScreenDataDeleteFunction::Run() { |
64 std::unique_ptr<api::lock_screen_data::Delete::Params> params( | 163 std::unique_ptr<api::lock_screen_data::Delete::Params> params( |
65 api::lock_screen_data::Delete::Params::Create(*args_)); | 164 api::lock_screen_data::Delete::Params::Create(*args_)); |
66 EXTENSION_FUNCTION_VALIDATE(params.get()); | 165 EXTENSION_FUNCTION_VALIDATE(params.get()); |
67 return RespondNow(Error("Not found.")); | 166 |
167 lock_screen_data::ItemStorage::Get(browser_context()) | |
168 ->DeleteItem(extension_id(), params->id, | |
169 base::Bind(&LockScreenDataDeleteFunction::OnDone, this)); | |
170 return RespondLater(); | |
171 } | |
172 | |
173 void LockScreenDataDeleteFunction::OnDone( | |
174 lock_screen_data::OperationResult result) { | |
175 if (result == lock_screen_data::OperationResult::kSuccess) { | |
176 Respond(NoArguments()); | |
177 return; | |
178 } | |
179 Respond(Error(GetErrorString(result))); | |
68 } | 180 } |
69 | 181 |
70 } // namespace extensions | 182 } // namespace extensions |
OLD | NEW |